Skip to content

Commit d0713f2

Browse files
committed
update 05-anahori: kill enemy and player, add score, bound enemy each others. off loose mode.
1 parent 3fd20fc commit d0713f2

File tree

2 files changed

+71
-21
lines changed

2 files changed

+71
-21
lines changed
217 Bytes
Binary file not shown.

10-anahori/main.py

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@
2020
scroll_x = 0
2121
_height = 0
2222
player = None
23-
is_loose = True
23+
is_loose = False
2424
show_bb = False
25-
is_pback = True
25+
is_pback = False
26+
score = 0
2627

2728

2829
def dbg(func):
@@ -108,6 +109,14 @@ def is_wall(x, y, *, include_ladder=False):
108109
return tile == TILE_FLOOR or tile[0] >= WALL_TILE_X
109110

110111

112+
def is_in_wall(x, y) -> bool:
113+
if is_wall(x, y) or is_wall(x + 7, y):
114+
return True
115+
if is_wall(x, y + 7) or is_wall(x + 7, y + 7):
116+
return True
117+
return False
118+
119+
111120
class BaseEnemy:
112121
x: int
113122
y: int
@@ -144,21 +153,42 @@ def reset(self):
144153
self.direction = 1
145154
self.is_alive = True
146155

156+
def is_other_enemy(self, x: int, y: int) -> bool:
157+
for enemy in enemies:
158+
if enemy is self:
159+
continue
160+
if abs(enemy.x - x) < 8 and abs(enemy.y - y) < 8:
161+
return True
162+
return False
163+
164+
def is_in_wall(self) -> bool:
165+
return is_in_wall(self.x, self.y)
166+
147167
def update(self):
168+
if self.is_alive and self.is_in_wall():
169+
self.is_alive = False
170+
global score
171+
score += 100
172+
return
173+
148174
self.dx = self.direction
149175
self.dy = min(self.dy + 1, 3)
176+
150177
if is_wall(self.x, self.y + 8) or is_wall(self.x + 7, self.y + 8):
151178
if self.direction < 0 and (
152-
is_wall(self.x - 1, self.y + 4) or not is_wall(self.x - 1, self.y + 8, include_ladder=True)
179+
is_wall(self.x - 1, self.y + 4) or not is_wall(self.x - 1, self.y + 8, include_ladder=True) or self.is_other_enemy(self.x - 1, self.y)
153180
):
154181
self.direction = 1
155182
elif self.direction > 0 and (
156-
is_wall(self.x + 8, self.y + 4) or not is_wall(self.x + 7, self.y + 8, include_ladder=True)
183+
is_wall(self.x + 8, self.y + 4) or not is_wall(self.x + 7, self.y + 8, include_ladder=True) or self.is_other_enemy(self.x + 8, self.y)
157184
):
158185
self.direction = -1
159186
self.x, self.y = push_back(self.x, self.y, self.dx, self.dy, False)
160187

161188
def draw(self):
189+
if not self.is_alive:
190+
return
191+
162192
u = pyxel.frame_count // 4 % 2 * 8 + 16
163193
w = 8 if self.direction > 0 else -8
164194
self.img.blt(self.x, self.y, 0, u, 16, w, 8, TRANSPARENT_COLOR)
@@ -192,33 +222,41 @@ def __init__(self, img, x, y, type):
192222
def __repr__(self):
193223
return f"Block({self.x=}, {self.y=}, {self.type=}, {self.damage=})"
194224

225+
def reset(self):
226+
self.damage = 0
227+
195228
@property
196229
def is_diggable(self) -> bool:
197230
return self.type == TILE_BRICK
198231

199232
@property
200233
def current_type(self) -> tuple[int, int]:
201234
if self.is_diggable:
202-
return TILE_BRICK if self.damage < 60 else (0, 0)
235+
return TILE_BRICK if self.damage < 20 else (0, 0)
203236
return self.type
204237

205238
def dig(self) -> bool:
206239
if self.is_diggable:
207-
self.damage = min(90, self.damage + 3)
240+
if self.damage < 60 and 60 <= self.damage + 3:
241+
self.damage = 180
242+
elif 60 < self.damage:
243+
pass
244+
else:
245+
self.damage = min(180, self.damage + 3)
208246
return True
209247
return False
210248

211249
def update(self):
212-
self.damage = min(90, self.damage)
250+
self.damage = min(180, self.damage)
213251
self.damage = max(0, self.damage - 1)
214252

215253
def draw(self):
216254
u, v = self.type
217255
if self.damage == 0:
218256
return
219-
elif self.damage <= 30:
257+
elif self.damage <= 20:
220258
u += 1
221-
elif self.damage <= 60:
259+
elif self.damage <= 40:
222260
u += 2
223261
else:
224262
u += 3
@@ -260,6 +298,9 @@ def reset(self):
260298
def die(self):
261299
self.is_die = True
262300

301+
def is_in_wall(self) -> bool:
302+
return is_in_wall(self.x, self.y)
303+
263304
def update_for_die(self):
264305
# サインカーブで飛び上がって落ちる
265306
df = pyxel.frame_count - self.frame_count
@@ -276,6 +317,9 @@ def update_for_die(self):
276317
def update(self):
277318
if self.is_die:
278319
return self.update_for_die()
320+
elif self.is_in_wall():
321+
self.die()
322+
return
279323

280324
self.frame_count = pyxel.frame_count
281325
global scroll_x
@@ -396,13 +440,13 @@ def __init__(self, width, height):
396440

397441
def update(self):
398442
global is_loose, show_bb, is_pback
399-
if pyxel.btnp(pyxel.KEY_1):
400-
show_bb = not show_bb
401-
elif pyxel.btnp(pyxel.KEY_2):
402-
is_loose = not is_loose
403-
elif pyxel.btnp(pyxel.KEY_3):
404-
is_pback = not is_pback
405-
elif pyxel.btnp(pyxel.KEY_4):
443+
# if pyxel.btnp(pyxel.KEY_1):
444+
# show_bb = not show_bb
445+
# elif pyxel.btnp(pyxel.KEY_2):
446+
# is_loose = not is_loose
447+
# elif pyxel.btnp(pyxel.KEY_3):
448+
# is_pback = not is_pback
449+
if pyxel.btnp(pyxel.KEY_4):
406450
game_over()
407451
for b in blocks.values():
408452
b.update()
@@ -411,6 +455,8 @@ def update(self):
411455
return
412456

413457
for enemy in enemies:
458+
if not enemy.is_alive:
459+
continue
414460
if abs(player.x - enemy.x) < 6 and abs(player.y - enemy.y) < 6:
415461
player.die()
416462
return
@@ -427,10 +473,12 @@ def render(self):
427473
g.bltm(0, 0, 0, scroll_x, 0, 128, 128, TRANSPARENT_COLOR)
428474
for b in blocks.values():
429475
b.draw()
430-
g.text(1, 1, "1:BBox", 7 if show_bb else 5)
431-
g.text(32, 1, "2:Loose", 7 if is_loose else 5)
432-
g.text(68, 1, "3:PBack", 7 if is_pback else 5)
433-
g.text(104, 1, "4:RST", 5)
476+
g.text(1, 1, "SCORE:", 5)
477+
g.text(24, 1, f"{score:04d}", 7)
478+
# g.text(1, 1, "1:BBox", 7 if show_bb else 5)
479+
# g.text(32, 1, "2:Loose", 7 if is_loose else 5)
480+
# g.text(68, 1, "3:PBack", 7 if is_pback else 5)
481+
g.text(98, 1, "4:RST", 5)
434482

435483
# Draw characters
436484
g.camera(scroll_x, 0)
@@ -446,11 +494,13 @@ def game_over():
446494
player.reset()
447495
for enemy in enemies:
448496
enemy.reset()
497+
for b in blocks.values():
498+
b.reset()
449499

450500

451501
class ParentApp:
452502
def __init__(self):
453-
pyxel.init(128, 96, title="anahori")
503+
pyxel.init(128, 96, title="anahori", fps=20)
454504
self.child = App(width=128, height=96)
455505
pyxel.run(self.update, self.draw)
456506

0 commit comments

Comments
 (0)