[我刚刚开始在python中使用turtle,它没有响应我的输入[关闭]

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

最终,我计划制作一个蛇形游戏,但我什至无法使程序响应输入。我对python还是相当陌生,对Turtle还是比较新,所以我不确定从哪里开始。

import turtle

speed = 25

wn = turtle.Screen()
wn.setup(1000, 1000)
wn.bgcolor("black")
wn.title("Snake")

snake = turtle.Turtle()
snake.color("green")
snake.speed(0)
snake.penup()
snake.setpos(0,0)
snake.shape("square")

def move_up():
    snake.fd(10)

turtle.listen()
turtle.onkey(move_up(), "Up")
python
1个回答
1
投票

move_up()功能中的当前代码将其方向错误。但这解决了我认为您遇到的两个问题。

 import turtle

    speed = 25

    wn = turtle.Screen()
    wn.setup(1000, 1000)
    wn.bgcolor("black")
    wn.title("Snake")

    snake = turtle.Turtle()
    snake.color("green")
    snake.speed(0)
    snake.penup()
    snake.setpos(0,0)
    snake.shape("square")

    def move_up():
        snake.fd(10)

    wn.listen()
    wn.onkey(move_up, "Up") # Removed the brackets from the call here, and
                            # call the screen - not the turtle.

    while True: # Puts the game into a loop to stop the window closing
        wn.update()
© www.soinside.com 2019 - 2024. All rights reserved.