2015-12-09 6 views
0
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 для запуска игры. Однако, когда я выхожу из функции 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

Ошибка на самом деле является «ошибкой: отображение Поверхность завершена» не шрифт не инициализирован, извините – nickmont

ответ

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 

, чтобы выйти из вашего MainLoop.

Смежные вопросы