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

带问答机制的Snake游戏选项不显示问题求助

问题描述

开发一款带答题机制的贪吃蛇游戏,需求是顶部显示问题,答案选项随机出现在屏幕上。使用turtle库面向对象开发,从CSV读取问题数据,在答案类中通过self.write()写选项文本,用randint生成随机坐标,调用了hideturtle()和showturtle(),但选项始终不显示。

主程序代码

from snake import Snake
from turtle import Screen
from food import CorrectAnswer, WrongAnswer, SecondWrong
import pygame
import time
from score import ScoreBoard


snake = Snake()
screen = Screen()
food = CorrectAnswer()
food_2 = WrongAnswer()
food_3 = SecondWrong()
scoreboard = ScoreBoard()

screen.setup(width=600, height=600)
screen.title("My Snake Game")
screen.bgcolor('light green')
screen.tracer(0)
pygame.mixer.init()

screen.listen()
screen.onkey(snake.up, "Up")
screen.onkey(snake.down, "Down")
screen.onkey(snake.left, "Left")
screen.onkey(snake.right,  "Right")

game_is_on = True

while game_is_on:
   screen.update()
   time.sleep(0.1)
   snake.move()

   if snake.head.distance(food) < 20:
       sound=pygame.mixer.Sound('Ding-sound-effect.mp3')
       pygame.mixer.Sound.play(sound)
       food.write_option()
       food_2.write_option()
       food_3.write_option()
       snake.extend()
       scoreboard.increase_score()

   if snake.head.distance(food_2) < 20:
       game_is_on=False

   if snake.head.distance(food_3) < 20:
       game_is_on=False

   if snake.head.xcor() > 290 or snake.head.xcor() < -290 or snake.head.ycor()>290 or snake.head.ycor() < -290:
       crash=pygame.mixer.Sound('losing-sound-effect.mp3')
       pygame.mixer.Sound.play(crash)
       time.sleep(2)
       scoreboard.reset()
       snake.reset()
   for segment in snake.segments[1:]:
        if snake.head.distance(segment) < 10:
            scoreboard.reset()

screen.mainloop()

food.py代码

from turtle import Turtle
from score import current_id
import pandas
import random


data = pandas.read_csv("questions.csv")

class CorrectAnswer(Turtle):
    def __init__(self):
        super().__init__()
        self.speed("fastest")
        self.hideturtle()

    def write_option(self, align="left", font=("Arial", 24, "normal")):
        self.penup()
        correct = data.loc[data['id'] == current_id, 'canswer'].values[0]
        self.goto(x=0, y=0)
        self.write(text=correct, align=align, font=font)
        self.pendown()
        self.showturtle()
        self.refresh()

    def refresh(self):
        self.clear()
        self.penup()
        self.goto(random.randint(-280, 280), random.randint(-280, 280))
        self.pendown()


class WrongAnswer(Turtle):
    def __init__(self):
        super().__init__()
        self.speed("fastest")
        self.hideturtle()

    def write_option(self, align="left", font=("Arial", 24, "normal")):
        self.penup()
        correct = data.loc[data['id'] == current_id, 'wanswer'].values[0]
        self.goto(random.randint(-280, 280), random.randint(-280, 280))
        self.write(text=correct, align=align, font=font)
        self.pendown()
        self.showturtle()
        self.refresh()

    def refresh(self):
        self.clear()
        self.penup()
        self.goto(random.randint(-280, 280), random.randint(-280, 280))
        self.pendown()


class SecondWrong(Turtle):
    def __init__(self):
        super().__init__()
        self.speed("fastest")
        self.hideturtle()
问题原因及修复方案

核心问题1:refresh()方法立即清除刚写入的文本

在write_option()最后调用refresh(),而refresh()第一行就是self.clear(),刚写入的答案文本直接被清除,导致屏幕上看不到内容。

核心问题2:SecondWrong类缺少write_option()方法

主程序中调用了food_3.write_option(),但该类未实现此方法,会触发AttributeError,中断程序执行。

其他问题:冗余的海龟显示操作

调用showturtle()会显示海龟箭头图标,但我们只需要显示文本,不需要显示海龟本身,属于多余操作。


修正后的food.py代码

from turtle import Turtle
from score import current_id
import pandas
import random


data = pandas.read_csv("questions.csv")

class CorrectAnswer(Turtle):
    def __init__(self):
        super().__init__()
        self.speed("fastest")
        self.hideturtle()
        # 初始化时直接显示第一个选项
        self.write_option()

    def write_option(self, align="center", font=("Arial", 24, "normal")):
        self.clear()  # 先清除旧文本
        self.penup()
        correct = data.loc[data['id'] == current_id, 'canswer'].values[0]
        # 生成随机坐标
        x = random.randint(-280, 280)
        y = random.randint(-280, 280)
        self.goto(x, y)
        self.write(text=correct, align=align, font=font)
        self.pendown()

    def refresh(self):
        # 刷新就是重新写入新的选项
        self.write_option()


class WrongAnswer(Turtle):
    def __init__(self):
        super().__init__()
        self.speed("fastest")
        self.hideturtle()
        self.write_option()

    def write_option(self, align="center", font=("Arial", 24, "normal")):
        self.clear()
        self.penup()
        wrong = data.loc[data['id'] == current_id, 'wanswer'].values[0]
        x = random.randint(-280, 280)
        y = random.randint(-280, 280)
        self.goto(x, y)
        self.write(text=wrong, align=align, font=font)
        self.pendown()

    def refresh(self):
        self.write_option()


class SecondWrong(Turtle):
    def __init__(self):
        super().__init__()
        self.speed("fastest")
        self.hideturtle()
        self.write_option()

    def write_option(self, align="center", font=("Arial", 24, "normal")):
        self.clear()
        self.penup()
        # 假设CSV里第二个错误选项列名为wanswer2,根据实际调整
        second_wrong = data.loc[data['id'] == current_id, 'wanswer2'].values[0]
        x = random.randint(-280, 280)
        y = random.randint(-280, 280)
        self.goto(x, y)
        self.write(text=second_wrong, align=align, font=font)
        self.pendown()

    def refresh(self):
        self.write_option()

主程序调整建议

  1. 移除主循环中吃到食物时的write_option()调用,改为调用refresh():
if snake.head.distance(food) < 20:
    sound=pygame.mixer.Sound('Ding-sound-effect.mp3')
    pygame.mixer.Sound.play(sound)
    food.refresh()
    food_2.refresh()
    food_3.refresh()
    snake.extend()
    scoreboard.increase_score()
  1. 确保current_id变量在score.py中正确更新,每次答题后切换到下一个问题ID。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 06:42:34