Python& Pygame:无法摆脱暂停状态

时间:2017-10-19 14:59:45

标签: python pygame python-idle

我是Python和Pygame的新手,我编写了一个小游戏并试图实现一个暂停菜单。虽然我可以进入暂停菜单,但我无法摆脱它。 我按照sentdex的教程进行了操作,并根据自己的喜好更改了代码。我正在使用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 个答案:

答案 0 :(得分:0)

您需要将type替换为keyif 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()