【问题标题】:How do I make sure that my pygame car keeps moving if one key is held down and another is released? [duplicate]如果按住一个键并释放另一个键,我如何确保我的 pygame 车继续移动? [复制]
【发布时间】:2021-10-17 15:17:15
【问题描述】:

解释起来有点混乱。所以,如果你有问题,请问他们。我正在使用 pygame 创建游戏,当您使用“上”、“下”、“左”和“右”键时,我的“汽车”会移动。但是,按照我的编码方式,例如,当我释放右键但仍按住左键时,它会将移动更改为 0,并且无法识别左键仍被按住。它使运动相当奇怪。我正在努力让这个动作更流畅,让游戏更有趣,任何想法都有帮助,谢谢。

这是我的代码:

import pygame

#Initializes pygame
pygame.init()

#Create the screen
screen = pygame.display.set_mode((1000, 800))

#Icon and Caption
icon = pygame.image.load('redcar.png')
pygame.display.set_icon(icon)
pygame.display.set_caption("Placeholder")

# Car

car = pygame.image.load('redcar.png')
x = 500
y = 600
x_change = 0
y_change = 0

def player(x,y):
    screen.blit(car, (x, y))

#Game loop, keep window open unless you quit the window
running = True
while running:
    #RGB
    screen.fill((255, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    #If key is pressed, check what key it was
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x_change = -1
            if event.key == pygame.K_RIGHT:
                x_change = 1
            if event.key == pygame.K_UP:
                y_change = -1
            if event.key == pygame.K_DOWN:
                y_change = 1
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                x_change = 0

            if event.key == pygame.K_RIGHT:
                x_change = 0

            if event.key == pygame.K_UP:
                y_change = 0

            if event.key == pygame.K_DOWN:
                y_change = 0
    x += x_change
    y += y_change
    player(x,y)
    #For each event, update window
    pygame.display.update()

【问题讨论】:

  • 您可以检查是否也没有按下计数器键:if event.key == pygame.K_LEFT and not event.key == pygame.K_RIGHT: x_change = 0 等等

标签: python pygame keyboard


【解决方案1】:

您可以使用pygame.key.get_pressed() 解决此问题。这将告诉您是否正在按住某个键。它可以在按住任意数量的键时起作用。

这是您的代码的简化版本,用于演示其工作原理:

import pygame

pygame.init()
pygame.font.init();
myfont = pygame.font.SysFont('Consolas', 30)

screen = pygame.display.set_mode((1000, 800))

running = True
while running:
    screen.fill((255, 255, 255))

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    pressed = pygame.key.get_pressed()
    if pressed[pygame.K_UP]:
        textsurface = myfont.render('Up', False, (0, 0, 0))
        screen.blit(textsurface,(0,0))
    if pressed[pygame.K_DOWN]:
        textsurface = myfont.render('Down', False, (0, 0, 0))
        screen.blit(textsurface,(0,50))    
    if pressed[pygame.K_LEFT]:
        textsurface = myfont.render('Left', False, (0, 0, 0))
        screen.blit(textsurface,(0,100))
    if pressed[pygame.K_RIGHT]:
        textsurface = myfont.render('Right', False, (0, 0, 0))
        screen.blit(textsurface,(0,150))          
    
    pygame.display.update()

如果我按住 LeftUp,窗口将显示:

您可以在调用get_pressed() 之前将x_changey_change 设置为零,然后在ifs 中设置这些变量,具体取决于哪个评估为真。例如如果按下左键,则将x_change 设置为-1

您可能想考虑当用户按下上+下(或左+右)时会发生什么。这可以取消,也可以一个键优先。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-30
    • 2017-10-23
    相关资源
    最近更新 更多