-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbullet.py
55 lines (47 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
#! /usr/bin/python
import pygame
from pygame.locals import *
from entity import *
from load import *
class Bullet(Entity):
def __init__(self, world, entities, mapx, mapy, x, y, direction):
Entity.__init__(self)
self.load = Load()
self.mapx = mapx
self.mapy = mapy
self.world = world
self.entities = entities
self.xvel = 0
self.yvel = 0
self.direction = direction
self.rect = pygame.Rect(mapx*self.world.mwidth*64+(x*64), mapy*self.world.mheight*64+(y*64), 64, 64)
self.load_image("bullet-%s.png" % direction);
self.type = "bullet"
self.player = False
print "bullet initialized."
def load_image(self, name):
self.image, _ = self.load.image(name, -1)
def update(self):
print "Bullet update."
if self.direction == "left":
self.xvel += 15
elif self.direction == "right":
self.yvel -= 15
self.rect.left += self.xvel
self.collide(self.xvel, 0)
def end_animation(self):
self.kill()
pass
def collide(self, xvel, yvel):
print "bullet collide check."
for entity in self.entities.sprites():
if pygame.sprite.collide_rect(self, entity):
if entity.type != "bullet":
if xvel > 0:
self.rect.right = entity.rect.left
self.end_animation()
if xvel < 0:
self.rect.left = entity.rect.right
self.end_animation()
if entity.type == "alive" and entity.player != True:
entity.kill();