Python & Pygame: не может выйти из состояния паузы

Я новичок в Python и Pygame, и я запрограммировал небольшую игру и попытался реализовать меню паузы. Хотя я могу добраться до меню паузы, я не могу выйти из него. Я последовал руководству по senddex и немного изменил код по своему вкусу. Я работаю с Python-Idle, но я не получаю никаких сообщений об ошибках, поэтому я понятия не имею, что я делаю неправильно.

import pygame
import time
import random
import shelve

pygame.init()

display_width = 800
display_height = 600

black = (0,0,0)
white = (255,255,255)

starfighter_width = 160

highscore = 0

paused = False

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('A bit Racey')
clock = pygame.time.Clock()

backgroundImg = pygame.image.load('background.jpg')

asteroidImg = pygame.image.load('asteroid.png')
asteroidImg = pygame.transform.scale(asteroidImg, (100,100))

starfighterImg = pygame.image.load('x_wing.png')
starfighterImg = pygame.transform.scale(starfighterImg, (160, 100))

def save_score(score):
    d = shelve.open('score.txt')
    if score > d['score']:
        d['score'] = score

    d.close()

def get_highscore():
    d = shelve.open('score.txt')
    global highscore
    highscore = d['score']
    d.close()

def display_highscore():
    global highscore
    font = pygame.font.SysFont(None, 25)
    text = font.render("Highscore: " + str(highscore), True, white)
    gameDisplay.blit(text,(660, 0))

def things_dodged(count):
    font = pygame.font.SysFont(None, 25)
    text = font.render("Dodged: " + str(count), True, white)
    gameDisplay.blit(text,(0,0))

def things(thingx, thingy):
    gameDisplay.blit(asteroidImg, (thingx, thingy))

def starfighter(x,y):
    gameDisplay.blit(starfighterImg,(x,y))

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

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',85)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((display_width/2),(display_height/2))
    gameDisplay.blit(TextSurf, TextRect)

    pygame.display.update()

    time.sleep(2)

    game_loop()

def crash():
    message_display('Crashed you have')

def pause():

    global paused

    while paused:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                if event.type == pygame.K_ESCAPE:
                    paused = False

        largeText = pygame.font.Font('freesansbold.ttf',85)
        TextSurf, TextRect = text_objects("Paused", largeText)
        TextRect.center = ((display_width/2),(display_height/2))

        gameDisplay.blit(TextSurf, TextRect)
        pygame.display.update()

def game_intro():

    intro = True

    while intro:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                quit()
            if event.type == pygame.KEYDOWN:
                intro = False

        gameDisplay.blit(backgroundImg, (0,0))
        largeText = pygame.font.Font('freesansbold.ttf',85)
        TextSurf, TextRect = text_objects("Starfighter", largeText)
        TextRect.center = ((display_width/2),(display_height/2))
        gameDisplay.blit(TextSurf, TextRect)

        smallText = pygame.font.Font('freesansbold.ttf',35)
        TextSurf, TextRect = text_objects("Press any key", smallText)
        TextRect.center = ((display_width/2),(display_height/2 + 50))

        gameDisplay.blit(TextSurf, TextRect)
        pygame.display.update()



def game_loop():
    x = (display_width * 0.45)
    y = (display_height * 0.8)

    x_change = 0

    thing_startx = random.randrange(0, display_width)
    thing_starty = -600
    thing_speed = 4
    thing_width = 100
    thing_height = 100

    dodged = 0

    gameExit = False

    get_highscore()

    global paused

    while not gameExit:

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

            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    x_change = -5
                elif event.key == pygame.K_RIGHT:
                    x_change = 5
                elif event.key == pygame.K_ESCAPE:
                    paused = True
                    pause()


            if event.type == pygame.KEYUP:
                if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                    x_change = 0

        x += x_change
        gameDisplay.blit(backgroundImg, (0,0))

        things(thing_startx, thing_starty)
        thing_starty += thing_speed
        starfighter(x,y)
        things_dodged(dodged)
        display_highscore()

        if x > display_width - starfighter_width or x < 0:
            save_score(dodged)
            crash()

        if thing_starty > display_height:
            thing_starty = 0 - thing_height
            thing_startx = random.randrange(0, display_width)
            dodged += 1
            thing_speed += 0.3

        if y < thing_starty + thing_height:

            if x + starfighter_width > thing_startx and x < thing_startx + thing_width:
                save_score(dodged)
                crash()

        pygame.display.update()
        clock.tick(60)

game_intro()
game_loop()
pygame.quit()
quit()

1 ответ

Решение

Вам нужно заменить type с key Вот: if event.type == pygame.K_ESCAPE:,

Вам также не нужны глобальные paused переменная, просто оставайтесь в pause функционировать, пока пользователь не нажмет escape, а затем просто return из функции.

import sys
import pygame


pygame.init()

display = pygame.display.set_mode((800, 600))
clock = pygame.time.Clock()


def pause():
    while True:
        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

        pygame.draw.rect(display, (250, 0, 0), display.get_rect(), 3)
        pygame.display.update()
        clock.tick(60)


def game_loop():
    x = 0
    gameExit = False

    while not gameExit:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_ESCAPE:
                    pause()

        x += 1
        display.fill((30, 30, 30))
        pygame.draw.rect(display, (0, 200, 250), (x, 200, 20, 20))

        pygame.display.update()
        clock.tick(60)


game_loop()
pygame.quit()
Другие вопросы по тегам