pygame.key.get_pressed() 中移动功能的问题

发布于 2025-01-21 01:51:09 字数 1081 浏览 1 评论 0原文

我对Python和编码很新。尝试使用Pygame编码一个简单的游戏,我发现了一个我无法在运动功能中解决的问题。

我有一个使用Pygame方法get_pressed()的函数,以设置播放器速度向量的方向(见下文)。使用此代码,如果在游戏中,我按住 a ,然后按 d 播放器表面会改变方向,即使我仍然按住 a 键,如果我做相反的话,玩家会继续向右移动,好像我不按 a 键一样。 我知道这是由于该功能中的IF语句的顺序所致,我的问题是:有没有办法在两种情况下都可以改变方向?

我真的希望这个问题很清楚,要提前感谢有人会帮助我。

设置速度方向的功能:

def get_keys(self):
        self.vel = vec(0, 0)
        keys = pg.key.get_pressed()
        if keys[pg.K_a]:
            self.vel.x = -PLAYER_SPEED
        if keys[pg.K_d]:
            self.vel.x = PLAYER_SPEED
        if keys[pg.K_w]:
            self.vel.y = -PLAYER_SPEED
        if keys[pg.K_s]:
            self.vel.y = PLAYER_SPEED
        if self.vel.x != 0 and self.vel.y != 0:
            self.vel *= 0.7071

更新功能:

def update(self)
    self.get_keys()
        self.pos += self.vel * self.game.dt
        self.rect.centerx = self.pos.x
        self.rect.centery = self.pos.y

I'm quite new with python and coding. Trying to code a simple game using pygame I have found an issue that I can't solve in my movement function.

I have a function that uses the pygame method get_pressed() in order to set the direction of the player velocity vector (see below). With this code if in the game I hold down a and then press d the player surface changes direction even if i'm still holding down the a key, while if I do the opposite the player continues moving to the right as if I don't press the a key.
I understand that this is due to the order of the if statements in the function and my question is: is there a way to make the player change direction in both cases?

I really hope that the question is clear and thanks in advance if someone will help me.

The function that sets the direction of the velocity:

def get_keys(self):
        self.vel = vec(0, 0)
        keys = pg.key.get_pressed()
        if keys[pg.K_a]:
            self.vel.x = -PLAYER_SPEED
        if keys[pg.K_d]:
            self.vel.x = PLAYER_SPEED
        if keys[pg.K_w]:
            self.vel.y = -PLAYER_SPEED
        if keys[pg.K_s]:
            self.vel.y = PLAYER_SPEED
        if self.vel.x != 0 and self.vel.y != 0:
            self.vel *= 0.7071

The update function:

def update(self)
    self.get_keys()
        self.pos += self.vel * self.game.dt
        self.rect.centerx = self.pos.x
        self.rect.centery = self.pos.y

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌面超妹 2025-01-28 01:51:09

对于您想做的事情,您必须使用键盘事件。请注意,您希望在按下某个键后立即改变方向。这就是事件。例如:

import pygame

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

pos = pygame.Vector2(window.get_rect().center)
dx, dy = 0, 0
speed = 5

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                dx = -1
            if event.key == pygame.K_d:
                dx = 1
            if event.key == pygame.K_w:
                dy = -1
            if event.key == pygame.K_s:
                dy = 1
        if event.type == pygame.KEYUP:
            if (event.key == pygame.K_a and dx < 0) or (event.key == pygame.K_d and dx > 0):
                dx = 0
            if (event.key == pygame.K_w and dy < 0) or (event.key == pygame.K_s and dy > 0):
                dy = 0

    vel = pygame.Vector2(dx, dy)
    if vel.x != 0 or vel.y != 0:
        vel.scale_to_length(speed)

    pos += vel
    pos.x %= window.get_width()
    pos.y %= window.get_height()

    window.fill(0)
    pygame.draw.circle(window, (255, 0, 0), pos, 20)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()

For what you want to do you have to use the keyboard events. Note that you want to change direction as soon as a key is pressed. This is and event. e.g.:

import pygame

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

pos = pygame.Vector2(window.get_rect().center)
dx, dy = 0, 0
speed = 5

run = True
while run:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False 
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_a:
                dx = -1
            if event.key == pygame.K_d:
                dx = 1
            if event.key == pygame.K_w:
                dy = -1
            if event.key == pygame.K_s:
                dy = 1
        if event.type == pygame.KEYUP:
            if (event.key == pygame.K_a and dx < 0) or (event.key == pygame.K_d and dx > 0):
                dx = 0
            if (event.key == pygame.K_w and dy < 0) or (event.key == pygame.K_s and dy > 0):
                dy = 0

    vel = pygame.Vector2(dx, dy)
    if vel.x != 0 or vel.y != 0:
        vel.scale_to_length(speed)

    pos += vel
    pos.x %= window.get_width()
    pos.y %= window.get_height()

    window.fill(0)
    pygame.draw.circle(window, (255, 0, 0), pos, 20)
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
exit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文