Mr. Francisco's fixes

Key fixes:
- Fixed the sword tracking bug by changing on_mouse_mmove to Pygame Zero’s correct on_mouse_move callback and using sword.pos = pos.
- Fixed dinosaur movement so they move at a steady speed toward the apple instead of accelerating wildly based on distance.
- Fixed the hit sound call to sounds.hit.play().
- Added basic score and missed-apple counters.
- Cleaned up naming, constants, and added comments around the important game logic.
This commit is contained in:
2026-06-16 09:43:18 +00:00
parent a1ad32432d
commit 58a663ce5f

75
main.py
View File

@@ -1,34 +1,42 @@
import pgzrun import pgzrun
import random import random
import math
WIDTH = 800 WIDTH = 800
HEIGHT = 600 HEIGHT = 600
apple = Actor("apple") APPLE_POS = (WIDTH / 2, HEIGHT / 2)
apple_x = WIDTH / 2 DINO_SPEED = 80
apple_y = HEIGHT / 2 START_SPAWN_INTERVAL = 2.0
apple.pos = (WIDTH / 2 , HEIGHT / 2) MIN_SPAWN_INTERVAL = 0.2
SPAWN_ACCELERATION = 0.005
CORPSE_LIFETIME = 2.0
APPLE_HIT_RADIUS = 25
sword = Actor("sword") apple = Actor("apple", APPLE_POS)
dinosuars = [] sword = Actor("sword", APPLE_POS)
dinosaurs = []
corpses = [] corpses = []
spawn_interval = 2.0 spawn_interval = START_SPAWN_INTERVAL
min_spawn_interval = 0.2
spawn_timer = 0 spawn_timer = 0
game_time = 0 game_time = 0
score = 0
missed_apples = 0
def on_mouse_mmove(pso , rel , buttons): def on_mouse_move(pos, rel, buttons):
sword.x = pos[0] """Keep the sword centered on the mouse pointer."""
sword.y = pos[1] sword.pos = pos
def spawn_dinosaurs(): def spawn_dinosaurs():
"""Create a dinosaur just outside one edge of the screen."""
dino = Actor("dino") dino = Actor("dino")
side = random.randint(0, 3) side = random.randint(0, 3)
if side == 0: # up if side == 0: # up
dino.x = random.randint(2,WIDTH) dino.x = random.randint(0, WIDTH)
dino.y = -50 dino.y = -50
elif side == 1: # down elif side == 1: # down
dino.x = random.randint(0, WIDTH) dino.x = random.randint(0, WIDTH)
@@ -39,43 +47,54 @@ def spawn_dinosaurs():
else: else:
dino.x = WIDTH + 50 dino.x = WIDTH + 50
dino.y = random.randint(0, HEIGHT) dino.y = random.randint(0, HEIGHT)
dinosuars.append(dino) dinosaurs.append(dino)
def update(dt): def update(dt):
global spawn_timer , spawn_interval , game_time global spawn_timer, spawn_interval, game_time, score, missed_apples
game_time += dt game_time += dt
spawn_timer += dt spawn_timer += dt
spawn_interval = max(min_spawn_interval , spawn_interval - 0.005 * dt) # Gradually reduce the delay between spawns so the game gets harder.
spawn_interval = max(MIN_SPAWN_INTERVAL, spawn_interval - SPAWN_ACCELERATION * dt)
if spawn_timer >= spawn_interval: if spawn_timer >= spawn_interval:
spawn_dinosaurs() spawn_dinosaurs()
spawn_timer = 0 spawn_timer = 0
for dino in dinosuars[:]: for dino in dinosaurs[:]:
dx = apple_x - dino.x dx = apple.x - dino.x
dy = apple_y - dino.y dy = apple.y - dino.y
speed = 80 distance = math.hypot(dx, dy)
dino.x += dx * speed * dt
dino.y += dy * speed * dt # Move at a steady speed toward the apple instead of jumping farther
# when the dinosaur is far away.
if distance > 0:
dino.x += (dx / distance) * DINO_SPEED * dt
dino.y += (dy / distance) * DINO_SPEED * dt
if dino.colliderect(sword): if dino.colliderect(sword):
dino.image = "dead" dino.image = "dead"
corpses.append({"Actor":dino,"die_time": game_time}) corpses.append({"actor": dino, "die_time": game_time})
sounds.hit.play(hit.mp3) sounds.hit.play()
dinosuars.remove(dino) score += 1
dinosaurs.remove(dino)
elif distance <= APPLE_HIT_RADIUS:
missed_apples += 1
dinosaurs.remove(dino)
for corpse in corpses[:]: for corpse in corpses[:]:
if game_time - corpse["die_time"] >= 2: if game_time - corpse["die_time"] >= CORPSE_LIFETIME:
corpses.remove(corpse) corpses.remove(corpse)
def draw(): def draw():
screen.fill((100, 200, 100)) screen.fill((100, 200, 100))
apple.draw() apple.draw()
sword.draw() for dino in dinosaurs:
for dino in dinosuars:
dino.draw() dino.draw()
for corpse in corpses: for corpse in corpses:
corpse["Actor"].draw() corpse["actor"].draw()
sword.draw()
screen.draw.text(f"Score: {score}", (10, 10), fontsize=36, color="white")
screen.draw.text(f"Missed: {missed_apples}", (10, 50), fontsize=36, color="white")
pgzrun.go() pgzrun.go()