Skip to content

Commit ca6cba3

Browse files
committed
obstacle-race: 難易度調整、ゲームオーバー追加
1 parent 120653a commit ca6cba3

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

11-obstacle-race/main.py

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# ]
1313
# ///
1414

15-
import time
1615
import random
1716
import pyxel
1817

@@ -92,7 +91,8 @@ def update(self):
9291
self.frame_count = pyxel.frame_count
9392
last_y = self.y
9493
if pyxel.btn(pyxel.KEY_LEFT):
95-
self.dx = -1 * (2 if pyxel.btn(pyxel.KEY_SHIFT) else 1)
94+
# 戻れないよ
95+
# self.dx = -1 * (2 if pyxel.btn(pyxel.KEY_SHIFT) else 1)
9696
self.direction = -1
9797
if pyxel.btn(pyxel.KEY_RIGHT):
9898
self.dx = 2 * (2 if pyxel.btn(pyxel.KEY_SHIFT) else 1)
@@ -126,7 +126,7 @@ def update(self):
126126
self.is_falling = self.y > last_y
127127

128128
if self.y >= _height:
129-
game_over()
129+
reset()
130130

131131
def draw(self):
132132
u = (2 if self.is_falling else self.frame_count // 3 % 2) * 8
@@ -181,10 +181,23 @@ def __init__(self, width, height):
181181
Obstacle(x, 72, self.img)
182182
]
183183
for i in range(100):
184-
x += random.randint(3, 15) * 8
184+
if x < 500:
185+
x += random.randint(4, 12) * 8
186+
elif x < 1000:
187+
x += random.randint(3, 10) * 8
188+
elif x < 1500:
189+
x += random.randint(2, 7) * 8
190+
else:
191+
x += random.randint(1, 4) * 8
185192
self.obstacles.append(Obstacle(x, 72, self.img))
186193

187194
def update(self):
195+
global is_gameover
196+
if is_gameover:
197+
if pyxel.btnp(pyxel.KEY_SPACE):
198+
reset()
199+
return
200+
188201
# global is_loose, show_bb, is_pback
189202
# if pyxel.btnp(pyxel.KEY_1):
190203
# show_bb = not show_bb
@@ -193,7 +206,7 @@ def update(self):
193206
# elif pyxel.btnp(pyxel.KEY_3):
194207
# is_pback = not is_pback
195208
if pyxel.btnp(pyxel.KEY_4):
196-
game_over()
209+
reset()
197210
player.update()
198211
for obs in self.obstacles:
199212
obs.update()
@@ -202,7 +215,7 @@ def update(self):
202215
for obs in self.obstacles:
203216
# もしobsとplayerが4ピクセル以上重なっていたら衝突
204217
if abs(obs.x - x) <= 4 and abs(obs.y - y) <= 4:
205-
game_over()
218+
is_gameover = True
206219

207220

208221
def render(self):
@@ -221,17 +234,23 @@ def render(self):
221234
for obs in self.obstacles:
222235
obs.draw()
223236
player.draw()
237+
238+
# Draw game over
239+
global is_gameover
240+
if is_gameover:
241+
g.rectb(20, 30, 88, 20, 8)
242+
g.text(48, 32, "GAME OVER", 8)
243+
g.text(30, 40, f"DISTANCE: {player.x/10:4.1f} m", 7)
224244
return g
225245

226246

227-
def game_over():
228-
time.sleep(5)
247+
def reset():
248+
global is_gameover
229249
player.x = 0
230250
player.y = 30
231251
player.dx = 0
232252
player.dy = 0
233-
global is_gameover
234-
is_gameover = True
253+
is_gameover = False
235254

236255

237256
class ParentApp:

0 commit comments

Comments
 (0)