首页 > 解决方案 > PyGame 窗口在 while 循环期间崩溃 - 没有错误消息

问题描述

import pygame, sys, time, os
from scripts import UltraColor
from scripts import textures

pygame.init()

displayWidth = 1280
displayHeight = 720
black = (0,0,0)
white = (255,255,255)
menu = True
gender = True

display = pygame.display.set_mode((displayWidth, displayHeight))

pygame.display.set_caption("Blazing Badge")
clock = pygame.time.Clock()

warriorImage = pygame.image.load("warrior.png")
grassImage = pygame.image.load("grass.png")
playButton = pygame.image.load("play button.png")
durandal = pygame.image.load("durandal.png")
mainscreen = pygame.image.load("mainmenu.jpg")
logo = pygame.image.load("logo.png")

running = True
menu_viewed = False

def empty_screen():
    display.fill(white)

def text_objects(text, font, colour):
    textSurface = font.render(text, True, colour)
    return textSurface, textSurface.get_rect()


action = None

def game_menu():
    intro = True
    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
        display.fill(white)
        largeText = pygame.font.Font("The Fighter.otf", 115)
        TextSurf, TextRect = text_objects("Tech Demo", largeText, black)
        TextSurf = pygame.transform.rotate(TextSurf, 15)
        TextRect.center = ((displayWidth*0.68), (displayHeight*0.4))
        playpos = (((displayWidth/2)-100), (displayHeight)*0.7)
        durandalpos = (((displayWidth/2)-280), (displayHeight*0.2))
        display.blit(mainscreen, (0,0))
        display.blit(playButton, playpos)
        durandalresized = pygame.transform.scale(durandal, (561, 333))
        display.blit(durandalresized, durandalpos)
        display.blit(logo, ((displayWidth*0.2), (displayHeight*0.35)))
        display.blit(TextSurf, TextRect)


        mouse = pygame.mouse.get_pos()
        click = pygame.mouse.get_pressed()

        print(click)
        print(mouse)


        if 580 < mouse[0] < 710 and 532 < mouse[1] < 674:
            if click[0] == 1:
                intro = False
                print("Start Game")
        pygame.display.update()
        clock.tick(15)

def character_creation():
    creation = True

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()
    gender_symbols = pygame.image.load(os.path.join("graphics\Character Creation", "Gender Symbols.png"))
    creation_background = pygame.image.load(os.path.join("graphics\Character Creation", "creationbackground.jpg"))
    TextSurf, TextRect = text_objects("Choose your gender", pygame.font.Font("The Fighter.otf", 115), white)


    display.blit(creation_background, (0,0))
    display.blit(TextSurf, (((1280/2)-500), displayHeight*0.1))
    display.blit(gender_symbols,(340, (displayHeight*0.6)))
    pygame.display.update()

    mouse = pygame.mouse.get_pos()
    click = pygame.mouse.get_pressed()
    print(click)
    print(mouse)
    mcgender = "Null"
    creation = False
    gender = True

    while gender:
        print(click)
        print(mouse)
        if 365 < mouse[0] < 602 and 457 < mouse[1] < 702:
            if click[0] == 1:
                mcgender = "Female"
        elif 457 < mouse[1] < 702 and 677 < mouse[0] < 916:
            if click[0] == 1:
                mcgender = "Male"
        if mcgender != "Null":
            gender = False

    print(mcgender)

    display.blit(creation_background, (0,0))
    pygame.display.update()

stone1 = pygame.image.load(os.path.join("graphics", "stone1.png"))

#Game Loop follows -----------------------------------------------------------------------------------------------------

while running:

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

        print(event)

    if menu_viewed == False:
        game_menu()
        menu_viewed = True

    clear_screen = True

    gender = True

    character_creation()


pygame.quit()
quit()

任何人都可以提供一些帮助吗?运行以下代码,将一直工作到以下部分:

while gender:
    print(click)
    print(mouse)
    if 365 < mouse[0] < 602 and 457 < mouse[1] < 702:
        if click[0] == 1:
            mcgender = "Female"
    elif 457 < mouse[1] < 702 and 677 < mouse[0] < 916:
        if click[0] == 1:
            mcgender = "Male"
    if mcgender != "Null":
        gender = False

没有产生错误代码,但游戏窗口停止响应,没有恢复的迹象。鼠标读数在终端中继续,但窗口没有响应。退出程序的唯一方法是强制停止 Pycharm 中的代码。

标签: pythoncrashpygame

解决方案


当您移动鼠标时,鼠标读数是否会持续变化?在我看来,您没有在循环中更新鼠标位置,因此即使您将鼠标悬停在某个选项上,pygame 也会认为您的鼠标仍位于它开始的默认位置。

本质上,这些行应该在您的while gender:循环内:

mouse = pygame.mouse.get_pos()
click = pygame.mouse.get_pressed()

推荐阅读