How to start moving a turtle via space bar with python

不羁的心 提交于 2020-05-29 11:53:05

问题


I'm trying to set up a simple turtle program in python where I can start moving the turtle with a press of the space bar, and he keeps moving until I hit the space bar again. I can get him to move a fixed distance with the space press but can't get it to continue.

Here is what I'm working with:

from turtle import *


# PUT YOUR CODE HERE
setup(800,600)
home()
pen_size = 2
color("blue")
title("Turtle")
speed("fastest") 
drawdist= 25 
current_state = penup
next_state = pendown

#Button Instructions
def move_up():
        seth(90)
        forward(drawdist)

def move_down():
        seth(270)
        forward(drawdist)

def move_left():
        seth(180)
        forward(drawdist)

def move_right():
        seth(0)
        forward(drawdist)


def space_bar():
    seth(90)
    forward(drawdist)
    global current_state, next_state
    next_state()
    current_state, next_state = next_state, current_state

#Change Pen Color
def red():
        color("red")

def green():
        color("green")

def blue():
        color("blue")


#Button Triggers
s= getscreen()

s.onkey(move_up,"Up")

s.onkey(move_down,"Down")

s.onkey(move_left,"Left")

s.onkey(move_right,"Right")

s.onkey(space_bar,"space")

s.onkey(red,"r")

s.onkey(green,"g")

s.onkey(blue,"b")

listen()

done()

回答1:


I don't see that you ever got an answer to your query:

start moving the turtle with a press of the space bar, and he keeps moving until I hit the space bar again

The suggested onkeypress() fix doesn't do this. Here's a simplified example that does what you want, starts the turtle when you hit the space bar and stops it when you hit the space bar again:

from turtle import Turtle, Screen

screen = Screen()
turtle = Turtle(shape="turtle")
turtle.speed("fastest")

def current_state():
    global moving
    moving = False
    turtle.penup()

def next_state():
    global moving
    turtle.pendown()
    moving = True
    move()

def space_bar():
    global current_state, next_state
    next_state()
    current_state, next_state = next_state, current_state

def move():
    if moving:
        turtle.circle(100, 3)
        screen.ontimer(move, 50)

current_state()

screen.onkey(space_bar, "space")

screen.listen()

screen.mainloop()

I've used circular motion in this example so you can start and stop the turtle as much as you want.




回答2:


Replace the function 'onkey' with the function 'onkeypress'.

The 'onkey' function fires once regardless of holding down the key while 'onkeypress' fires as you would expect when holding down the key.




回答3:


The correct and easiest way is this(NOT FOR EVENT LISTENER SPACE BAR, THIS IS JUST EVENT LISTENERS):

import turtle 
import random

t = turtle.Turtle()
screen = turtle.Screen(

def goForward():
  t.forward(input_value)

screen.onkey(goForward, "Forward")

The word "right" in hashtags only means to press the right key.



来源:https://stackoverflow.com/questions/29904032/how-to-start-moving-a-turtle-via-space-bar-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!