-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
262 lines (197 loc) · 5.83 KB
/
game.py
File metadata and controls
262 lines (197 loc) · 5.83 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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
import PCF8591 as ADC
import pgzrun
from random import *
import math
import os
import sys
# restart program
def restartp():
os.execl(sys.executable, sys.executable, *sys.argv)
# setup raspberrypi
def setup():
ADC.setup(0x48) # Setup PCF8591
global state
# play: game_over
def pgo():
sounds.game_over.play()
# set game status
game_over = False
end = False
finish_choose = False
# define windows size
HEIGHT = 788
WIDTH = 1024
# music.play("bgm") # to play bgm
# restart button, use after game-over
restart = Actor("restart")
restart.pos = WIDTH / 2, 200
# alien's house
house = Actor("house")
house.pos = 75, 75
# bomb's house
bombox = Actor("bombox")
bombox.pos = WIDTH - 75, HEIGHT - 75
# class: alien
class Alien(Actor):
def __init__(self, code):
self.code = str(code) # alien code, 1~3
Actor.__init__(self, "p" + self.code + "/p" + self.code + "_stand")
self.blood = 5
self.home() # init alien's position(on alien's home)
self.ishurt = False # alien if not hurt
def hurt(self):
self.ishurt = True
self.image = "p" + self.code + "/p" + \
self.code + "_hurt" # change image to hurt
self.blood -= 1
sounds.eep.play() # play sound
self.home()
clock.schedule_unique(self.recovery, 1.0) # recovery after 1.0s
def recovery(self):
self.ishurt = False
self.image = "p" + self.code + "/p" + self.code + "_stand" # reset image
def home(self):
self.pos = house.pos
class Bomb(Actor):
def __init__(self):
Actor.__init__(self, "bomb") # set image
self.home() # set position
def move(self):
# move to alien
self.left += (alien.pos[0] - self.left + randint(-700, 700)) / 64
self.top += (alien.pos[1] - self.top + randint(-700, 700)) / 64
def nearalien(self):
# if bomb is near alien
if math.sqrt((self.left - alien.left) ** 2 +
(self.top - alien.top) ** 2) <= 175:
return True
def home(self):
self.left = bombox.left + randint(-100, 100)
self.top = bombox.top + randint(-100, 100)
class lock(Actor):
def __init__(self, code, pos):
self.complete = False
self.second = 300 # 5 second
self.code = str(code)
self.pos = pos
Actor.__init__(self, "lock" + self.code)
def run(self):
self.second -= 1
# alien for choose
alien1 = Actor('p1/p1_front')
alien2 = Actor('p2/p2_front')
alien3 = Actor('p3/p3_front')
alien1.pos = 200, 394
alien2.pos = 500, 394
alien3.pos = 800, 394
# 4 bombs
bomb1 = Bomb()
bomb2 = Bomb()
bomb3 = Bomb()
bomb4 = Bomb()
lock1 = lock(1, (WIDTH / 4, HEIGHT / 4))
lock2 = lock(2, (WIDTH / 4 * 3, HEIGHT / 4 * 3))
lock3 = lock(3, (WIDTH / 4, HEIGHT / 4))
lock4 = lock(4, (WIDTH / 4 * 3, HEIGHT / 4 * 3))
def process(x, y):
# processing datea
if x - 125 < 15 and x - 125 > -15 and y - 125 < 15 and y - 125 > -15:
return "home"
if y == 125:
y = 127 # there is a little error with my device
x -= 127
y -= 127
return [x / 8, y / 8]
def draw():
screen.clear()
screen.blit("background.jpg", (0, 0))
house.draw()
bombox.draw()
# choose
if finish_choose == False:
alien1.draw()
alien2.draw()
alien3.draw()
screen.draw.text(
"choose your alien!", (200, 100), color=(
255, 0, 0), fontsize=100)
# game over
if end:
alien.pos = WIDTH / 2, HEIGHT / 2
alien.image = "p" + alien.code + "/p" + alien.code + "_hurt"
alien.draw()
restart.draw()
# main
elif finish_choose:
alien.draw()
bomb1.draw()
bomb2.draw()
bomb3.draw()
bomb4.draw()
lock1.draw()
lock2.draw()
lock3.draw()
lock4.draw()
for x in range(alien.blood):
x += 1
def update():
global game_over
global end
a = process(ADC.read(0), ADC.read(1))
if finish_choose and not end: # if game start and not game over
if alien.blood <= 0:
game_over = True # if alien don't have blood
if a != "home" and not alien.ishurt:
# don't let alien flies out of stage
if alien.top <= 0:
alien.top += 20
if alien.top + alien.height >= HEIGHT:
alien.top -= 20
if alien.left <= 0:
alien.left += 20
if (alien.left + alien.width) >= WIDTH:
alien.left -= 20
# normal move
alien.top += a[0]
alien.left += a[1]
if alien.ishurt == False:
# bomb move
bomb1.move()
bomb2.move()
bomb3.move()
bomb4.move()
# if one or more bombs are near alien
if bomb1.nearalien() or bomb2.nearalien() or bomb3.nearalien() or bomb4.nearalien():
alien.hurt()
bomb1.home()
bomb2.home()
bomb3.home()
bomb4.home()
if game_over and not end:
# if don't define "end", the "game_over" sounds will loop
end = True
sounds.you_lose.play()
clock.schedule(pgo, 2.0)
def on_mouse_down(pos):
global alien
global finish_choose
if end:
if restart.collidepoint(pos):
restartp()
# if click "restart"
# choose alien
if not finish_choose:
if alien1.collidepoint(pos):
alien = Alien(1)
finish_choose = True
sounds.go.play()
if alien2.collidepoint(pos):
alien = Alien(2)
finish_choose = True
sounds.go.play()
if alien3.collidepoint(pos):
alien = Alien(3)
finish_choose = True
sounds.go.play()
setup()
pgzrun.go()