[Python] Why do the options not appear for my Snake game?

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I am creating the snake game with a twist. I want questions to appear on top and their answers to appear randomly on the screen.

    I am using turtle and creating classes while retrieving the question from a CSV file. I used the self.write method while creating a function in the answer class. I use randint to get x and y and also use hideturtle() and showturtle(), yet the options do not appear.

    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")
    #image = "snake.png"
    #screen.addshape(image)
    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()

    Continue reading...

Compartilhe esta Página