This commit is contained in:
Anna
2026-06-12 14:29:40 +08:00
parent c03de224a9
commit 150c127fd6
3 changed files with 53 additions and 15 deletions

BIN
images/dead.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB

BIN
images/dino.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.9 KiB

68
main.py
View File

@@ -19,24 +19,62 @@ min_spawn_interval = 0.2
spawn_timer = 0
game_time = 0
def draw():
screen.fill((100,220,100))
apple.draw()
def spawn_dinosaur():
dino = Actor("dino")
def spawn_dinosaurs():
dino = Actor("dino")
side = random.randint(0,3)
if side == 0:
dino_x = random.randint(0.WIDTH)
dino_y = -50
elif side == 1:
dino_x = random.randint(0,WIDTH)
dino_y = HEIGHT + 50
elif side == 2:
dino_x = WIDTH + 50
dino_y = random.randint(0,HEIGHT)
if side == 0: #up
dino.x = random.randint(2,WIDTH)
dino.y = -50
elif side == 1: #down
dino.x = random.randint(0,WIDTH)
dino.y = HEIGHT + 50
elif side == 2: #left
dino.x = -50
dino.y = random.randint(0,HEIGHT)
else:
dino.x = WIDTH + 50
dino.y = random.randint(0,HEIGHT)
dinosuars.append(dino)
def update(dt):
global spawn_timer , spawn_interval , game_time
game_time += dt
spawn_timer += dt
spawn_interval = max(min_spawn_interval , spawn_interval - 0.005 * dt)
if spawn_timer >= spawn_interval:
spawn_dinosaurs()
spawn_timer = 0
sword.pos = mouse.pos
for dino in dinosuars[:]:
dx = apple_x - dino.x
dy = apple_y - dino.y
speed = 80
dino.x += dx * speed * dt
dino.y += dy * speed * dt
if dino.colliderect(sword):
dino.image = "dead"
corpses.append({"Actor":dino,"die_time": game_time})
sounds.hit.play()
dinosuars.remove(dino)
for corpse in corpses[:]:
if game_time - corpse["die_time"] >= 2:
corpses.remove(corpse)
def draw():
screen.fill((100,200,100))
apple.draw()
sword.draw()
for dino in dinosuars:
dino.draw()
for corpse in corpses:
corpse["Actor"].draw()
pgzrun.go()