Skip to content

Commit

Permalink
Added new features
Browse files Browse the repository at this point in the history
  • Loading branch information
MysteryCoder456 committed Sep 26, 2019
1 parent a73b777 commit 425b283
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

## New Features

1. Added Bullet class.
1. Added ability for players to shoot bullets.
2. Added collisions between rockets and bullets.
3. Bullets will reappear on the other edge of the screen if they go out of bounds. They will disappear after a while.

## Changes

None
1. Reduced the collider size of rockets.
7 changes: 5 additions & 2 deletions scripts/bullet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ class Bullet:
def __init__(self, x, y, hdg, color):
self.x = x
self.y = y
self.radius = 10
self.radius = 8
self.draw_x = self.x - self.radius
self.draw_y = self.y - self.radius
self.heading = hdg
self.speed = 7
self.speed = 10
self.x_vel = 0
self.y_vel = 0
self.color = color
self.despawn_timer = 0

def render(self, window):
pygame.draw.ellipse(window, self.color, (self.draw_x, self.draw_y, self.radius * 2, self.radius * 2))
Expand All @@ -27,3 +28,5 @@ def update(self):

self.draw_x = self.x - self.radius
self.draw_y = self.y - self.radius

self.despawn_timer += 1
71 changes: 71 additions & 0 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
pygame.init()

from math import sqrt, atan2
from time import sleep
from rocket import Rocket
from bullet import Bullet


class AstroRockets:
Expand Down Expand Up @@ -42,6 +44,13 @@ def logic(self):

self.p1.speed *= friction

if keys[pygame.K_s]:
if not self.p1.bullet_is_shot:
b = Bullet(self.p1.x, self.p1.y, self.p1.drift_heading, self.p1.color)
self.p1.bullets.append(b)
self.p1.bullet_is_shot = True
self.p1.shoot_timer = 0

if keys[pygame.K_a]:
self.p1.drift_heading -= turn_speed

Expand All @@ -55,6 +64,13 @@ def logic(self):

self.p2.speed *= friction

if keys[pygame.K_DOWN]:
if not self.p2.bullet_is_shot:
b = Bullet(self.p2.x, self.p2.y, self.p2.drift_heading, self.p2.color)
self.p2.bullets.append(b)
self.p2.bullet_is_shot = True
self.p2.shoot_timer = 0

if keys[pygame.K_LEFT]:
self.p2.drift_heading -= turn_speed

Expand All @@ -64,6 +80,51 @@ def logic(self):
self.p1.update()
self.p2.update()

despawn_time = 400

for bullet in self.p1.bullets:
bullet.update()

if bullet.x < 0:
bullet.x = self.width
elif bullet.x > self.width:
bullet.x = 0

if bullet.y < 0:
bullet.y = self.height
elif bullet.y > self.height:
bullet.y = 0

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.despawn_timer > despawn_time:
self.p1.bullets.remove(bullet)

for bullet in self.p2.bullets:
bullet.update()

if bullet.x < 0:
bullet.x = self.width
elif bullet.x > self.width:
bullet.x = 0

if bullet.y < 0:
bullet.y = self.height
elif bullet.y > self.height:
bullet.y = 0

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.despawn_timer > despawn_time:
self.p2.bullets.remove(bullet)


# Player 1 boundary collisions
if self.p1.x < 0:
self.p1.x = self.width
Expand All @@ -90,6 +151,12 @@ def render(self):
self.p1.render(self.win)
self.p2.render(self.win)

for bullet in self.p1.bullets:
bullet.render(self.win)

for bullet in self.p2.bullets:
bullet.render(self.win)

# Gameplay functions

def dist(self, x1, y1, x2, y2):
Expand Down Expand Up @@ -155,6 +222,10 @@ def collision_circle(self, x1, y1, r1, x2, y2, r2):










Expand Down
8 changes: 7 additions & 1 deletion scripts/rocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ def __init__(self, x, y, color):
self.vertices = [(self.vertex_distance, 0), (self.vertex_distance, self.rear_vertex_angle), (self.vertex_distance, -self.rear_vertex_angle)] # Polar coordinates

self.color = color
self.collider_size = 28
self.collider_size = 20
self.bullets = []
self.bullet_is_shot = False
self.shoot_timer = 0

def render(self, window):
v = self.vertices
Expand Down Expand Up @@ -48,3 +50,7 @@ def update(self):
self.y += self.y_vel

self.vertices = [(self.vertex_distance, self.drift_heading), (self.vertex_distance, self.rear_vertex_angle+self.drift_heading), (self.vertex_distance, -self.rear_vertex_angle+self.drift_heading)]
self.shoot_timer += 1

if self.shoot_timer > 100:
self.bullet_is_shot = False

0 comments on commit 425b283

Please sign in to comment.