-
Notifications
You must be signed in to change notification settings - Fork 2
/
shooter.py
46 lines (34 loc) · 1.29 KB
/
shooter.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
import random
from util.vectorsprites import *
from util import *
class Shooter(VectorSprite):
def __init__(self, position, heading, pointlist, stage):
VectorSprite.__init__(self, position, heading, pointlist)
self.bullets = []
self.stage = stage
def fireBullet(self, heading, ttl, velocity):
if (len(self.bullets) < self.maxBullets):
position = Vector2d(self.position.x, self.position.y)
newBullet = Bullet(position, heading, self,
ttl, velocity, self.stage)
self.bullets.append(newBullet)
self.stage.addSprite(newBullet)
return True
def bulletCollision(self, target):
collisionDetected = False
for bullet in self.bullets:
if bullet.ttl > 0 and target.collidesWith(bullet):
collisionDetected = True
bullet.ttl = 0
return collisionDetected
# Bullet class
class Bullet(Point):
def __init__(self, position, heading, shooter, ttl, velocity, stage):
Point.__init__(self, position, heading, stage)
self.shooter = shooter
self.ttl = ttl
self.velocity = velocity
def move(self):
Point.move(self)
if (self.ttl <= 0):
self.shooter.bullets.remove(self)