需要增强 Python Turtle 代码的帮助

问题描述 投票:0回答:1

我一直在上 Python 课程。我已经构建了贪吃蛇游戏 - 我很惊讶仅用 Turtle 就能完成这个游戏。

所以我决定增强游戏。我花了一整天的时间进行一项额外的改进:添加额外的食物。 我想要的是,如果用户获得 x 分,那么他们将获得一个大水果,该水果可以提供 5x 分 - 但这个水果只能停留 5 秒。停留了 5 秒是我被卡住的地方。

我首先尝试保持模块化,并在新创建的“super_food.py”类中放置一个循环。我认为我的逻辑很完美。但是当我运行代码时,当循环运行时,代码速度减慢到非常低的速度。

然后我将循环移至 main.py 文件,我整天都在搞乱 - 但无法弄清楚如何工作逻辑并有一个计时器在 x 秒后删除“超级食物”。

这是我从 main.py 获得的代码:

from turtle import Screen
from snake import Snake
from food import Food
from super_food import SuperFood
from scoreboard import Scoreboard
import time

SPEED_IN_TIME = 0.1
ACCURACY = 45 # This determines how close we get to the snake to determine a hit
ACCURACY_COLLISION = 10
BONUS_SCORE_OCCUR = 3
SUPER_FOOD_DURATION = 5

super_food_exists = False # Trying a hack to see if I can add super food - I've been struggling with this one all day
start_time = 0 # This is used in the super_food code

# All these we don't have in the Snake Class - these are separate to the snake
screen = Screen()
screen.setup(width=600, height=600)
Y_EDGE = screen.window_height() // 2
X_EDGE = screen.window_height() // 2

screen.bgcolor("black")
screen.title("My Snake Game with super food Gemini Version")
screen.tracer(0) # We turn off screen animation - this was making the movements look jagged.

snake = Snake()
food = Food()
super_food = SuperFood() # We add the new Super Food
scoreboard = Scoreboard()

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

temp_time_controller = False

while game_is_on:

    screen.update()
    time.sleep(SPEED_IN_TIME)

    snake.move()

    # Let's detect collision with food
    if snake.head.distance(food) < ACCURACY:
        # We have a hit! So we need to have the food appear elsewhere and increase the score
        food.refresh()

        # Now we increase the score
        scoreboard.increase_score()

        # We increase the lenght of the snake
        snake.extend()

    # print(snake.head.xcor()) # Just testing I can access the x coordinate

    if snake.head.xcor() > X_EDGE or snake.head.xcor() < -X_EDGE or snake.head.ycor() > Y_EDGE or snake.head.ycor() < -Y_EDGE:
        game_is_on = False
        scoreboard.game_over()

    # Nearly finished! Now we write code to detect if the snake colides with itself
    for segment in snake.turtle_segments[1:]:
        if snake.head.distance(segment) < ACCURACY_COLLISION:
            game_is_on = False
            scoreboard.game_over()

    # New part adding: super foods
    if scoreboard.score != 0: # Problem was 0 % BONUS_SCORE_OCCUR == 0
        if scoreboard.score % BONUS_SCORE_OCCUR == 0:

            super_food_exists = True

            start_time = time.time() # We start of the timer

        if super_food_exists:

            if time.time() - start_time < SUPER_FOOD_DURATION:
                print(f"Time check: {time.time() - start_time < SUPER_FOOD_DURATION}")
                print(f"Time check: {int(time.time() - start_time)}")
                super_food.unhide()
            else:
                super_food.hide()
                super_food_exists = False
                # start_time = 0
        # else:
        #     super_food.hide()

screen.exitonclick()

我猜线程在这里会有所帮助 - 让倒计时在自己的线程中运行? 是否可以不使用线程?我完全饱和了,无法正常思考。

谢谢。

我期待代码能够与我的新添加一起运行 - 它几乎可以工作!

python turtle-graphics python-turtle
1个回答
-1
投票

一种选择可能是在 super_food.py 文件中保留一个计时器变量,然后使用 datetime.datetime.now 保存它的创建时间,然后每次更新屏幕时在前一个时间和新的当前时间之间进行减法,像这样,

#inside the super food file
then = datetime.datetime.now()

#inside the updating loop, 
now = datetime.datetime.now()
deltaTime = now - then
if deltaTime > 5:
    # do whatever you need to make it disappear
© www.soinside.com 2019 - 2024. All rights reserved.