gpt4 book ai didi

python - 按住键 - Python Turtle

转载 作者:太空宇宙 更新时间:2023-11-04 03:14:51 27 4
gpt4 key购买 nike

开门见山,在python的Turtle中是否可以按住一个键执行一段代码,eg:

当空格保持 3 秒时 - Space()

如果需要,这里是代码:

import time
import sys
import turtle

width = 800
height = 600

turtle.screensize(width, height)
turtle.title('Youtube')
turtle.hideturtle()
turtle.penup()

def text(text, posx, posy, size):
turtle.pencolor('white')
turtle.goto(posx, posy)
turtle.write(text, font=("Arial", size, "normal"))

##ScreenRender
turtle.bgpic("background.gif")
turtle.hideturtle
#Text
text('Record A Video', -400, 225, 20)
text('Hold Space...', -345, 200, 15)

##End
turtle.done()

最佳答案

是的,您可以将 turtle.listen()turtle.onkey*() 例程之一结合使用。

如果需要,这里是代码:

import time
import turtle

WIDTH = 800
HEIGHT = 600

seconds = 0

def text(text, posx, posy, size):
turtle.pencolor('black')
turtle.goto(posx, posy)
turtle.write(text, font=("Arial", size, "normal"))

def press_space():
global seconds
seconds = time.time()
turtle.onkeypress(None, ' ')

def release_space():
if time.time() - seconds >= 3.0:
turtle.onkeyrelease(None, ' ')
text("thank you.", -200, 200, 15)

# ScreenRender
turtle.screensize(WIDTH, HEIGHT)
turtle.hideturtle()
turtle.penup()

# Text
text('Hold Space for 3 seconds...', -400, 200, 15)

# Event Handlers
turtle.listen()
turtle.onkeypress(press_space, ' ')
turtle.onkeyrelease(release_space, ' ')

# End
turtle.done()

保持时间可能并不简单,因为按键有自己的重复频率。

关于python - 按住键 - Python Turtle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36630896/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com