-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathc07_aliengame.py
executable file
·180 lines (144 loc) · 4.61 KB
/
c07_aliengame.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#!/usr/bin/env python3
"""
@author Michele Tomaiuolo - https://tomamic.github.io/
@license This software is free - https://opensource.org/license/mit
"""
import random
from actor import Actor, Arena, Point
class Alien(Actor):
def __init__(self, pos: Point):
self._x, self._y = pos
self._w, self._h = 24, 16
self._dx, self._dy = 4, 8
# each alien has its own moving space, e.g. 150px
self._xmin, self._xmax = self._x, self._x + 150
self._pose = 0 # which sprite to use?
def move(self, arena: Arena):
for other in arena.collisions():
if isinstance(other, Missile):
arena.kill(self)
if self._xmin <= self._x + self._dx <= self._xmax:
self._x += self._dx
else:
self._dx = -self._dx
self._y += self._dy
chances = 25000 // (1 + arena.count())
if random.randrange(chances) == 0:
pos = self._x + self._w / 2, self._y + self._h
arena.spawn(Bomb(pos))
self._pose = arena.count() // 8 % 2
def pos(self) -> Point:
return self._x, self._y
def size(self) -> Point:
return self._w, self._h
def sprite(self) -> Point:
if self._pose:
return 24, 354
return 0, 354
class Missile(Actor):
def __init__(self, pos):
self._w, self._h = 4, 8
self._x, self._y = pos
self._x -= self._w / 2
self._y -= self._h
def move(self, arena):
for other in arena.collisions():
if isinstance(other, Alien):
arena.kill(self)
self._y -= 4
if self._y < 0:
arena.kill(self)
def pos(self):
return self._x, self._y
def size(self):
return self._w, self._h
def sprite(self):
return 165, 354
class Bomb(Actor):
def __init__(self, pos):
self._w, self._h = 8, 16
self._x, self._y = pos
self._x -= self._w / 2
def move(self, arena):
for other in arena.collisions():
if isinstance(other, Cannon):
arena.kill(self)
aw, ah = arena.size()
self._y += 4
if self._y > ah:
arena.kill(self)
def pos(self):
return self._x, self._y
def size(self):
return self._w, self._h
def sprite(self):
return 53, 338
class Cannon(Actor):
def __init__(self, pos):
self._x, self._y = pos
self._w, self._h = 28, 16
def move(self, arena):
for other in arena.collisions():
if isinstance(other, Bomb):
arena.kill(self)
aw, ah = arena.size()
keys = arena.current_keys()
prev = arena.previous_keys()
if "ArrowUp" in keys and "ArrowUp" not in prev:
pos = self._x + self._w / 2, self._y
arena.spawn(Missile(pos))
if "ArrowLeft" in keys:
self._x -= 4
elif "ArrowRight" in keys:
self._x += 4
self._x = max(self._x, 0) # clamp
self._x = min(self._x, aw - self._w) # clamp
def pos(self):
return self._x, self._y
def size(self):
return self._w, self._h
def sprite(self):
return 153, 364
class AlienGame(Arena):
def __init__(self, size=(480, 360)):
super().__init__(size)
for y in range(4):
for x in range(8):
self.spawn(Alien((x * 42, y * 24)))
self.spawn(Cannon((240, 340)))
def game_over(self) -> bool:
cannon = False
for a in self.actors():
x, y = a.pos()
if isinstance(a, Alien) and y > 320:
return True
if isinstance(a, Cannon):
cannon = True
return not cannon
def game_won(self) -> bool:
for a in self.actors():
if isinstance(a, Alien):
return False
return True
class AlienGui:
def __init__(self):
self._game = AlienGame()
g2d.init_canvas(self._game.size())
g2d.main_loop(self.tick)
def tick(self):
sprites = "https://fondinfo.github.io/sprites/invaders.png"
g2d.set_color((0, 0, 0))
g2d.draw_rect((0, 0), self._game.size())
for a in self._game.actors():
g2d.draw_image(sprites, a.pos(), a.sprite(), a.size())
if self._game.game_over():
g2d.alert("Game over")
g2d.close_canvas()
elif self._game.game_won():
g2d.alert("Game won")
g2d.close_canvas()
else:
self._game.tick(g2d.current_keys()) # Game logic
if __name__ == "__main__":
import g2d
gui = AlienGui()