From 1271ed2e76b197bcd33fe8e60615eff65c65a213 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Tue, 22 Oct 2019 20:08:04 +0400 Subject: [PATCH 01/10] Added Scatter Shot --- CHANGELOG.md | 5 ++--- scripts/main.py | 28 ++++++++++++++++++++++++++-- scripts/powerups.py | 33 +++++++++++++++++++++++++++++++++ scripts/rocket.py | 6 ++++++ setup_unix.sh | 2 +- setup_win.bat | 2 +- start_unix.sh | 2 +- start_win.bat | 2 +- 8 files changed, 71 insertions(+), 9 deletions(-) create mode 100644 scripts/powerups.py diff --git a/CHANGELOG.md b/CHANGELOG.md index 36f19dd..5168431 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,9 +2,8 @@ ## New Features -1. Added new level. -2. New level will be selected after a player kills another player. +1. Added Scatter Shot PowerUp. However, the PowerUps do not function at the moment. ## Changes -1. Increased bullet speed (again). +1. Tweaked rocket movement. diff --git a/scripts/main.py b/scripts/main.py index 410b283..24ad4c7 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -13,6 +13,7 @@ from rocket import Rocket from bullet import Bullet from wall import Wall +from powerups import ScatterShot class AstroRockets: @@ -45,12 +46,20 @@ def start(self): self.current_level = randint(0 ,len(self.levels)-1) + self.powerup_ids = { + 0 : "none", + 1 : "scatter_shot", + 2 : "missile" + } + + self.powerups = [ScatterShot(self.width / 2, self.height / 2)] + def logic(self): keys = pygame.key.get_pressed() # speed = 5 - acceleration = 0.3 + acceleration = 0.25 turn_speed = 3 - friction = 0.974 + friction = 0.978 recoil = -5 # Player 1 controls @@ -196,6 +205,18 @@ def logic(self): if bullet.wall_collider.colliderect(wall.collider): self.p2.bullets.remove(bullet) + # Handle collisions between rockets and powerups + for powerup in self.powerups: + # Player 1 + if self.collision_circle(self.p1.x, self.p1.y, self.p1.collider_size, powerup.x, powerup.y, powerup.radius): + self.p1.assign_powerup(powerup.id) + self.powerups.remove(powerup) + + # Player 2 + if self.collision_circle(self.p2.x, self.p2.y, self.p2.collider_size, powerup.x, powerup.y, powerup.radius): + self.p2.assign_powerup(powerup.id) + self.powerups.remove(powerup) + self.p1.update_old_coords() self.p2.update_old_coords() @@ -212,6 +233,9 @@ def render(self): for wall in self.levels[self.current_level]: wall.render(self.win) + for powerup in self.powerups: + powerup.render(self.win) + # Gameplay functions def dist(self, x1, y1, x2, y2): diff --git a/scripts/powerups.py b/scripts/powerups.py new file mode 100644 index 0000000..ac0ccfd --- /dev/null +++ b/scripts/powerups.py @@ -0,0 +1,33 @@ +""" +Produced by Rehatbir Singh(MysteryCoder456) +Contact at rehatbir.singh@gmail.com for any questions +Full Rights Reserved under MIT License +""" + +import pygame + + +# Base Class for PowerUps +class PowerUp: + def __init__(self, x, y): + self.id = 0 + + self.x = x + self.y = y + self.radius = 10 + self.color = (255, 255, 255) + self.draw_x = self.x - self.radius + self.draw_y = self.y - self.radius + + def render(self, window): + pygame.draw.ellipse(window, self.color, (self.draw_x, self.draw_y, self.radius * 2, self.radius * 2)) + + +# Subclasses of PowerUp base class (Arranged by ID number) + +class ScatterShot(PowerUp): + def __init__(self, x, y): + super().__init__(x, y) + self.id = 1 + + self.color = (30, 230, 209) diff --git a/scripts/rocket.py b/scripts/rocket.py index 15ecd4c..8a974fb 100644 --- a/scripts/rocket.py +++ b/scripts/rocket.py @@ -27,9 +27,15 @@ def __init__(self, x, y, color): self.color = color self.collider_size = 20 self.wall_collider = pygame.Rect(self.x - self.collider_size, self.y - self.collider_size, self.collider_size * 2, self.collider_size * 2) + self.bullets = [] self.bullet_is_shot = False self.shoot_timer = 0 + + self.current_powerup = 0 + + def assign_powerup(self, powerup=0): + self.current_powerup = powerup def update_collider(self): self.wall_collider = pygame.Rect(self.x - self.collider_size, self.y - self.collider_size, self.collider_size * 2, self.collider_size * 2) diff --git a/setup_unix.sh b/setup_unix.sh index 60ec325..564428b 100644 --- a/setup_unix.sh +++ b/setup_unix.sh @@ -1,3 +1,3 @@ echo "installing dependencies..." pip install pygame -sh start_unix.sh +sh ./start_unix.sh diff --git a/setup_win.bat b/setup_win.bat index 60ec325..564428b 100644 --- a/setup_win.bat +++ b/setup_win.bat @@ -1,3 +1,3 @@ echo "installing dependencies..." pip install pygame -sh start_unix.sh +sh ./start_unix.sh diff --git a/start_unix.sh b/start_unix.sh index b45d45e..9ba92e2 100644 --- a/start_unix.sh +++ b/start_unix.sh @@ -1,2 +1,2 @@ echo "running game..." -python3 scripts/main.py \ No newline at end of file +python3 ./scripts/main.py \ No newline at end of file diff --git a/start_win.bat b/start_win.bat index b45d45e..9ba92e2 100644 --- a/start_win.bat +++ b/start_win.bat @@ -1,2 +1,2 @@ echo "running game..." -python3 scripts/main.py \ No newline at end of file +python3 ./scripts/main.py \ No newline at end of file From 57218a96640643961f2e8f49504a32b0cb43f9dc Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Thu, 24 Oct 2019 15:02:33 +0400 Subject: [PATCH 02/10] Added Scatter Shot --- .vscode/settings.json | 3 +++ CHANGELOG.md | 4 ++-- scripts/bullet.py | 2 +- scripts/main.py | 54 +++++++++++++++++++++++++++++++++++++------ 4 files changed, 53 insertions(+), 10 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..6dfa6ae --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3" +} \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 5168431..e4be485 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,8 @@ ## New Features -1. Added Scatter Shot PowerUp. However, the PowerUps do not function at the moment. +1. Added functionality to Scatter Shot PowerUp. ## Changes -1. Tweaked rocket movement. +1. Reduce bullet speed in order to prevent glitches. diff --git a/scripts/bullet.py b/scripts/bullet.py index ffb5e04..46c41ac 100644 --- a/scripts/bullet.py +++ b/scripts/bullet.py @@ -16,7 +16,7 @@ def __init__(self, x, y, hdg, color): self.draw_x = self.x - self.radius self.draw_y = self.y - self.radius self.heading = hdg - self.speed = 25 + self.speed = 18 self.x_vel = 0 self.y_vel = 0 self.color = color diff --git a/scripts/main.py b/scripts/main.py index 24ad4c7..2095409 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -46,11 +46,11 @@ def start(self): self.current_level = randint(0 ,len(self.levels)-1) - self.powerup_ids = { - 0 : "none", - 1 : "scatter_shot", - 2 : "missile" - } + self.powerup_ids = [ + "none", # 0 + "scatter_shot", # 1 + "missile" # 2 + ] self.powerups = [ScatterShot(self.width / 2, self.height / 2)] @@ -82,11 +82,26 @@ def logic(self): if keys[pygame.K_d]: self.p1.drift_heading += turn_speed + # PowerUp Shooting for Player 1 + if keys[pygame.K_e] or keys[pygame.K_q]: + + if self.powerup_ids[self.p1.current_powerup] == "none": + print("No Powerup Collected!") + + elif self.powerup_ids[self.p1.current_powerup] == "scatter_shot": + bullet_count = 8 + for i in range(bullet_count): + bullet_hdg = (360 / bullet_count) * i + b = Bullet(self.p1.x, self.p1.y, bullet_hdg, ScatterShot(0, 0).color) + b.speed = 8 + self.p1.bullets.append(b) + + self.p1.assign_powerup(0) + + # Player 2 controls if keys[pygame.K_UP]: self.p2.accelerate(acceleration) - - self.p2.speed *= friction if keys[pygame.K_DOWN]: if not self.p2.bullet_is_shot: @@ -102,11 +117,31 @@ def logic(self): if keys[pygame.K_RIGHT]: self.p2.drift_heading += turn_speed + # PowerUp Shooting for Player 2 + if keys[pygame.K_RSHIFT] or keys[pygame.K_QUESTION]: + + if self.powerup_ids[self.p2.current_powerup] == "none": + print("No Powerup Collected!") + + elif self.powerup_ids[self.p2.current_powerup] == "scatter_shot": + bullet_count = 8 + for i in range(bullet_count): + bullet_hdg = (360 / bullet_count) * i + b = Bullet(self.p2.x, self.p2.y, bullet_hdg, ScatterShot(0, 0).color) + b.speed = 8 + self.p2.bullets.append(b) + + self.p2.assign_powerup(0) + self.p1.update() + self.p1.speed *= friction + self.p2.update() + self.p2.speed *= friction despawn_time = 200 + # Update Player 1's Bullets for bullet in self.p1.bullets: bullet.update() @@ -122,14 +157,17 @@ def logic(self): bullet.update_collider() + # Collision between player 2 and player 1's bullets if self.collision_circle(bullet.x, bullet.y, bullet.radius, self.p2.x, self.p2.y, self.p2.collider_size): print("RED WINS!!") sleep(3) self.running = False + # Automatic despawn if bullet.despawn_timer > despawn_time: self.p1.bullets.remove(bullet) + # Update Player 2's Bullets for bullet in self.p2.bullets: bullet.update() @@ -145,11 +183,13 @@ def logic(self): bullet.update_collider() + # Collisions between player 1 and player 2's bullets if self.collision_circle(bullet.x, bullet.y, bullet.radius, self.p1.x, self.p1.y, self.p1.collider_size): print("BLUE WINS!!") sleep(3) self.running = False + # Automatic despawn if bullet.despawn_timer > despawn_time: self.p2.bullets.remove(bullet) From e368ddf107fef8aab33a4d57b0359bd5869c7275 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Thu, 24 Oct 2019 15:22:09 +0400 Subject: [PATCH 03/10] Bug Fixes --- scripts/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/main.py b/scripts/main.py index 2095409..0356965 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -29,9 +29,9 @@ def __init__(self, size, title, background): self.win = pygame.display.set_mode(size) pygame.display.set_caption(title) - # ###################### # - # ### Ignore this 🔼 ### # - # ###################### # + # ##################### # + # #### Ignore this #### # + # ##################### # def start(self): self.p1 = Rocket(self.width / 2 - 250, self.height / 2, (255, 0, 0)) From 6250acdaa2c928c1a3a1170d3ba155832ee46fd6 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Thu, 24 Oct 2019 16:57:47 +0400 Subject: [PATCH 04/10] Bug Fix --- .gitignore | 3 +++ .vscode/settings.json | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 894a44c..14d6198 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.vscode/ +.json + # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 6dfa6ae..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "python.pythonPath": "/Library/Frameworks/Python.framework/Versions/3.8/bin/python3" -} \ No newline at end of file From d3c9d58921b96287ef18957f7a86350821ca6810 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Thu, 24 Oct 2019 22:16:11 +0400 Subject: [PATCH 05/10] Improved Scatter Shot --- CHANGELOG.md | 6 ++++-- README.md | 4 ++-- scripts/main.py | 50 +++++++++++++++++++++++++++++++++++++------------ 3 files changed, 44 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4be485..2b23301 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,10 @@ ## New Features -1. Added functionality to Scatter Shot PowerUp. +1. PowerUps will now spawn randomly at random locations. They will not spawn inside walls. ## Changes -1. Reduce bullet speed in order to prevent glitches. +1. Increased time required to despawn bullets. +2. Increased the number of bullets shot out by Scatter Shot. +3. Increased the speed of Scatter Shot Bullets. diff --git a/README.md b/README.md index e6d364e..e3ed93d 100644 --- a/README.md +++ b/README.md @@ -30,10 +30,10 @@ Open the game folder in Terminal and enter `sh start_unix.sh`. ## How to play **Player 1:** -A and D for turning, W for accelerating and S for shooting. +A and D for turning, W for accelerating and S for shooting. Press E for using a PowerUp (when available). **Player 2:** -Left and right arrow keys for turning, up arrow key for accelerating and down arrow key for shooting. +Left and right arrow keys for turning, up arrow key for accelerating and down arrow key for shooting. Press Right Shift for using a PowerUp (when available). ## Issues diff --git a/scripts/main.py b/scripts/main.py index 0356965..29b774d 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -52,15 +52,27 @@ def start(self): "missile" # 2 ] - self.powerups = [ScatterShot(self.width / 2, self.height / 2)] + self.powerups = [] 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) + keys = pygame.key.get_pressed() # speed = 5 acceleration = 0.25 turn_speed = 3 friction = 0.978 recoil = -5 + despawn_time = 300 + scattershot_count = 16 + scattershot_speed = 12 # Player 1 controls if keys[pygame.K_w]: @@ -83,17 +95,16 @@ def logic(self): self.p1.drift_heading += turn_speed # PowerUp Shooting for Player 1 - if keys[pygame.K_e] or keys[pygame.K_q]: + if keys[pygame.K_e]: if self.powerup_ids[self.p1.current_powerup] == "none": print("No Powerup Collected!") elif self.powerup_ids[self.p1.current_powerup] == "scatter_shot": - bullet_count = 8 - for i in range(bullet_count): - bullet_hdg = (360 / bullet_count) * i + for i in range(scattershot_count): + bullet_hdg = ((360 / scattershot_count) * i) + self.p1.drift_heading b = Bullet(self.p1.x, self.p1.y, bullet_hdg, ScatterShot(0, 0).color) - b.speed = 8 + b.speed = scattershot_speed self.p1.bullets.append(b) self.p1.assign_powerup(0) @@ -124,11 +135,10 @@ def logic(self): print("No Powerup Collected!") elif self.powerup_ids[self.p2.current_powerup] == "scatter_shot": - bullet_count = 8 - for i in range(bullet_count): - bullet_hdg = (360 / bullet_count) * i + for i in range(scattershot_count): + bullet_hdg = ((360 / scattershot_count) * i) + self.p2.drift_heading b = Bullet(self.p2.x, self.p2.y, bullet_hdg, ScatterShot(0, 0).color) - b.speed = 8 + b.speed = scattershot_speed self.p2.bullets.append(b) self.p2.assign_powerup(0) @@ -139,8 +149,6 @@ def logic(self): self.p2.update() self.p2.speed *= friction - despawn_time = 200 - # Update Player 1's Bullets for bullet in self.p1.bullets: bullet.update() @@ -278,6 +286,24 @@ def render(self): # Gameplay functions + 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 + """ + + 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) + def dist(self, x1, y1, x2, y2): """Finds the distance between 2 points on a 2D plane using the Pythagorean Theorem. a^2 + b^2 = c^2 From 28b3653136515e7bb99bc1851d17c651ceaeacfc Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Fri, 25 Oct 2019 11:09:20 +0400 Subject: [PATCH 06/10] Bug Fixes --- CHANGELOG.md | 6 ++---- scripts/main.py | 2 -- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2b23301..3e66990 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,10 +2,8 @@ ## New Features -1. PowerUps will now spawn randomly at random locations. They will not spawn inside walls. +None... ## Changes -1. Increased time required to despawn bullets. -2. Increased the number of bullets shot out by Scatter Shot. -3. Increased the speed of Scatter Shot Bullets. +1. Fixed bug where Player 2 would move faster than Player 1. diff --git a/scripts/main.py b/scripts/main.py index 29b774d..ea41aed 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -77,8 +77,6 @@ def logic(self): # Player 1 controls if keys[pygame.K_w]: self.p1.accelerate(acceleration) - - self.p1.speed *= friction if keys[pygame.K_s]: if not self.p1.bullet_is_shot: From 21787eef5dce95928533689ffae05d04028cddde Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Fri, 8 Nov 2019 03:32:45 +0400 Subject: [PATCH 07/10] Added Missiles --- CHANGELOG.md | 5 ++- scripts/main.py | 102 +++++++++++++++++++++++++++++++++++++++----- scripts/powerups.py | 16 ++++++- scripts/rocket.py | 5 +++ 4 files changed, 113 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3e66990..95cb735 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,9 @@ ## New Features -None... +1. Added Missile PowerUp. +2. When you shot 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. ## Changes -1. Fixed bug where Player 2 would move faster than Player 1. +None... diff --git a/scripts/main.py b/scripts/main.py index ea41aed..3af0d04 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -13,7 +13,7 @@ from rocket import Rocket from bullet import Bullet from wall import Wall -from powerups import ScatterShot +from powerups import PowerUp, ScatterShot, Missile class AstroRockets: @@ -47,9 +47,9 @@ def start(self): self.current_level = randint(0 ,len(self.levels)-1) self.powerup_ids = [ - "none", # 0 - "scatter_shot", # 1 - "missile" # 2 + "none", # id = 0 + "scatter_shot", # id = 1 + "missile" # id = 2 ] self.powerups = [] @@ -63,6 +63,11 @@ def logic(self): 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) keys = pygame.key.get_pressed() # speed = 5 @@ -71,9 +76,13 @@ def logic(self): friction = 0.978 recoil = -5 despawn_time = 300 + scattershot_count = 16 scattershot_speed = 12 + missile_speed = 10 + missile_fade = 80 + # Player 1 controls if keys[pygame.K_w]: self.p1.accelerate(acceleration) @@ -104,6 +113,11 @@ def logic(self): b = Bullet(self.p1.x, self.p1.y, bullet_hdg, ScatterShot(0, 0).color) b.speed = scattershot_speed self.p1.bullets.append(b) + + elif self.powerup_ids[self.p1.current_powerup] == "missile": + 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.assign_powerup(0) @@ -139,6 +153,11 @@ def logic(self): b.speed = scattershot_speed self.p2.bullets.append(b) + elif self.powerup_ids[self.p2.current_powerup] == "missile": + 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.assign_powerup(0) self.p1.update() @@ -165,9 +184,10 @@ def logic(self): # Collision between player 2 and player 1's bullets if self.collision_circle(bullet.x, bullet.y, bullet.radius, self.p2.x, self.p2.y, self.p2.collider_size): - print("RED WINS!!") - sleep(3) - self.running = False + if bullet.color != Missile(0, 0).color: + print("RED WINS!!") + sleep(3) + self.running = False # Automatic despawn if bullet.despawn_timer > despawn_time: @@ -191,9 +211,10 @@ def logic(self): # Collisions between player 1 and player 2's bullets if self.collision_circle(bullet.x, bullet.y, bullet.radius, self.p1.x, self.p1.y, self.p1.collider_size): - print("BLUE WINS!!") - sleep(3) - self.running = False + if bullet.color != Missile(0, 0).color: + print("BLUE WINS!!") + sleep(3) + self.running = False # Automatic despawn if bullet.despawn_timer > despawn_time: @@ -244,11 +265,35 @@ def logic(self): # Player 1 for bullet in self.p1.bullets: if bullet.wall_collider.colliderect(wall.collider): + if bullet.color == Missile(0, 0).color: + self.p1.missile_exploded = True + self.p1.missile_explosion_x = bullet.x + 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 + + 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.p1.bullets.remove(bullet) # Player 2 for bullet in self.p2.bullets: if bullet.wall_collider.colliderect(wall.collider): + if bullet.color == Missile(0, 0).color: + self.p2.missile_exploded = True + self.p2.missile_explosion_x = bullet.x + 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 + + 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.p2.bullets.remove(bullet) # Handle collisions between rockets and powerups @@ -266,6 +311,22 @@ def logic(self): self.p1.update_old_coords() self.p2.update_old_coords() + # Missile Explosion Fade Player 1 + if self.p1.missile_exploded: + self.p1.missile_timer += 1 + + if self.p1.missile_timer > missile_fade: + self.p1.missile_exploded = False + self.running = False + + # Missile Explosion Fade Player 2 + if self.p2.missile_exploded: + self.p2.missile_timer += 1 + + if self.p2.missile_timer > missile_fade: + self.p2.missile_exploded = False + self.running = False + def render(self): self.p1.render(self.win) self.p2.render(self.win) @@ -282,6 +343,26 @@ def render(self): for powerup in self.powerups: powerup.render(self.win) + if self.p1.missile_exploded: + pygame.draw.ellipse(self.win, (255, 255, 255), (self.p1.missile_explosion_x - self.p1.missile_explosion_radius, + self.p1.missile_explosion_y - self.p1.missile_explosion_radius, + self.p1.missile_explosion_radius * 2, + self.p1.missile_explosion_radius * 2 + )) + + if self.p2.missile_exploded: + pygame.draw.ellipse(self.win, (255, 255, 255), (self.p2.missile_explosion_x - self.p2.missile_explosion_radius, + self.p2.missile_explosion_y - self.p2.missile_explosion_radius, + self.p2.missile_explosion_radius * 2, + self.p2.missile_explosion_radius * 2 + )) + + 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): @@ -387,7 +468,6 @@ def main(): game.logic() game.win.fill(game.background) game.render() - pygame.display.update() if __name__ == "__main__": diff --git a/scripts/powerups.py b/scripts/powerups.py index ac0ccfd..7f9ad44 100644 --- a/scripts/powerups.py +++ b/scripts/powerups.py @@ -14,7 +14,7 @@ def __init__(self, x, y): self.x = x self.y = y - self.radius = 10 + self.radius = 12 self.color = (255, 255, 255) self.draw_x = self.x - self.radius self.draw_y = self.y - self.radius @@ -29,5 +29,17 @@ 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 + + def render(self, window): + pygame.draw.ellipse(window, self.color, (self.draw_x, self.draw_y, self.radius * 2, self.radius * 2)) + pygame.draw.ellipse(window, self.inner_color, (self.x - self.inner_radius, self.y - self.inner_radius, self.inner_radius * 2, self.inner_radius * 2)) diff --git a/scripts/rocket.py b/scripts/rocket.py index 8a974fb..87bd667 100644 --- a/scripts/rocket.py +++ b/scripts/rocket.py @@ -33,6 +33,11 @@ def __init__(self, x, y, color): self.shoot_timer = 0 self.current_powerup = 0 + self.missile_exploded = False + self.missile_explosion_radius = 150 + self.missile_timer = 0 + self.missile_explosion_x = 0 + self.missile_explosion_y = 0 def assign_powerup(self, powerup=0): self.current_powerup = powerup From b3f80e543e6f0c210c9029c720bfd54b2a84f156 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh <43755491+MysteryCoder456@users.noreply.github.com> Date: Fri, 8 Nov 2019 03:34:46 +0400 Subject: [PATCH 08/10] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 95cb735..2019f48 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## New Features 1. Added Missile PowerUp. -2. When you shot 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. +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. ## Changes From 19bb34ea53daeb71b4b87e20d11586433e2092bd Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Fri, 8 Nov 2019 13:11:37 +0400 Subject: [PATCH 09/10] Updated shell scripts --- README.md | 2 +- setup_unix.sh | 3 ++- start_unix.sh | 0 3 files changed, 3 insertions(+), 2 deletions(-) mode change 100644 => 100755 setup_unix.sh mode change 100644 => 100755 start_unix.sh diff --git a/README.md b/README.md index e3ed93d..fcc0091 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ To install this game follow these steps: Right-click the file `start_win.bat` And click Run as Administator. **MacOS and Linux**: -Open the game folder in Terminal and enter `sh start_unix.sh`. +Open the game folder in Terminal and enter `./start_unix.sh`. ## How to play diff --git a/setup_unix.sh b/setup_unix.sh old mode 100644 new mode 100755 index 564428b..4157b39 --- a/setup_unix.sh +++ b/setup_unix.sh @@ -1,3 +1,4 @@ echo "installing dependencies..." pip install pygame -sh ./start_unix.sh +chmod +x ./start_unix.sh +./start_unix.sh diff --git a/start_unix.sh b/start_unix.sh old mode 100644 new mode 100755 From 7498d2bd1a7a4b061db30d9641514b40069c11c2 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Fri, 8 Nov 2019 15:23:08 +0400 Subject: [PATCH 10/10] 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