From 425b2838fa7a6c444a7c79ffa97ce663d82ce8c5 Mon Sep 17 00:00:00 2001 From: Rehatbir Singh Date: Fri, 27 Sep 2019 01:19:25 +0400 Subject: [PATCH] Added new features --- CHANGELOG.md | 6 ++-- scripts/bullet.py | 7 +++-- scripts/main.py | 71 +++++++++++++++++++++++++++++++++++++++++++++++ scripts/rocket.py | 8 +++++- 4 files changed, 87 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44df995..7e05f72 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/scripts/bullet.py b/scripts/bullet.py index 18bcb52..4ac769f 100644 --- a/scripts/bullet.py +++ b/scripts/bullet.py @@ -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)) @@ -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 diff --git a/scripts/main.py b/scripts/main.py index 21efe99..de3d8b9 100644 --- a/scripts/main.py +++ b/scripts/main.py @@ -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: @@ -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 @@ -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 @@ -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 @@ -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): @@ -155,6 +222,10 @@ def collision_circle(self, x1, y1, r1, x2, y2, r2): + + + + diff --git a/scripts/rocket.py b/scripts/rocket.py index 30eef57..93a2d42 100644 --- a/scripts/rocket.py +++ b/scripts/rocket.py @@ -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 @@ -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