Pygame 1.9.3在Python 3.6.2中启动菜单错误

时间:2017-09-02 09:48:41

标签: python python-3.x menu pygame

我决定让pygame中的Pong成为一个不错的入门项目。这个错误虽然不太好。我到了舞台,我想添加一个带有两个按钮的开始菜单,启动和退出。我做了这样,当你的鼠标悬停在开始按钮上并按下空格时,它将用黑色填充显示。然后我会继续制作游戏。但是,当我这样做时,窗口冻结并且没有响应。这是我的开始菜单代码:

def startMenu():
        gameDisplay.fill(black)
        gameDisplay.blit(font2.render("PONG", True, white), (250, 0))
        start = pygame.draw.rect(gameDisplay, white, (210, 300, 40, 40))
        quit = pygame.draw.rect(gameDisplay, white, (600, 300, 40, 40))
        gameDisplay.blit(font1.render("Start", True, white), (170, 350))
        gameDisplay.blit(font1.render("Quit", True, white), (560, 350))

        mousePos = pygame.mouse.get_pos()

        #start button
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE:
                if mousePos[0] > 210 and mousePos[0] < 250:
                    if mousePos[1] > 300 and mousePos[1] < 340:
                        play = True
                        while play:
                            gameDisplay.fill(black)
startMenu()

我还在这里包含了我的整个主要代码:

import pygame, time

pygame.init()

displayWidth = 800
displayHeight = 600

black = (0, 0, 0)
white = (255, 255, 255)

gameDisplay = pygame.display.set_mode((displayWidth, displayHeight))
gameCaption = pygame.display.set_caption("Pong")
gameClock = pygame.time.Clock()
font1 = pygame.font.Font('freesansbold.ttf', 50)
font2 = pygame.font.Font('freesansbold.ttf', 115)

def gameLoop():

    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True

        def startMenu():
            gameDisplay.fill(black)
            gameDisplay.blit(font2.render("PONG", True, white), (250, 0))
            start = pygame.draw.rect(gameDisplay, white, (210, 300, 40, 40))
            quit = pygame.draw.rect(gameDisplay, white, (600, 300, 40, 40))
            gameDisplay.blit(font1.render("Start", True, white), (170, 350))
            gameDisplay.blit(font1.render("Quit", True, white), (560, 350))

            mousePos = pygame.mouse.get_pos()



            #start button
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    if mousePos[0] > 210 and mousePos[0] < 250:
                        if mousePos[1] > 300 and mousePos[1] < 340:
                            play = True
                            while play:
                                gameDisplay.fill(black)
        startMenu()

        pygame.display.update()
        gameClock.tick(60)

gameLoop()
pygame.quit()
quit()

感谢您的阅读,希望您能提供帮助!

2 个答案:

答案 0 :(得分:1)

while play:
    gameDisplay.fill(black)

那将永远运行,永远用黑色填充显示器。

答案 1 :(得分:0)

由于您未在while play:循环中处理事件,窗口会冻结,因此操作系统会判断程序已无响应并将其锁定。您需要在while循环中至少调用pygame.event.pump()以防止冻结或仅使用事件循环for event in pygame.event.get():

你尝试实现场景的方式有点奇怪。作为初学者,你不应该尝试将函数嵌套在其他函数中。而是执行类似于以下示例的操作:使用自己的while和event循环为菜单和游戏创建单独的函数,然后添加管理这些场景的另一个函数(main)。如果用户点击start按钮,只需返回下一个场景功能,然后在main中将其分配给scene变量并在下一次迭代中调用它以运行当前场景功能

另外,请查看我如何使用pygame.Rect返回的draw.rect及其collidepoint方法与按钮进行碰撞检测。 MOUSEBUTTONDOWN个事件具有pos属性,您可以使用该属性而不是调用pygame.mouse.get_pos

import sys
import pygame


pygame.init()

black = (0, 0, 0)
white = (255, 255, 255)

gameDisplay = pygame.display.set_mode((800, 600))
gameClock = pygame.time.Clock()
font1 = pygame.font.Font('freesansbold.ttf', 50)
font2 = pygame.font.Font('freesansbold.ttf', 115)


def startMenu():
    scene_done = False

    gameDisplay.fill(black)
    gameDisplay.blit(font2.render("PONG", True, white), (250, 0))
    start_button = pygame.draw.rect(gameDisplay, white, (210, 300, 40, 40))
    quit_button = pygame.draw.rect(gameDisplay, white, (600, 300, 40, 40))
    gameDisplay.blit(font1.render("Start", True, white), (170, 350))
    gameDisplay.blit(font1.render("Quit", True, white), (560, 350))

    pygame.display.update()

    while not scene_done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:  # Left mouse button.
                    if start_button.collidepoint(event.pos):
                        return gameLoop
                    elif quit_button.collidepoint(event.pos):
                        pygame.quit()
                        sys.exit()

        gameClock.tick(60)


def gameLoop():
    scene_done = False

    while not scene_done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    return startMenu

        gameDisplay.fill(black)
        gameDisplay.blit(font1.render("Game loop", True, white), (260, 150))
        pygame.display.update()
        gameClock.tick(60)


def main():
    """This function manages the scenes.

    Call the current scene function and when it's done
    assign the returned function to `scene`."""
    scene = startMenu

    while True:
        scene = scene()

main()