/
newtototone.py
272 lines (214 loc) · 6.27 KB
/
newtototone.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
#!python
# -*- mode: python; Encoding: utf-8; coding: utf-8 -*-
# Last updated: <2019/02/10 02:52:35 +0900>
"""
NEWTOTOTONE (Pyxel version)
Windows10 x64 + Python 3.7.1 32bit
"""
import pyxel
import random
scrw, scrh = 160, 120
score = 0
infos = []
class Player:
def __init__(self):
self.init()
def init(self):
self.alive = True
self.x = pyxel.width / 2
self.y = pyxel.height - 8 - 8
self.flip = 0
self.t = 0
def update(self):
if self.alive:
# move player
spd = 1
if pyxel.btn(pyxel.KEY_LEFT):
self.x -= spd
self.flip = 0
elif pyxel.btn(pyxel.KEY_RIGHT):
self.x += spd
self.flip = 1
if self.x <= 8:
self.x = 8
if self.x >= pyxel.width - 8:
self.x = pyxel.width - 8
self.t += 1
def draw(self):
id = 6
if self.alive:
id += (self.t % 40) // 20 * 2
else:
id = 10
self.flip = self.t % 20 // 10
x, y = self.x - 8, self.y - 8
u, v = id * 8, 0
w, h = 16, 16
if self.flip:
w = -16
imgn, colkey = 0, 10
pyxel.blt(x, y, imgn, u, v, w, h, colkey)
class Item:
def __init__(self, x, y, kind):
self.alive = True
self.x, self.y = x, y
self.dx, self.dy = 0, 0
self.kind = kind
self.hit = False
self.sprid = 2 + (self.kind) * 2
def update(self):
if self.alive:
self.x += self.dx
self.y += self.dy
self.dy += 0.25 * 9.8 / 60.0
if self.y - 8 > pyxel.height:
self.y = -16
self.dy = 0
self.alive = False
elif self.hit:
if self.kind == 0:
global score
score += 10
born_info(self.x, self.y, 10)
pyxel.play(0, 0, loop=False)
self.alive = False
def draw(self):
if self.alive:
x, y = self.x - 8, self.y - 8
u, v = self.sprid * 8, 0
w, h = 16, 16
imgn, colkey = 0, 10
pyxel.blt(x, y, imgn, u, v, w, h, colkey)
class Info:
def __init__(self, x, y, str):
self.alive = True
self.x, self.y = x, y
self.str = str
self.timer = 0
def update(self):
self.y -= 0.5
self.timer += 1
if self.timer > 30:
self.alive = False
def draw(self):
pyxel.text(self.x + 1, self.y + 1, self.str, 0)
pyxel.text(self.x, self.y, self.str, 7)
def born_info(_x, _y, _score):
global infos
infos.append(Info(_x, _y - 16, "+%d" % _score))
def update_sprites(objs):
for o in objs:
o.update()
def remove_sprites(objs):
if len(objs) > 0:
elen = len(objs)
for i in reversed(range(elen)):
if not objs[i].alive:
objs.pop(i)
def draw_sprites(objs):
for o in objs:
o.draw()
def hitcheck(p, objs):
for spr in objs:
dx = abs(spr.x - p.x)
dy = abs(spr.y - p.y)
if spr.kind == 0:
if dx <= 8.0 and dy <= 8.0:
spr.hit = True
elif spr.kind == 1:
if (dx * dx + dy * dy) < (8 * 8):
spr.hit = True
p.alive = False
def print_center(s, y):
x = (pyxel.width - (len(s) * 4)) // 2
pyxel.text(x + 1, y + 1, s, 0)
pyxel.text(x, y, s, 7)
def draw_bg():
x, y = 0, 0
tm = 0
u, v, w, h = 0, 0, 20, 15
pyxel.bltm(x, y, tm, u, v, w, h)
class App:
def __init__(self):
global score
global infos
pyxel.init(scrw, scrh, caption="NEWTOTOTONE", fps=60)
pyxel.load("assets/newtototone.pyxel")
self.player = Player()
score = 0
self.hiscore = 0
self.gmstep = 0
self.brate = 0
self.t = 0
self.brate = 100
self.items = []
infos = []
pyxel.run(self.update, self.draw)
def update(self):
global score
global infos
if pyxel.btnp(pyxel.KEY_Q):
pyxel.quit()
if self.gmstep == 0:
# title
if pyxel.btnp(pyxel.KEY_UP):
score = 0
self.gmstep = 1
self.brate = 100
self.t = 0
random.seed(0)
# start BGM
pyxel.playm(0, loop=True)
elif self.gmstep == 1:
# game main
hitcheck(self.player, self.items)
self.player.update()
# born item
if self.t % 60 == 0:
self.brate -= 3
if self.brate <= 1:
self.brate = 100
if self.t % 10 == 0:
x = random.randint(8, pyxel.width - 8 - 1)
b = max(20, self.brate)
k = 1 if random.randint(0, 100) > b else 0
self.items.append(Item(x, -16, k))
update_sprites(self.items)
update_sprites(infos)
remove_sprites(self.items)
remove_sprites(infos)
if score > self.hiscore:
self.hiscore = score
if not self.player.alive:
self.gmstep = 2
self.t = 0
# stop BGM
pyxel.stop()
# play dead SE
pyxel.play(0, 1, loop=False)
elif self.gmstep == 2:
# game over
self.player.update()
if self.t >= 60 + 30:
self.player.init()
self.gmstep = 0
self.items = []
infos = []
self.t += 1
def draw(self):
global score
global infos
pyxel.cls(0)
draw_bg()
self.player.draw()
draw_sprites(self.items)
draw_sprites(infos)
s = "SCORE: %d HI-SCORE: %d" % (score, self.hiscore)
pyxel.text(1, 1, s, 7)
if self.gmstep == 0:
print_center("NEWTOTOTONE", 40)
print_center("MOVE: LEFT,RIGHT KEY", 74)
print_center("PUSH UP KEY TO START", 84)
elif self.gmstep == 2:
print_center("GAME OVER", pyxel.height / 2 - 6)
App()