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来运行游戏。但是,当我通过单击play退出函数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
非常感谢任何帮助!
答案 0 :(得分:0)
引发错误是因为您的代码在调用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
退出你的主循环。