pygame的 - 搁置按钮向下(pygame - on hold button down)

2019-08-03 10:53发布

我发现了一个解决方案,以一个精灵的举动,当你按住某个键。 问题是,它迫使书写丑陋重复的代码。 目前的解决方案,我发现是:

       for event in pygame.event.get():
            if event.type == KEYDOWN:
                keystate = pygame.key.get_pressed()
                while keystate[K_RIGHT]:
                    screen.fill((255,255,255))
                    pygame.event.get()

                    for sprite in sprites:
                        rimage = sprite[1].getimage()

                        if sprite[2] is None:
                            x+=3
                            sprite[1].update(time)
                            screen.blit(rimage, (x,y))
                            if sprite[1].isfinished() == True:
                                sprite[1].reset()
                            last_dir = "right"
                            if x >= screen_width - rimage.get_width():
                                x = screen_width - rimage.get_width()

                    #update player sprite movement
                    #update player sprite animation
                    #update rest of game map

                    keystate = pygame.key.get_pressed()
                    time = pygame.time.get_ticks()


                    pygame.display.update()

问题是,虽然keystate块。 它必须重复每个方向和游戏世界需要在每个块的同时进行更新。 这是五个地方在需要复制.... 4个方向,再加上如果一个键被按下不相同的代码。 我可以把它包在一个函数,但我不知道是否有更好的方法来处理按住在pygame的一个按钮。

Answer 1:

在pygame的通常的方法程序使用事件进行更新的方向,然后写入更新位置代码之外的事件,这样你就不需要重复的代码。

clock = pygame.time.Clock()

direction = (0,0)

while True:    # main loop

   for event in pygame.event.get():

        if event.type == KEYDOWN:
            if event.key == K_RIGHT:
                direction = (3, 0)
            elif event.key == K_LEFT:
                direction = (-3, 0)
            elif event.key == K_UP:
                direction = (0, 3)
            elif event.key == K_DOWN:
                direction = (0, -3)
            else:
                print "Unrecognized key"

        if event.type == KEYUP:
            direction = (0, 0)

    screen.fill((255,255,255))

    for sprite in sprites:
        rimage = sprite[1].getimage()

        if sprite[2] is None:

            # Check if new position is inside the screen
            new_pos = x + direction[0], y + direction[1]
            if new_pos[0] + rimage.get_width() < screen_width:
                x = new_pos[0]
            if new_pos[1] + rimage.get_height() < screen_height:
                y = new_pos[1]

            # Draw the sprite
            sprite[1].update(time)
            screen.blit(rimage, (x,y))
            if sprite[1].isfinished() == True:
                sprite[1].reset()
                last_dir = direction

    #update player sprite movement
    #update player sprite animation
    #update rest of game map

    time = pygame.time.get_ticks()

    pygame.display.update()

    clock.tick(40)  # Keep game running at 40 fps

如果你愿意,你可以实现与keystate一样,在这种情况下,你不需要过程中的关键事件。



Answer 2:

Pygame suggests or implies the division of programs into 3 parts:

The event handling, updating and drawing. As pmoleri already said, you simply change the direction of the movement. In the update function, you should pass in a delta time parameter, to move all the sprites according to the time passed. It is quite important, since the other technique doesn't take into account the variable speed of the processor. Games in DOS have been made this way, so now we need emulators to artificially slow down the processor. The draw part simply draws all the sprites.

This way you have a clear division between these 3 distinc parts in games: player input, game logic(movement, collision, actions etc.) and drawing.



Answer 3:

此外, pygame.key.get_pressed()都可以使用。

这将返回当前按住所有键的列表。

keys = pygame.key.get_pressed()

if keys[pygame.K_w]:
  # Move Up
if keys[pygame.K_a]:
  # Move Down
... etc ...

这并不需要在事件部分,但可能让玩家更新。

这可以使代码不太笨重,(可能)更有效

-Hope我帮助!



文章来源: pygame - on hold button down