-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathej10.py
146 lines (119 loc) · 4.16 KB
/
ej10.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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from PySFML import sf
class Sprite(object):
def __init__(self, image, x, y):
self.image = sf.Image()
self.image.LoadFromFile(image)
self.sprite = sf.Sprite(self.image)
self.width, self.height = self.sprite.GetSize()
self.sprite.SetCenter(self.width/2, self.height/2)
self.sprite.SetPosition(x, y)
self.dead = False
def get_rect(self):
x, y = self.sprite.GetPosition()
return sf.FloatRect(x - self.width/2,
y - self.height/2,
x + self.width/2,
y + self.height/2)
def draw(self, window):
window.Draw(self.sprite)
def die(self):
self.dead = True
class Ship(Sprite):
def __init__(self, x, y):
super(Ship, self).__init__("ship.png", x, y)
self.velocity = 150
self.score = 0
self.bullet_rest = sf.Clock()
def update(self, input_, delta):
if input_.IsKeyDown(sf.Key.Left):
self.sprite.Move(-self.velocity * delta, 0)
if input_.IsKeyDown(sf.Key.Right):
self.sprite.Move(self.velocity * delta, 0)
def score_up(self, points):
self.score += points
class Invader(Sprite):
def __init__(self, x, y):
super(Invader, self).__init__("alien.png", x, y)
self.x_velocity = 100
self.y_velocity = 10
self.going_right = True
self.going_down = False
self.initial_x = x
self.y_distance = 0
self.sprite.SetColor(sf.Color.Green)
self.count = 0
def update(self, delta):
x_pos, y_pos = self.sprite.GetPosition()
x, y = 0, 0
if not self.going_down:
if x_pos > self.initial_x + 50 or \
x_pos < self.initial_x - 50:
self.going_right = not self.going_right
self.count = (self.count +1) % 5
x = self.x_velocity * (self.going_right * 2 -1) * delta
if self.count == 4:
self.going_down = True
if self.going_down:
y = self.y_velocity * delta
self.y_distance += y
if self.y_distance > 3:
self.going_down = False
self.y_distance = 0
self.sprite.Move(x, y)
class Bullet(Sprite):
def __init__(self, ship):
x, y = ship.sprite.GetPosition()
super(Bullet, self).__init__("bullet.png", x, y)
self.velocity = -300
def update(self, aliens, ship, delta):
self.sprite.Move(0, self.velocity * delta)
rect = self.get_rect()
for alien in aliens:
if rect.Intersects(alien.get_rect()):
alien.die()
self.die()
ship.score_up(10)
def main():
window = sf.RenderWindow(sf.VideoMode(640, 480),
"Ejemplo 08")
window.SetFramerateLimit(60)
score = sf.String("Score: ")
score.SetSize(30)
score.SetPosition(5, 5)
ship = Ship(320, 400)
aliens = []
bullets = []
for i in range(8):
for j in range(4):
aliens.append(Invader(100 + i * 50, 30 + j * 50))
event = sf.Event()
running = True
while running:
while window.GetEvent(event):
if event.Type == sf.Event.Closed:
running = False
elif event.Type == sf.Event.KeyPressed and \
event.Key.Code == sf.Key.Space:
if ship.bullet_rest.GetElapsedTime() > 0.5:
bullets.append(Bullet(ship))
ship.bullet_rest.Reset()
input_ = window.GetInput()
delta = window.GetFrameTime()
ship.update(input_, delta)
for alien in aliens:
alien.update(delta)
aliens = filter(lambda x: not x.dead, aliens)
for bullet in bullets:
bullet.update(aliens, ship, delta)
bullets = filter(lambda x: not x.dead, bullets)
score.SetText("Score: %d" % ship.score)
window.Clear()
ship.draw(window)
for alien in aliens:
alien.draw(window)
for bullet in bullets:
bullet.draw(window)
window.Draw(score)
window.Display()
if __name__ == '__main__':
main()