Skip to content

Commit

Permalink
Changed some things
Browse files Browse the repository at this point in the history
  • Loading branch information
MysteryCoder456 committed Nov 8, 2019
1 parent 0d2550c commit 7498d2b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 45 deletions.
8 changes: 5 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
97 changes: 56 additions & 41 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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]:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -317,17 +331,23 @@ 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:
self.p2.missile_timer += 1

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)

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -466,7 +482,6 @@ def main():
quit()

game.logic()
game.win.fill(game.background)
game.render()


Expand Down
5 changes: 5 additions & 0 deletions scripts/powerups.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion scripts/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7498d2b

Please sign in to comment.