-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbullet.py
56 lines (40 loc) · 1.35 KB
/
bullet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import time
import math
import pygame
from wall import WallDirection
class Bullet:
def __init__(self, x, y, direction, radius=2, color=(255,255,255)):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.fired_at = time.time()
self.vel = 3.5
self.direction = direction
self.hit_wall = False
self.active = True
self.px = None # previous x value
self.py = None # previous y value
def move(self, walls):
self.px = self.x
self.py = self.y
self.y += self.vel * math.sin(math.radians(self.direction))
self.x += self.vel * math.cos(math.radians(self.direction))
wall = self.contact_wall(walls)
if wall:
self.hit_wall = True
self.bounce(wall)
def contact_wall(self, walls):
for wall in walls:
if wall.has_collision((self.x,self.y), self.radius):
return wall
return None
def bounce(self, wall):
if wall.direction == WallDirection.VERT:
self.direction = 180 - self.direction
else:
self.direction = -(self.direction)
def draw(self, win):
self.rect = pygame.draw.circle(win, self.color, (self.x,self.y), self.radius)
def inactive(self):
self.active = False