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

93
main.py
View File

@@ -1,81 +1,100 @@
import pgzrun
import random
import math
WIDTH = 800
HEIGHT = 600
apple = Actor("apple")
apple_x = WIDTH / 2
apple_y = HEIGHT / 2
apple.pos = (WIDTH / 2 , HEIGHT / 2)
APPLE_POS = (WIDTH / 2, HEIGHT / 2)
DINO_SPEED = 80
START_SPAWN_INTERVAL = 2.0
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 = []
spawn_interval = 2.0
min_spawn_interval = 0.2
spawn_interval = START_SPAWN_INTERVAL
spawn_timer = 0
game_time = 0
score = 0
missed_apples = 0
def on_mouse_mmove(pso , rel , buttons):
sword.x = pos[0]
sword.y = pos[1]
def on_mouse_move(pos, rel, buttons):
"""Keep the sword centered on the mouse pointer."""
sword.pos = pos
def spawn_dinosaurs():
"""Create a dinosaur just outside one edge of the screen."""
dino = Actor("dino")
side = random.randint(0,3)
if side == 0: #up
dino.x = random.randint(2,WIDTH)
side = random.randint(0, 3)
if side == 0: # up
dino.x = random.randint(0, WIDTH)
dino.y = -50
elif side == 1: #down
dino.x = random.randint(0,WIDTH)
elif side == 1: # down
dino.x = random.randint(0, WIDTH)
dino.y = HEIGHT + 50
elif side == 2: #left
elif side == 2: # left
dino.x = -50
dino.y = random.randint(0,HEIGHT)
dino.y = random.randint(0, HEIGHT)
else:
dino.x = WIDTH + 50
dino.y = random.randint(0,HEIGHT)
dinosuars.append(dino)
dino.y = random.randint(0, HEIGHT)
dinosaurs.append(dino)
def update(dt):
global spawn_timer , spawn_interval , game_time
global spawn_timer, spawn_interval, game_time, score, missed_apples
game_time += 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:
spawn_dinosaurs()
spawn_timer = 0
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
for dino in dinosaurs[:]:
dx = apple.x - dino.x
dy = apple.y - dino.y
distance = math.hypot(dx, dy)
# 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):
dino.image = "dead"
corpses.append({"Actor":dino,"die_time": game_time})
sounds.hit.play(hit.mp3)
dinosuars.remove(dino)
corpses.append({"actor": dino, "die_time": game_time})
sounds.hit.play()
score += 1
dinosaurs.remove(dino)
elif distance <= APPLE_HIT_RADIUS:
missed_apples += 1
dinosaurs.remove(dino)
for corpse in corpses[:]:
if game_time - corpse["die_time"] >= 2:
if game_time - corpse["die_time"] >= CORPSE_LIFETIME:
corpses.remove(corpse)
def draw():
screen.fill((100,200,100))
screen.fill((100, 200, 100))
apple.draw()
sword.draw()
for dino in dinosuars:
for dino in dinosaurs:
dino.draw()
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()