Python 3.6.2,Pygame 1.9.3中的启动菜单错误



我决定将pong在pygame中作为一个不错的入门项目。这个错误不是很好。我到达了我想添加带有两个按钮的开始菜单的阶段,开始和退出。我做到了,这样当您的鼠标悬停在开始按钮上时,您按下空间会填充显示器的黑色。然后,我将继续进行游戏。但是,当我只是这样做时,窗户就会冻结并且没有响应。这是我的开始菜单代码:

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

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

while play:
    gameDisplay.fill(black)

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

窗口会冻结,因为您没有在while play:循环中处理事件,因此操作系统决定程序已变得无响应并将其锁定。您需要在段循环中至少致电pygame.event.pump(),以防止冻结或仅使用事件循环for event in pygame.event.get():

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

另外,请查看我如何使用draw.rect返回的pygame.Rect s及其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()

最新更新