You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Turtle过马路游戏:碰撞检测函数失效问题排查

Turtle过马路游戏碰撞检测问题修复

问题描述

开发Turtle过马路游戏,核心逻辑是让乌龟从屏幕底部出发,穿越屏幕时躲避从左向右随机生成的汽车。目前其他功能运行正常,但碰撞检测存在异常:有时检测有效,有时汽车直接穿过乌龟,还会出现误判。尝试通过.pos匹配位置无效。

问题代码

主程序代码

import time
from turtle import Screen
from player import Player
from car_manager import CarManager
screen = Screen()
screen.tracer(0)
screen.setup(width=600, height=600)
player = Player()
cars = CarManager()
cars.hideturtle()
player.hideturtle()
game_is_on = True
screen.listen()
screen.onkey(fun=player.move, key="Up")

while game_is_on:
    time.sleep(0.1)
    # scoreboard.display_score()  # 注:代码未定义scoreboard,需补充实现
    screen.update()
    # 生成汽车
    cars.generate_cars()
    cars.move()
    # 碰撞检测
    for car in cars.car_list:
        if(car.distance(player)) < 20:
            game_is_on = False
            screen.exitonclick()

Player类代码

from turtle import Turtle

STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
class Player(Turtle):
    def __init__(self):
        super().__init__()
        self.player = Turtle()
        self.player.penup()
        self.player.shape("turtle")
        self.player.color("green")
        self.player.shapesize(stretch_wid=2, stretch_len=2)
        self.player.goto(0,-250)
        self.player.setheading(90)
    def move(self):
        self.player.forward(10)
    def check_y_pos(self):
        return self.player.ycor()

CarManager类代码

from turtle import Turtle
import random

COLORS = ["red", "orange", "yellow", "green", "blue", "purple"]
STARTING_MOVE_DISTANCE = 5
MOVE_INCREMENT = 10

class CarManager(Turtle):
    def __init__(self):
        super().__init__()
        self.car_list = []
    def generate_cars(self):
        random_chance = random.randint(1,6)
        if random_chance == 5:
            cars = Turtle()
            cars.penup()
            cars.shape("square")
            cars.goto(x=270, y=random.randint(a = -270, b = 270))
            cars.setheading(180)
            cars.shapesize(stretch_len=2, stretch_wid=1)
            cars.color(COLORS[random.randint(0, 5)])
            self.car_list.append(cars)
    def move(self):
        for car in self.car_list:
            car.forward(STARTING_MOVE_DISTANCE)

修复方案

1. 修正Player类的实例化错误

Player类已继承Turtle,无需额外创建self.player实例——当前主程序中player变量指向的是一个空的Turtle对象,而实际显示的乌龟是self.player,导致碰撞检测时获取的位置完全错误。

修改后的Player类:

from turtle import Turtle

STARTING_POSITION = (0, -280)
MOVE_DISTANCE = 10
FINISH_LINE_Y = 280
class Player(Turtle):
    def __init__(self):
        super().__init__()
        self.penup()
        self.shape("turtle")
        self.color("green")
        self.shapesize(stretch_wid=2, stretch_len=2)
        self.goto(0, -250)
        self.setheading(90)
    
    def move(self):
        self.forward(MOVE_DISTANCE)
    
    def check_y_pos(self):
        return self.ycor()

2. 调整碰撞检测距离阈值

乌龟和汽车都被放大(乌龟放大2倍、汽车长度放大2倍),默认的20像素阈值无法覆盖实际图形的碰撞范围,需增大到40左右。

修改主程序中的碰撞检测代码:

# 碰撞检测部分
for car in cars.car_list:
    if car.distance(player) < 40:
        game_is_on = False
        screen.exitonclick()

3. 移除不必要的隐藏操作

主程序中的player.hideturtle()可以直接删除,修改后的Player类会正确显示乌龟。

额外提示

  • 代码中scoreboard.display_score()未定义,需补充Scoreboard类的实现。
  • 可优化汽车生成逻辑,避免过多汽车堆积在屏幕左侧。

内容的提问来源于stack exchange,提问作者KrH

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.17 17:05:57