Python 如何让海龟连续移动而无需多次按键?

Python 如何让海龟连续移动而无需多次按键?,python,turtle-graphics,Python,Turtle Graphics,我正在做一个游戏,玩家可以在屏幕底部的一条线上移动 在这个时候移动它,用户反复点击箭头键,但我想让它在按键时连续移动 如何解决此问题?您需要使用键的按下按钮()功能 请记住,这只是一个基本的布局,您需要进一步了解如何使用其他关键点完成碰撞和/或移动 下面是我基于海龟的解决方案。它使用一个计时器使turtle在窗口底部移动,但当没有任何事情发生时(turtle在边缘停止),计时器会过期,并根据需要重新启动计时器: from turtle import Turtle, Screen CURSOR_

我正在做一个游戏,玩家可以在屏幕底部的一条线上移动

在这个时候移动它,用户反复点击箭头键,但我想让它在按键时连续移动


如何解决此问题?

您需要使用键的按下按钮()功能


请记住,这只是一个基本的布局,您需要进一步了解如何使用其他关键点完成碰撞和/或移动

下面是我基于海龟的解决方案。它使用一个计时器使turtle在窗口底部移动,但当没有任何事情发生时(turtle在边缘停止),计时器会过期,并根据需要重新启动计时器:

from turtle import Turtle, Screen

CURSOR_SIZE = 20

def move(new_direction=False):
    global direction

    momentum = direction is not None  # we're moving automatically

    if new_direction:
        direction = new_direction

    if direction == 'right' and turtle.xcor() < width / 2 - CURSOR_SIZE:
        turtle.forward(1)
    elif direction == 'left' and turtle.xcor() > CURSOR_SIZE - width / 2:
        turtle.backward(1)
    else:
        direction = None

    if ((not new_direction) and direction) or (new_direction and not momentum):
        screen.ontimer(move, 10)

screen = Screen()
width, height = screen.window_width(), screen.window_height()

turtle = Turtle('turtle', visible=False)
turtle.speed('fastest')
turtle.penup()
turtle.sety(CURSOR_SIZE - height / 2)
turtle.tilt(90)
turtle.showturtle()

direction = None

screen.onkeypress(lambda: move('left'), "Left")
screen.onkeypress(lambda: move('right'), "Right")

screen.listen()
screen.mainloop()
从海龟导入海龟,屏幕
光标大小=20
def移动(新方向=假):
全球方向
动量=方向不是零#我们在自动移动
如果是新方向:
方向=新的方向
如果方向==“right”和turtle.xcor()光标大小-宽度/2:
乌龟。向后(1)
其他:
方向=无
如果((非新方向)和方向)或(新方向和非动量):
屏幕时间(移动,10)
screen=screen()
宽度,高度=screen.window\u width(),screen.window\u height()
海龟=海龟(“海龟”,可见=假)
乌龟。速度(“最快”)
乌龟
乌龟。sety(光标大小-高度/2)
海龟倾斜(90)
海龟
方向=无
屏幕上的按键(lambda:move('left'),“left”)
屏幕上的按键(lambda:move('right'),“right”)
screen.listen()
screen.mainloop()

使用左箭头和右箭头控制海龟的方向。

我在没有pygame的标题中说了。你知道如何在Python3.6.4上获得pygame吗installation@StealthTuba3480我能够输入'pip install pygame',效果很好。我将完全卸载您的Python版本,然后再次下载。至于你的问题,我看错了,但它太模糊了,我帮不上忙。如果您没有使用pygame模块为角色移动编写物理和动作,那么您具体使用了什么?有许多python游戏库可供您使用。我听说对初学者来说容易多了。我输入了你之前输入的代码,但我得到了一个错误:pygame.error:视频系统没有初始化。哦,你还需要一些初始化器。我将编辑我的代码以更新您需要的所有内容
from turtle import Turtle, Screen

CURSOR_SIZE = 20

def move(new_direction=False):
    global direction

    momentum = direction is not None  # we're moving automatically

    if new_direction:
        direction = new_direction

    if direction == 'right' and turtle.xcor() < width / 2 - CURSOR_SIZE:
        turtle.forward(1)
    elif direction == 'left' and turtle.xcor() > CURSOR_SIZE - width / 2:
        turtle.backward(1)
    else:
        direction = None

    if ((not new_direction) and direction) or (new_direction and not momentum):
        screen.ontimer(move, 10)

screen = Screen()
width, height = screen.window_width(), screen.window_height()

turtle = Turtle('turtle', visible=False)
turtle.speed('fastest')
turtle.penup()
turtle.sety(CURSOR_SIZE - height / 2)
turtle.tilt(90)
turtle.showturtle()

direction = None

screen.onkeypress(lambda: move('left'), "Left")
screen.onkeypress(lambda: move('right'), "Right")

screen.listen()
screen.mainloop()