From 7498d2bd1a7a4b061db30d9641514b40069c11c2 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Fri, 8 Nov 2019 15:23:08 +0400 Subject: [PATCH] Changed some things --- CHANGELOG.md | 8 ++-- scripts/main.py | 97 ++++++++++++++++++++++++++------------------- scripts/powerups.py | 5 +++ scripts/rocket.py | 2 +- 4 files changed, 67 insertions(+), 45 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2019f48..e44a0a3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,11 @@ ## New Features -1. Added Missile PowerUp. -2. When you shoot a missile, a small red bullet will be shot out. When this bullet hits a wall, it will explode and destroy all rockets within a fixed radius from it regardless of the owner of the missile. +None... ## Changes -None... +1. Increased explosion radius of Missiles. +2. Missiles will no longer naturally despawn. +3. Powerups will now have a chance of spawning at fixed intervals in the game, intead of spawning at random times. This will prevent overcrowding of the powerups. +4. Fixed Bug where powerups would overlap with walls. diff --git a/scripts/main.py b/scripts/main.py index 3af0d04..24e1f07 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -25,6 +25,7 @@ def __init__(self, size, title, background): self.clock = pygame.time.Clock() self.FPS = 60 self.running = True + self.game_over = False self.win = pygame.display.set_mode(size) pygame.display.set_caption(title) @@ -53,21 +54,23 @@ def start(self): ] self.powerups = [] + self.powerup_spawn_timer = 0 def logic(self): # Randomly spawn powerups - if randint(0, 10000) < 40: # 0.4 percent chance of a powerup spawning randomly - powerup_id = self.powerup_ids[randint(1, len(self.powerup_ids)-1)] - - if powerup_id == "scatter_shot": - pos = self.get_spawn_location() - powerup = ScatterShot(pos[0], pos[1]) - self.powerups.append(powerup) - - if powerup_id == "missile": - pos = self.get_spawn_location() - powerup = Missile(pos[0], pos[1]) - self.powerups.append(powerup) + if self.powerup_spawn_timer % 600 == 0: + if randint(0, 100) <= 50: # 50% chance of a powerups spawning + powerup_id = self.powerup_ids[randint(1, len(self.powerup_ids)-1)] + x = randint(5, self.width - 5) + y = randint(5, self.height - 5) + + if powerup_id == "scatter_shot": + powerup = ScatterShot(x, y) + self.powerups.append(powerup) + + if powerup_id == "missile": + powerup = Missile(x, y) + self.powerups.append(powerup) keys = pygame.key.get_pressed() # speed = 5 @@ -81,7 +84,7 @@ def logic(self): scattershot_speed = 12 missile_speed = 10 - missile_fade = 80 + missile_fade = 150 # Player 1 controls if keys[pygame.K_w]: @@ -118,6 +121,7 @@ def logic(self): m = Bullet(self.p1.x, self.p1.y, self.p1.drift_heading, Missile(0, 0).color) m.speed = missile_speed self.p1.bullets.append(m) + self.p1.accelerate(recoil) self.p1.assign_powerup(0) @@ -157,6 +161,7 @@ def logic(self): m = Bullet(self.p2.x, self.p2.y, self.p2.drift_heading, Missile(0, 0).color) m.speed = missile_speed self.p2.bullets.append(m) + self.p2.accelerate(recoil) self.p2.assign_powerup(0) @@ -191,7 +196,8 @@ def logic(self): # Automatic despawn if bullet.despawn_timer > despawn_time: - self.p1.bullets.remove(bullet) + if bullet.color != Missile(0, 0).color: + self.p1.bullets.remove(bullet) # Update Player 2's Bullets for bullet in self.p2.bullets: @@ -218,7 +224,8 @@ def logic(self): # Automatic despawn if bullet.despawn_timer > despawn_time: - self.p2.bullets.remove(bullet) + if bullet.color != Missile(0, 0).color: + self.p2.bullets.remove(bullet) # Player 1 boundary collisions @@ -246,7 +253,7 @@ def logic(self): self.p1.update_collider() self.p2.update_collider() - # Handle collisions between rockets and walls + # Handle collisions between rockets and walls, and powerups and walls for wall in self.levels[self.current_level]: # Player 1 if wall.collider.colliderect(self.p1.wall_collider): @@ -260,6 +267,13 @@ def logic(self): self.p2.y = self.p2.old_y self.p2.speed = 0 + for powerup in self.powerups: + if wall.collider.colliderect(powerup.wall_collider): + x = randint(5, self.width - 5) + y = randint(5, self.height - 5) + powerup.x = x + powerup.y = y + # Handle collisions between bullets and walls for wall in self.levels[self.current_level]: # Player 1 @@ -271,11 +285,11 @@ def logic(self): self.p1.missile_explosion_y = bullet.y if self.dist(bullet.x, bullet.y, self.p1.x, self.p1.y) <= self.p1.collider_size + self.p1.missile_explosion_radius: print("BLUE WINS") - # self.running = False + self.game_over = True if self.dist(bullet.x, bullet.y, self.p2.x, self.p2.y) <= self.p2.collider_size + self.p2.missile_explosion_radius: print("RED WINS") - # self.running = False + self.game_over = True self.p1.bullets.remove(bullet) @@ -288,11 +302,11 @@ def logic(self): self.p2.missile_explosion_y = bullet.y if self.dist(bullet.x, bullet.y, self.p1.x, self.p1.y) <= self.p1.collider_size + self.p1.missile_explosion_radius: print("BLUE WINS") - # self.running = False + self.game_over = True if self.dist(bullet.x, bullet.y, self.p2.x, self.p2.y) <= self.p2.collider_size + self.p2.missile_explosion_radius: print("RED WINS") - # self.running = False + self.game_over = True self.p2.bullets.remove(bullet) @@ -317,7 +331,8 @@ def logic(self): if self.p1.missile_timer > missile_fade: self.p1.missile_exploded = False - self.running = False + if self.game_over: + self.running = False # Missile Explosion Fade Player 2 if self.p2.missile_exploded: @@ -325,9 +340,14 @@ def logic(self): if self.p2.missile_timer > missile_fade: self.p2.missile_exploded = False - self.running = False + if self.game_over: + self.running = False + + self.powerup_spawn_timer += 1 def render(self): + self.win.fill(self.background) + self.p1.render(self.win) self.p2.render(self.win) @@ -359,29 +379,25 @@ def render(self): pygame.display.update() - # if self.p1.missile_exploded or self.p2.missile_exploded: - # if not self.running: - # sleep(3) - # Gameplay functions - def get_spawn_location(self): - """Finds a suitable random spawn location for a powerup, staying within the - boundaries and avoiding all walls. + # def get_spawn_location(self): + # """Finds a suitable random spawn location for a powerup, staying within the + # boundaries and avoiding all walls. - Returns: - tuple -- Contains the x and y coordinates for the suitable location - """ + # Returns: + # tuple -- Contains the x and y coordinates for the suitable location + # """ - x = randint(5, self.width - 5) - y = randint(5, self.height - 5) - size = ScatterShot(0, 0).radius - rect = pygame.Rect(x - size, y - size, size * 2, size * 2) - for wall in self.levels[self.current_level]: - if wall.collider.colliderect(rect): - self.get_spawn_location() + # x = randint(5, self.width - 5) + # y = randint(5, self.height - 5) + # size = ScatterShot(0, 0).radius + # rect = pygame.Rect(x - size, y - size, size * 2, size * 2) + # for wall in self.levels[self.current_level]: + # if wall.collider.colliderect(rect): + # self.get_spawn_location() - return (x, y) + # return (x, y) def dist(self, x1, y1, x2, y2): """Finds the distance between 2 points on a 2D plane using the Pythagorean Theorem. @@ -466,7 +482,6 @@ def main(): quit() game.logic() - game.win.fill(game.background) game.render() diff --git a/scripts/powerups.py b/scripts/powerups.py index 7f9ad44..9846def 100644 --- a/scripts/powerups.py +++ b/scripts/powerups.py @@ -18,6 +18,7 @@ def __init__(self, x, y): self.color = (255, 255, 255) self.draw_x = self.x - self.radius self.draw_y = self.y - self.radius + self.wall_collider = pygame.Rect(self.draw_x, self.draw_y, self.radius * 2, self.radius * 2) def render(self, window): pygame.draw.ellipse(window, self.color, (self.draw_x, self.draw_y, self.radius * 2, self.radius * 2)) @@ -28,14 +29,18 @@ def render(self, window): class ScatterShot(PowerUp): def __init__(self, x, y): super().__init__(x, y) + self.id = 1 + self.color = (30, 230, 209) class Missile(PowerUp): def __init__(self, x, y): super().__init__(x, y) + self.id = 2 + self.color = (149, 0, 2) self.inner_color = (251, 251, 251) self.inner_radius = self.radius / 2 diff --git a/scripts/rocket.py b/scripts/rocket.py index 87bd667..ecae27a 100644 --- a/scripts/rocket.py +++ b/scripts/rocket.py @@ -34,7 +34,7 @@ def __init__(self, x, y, color): self.current_powerup = 0 self.missile_exploded = False - self.missile_explosion_radius = 150 + self.missile_explosion_radius = 250 self.missile_timer = 0 self.missile_explosion_x = 0 self.missile_explosion_y = 0