【问题标题】:Quitting pygame without quitting idle退出pygame而不退出idle
【发布时间】:2015-12-10 18:15:51
【问题描述】:
import pygame
import sys

pygame.init()

displayHeight = 600
displayWidth = 800

gameDisplay = pygame.display.set_mode((displayWidth,displayHeight))
pygame.display.set_caption('Pythoral Reef')

green = (0, 100, 0)
white = (255,255,255)
blue = (39, 64, 139)
black = (0,0,0)

clock = pygame.time.Clock()
img = pygame.image.load('coral reef.jpg')

play = False
instructions = False
done = False

def checkClick():
    global instructions, play, done
    if pygame.mouse.get_pos()[0] > 50 and pygame.mouse.get_pos()[0] < 250 and pygame.mouse.get_pos()[1] > 400 and pygame.mouse.get_pos()[1] < 500:
        instructions = True
    elif pygame.mouse.get_pos()[0] > 300 and pygame.mouse.get_pos()[0] < 500 and pygame.mouse.get_pos()[1] > 400 and pygame.mouse.get_pos()[1] < 500:
        play = True
    elif pygame.mouse.get_pos()[0] > 550 and pygame.mouse.get_pos()[0] < 750 and pygame.mouse.get_pos()[1] > 400 and pygame.mouse.get_pos()[1] < 500:
        done = True

def evalOption():
    global instructions, play, done, gameExit
    if instructions == True:
        print 'instructions'
        instructions = False

    elif play == True:
        pygame.display.quit()

    elif done == True:
        pygame.quit()
        sys.exit()

def textObjects(text, font, color):
    textSurface = font.render(text, True, color)
    return textSurface, textSurface.get_rect()

def messageDisplay(text):
    largeText = pygame.font.Font('freesansbold.ttf', 90)
    TextSurf, TextRect = textObjects(text, largeText, blue)    
    TextRect.center = ((displayWidth/2, displayHeight/4))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

def optionsDisplay(x, text, size):
    smallText = pygame.font.Font('freesansbold.ttf', size)
    TextSurf, TextRect = textObjects(text, smallText, white)    
    TextRect.center = ((x, 450))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

def nameDisplay(x, y, text):
    smallText = pygame.font.Font('freesansbold.ttf', 35)
    TextSurf, TextRect = textObjects(text, smallText, green)    
    TextRect.center = ((x, y))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

def rectangle(color, x, y):
    pygame.draw.rect(gameDisplay, color, [x, y, 200, 100])


def menu():
    gameExit = False
    if gameExit == False:
        gameDisplay.blit(img, [0, 0])
    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_x:
                    pygame.quit()
                    quit()
            elif event.type == pygame.MOUSEBUTTONDOWN:
                if event.button == 1:
                    checkClick()
                    evalOption()

        messageDisplay('PYTHORAL REEF')
        rectangle(black, 50, 400)
        rectangle(black, 300, 400)
        rectangle(black, 550, 400)
        optionsDisplay(150, 'INSTRUCTIONS', 22)
        optionsDisplay(400, 'PLAY', 35)
        optionsDisplay(650, 'QUIT', 35)
        nameDisplay(displayWidth/2, 260, 'NICK MONTELEONE')
        nameDisplay(displayWidth/2, 225, 'BY')
        nameDisplay(displayWidth/2, 295, '2015')
        pygame.display.update

menu()
pygame.quit()

这是我正在制作的文本冒险游戏的菜单屏幕代码。我想将其用作菜单屏幕,因此当您单击播放时,它将退出 pygame 并返回 python shell 运行游戏。但是,当我通过单击播放退出函数 evalOption() 时,它将打印错误:字体未初始化,就好像程序仍在尝试运行一样。有谁知道如何解决这个问题,以便我可以退出 pygame,但仍然在 python 中运行一个模块。这个程序最终会被导入到我的真实文本冒险模块中。如果需要,这里是珊瑚礁背景的链接:https://www.google.com/search?q=coral+reef&espv=2&biw=1440&bih=799&source=lnms&tbm=isch&sa=X&ved=0ahUKEwix_OmD7s_JAhXGWD4KHXW-CrsQ_AUIBigB#imgrc=_dFX3xqK97FGNM%3A

任何帮助将不胜感激!

【问题讨论】:

  • 这个错误其实是'error: display Surface quit' 不是字体没有初始化,抱歉

标签: python fonts pygame exit


【解决方案1】:

引发错误是因为您的代码在您调用 pygame.quit() 后尝试绘制到屏幕上。

代替

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_x:
        pygame.quit()
        quit()

随便用

if event.type == pygame.KEYDOWN:
    if event.key == pygame.K_x:
        return

退出你的主循环。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多