屏幕上的Python游戏不会接受输入

时间:2018-10-28 17:26:07

标签: python pygame

我正在尝试做到这一点,以便当屏幕上方的游戏显示用户可以按下空格键重新进入游戏时。当前,当游戏结束时,它将在屏幕上显示游戏,但不接受任何输入,或者至少不对输入执行任何操作。在某些情况下,游戏基本上是左右移动以避免障碍。目前,我只有一个障碍,但是我还没有意识到。谢谢!

import pygame
import random
import math

pygame.init()

screenWidth = 700
screenHeight = 800

x = screenWidth / 2
y = (screenHeight / 4) * 3
width = 50
height = 50

win = pygame.display.set_mode((screenWidth, screenHeight))
pygame.display.set_caption("Test Game")

bg = pygame.image.load("background.png").convert()
gameover = pygame.image.load("gameover.png").convert()
bgx = (screenWidth / 6) * 2
bgy = 0

clock = pygame.time.Clock()

class enemy():
    def __init__(self,c,y,width,height):
        self.c = c
        self.y = y
        self.width = width
        self.height = height
        self.vel = 5

    def draw(self, win):
        if self.c == 1:
            self.x = 250
            #250
        elif self.c == 2:
            self.x = 350
            #350
        else:
            self.x = 450
            #450
        self.y += self.vel
        pygame.draw.rect(win, (0,0,255), (self.x,self.y,self.width,self.height))

evil = enemy(random.randint(1,3),0,50,50)

#def redrawGameWindow():
   # evil.draw(win)

   # pygame.display.update()

running = True
gameOver = False

while running:
    clock.tick(60)
    while gameOver:
        win.blit(gameover, (0,0))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
                pygame.quit()
            if pygame.event.type == pygame.KEYUP:
                if event.key == pygame.K_SPACE:
                    gameOver = True




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

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                x+=100
            if event.key == pygame.K_LEFT:
                x-=100

    win.fill((0,0,0))
    win.blit(bg, (bgx, bgy))
    evil.draw(win)
    dist = math.hypot(evil.x - x, evil.y - y)
    if dist <= 50:
            print("Game Over!")
            running = False
            gameOver = True
    pygame.draw.rect(win, (255,0,0), (x,y,width,height))
    pygame.display.update()
    #redrawGameWindow()

while gameOver:
    win.blit(gameover, (0,0))
    pygame.display.update()

    keys = pygame.key.get_pressed()

    if keys[pygame.K_SPACE]:
        gameOver = False

pygame.quit()

1 个答案:

答案 0 :(得分:0)

主要问题是,您在场上的游戏是主要while running:循环背后的循环,到达该循环时无法回到第一个循环。触摸邪恶对象时,您设置了running = False,离开了主体并进入了while gameOver:循环。

在此循环中,您还需要调用pygame.event.pump(),否则pygame.key.get_pressed()函数将无法正常工作,并且由于未处理事件,窗口会在一段时间后冻结。如果要重新启动游戏,则应该只使用嵌套的while gameOver循环。实际上,我建议您更多地重构场景,并改用有限状态机(在answer中,我将函数用作场景,但也请查看下面评论中的链接)。

这是您代码的有效版本。我已经做了一些更改,并添加了注释以解释这些更改。

while running:
    # -----The game over scene.-----
    while gameOver:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                # pygame.quit only uninitializes the pygame modules and
                # doesn't quit the program.
                pygame.quit()
                # This will quit the whole program. You need to import sys.
                sys.exit()
            elif event.type == pygame.KEYUP:  # event.type not pygame.event.type
                if event.key == pygame.K_SPACE:
                    # Change it to False to break out of the loop.
                    gameOver = False
                    # Reset the game. You could reset the position of the
                    # `evil` object or instantiate a new one.
                    evil.x = 350
                    evil.y = 0

        win.blit(gameover, (0,0))
        pygame.display.update()
        clock.tick(60)  # You need to call tick in this loop as well.
    # ------------------------------
    # -----The main scene.-----
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_RIGHT:
                x += 100
            elif event.key == pygame.K_LEFT:
                x -= 100

    win.fill((0,0,0))
    win.blit(bg, (bgx, bgy))
    evil.draw(win)
    dist = math.hypot(evil.x - x, evil.y - y)
    if dist <= 50:
        print("Game Over!")
        # running = False  # This would stop the main loop.
        gameOver = True
    pygame.draw.rect(win, (255,0,0), (x,y,width,height))
    pygame.display.update()
    clock.tick(60)


# The other while loop was removed.
pygame.quit()