python判断按键是否按下_Pygame检测是否按下了一个键?

使用pygame.key.get_pressed()来检查某个键是否被按下更容易,因为您不需要自己通过事件跟踪这些键的状态。在

我通常会创建一个字典,将键映射到它们应该移动对象的方向。在

这样,很容易查询pygame.key.get_pressed()的结果。在

你可以使用一些简单的矢量数学来规范化运动方向,这样你就可以以与只沿x或y轴相同的速度对角移动。在

另外,使用Rect来存储对象的位置也更容易,因为pygame提供了很多有用的函数来处理Rect类。在import pygame, math

from pygame.locals import *

# some simple functions for vetor math

# note that the next pygame release will ship with a vector math module included!

def magnitude(v):

return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def add(u, v):

return [ u[i]+v[i] for i in range(len(u)) ]

def normalize(v):

vmag = magnitude(v)

return [ v[i]/vmag for i in range(len(v)) ]

pygame.init()

screen = pygame.display.set_mode((1440,900))

screen_r = screen.get_rect()

pygame.display.update()

black=(0,0,0)

white=(255,255,255)

background = pygame.image.load("G:/starfield.jpg")##loads the background

# here we define which button moves to which direction

move_map = {pygame.K_w: ( 0, -1),

pygame.K_s: ( 0, 1),

pygame.K_a: (-1, 0),

pygame.K_d: ( 1, 0)}

banshee = pygame.image.load("G:/banshee.png")

# create a Rect from the Surface to store the position of the object

# we can pass the initial starting position by providing the kwargs top and left

# another useful feature is that when we create the Rect directly from the

# Surface, we also have its size stored in the Rect

banshee_r = banshee.get_rect(top=50, left=50)

speed = 5

gameexit = False

while not gameexit:

# exit the game when the window closes

for event in pygame.event.get():

if event.type == pygame.QUIT:

gameexit = True

# if it touches the sides of the window, end the game

# note how easy this part becomes if you use the Rect class

if not screen_r.contains(banshee_r):

gameexit = True

# get all pressed keys

pressed = pygame.key.get_pressed()

# get all directions the ship should move

move = [move_map[key] for key in move_map if pressed[key]]

# add all directions together to get the final direction

reduced = reduce(add, move, (0, 0))

if reduced != (0, 0):

# normalize the target direction and apply a speed

# this ensures that the same speed is used when moving diagonally

# and along the x or y axis. Also, this makes it easy to

# change the speed later on.

move_vector = [c * speed for c in normalize(reduced)]

# move the banshee

# Another useful pygame functions for Rects

banshee_r.move_ip(*move_vector)

screen.blit(background,(0,0))

# we can use banshee_r directly for drawing

screen.blit(banshee, banshee_r)

pygame.display.update()

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Python一个按键的短按和长按的实现,可以使用模块`keyboard`来完成。具体实现如下: 首先,需要安装`keyboard`模块,可以通过以下命令来安装: ``` pip install keyboard ``` 然后,可以使用`keyboard`模块的`add_hotkey`函数来注册快捷,并设置不同的按键事件回调函数来实现按键的短按和长按事件。例如,下面的代码实现了按下'A'短按和长按的不同效果: ``` import keyboard import time def short_press(): print('short press') def long_press(): print('long press') keyboard.add_hotkey('a', short_press) keyboard.add_hotkey('a', long_press, args=[], trigger_on_release=True, long_press_duration=0.5) while True: time.sleep(1) ``` 在上面的代码,我们首先定义了两个回调函数`short_press`和`long_press`,分别用于处理'A'的短按和长按事件。然后,通过`keyboard`模块的`add_hotkey`函数来注册快捷,第一个参数为要监听的按键,第二个参数为按键事件回调函数。其,`short_press`函数是默认的短按事件回调函数,`long_press`函数是长按事件回调函数,`args=[]`用于传递额外的参数给回调函数,`trigger_on_release=True`表示事件在按键松开时触发,`long_press_duration=0.5`表示按键按下超过0.5秒后触发长按事件。 最后,通过一个无限循环来保持程序的运行。这样,当用户按下'A'时,根据按下的时间长短,就会调用不同的事件回调函数来处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值