我试图通过按下按钮使文本连续移动,但现在它每次按下时只会移动一个间隔。
def move():
global y
global checkmove
checkmove = True
if y > 280:
y = 0
else:
y += 2
我试图通过按下按钮使文本连续移动,但现在它每次按下时只会移动一个间隔。
def move():
global y
global checkmove
checkmove = True
if y > 280:
y = 0
else:
y += 2
没有内置方法可以判断按钮是否被按住。但是,有一个 button_up 处理程序和一个 button_down 处理程序。如果你使用计时器,你可以知道一个键被按下了多长时间。这是一个代码示例,可以判断哪些键被按下。可以同时按住多个键。如果要更改计时,可以在代码底部更改计时器的长度。更改计时器 1 的值将更改在模拟按键之前必须按住按键的时间,更改计时器 2 的长度会更改按键按下的速度。您还可以在此处查看代码:https ://py3.codeskulptor.org/#user303_gtJ15kIGNV_0.py
def timer_handler():
#start timer 2 if it is off
if not timer_on:
timer2.start()
#stop timer 1
timer.stop()
def timer2_handler():
#simulates repeated keypresses if the key is held down
global timer_on
timer_on=True
if len(held_keys)==0:
timer2.stop()
timer_on=False
else:
for item in held_keys:
action(item)
def action(key):
#this is where all the code to make something happen goes
if key==upkey:
#do something
print('up key pressed')
elif key==downkey:
#do something else
print('down key pressed')
def key_handler(key):
#handles user keypresses
#add key to held keys
global held_keys
held_keys.append(key)
#do something with key
action(key)
#start timer 1 if timer 2 is off
if not timer_on:
timer.start()
def release_handler(key):
#handles key releases
#remove the key from the list
global held_keys
if key in held_keys:
held_keys.remove(key)
#if no keys are held, stop both timers
global timer_on
if len(held_keys)==0:
timer.stop()
timer2.stop()
timer_on=False
import simplegui
timer_on=False
held_keys=[]
#starts timer 2
timer = simplegui.create_timer(300, timer_handler)
#automatic keypress
timer2 = simplegui.create_timer(100, timer2_handler)
#map whatever keys you need
spacekey=simplegui.KEY_MAP['space']
leftkey=simplegui.KEY_MAP['left']
rightkey=simplegui.KEY_MAP['right']
upkey=simplegui.KEY_MAP['up']
downkey=simplegui.KEY_MAP['down']
#create a frame and set key down/up handlers
frame=simplegui.create_frame('Click the box, then use arrow keys to move', 100, 100)
frame.set_keydown_handler(key_handler)
frame.set_keyup_handler(release_handler)
frame.start()