-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLaser.py
More file actions
97 lines (80 loc) · 2.47 KB
/
Laser.py
File metadata and controls
97 lines (80 loc) · 2.47 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import sys, pygame
from random import randrange, uniform
class Laser(pygame.sprite.Sprite):
x = 0
y = 0
dx = 0
dy = 0
image = None
image_w = 0
image_h = 0
rect = None
active = True
screen = None
speed = (0 , 7)
def __init__(self, screen_, location):
super(Laser, self).__init__()
self.screen = screen_
try:
Laser.image = pygame.image.load("assets/laser.gif").convert()
self.image = Laser.image
self.rect = self.image.get_rect()
except IOError:
print "An error occured trying to read the file."
self.x, self.y = location[0], location[1]
self.image_w = self.rect.width
self.image_h = self.rect.height
self.reset()
self.play_sound()
def play_sound(self):
pygame.mixer.music.load('assets/laser.wav')
pygame.mixer.music.play()
def draw(self):
self.screen.blit(self.image, self.rect)
def update(self):
self.x += self.speed[0]
self.y -= self.speed[1]
self.rect.topleft = [self.x, self.y]
if self.y < -10 :
self.active = False
def event_handler(self, event):
pass
def keyhold_handler(self):
pass
def reset(self):
self.x -= self.image_w / 2
self.y -= self.image_h / 2
def kill(self):
del self
#=========================================================================
if __name__ == "__main__":
pygame.init()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((800, 600))
counter = 0
span = randrange(10, 35)
lasers = []
while(True):
fpsClock.tick(50)
counter += 1
screen.fill((0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT :
pygame.quit();sys.exit();
if event.type == pygame.KEYDOWN :
if event.key == pygame.K_ESCAPE:
pygame.quit();sys.exit();
for las_ in lasers :
if las_.active :
las_.update()
las_.draw()
else :
lasers.remove(las_)
las_.kill()
if counter > span :
las = Laser(screen, (randrange(0, 800), 600))
las.speed = (0, randrange(5, 25))
lasers.append(las)
counter = 0
span = randrange(10, 35)
pygame.display.flip()