62

我正在 pygame 1.9.2 中制作游戏。这是一个失败的简单游戏,其中一艘船在五列坏人之间移动,他们通过缓慢向下移动来攻击。我正试图让船用左右箭头键左右移动。这是我的代码:

keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    location-=1
    if location==-1:
        location=0
if keys[K_RIGHT]:
    location+=1
    if location==5:
        location=4

它工作得太好了。船移动得太快了。让它只向左或向右移动一个位置几乎是不可能的。我怎样才能让它每次按下键时船只移动一次?

4

10 回答 10

101

您可以从 pygame 获取事件,然后注意该KEYDOWN事件,而不是查看返回的键get_pressed()(它为您提供当前按下的键,而事件显示您在该帧KEYDOWN上按下了哪些键)。

您的代码现在发生的情况是,如果您的游戏以 30fps 的速度渲染,并且您按住左箭头键半秒钟,您将更新位置 15 次。

events = pygame.event.get()
for event in events:
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            location -= 1
        if event.key == pygame.K_RIGHT:
            location += 1

为了在按住键时支持连续移动,您必须建立某种限制,或者基于游戏循环的强制最大帧速率,或者通过一个计数器,它只允许您移动每这么多滴答声。环形。

move_ticker = 0
keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    if move_ticker == 0:
        move_ticker = 10
        location -= 1
        if location == -1:
            location = 0
if keys[K_RIGHT]:
    if move_ticker == 0:   
        move_ticker = 10     
        location+=1
        if location == 5:
            location = 4

然后在游戏循环的某个地方,你会做这样的事情:

if move_ticker > 0:
    move_ticker -= 1

这只会让您每 10 帧移动一次(因此,如果您移动,自动收报机将设置为 10,并且在 10 帧后它将允许您再次移动)

于 2013-04-16T18:24:09.410 回答
13

pygame.key.get_pressed()返回一个包含每个键状态的列表。如果按住某个键,则该键的状态为True,否则为False。用于pygame.key.get_pressed()评估按钮的当前状态并获得连续移动:

while True:

    keys = pygame.key.get_pressed()
    x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * speed
    y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * speed

键盘事件(参见pygame.event模块)仅在按键状态更改时发生一次。KEYDOWN每次按下某个键时,该事件发生一次。KEYUP每次释放键时发生一次。将键盘事件用于单个动作或移动:

while True:

    for event in pygame.event.get():
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x -= speed
            if event.key == pygame.K_RIGHT:
                x += speed
            if event.key == pygame.K_UP:
                y -= speed
            if event.key == pygame.K_DOWN:
                y += speed

另请参阅键和键盘事件


连续运动的最小示例: replit.com/@Rabbid76/PyGame-ContinuousMovement

import pygame

pygame.init()
window = pygame.display.set_mode((300, 300))
clock = pygame.time.Clock()

rect = pygame.Rect(0, 0, 20, 20)
rect.center = window.get_rect().center
vel = 5

run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.KEYDOWN:
            print(pygame.key.name(event.key))

    keys = pygame.key.get_pressed()
    
    rect.x += (keys[pygame.K_RIGHT] - keys[pygame.K_LEFT]) * vel
    rect.y += (keys[pygame.K_DOWN] - keys[pygame.K_UP]) * vel
        
    rect.centerx = rect.centerx % window.get_width()
    rect.centery = rect.centery % window.get_height()

    window.fill(0)
    pygame.draw.rect(window, (255, 0, 0), rect)
    pygame.display.flip()

pygame.quit()
exit()

单个动作的最小示例: replit.com/@Rabbid76/PyGame-ShootBullet

import pygame
pygame.init()

window = pygame.display.set_mode((500, 200))
clock = pygame.time.Clock()

tank_surf = pygame.Surface((60, 40), pygame.SRCALPHA)
pygame.draw.rect(tank_surf, (0, 96, 0), (0, 00, 50, 40))
pygame.draw.rect(tank_surf, (0, 128, 0), (10, 10, 30, 20))
pygame.draw.rect(tank_surf, (32, 32, 96), (20, 16, 40, 8))
tank_rect = tank_surf.get_rect(midleft = (20, window.get_height() // 2))

bullet_surf = pygame.Surface((10, 10), pygame.SRCALPHA)
pygame.draw.circle(bullet_surf, (64, 64, 62), bullet_surf.get_rect().center, bullet_surf.get_width() // 2)
bullet_list = []

run = True
while run:
    clock.tick(60)
    current_time = pygame.time.get_ticks()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

        if event.type == pygame.KEYDOWN:
            bullet_list.insert(0, tank_rect.midright)

    for i, bullet_pos in enumerate(bullet_list):
        bullet_list[i] = bullet_pos[0] + 5, bullet_pos[1]
        if bullet_surf.get_rect(center = bullet_pos).left > window.get_width():
            del bullet_list[i:]
            break

    window.fill((224, 192, 160))
    window.blit(tank_surf, tank_rect)
    for bullet_pos in bullet_list:
        window.blit(bullet_surf, bullet_surf.get_rect(center = bullet_pos))
    pygame.display.flip()

pygame.quit()
exit()
于 2020-10-23T06:19:32.170 回答
10
import pygame
pygame.init()
pygame.display.set_mode()
while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit(); #sys.exit() if sys is imported
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_0:
                print("Hey, you pressed the key, '0'!")
            if event.key == pygame.K_1:
                print("Doing whatever")

注意 K_0 和 K_1 不是唯一的键,要查看所有键,请参阅 pygame 文档,否则,tab在输入后点击

游戏。

(注意 .pygame 之后的)进入一个空闲程序。请注意,K 必须大写。另请注意,如果您不为 pygame 提供显示大小(不传递参数),那么它将自动使用计算机屏幕/监视器的大小。快乐编码!

于 2015-09-08T01:51:52.393 回答
2

我认为你可以使用:

pygame.time.delay(delayTime)

其中delayTime以毫秒为单位。

把它放在事件之前。

于 2020-03-13T08:44:16.660 回答
1

这背后的原因是 pygame 窗口以 60 fps(每秒帧数)运行,当您按下键 1 秒时,它会根据事件块的循环更新 60 帧。

clock = pygame.time.Clock()
flag = true
while flag :
    clock.tick(60)

请注意,如果您的项目中有动画,那么图像的数量将定义tick(). 假设您有一个角色,它需要 20 组图像用于行走和跳跃,那么您必须以tick(20)正确的方式移动角色。

于 2020-08-21T19:21:50.953 回答
0

尝试这个:

keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    if count == 10:
        location-=1
        count=0
    else:
        count +=1
    if location==-1:
        location=0
if keys[K_RIGHT]:
    if count == 10:
        location+=1
        count=0
    else:
        count +=1
    if location==5:
        location=4

这意味着你只移动 1/10 的时间。如果它仍然快速移动,您也可以尝试增加您设置的“计数”值。

于 2015-07-04T18:04:23.210 回答
0

仅供参考,如果你想确保船不会离开屏幕

location-=1
if location==-1:
    location=0

你可能可以更好地使用

location -= 1
location = max(0, location)

这样,如果它跳过-1,您的程序就不会中断

于 2018-01-08T15:21:24.210 回答
-2

您应该clock.tick(10)按照文档中的说明使用。

于 2015-03-10T14:25:40.897 回答
-3

上面的所有答案都太复杂了我只会将变量更改为 0.1 而不是 1 如果仍然太快,这会使船慢 10 倍 将变量更改为 0.01 这会使船慢 100 倍试试这个

keys=pygame.key.get_pressed()
if keys[K_LEFT]:
    location -= 0.1 #or 0.01
    if location==-1:
    location=0
if keys[K_RIGHT]:
    location += 0.1 #or 0.01
    if location==5:
        location=4
于 2016-08-02T20:29:07.813 回答
-3

要减慢游戏速度,请使用pygame.clock.tick(10)

于 2019-05-26T09:58:23.730 回答