如何通过按住按钮让 pygame 中的对象移动? [英] How do I get my object in pygame to move by holding the button?

查看:88
本文介绍了如何通过按住按钮让 pygame 中的对象移动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用类似的东西,但没有用.

I tried using something like this but it did not work.

while running:
keys = pygame.key.get_pressed()  #checking pressed keys
if keys[pygame.K_UP]:
    y1 -= 1
if keys[pygame.K_DOWN]:
    y1 += 1

这是我目前拥有的代码.我尝试使用不同的方法,因为上述方法对我不起作用.它一次一键移动.

Here is the code I have at the moment. I tried using a different method because the above method did not work for me. It moves it one button tap at a time.

import pygame
spaceX = 100
spaceY = 100
pygame.init
black = (0,0,0)
size = (800, 800)
screen = pygame.display.set_mode(size)

def game(spaceX, spaceY):
    spaceship_file = 'spaceship.png'
    spaceship_image = pygame.image.load(spaceship_file)
    spaceship = pygame.transform.scale(spaceship_image, (30, 30))
    screen.fill(black)
    screen.blit(spaceship, (spaceX, spaceY))
    pygame.display.flip()

    while True:
        for event2 in pygame.event.get():
            if event2.type == pygame.KEYDOWN and event2.key == pygame.K_LEFT:
                spaceX2 = spaceX - 5
                game(spaceX2, spaceY)

            elif event2.type == pygame.KEYDOWN and event2.key == pygame.K_RIGHT:
                spaceX2 = spaceX + 5
                game(spaceX2, spaceY)

game(spaceX, spaceY)

我现在有这个代码.我的对象仍然只在我反复点击按钮时移动,而不是在我按住它时移动.

I have this code now. My object still only moves when I repeatedly tap the button and not when I hold it down.

def game(spaceX, spaceY):

    while True:
        screen.fill(black)
        screen.blit(spaceship, (spaceX, spaceY))
        pygame.display.flip()
        keys = pygame.key.get_pressed()

        for event in pygame.event.get():
            if keys[pygame.K_LEFT]:
                spaceX = spaceX - 5
                game(spaceX, spaceY)

            elif keys[pygame.K_RIGHT]:
                spaceX = spaceX + 5
                game(spaceX, spaceY)

推荐答案

有两种方法可以检查键输入,如果它们在事件队列中,或者检查它们的当前状态.

There are two ways to check for key input, either if they're in the event queue or checking their current state.

键在按下或释放时被放入事件队列,因此您无法直接判断键是否被按住.有两种方法可以解决这个问题:

Keys are put into the event queue when they're pressed or realesed, so you cannot directly tell if a key is held or not. There are two ways around this:

  1. 在进入主循环之前调用 pygame.key.set_repeat(True).这将重复地将 KEYDOWN 事件放入事件队列,只要它被保持.

  1. Call pygame.key.set_repeat(True) before you enter the main loop. This will repeatedly put the KEYDOWN event into the event queue, as long as it's held.

引入速度变量.当用户按下按键时将速度设置为某个速度,当用户抬起按键时将速度设置为 0.

Introduce a velocity variable. Set the velocity to some speed when the user press down the key, and set the velocity to 0 when the user lift the key.

例如:

for event in pygame.event.get():
    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_LEFT:
            velocity_x = -5
        elif event.key == pygame.K_RIGHT:
            velocity_x = 5
    elif event.type == pygame.KEYUP:
        if event.key == pygame.K_LEFT:
            velocity_x = 0
        elif event.key == pygame.K_RIGHT:
            velocity_x = 0

position_x += velocity_x

状态检查

您可以通过调用 pygame.key.get_pressed() 获取键的当前状态.您必须调用 pygame.event 中的函数之一,以便 pygame 执行一些必需的内部操作,因此如果不这样做,请使用 pygame.event.pump()不想使用事件队列.如果您不这样做,您的操作系统会认为 pygame 已崩溃,从而导致游戏无响应.虽然,您通常希望处理 pygame.QUIT 事件,以便用户可以退出.

State checking

You get the current state of the keys by calling pygame.key.get_pressed(). You have to call one of the functions in pygame.event in order for pygame to do some required internal actions, so use pygame.event.pump() if you don't want to use the event queue. Your OS will think pygame has crashed if you don't, resulting in the game being unresponsive. Although, you usually want to handle the pygame.QUIT event, so the user can quit.

pygame.event.pump()
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
    position_x -= 5
elif keys[pygame.K_RIGHT]:
    position_x += 5

这篇关于如何通过按住按钮让 pygame 中的对象移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆