pygame-按住按钮不放 [英] pygame - on hold button down

查看:375
本文介绍了pygame-按住按钮不放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了一种解决方案,可以在按住键时移动精灵.问题是它迫使编写难看的重复代码.我发现的当前解决方案是:

I found a solution to make a sprite move when you hold a key down. The problem is that it forces writing ugly duplicated code. The current solution I found is:

       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()

问题在于while键状态块.必须对每个方向重复进行此操作,并且需要在每个while块中更新游戏世界.那是需要复制相同代码的五个地方....4个方向,如果不按任何键,也是如此.我可以将其包装在一个函数中,但是我想知道是否有更好的方法可以在pygame中按住一个按钮.

The problem is that the while keystate block. It has to be repeated for each direction and the game world needs to be updated in each while block. That is five places where the same code needs to be duplicated....4 directions plus if a key is not pressed. I could wrap it in a function but I was wondering if there was a better way to handle holding down a button in pygame.

推荐答案

在pygame中,程序通常使用事件来更新方向,然后在事件外编写更新位置代码,这样就无需复制代码

The usual way program in pygame is use the events to update the direction, then write the update position code outside events, that way you don't need replicated code.

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达到相同的目的,在这种情况下,您不需要处理键事件.

If you want you can achieve the same with keystate, in such case you don't need to process key events.

这篇关于pygame-按住按钮不放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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