当同时按下太多键时,Pygame播放器移动停止

时间:2017-06-04 01:01:10

标签: python pygame python-3.6

当我试图移动我的角色并同时按任何其他键时,该动作突然停止。例如,我的代码中的空格键用于射出太空船中的一个小球。每当我按空格键并且我快速左右移动时,球体将会射出但玩家的动作会被冻结一两秒钟。

我试图切换到处理键输入方式的不同方法,但所有这些方法似乎都会导致同样的问题。在我的代码中,pygame.key.get_pressed()也存在此问题。

我不太确定这是我的笔记本电脑键盘或代码中的某些问题,因此整个文件的代码如下所示。谢谢!

import pygame, sys, decimal

# Screen Size
SCREEN_X = 400
SCREEN_Y = 400

# Loading Images
backgroundImg = pygame.image.load('StarBackground.png')
menuBar = pygame.image.load('Menu_Bar.png')
shipImg = pygame.image.load('PowerShip.png')
orb = pygame.image.load('Orb00.png')

class Ship(pygame.sprite.Sprite):
    # Movement rate of change
    change_x = 0

    # Methods
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = shipImg.convert_alpha()

        self.rect = self.image.get_rect()

        self.rect.x = SCREEN_X / 2 - 8
        self.rect.y = SCREEN_Y - 40

    def move(self, speed):
        self.change_x = speed

    def stop(self):
        self.change_x = 0


    def update(self, screen):
        self.rect.x += self.change_x

        if self.rect.x < 0:
            self.rect.x = 0
        elif self.rect.right > SCREEN_X:
            self.rect.x -= 1

        screen.blit(self.image, self.rect)

class MenuBar(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = menuBar.convert_alpha()

        self.rect = self.image.get_rect()

        self.rect.x = 10
        self.rect.y = 0

    def update(self, screen):

        screen.blit(self.image,self.rect)

class Bullet1(pygame.sprite.Sprite):

    def __init__(self,x,y):
        pygame.sprite.Sprite.__init__(self)

        self.image = orb.convert_alpha()
        self.rect = self.image.get_rect()

        self.rect.x = x
        self.rect.y = y
        self.alive = True

    def update(self):
        if self.alive == True:
            self.rect.y -= 1

        if self.alive == False:
            self.rect.y = -10000

class HealthBar(pygame.sprite.Sprite):
    pass

class EnergyBar(pygame.sprite.Sprite):
    pass

class PointsBar(pygame.sprite.Sprite):
    pass

class Background(pygame.sprite.Sprite):

    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = backgroundImg.convert_alpha()

        self.rect = self.image.get_rect()

        self.rect.x = 0
        self.rect.y = 0

    def update(self, screen):
        if self.rect.top > 0:
            self.rect.y = SCREEN_Y * -1

        self.rect.y += 1

        screen.blit(self.image, self.rect)

def main():
    pygame.init()

    size = [SCREEN_X, SCREEN_Y]
    screen = pygame.display.set_mode(size, pygame.DOUBLEBUF)    # Set the height and width of the screen

    pygame.display.set_caption("Space Adventure")               # Setting the game name in the title bar

    background = Background()       # Creating the game objects
    menubar = MenuBar()
    ship = Ship()
    finished = False                # Close button exit code

    bullet1Enabled = True
    bullet1Count = 1
    spacePressed = False

    clock = pygame.time.Clock()     # Manages the frames per second

    lastkey = None                  # Variable that stores the last key pressed

    bulletlist = []

    # Game loop
    while not finished:
        for event in pygame.event.get():
            print(lastkey)
            if event.type == pygame.QUIT:
                finished = True

            pygame.event.set_blocked(pygame.MOUSEMOTION)

            if event.type == pygame.KEYDOWN:

                if lastkey != pygame.K_SPACE:
                    lastkey = event.key


                if event.key == pygame.K_SPACE:
                    spacePressed = True
                    if bullet1Enabled == True:
                        bullet1 = Bullet1(ship.rect.x, ship.rect.y)
                        bulletlist.append(bullet1)
                        bullet1Count = 1
                else:
                    spacePressed = False

            if event.type == pygame.KEYUP:

                if event.key == pygame.K_RIGHT and lastkey != pygame.K_LEFT:
                    lastkey = None
                    ship.move(0)
                if event.key == pygame.K_LEFT and lastkey != pygame.K_RIGHT:
                    lastkey = None
                    ship.move(0)

                if event.key == pygame.K_RIGHT or lastkey == pygame.K_LEFT:
                    spacePressed = False

                if event.key == pygame.K_LEFT or lastkey == pygame.K_RIGHT:
                    spacePressed = False

            #Bullet Delay

            if spacePressed == True:
                 bullet1Count = True
            if spacePressed == False:
                bullet1Count =  False

            if lastkey == pygame.K_RIGHT:
                ship.move(1)
            if lastkey == pygame.K_LEFT:
                ship.move(-1)

        clock.tick(240)             # Frames per second

        background.update(screen)   # Background update
       # Menu Bar update
        ship.update(screen)         # Ship update

        for b in bulletlist:
            if b.rect.bottom <= 0:
                b.alive = False

            b.update()

            screen.blit(b.image, b.rect)

        menubar.update(screen)

        pygame.display.flip()       # Updates the display for everything

    pygame.quit() # Clean shutdown on IDLE

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:1)

出现此问题的原因是您在释放空格键后未将lastkey重置为None,因此您必须向左或向右按​​两次。

if event.type == pygame.KEYUP:
    if event.key == pygame.K_SPACE:
        lastkey = None

我不明白为什么你需要lastkey变量。我将从主循环中删除这些行,

if lastkey == pygame.K_RIGHT:
    ship.move(1)
if lastkey == pygame.K_LEFT:
    ship.move(-1)

将它们插入事件循环并将lastkey更改为event.key

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_RIGHT:
        ship.move(1)
    if event.key == pygame.K_LEFT:
        ship.move(-1)

现在您应该可以完全删除lastkey