-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamelayers.py
268 lines (230 loc) · 7.56 KB
/
gamelayers.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
import pyglet
from pyglet.sprite import Sprite
from pyglet.gl import glPushMatrix, glPopMatrix
from cocos.layer import Layer, ColorLayer
from cocos.text import Label
from cocos.director import director
from cocos.actions import Accelerate, MoveBy, Delay, Hide, CallFunc
from cocos.scenes.transitions import ShuffleTransition
from cocos.scene import Scene
from cocos.layer import MultiplexLayer
from status import status
from menus import MainMenu, OptionsMenu
import hiscore
class PygletParallax(Layer):
""" Do some parallax with pyglet """
def __init__(self):
super().__init__()
self.batch = pyglet.graphics.Batch()
self.background = pyglet.graphics.OrderedGroup(0)
self.foreground = pyglet.graphics.OrderedGroup(1)
self.bg = Sprite(
pyglet.resource.image("parallax1-1024x1900.png"),
batch=self.batch,
group=self.background,
)
self.fg = Sprite(
pyglet.resource.image("parallax2-1024x768.png"),
batch=self.batch,
group=self.foreground,
)
self.yspeed = 10
self.yscroll = 0
self.yscrollb = 0
self.bg.x, self.bg.y = 0, 0
self.fg.x, self.fg.y = 0, 0
def update(self, dt):
self.yscroll = self.yscroll + self.yspeed
if self.yscroll == 320:
self.yspeed = -1
if self.yscroll == -960:
self.yspeed = 2
self.yscrollb = self.yscroll
if self.yscrollb < -640:
self.yscrollb = -640
if self.yscrollb > 0:
self.yscrollb = 0
self.bg.y = self.yscroll
self.fg.y = self.yscrollb
def draw(self):
self.batch.draw()
class BackgroundLayer(Layer):
def __init__(self):
super(BackgroundLayer, self).__init__()
self.img = pyglet.resource.image("background.png")
def draw(self):
glPushMatrix()
self.transform()
self.img.blit(0, 0)
glPopMatrix()
class IntroLayer(Layer):
is_event_handler = True
def __init__(self):
super(IntroLayer, self).__init__()
self.img = pyglet.resource.image("background.png")
self.intro_text = Label(
"Made by some Asshole:",
font_size=24,
font_name="Times New Roman",
color=(255, 255, 255, 255),
anchor_x="left",
anchor_y="bottom",
)
self.intro_text.position = (0, 0)
self.add(self.intro_text)
def draw(self):
glPushMatrix()
self.transform()
self.intro_text.draw()
self.img.blit(0, 0)
glPopMatrix()
def start_game(self):
scene = Scene()
scene.add(
MultiplexLayer(
MainMenu(),
OptionsMenu(),
HiScoresLayer(),
),
z=1,
)
scene.add(BackgroundLayer(), z=0)
director.push(ShuffleTransition(scene, 1.5))
def on_key_press(self, k, m):
if k in (
pyglet.window.key.ENTER,
pyglet.window.key.ESCAPE,
pyglet.window.key.SPACE,
):
self.start_game()
return True
def on_mouse_press(self, x, y, b, m):
self.start_game()
return True
class ScoreLayer(Layer):
def __init__(self):
width, height = director.get_window_size()
super(ScoreLayer, self).__init__()
self.add(ColorLayer(32, 32, 32, 32, width=width, height=height), z=-1)
self.position = (0, height - 48)
self.score = Label(
"Score:",
font_size=36,
font_name="Times New Roman",
color=(255, 255, 255, 255),
anchor_x="left",
anchor_y="bottom",
)
self.score.position = (0, 0)
self.add(self.score)
def draw(self):
super(ScoreLayer, self).draw()
self.score.element.text = "Score:%d" % status.score
class MessageLayer(Layer):
def show_message(self, message, callback=None):
width, height = director.get_window_size()
self.message = Label(
message,
font_size=52,
font_name="Times New Roman",
anchor_x="center",
anchor_y="center",
)
self.message.position = (width / 2.0, height)
self.add(self.message)
actions = (
Accelerate(MoveBy((0, -height / 2.0), duration=0.5))
+ Delay(1)
+ Accelerate(MoveBy((0, -height / 2.0), duration=0.5))
+ Hide()
)
if callback:
actions += CallFunc(callback)
self.message.do(actions)
class HUD(Layer):
def __init__(self):
super(HUD, self).__init__()
self.add(ScoreLayer())
self.add(MessageLayer(), name="message")
def show_message(self, message, callback=None):
self.get("message").show_message(message, callback)
class HiScoresLayer(ColorLayer):
FONT_SIZE = 30
is_event_handler = True
def __init__(self):
width, height = director.get_window_size()
super(HiScoresLayer, self).__init__(
32, 32, 32, 16, width=width, height=height - 86
)
self.font_title = {}
self.font_title["font_name"] = "Times New Roman"
self.font_title["font_size"] = 72
self.font_title["color"] = (204, 164, 164, 255)
self.font_title["anchor_x"] = "center"
self.font_title["anchor_y"] = "top"
title = Label("Scores!", **self.font_title)
title.position = (width / 2.0, height)
self.add(title, z=1)
self.table = None
def on_enter(self):
super(HiScoresLayer, self).on_enter()
scores = hiscore.hiscore.get()
if self.table:
self.remove_old()
self.table = []
for idx, s in enumerate(scores):
pos = Label(
"%d:" % (idx + 1),
font_name="Times New Roman",
font_size=self.FONT_SIZE,
anchor_y="top",
anchor_x="left",
color=(255, 255, 255, 255),
)
name = Label(
s[1],
font_name="Times New Roman",
font_size=self.FONT_SIZE,
anchor_y="top",
anchor_x="left",
color=(255, 255, 255, 255),
)
score = Label(
str(s[0]),
font_name="Times New Roman",
font_size=self.FONT_SIZE,
anchor_y="top",
anchor_x="right",
color=(255, 255, 255, 255),
)
self.table.append((pos, name, score))
self.process_table()
def remove_old(self):
for item in self.table:
pos, name, score = item
self.remove(pos)
self.remove(name)
self.remove(score)
self.table = None
def process_table(self):
w, h = director.get_window_size()
for idx, item in enumerate(self.table):
pos, name, score = item
posy = h - 100 - ((self.FONT_SIZE + 15) * idx)
pos.position = (5, posy)
name.position = (48, posy)
score.position = (w - 150, posy)
self.add(pos, z=2)
self.add(name, z=2)
self.add(score, z=2)
def on_key_press(self, k, m):
if k in (
pyglet.window.key.ENTER,
pyglet.window.key.ESCAPE,
pyglet.window.key.SPACE,
):
self.parent.switch_to(0)
return True
def on_mouse_release(self, x, y, b, m):
self.parent.switch_to(0)
return True