Skip to content

Commit

Permalink
Added walls
Browse files Browse the repository at this point in the history
  • Loading branch information
MysteryCoder456 committed Oct 4, 2019
1 parent a6eef90 commit 0e54128
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## New Features

1. Added wall class
1. Added walls

## Changes

Expand Down
4 changes: 4 additions & 0 deletions scripts/bullet.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ def __init__(self, x, y, hdg, color):
self.y_vel = 0
self.color = color
self.despawn_timer = 0
self.wall_collider = pygame.Rect(self.draw_x, self.draw_y, self.radius * 2, self.radius * 2)

def update_collider(self):
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 Down
45 changes: 43 additions & 2 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def start(self):
self.p2.heading = 180
self.p2.drift_heading = 180

self.wall = Wall(100, 50, 100, 50)
self.levels = [(Wall(100, 100, 300, 135), Wall(500, 500, 50, 100))]
self.current_level = 0

def logic(self):
keys = pygame.key.get_pressed()
Expand Down Expand Up @@ -98,6 +99,8 @@ def logic(self):
bullet.y = self.height
elif bullet.y > self.height:
bullet.y = 0

bullet.update_collider()

if self.collision_circle(bullet.x, bullet.y, bullet.radius, self.p2.x, self.p2.y, self.p2.collider_size):
print("RED WINS!!")
Expand All @@ -120,6 +123,8 @@ def logic(self):
elif bullet.y > self.height:
bullet.y = 0

bullet.update_collider()

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)
Expand Down Expand Up @@ -151,6 +156,41 @@ def logic(self):
elif self.p2.y > self.height:
self.p2.y = 0

self.p1.update_collider()
self.p2.update_collider()

# Handle collisions between rockets and walls
for wall in self.levels[self.current_level]:
# Player 1
if wall.collider.colliderect(self.p1.wall_collider):
self.p1.x = self.p1.old_x
self.p1.y = self.p1.old_y
self.p1.x_vel = 0
self.p1.y_vel = 0

# Player 2
if wall.collider.colliderect(self.p2.wall_collider):
self.p2.x = self.p2.old_x
self.p2.y = self.p2.old_y
self.p2.x_vel = 0
self.p2.y_vel = 0

# Handle collisions between bullets and walls
for wall in self.levels[self.current_level]:
# Player 1
for bullet in self.p1.bullets:
print(bullet.wall_collider.colliderect(wall.collider))
if bullet.wall_collider.colliderect(wall.collider):
self.p1.bullets.remove(bullet)

# Player 2
for bullet in self.p2.bullets:
if bullet.wall_collider.colliderect(wall.collider):
self.p2.bullets.remove(bullet)

self.p1.update_old_coords()
self.p2.update_old_coords()

def render(self):
self.p1.render(self.win)
self.p2.render(self.win)
Expand All @@ -161,7 +201,8 @@ def render(self):
for bullet in self.p2.bullets:
bullet.render(self.win)

self.wall.render(self.win)
for wall in self.levels[self.current_level]:
wall.render(self.win)

# Gameplay functions

Expand Down
12 changes: 10 additions & 2 deletions scripts/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ class Rocket:
def __init__(self, x, y, color):
self.x = x
self.y = y
self.old_x = x
self.old_y = y
self.x_vel = 0
self.y_vel = 0
self.drift_x_vel = 1
self.drift_y_vel = 1
self.drift_heading = 0
self.heading = 0
self.speed = 0
Expand All @@ -20,10 +20,18 @@ 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

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)

def update_old_coords(self):
self.old_x = self.x
self.old_y = self.y

def accelerate(self, speed):
self.speed += speed
self.heading = self.drift_heading
Expand Down

0 comments on commit 0e54128

Please sign in to comment.