-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
280 lines (213 loc) · 8.04 KB
/
example.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
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/usr/bin/env python
"""
Harness demo, a game example.
Tested with Python 3.4, may or may not work with Python 2!
"""
from random import randint, shuffle
from harness import Harness
game = Harness(title="pysdl2 HARNESS demo", width=240, height=240, zoom=3)
if game.has_controllers:
# use first game controller; harness will manage this internally
# but it's useful to keep a reference (eg, to set the keyboard mapping,
# get the controller name, etc)
controller = game.controllers[0]
background = game.load_resource("background.png")
title = game.load_resource("title.png")
font = game.load_bitmap_font("font.png", width=6, height=10)
# load all the sprites in just one image so they're in one big texture
tiles = game.load_resource("tiles.png")
game.set_icon("icon.png")
dance = game.load_resource("harness-dance.ogg")
dance_hurry = game.load_resource("harness-dance-hurry.ogg")
gameover = game.load_resource("gameover.ogg")
hurryup = game.load_resource("hurryup.ogg")
time = game.load_resource("time.ogg")
scenes = []
hiscore = 0
class MenuScene(object):
# only one direction
dragon_frames = ((0, 24, 24, 24), (24, 24, 24, 24))
knight_frames = ((48, 48, 24, 24), (72, 48, 24, 24))
def __init__(self):
self.intro_channel = None
self.counter = 2
self.title_y = -100
self.anim_delay = 0
self.frame = 0
self.dragon = [tiles.get_texture(*frames) for frames in self.dragon_frames]
self.knight = [tiles.get_texture(*frames) for frames in self.knight_frames]
self.boing = game.load_resource("boing.ogg")
game.play(self.boing)
def draw(self, renderer):
renderer.draw(background)
renderer.draw(title, dest_rect=(0, int(self.title_y), 240, 60))
# wait until the title is in place
if self.title_y >= 40:
if 2 < self.counter < 12:
renderer.draw_text(font, 120, 132, "Press 's' to start!", align="center")
renderer.draw_text(font, 120, 10, "Copyright (c) 2015 usebox.net", align="center")
renderer.draw_text(font, 120, 48, "HI: %04i" % hiscore, align="center")
renderer.draw_text(font, 120, 196, "Use the arrows to move", align="center")
renderer.draw_text(font, 120, 208, "and collect the goodies!", align="center")
renderer.draw_text(font, 120, 106, "a game by @reidrac",
align="center", tint=(98, 100, 220, 255))
# draw the animation cycle; frame 1 will be drawn 1 pixel higher
renderer.draw(self.dragon[self.frame],
x = 12,
y = 192 - self.frame,
)
renderer.draw(self.knight[self.frame],
x = 204,
y = 192 - self.frame,
)
def update(self, dt):
# initial animation (title falls into place)
if self.title_y < 40:
self.title_y += dt * 120
return
elif self.boing:
# won't be used again
game.free_resource("boing.ogg")
self.boing = None
elif self.intro_channel is None:
# loop the intro music
self.intro_channel = game.play(dance, loops=-1)
# the "start" will blink
self.counter += dt * 10
if self.counter > 12:
self.counter -= 12
# frame animation
self.anim_delay += dt * 10
if self.anim_delay > 1.5:
self.anim_delay = 0
self.frame = 0 if self.frame else 1
# controls
if game.keys[game.KEY_ESCAPE]:
game.quit()
elif game.keys[game.KEY_S]:
# press "s" to play
game.stop_playback(self.intro_channel)
self.intro_channel = None
scenes.append(ReadyScene())
return
class ReadyScene(object):
def __init__(self):
self.delay = 16
def draw(self, renderer):
renderer.draw(background)
renderer.draw_text(font, 120, 100, "READY?", align="center")
def update(self, dt):
if self.delay > 0:
self.delay -= dt * 10
if self.delay <= 0:
scenes.pop()
scenes.append(PlayScene())
class GameOverScene(object):
def __init__(self):
self.delay = 80
# play it once
game.play(gameover)
def draw(self, renderer):
renderer.draw(background)
renderer.draw_text(font, 120, 100, "GAME OVER", align="center")
def update(self, dt):
if self.delay > 0:
self.delay -= dt * 10
if self.delay <= 0:
# back to menu
scenes.pop()
class PlayScene(object):
BW = 7
BH = 7
MAX_TILES = 10
TILES = 8
def __init__(self):
self.stage = 1
self.score = 0
# subtextures for the board tiles
self.tiles = [tiles.get_texture(*tuple([i * 24, 0, 24, 24])) for i in range(self.MAX_TILES)]
self.music_channel = game.play(dance, loops=-1)
self.next_stage()
def next_stage(self):
self.time = 12
self.hurry_up = None
self.time_tint = None
self.game_over = None
self.prev_time = 0
# generate a random board
tileset = [range(self.MAX_TILES)]
shuffle(tileset)
self.board = [randint(0, self.TILES - 1) for i in range(self.BW * self.BH)]
def draw(self, renderer):
renderer.draw(background)
renderer.draw_text(font, 4, 4, "SCORE %04i" % self.score)
renderer.draw_text(font, 236, 4, "STAGE %i" % self.stage, align="right")
if self.hurry_up:
# blink a warning when the time is running out
if int(self.hurry_up) & 1:
renderer.draw_text(font, 120, 9, "HURRY UP!", align="center")
else:
renderer.draw_text(font, 120, 9, "TIME: %02i" % int(self.time), align="center", tint=self.time_tint)
# draw the board
for y in range(self.BH):
for x in range(self.BW):
renderer.draw(self.tiles[self.board[x + y * self.BW]],
x=(120 - (24 * self.BW // 2)) + x * 24,
y=(120 - (24 * self.BH // 2)) + y * 24,
)
def update(self, dt):
# HURRY UP! delay
if self.hurry_up:
self.hurry_up -= dt * 10
if self.hurry_up <= 0:
self.hurry_up = 0
if self.music_channel is None:
self.music_channel = game.play(dance_hurry, loops=-1)
else:
return
self.time -= dt
# set HURRY UP once
if int(self.time) == 10 and self.hurry_up is None:
self.hurry_up = 12
self.time_tint = (255, 0, 0, 255)
self.prev_time = 10
game.play(hurryup)
game.stop_playback(self.music_channel)
self.music_channel = None
return
# beep on the last seconds
if self.prev_time and int(self.time) != self.prev_time:
self.prev_time = int(self.time)
game.play(time)
# set GAME OVER
if int(self.time) == 0:
# stop music
if self.music_channel is not None:
game.stop_playback(self.music_channel)
global hiscore
if self.score > hiscore:
hiscore = self.score
scenes.pop()
scenes.append(GameOverScene())
return
# controls
if game.keys[game.KEY_ESCAPE]:
# avoid leaving the game just after
# leaving this scene!
game.keys[game.KEY_ESCAPE] = False
# stop music
if self.music_channel is not None:
game.stop_playback(self.music_channel)
# go to previous scene
scenes.pop()
return
@game.draw
def draw(renderer):
# draw last scene
scenes[-1].draw(renderer)
@game.update
def update(dt):
# uddate last scene
scenes[-1].update(dt)
scenes.append(MenuScene())
game.loop()