蟒蛇&Pygame:无法退出暂停状态 [英] Python & Pygame: Can't get out of paused state
问题描述
我是 Python 和 Pygame 的新手,我编写了一个小游戏并尝试实现暂停菜单.虽然我可以进入暂停菜单,但我无法摆脱它.我遵循了 sentdex 的教程,并根据我的喜好稍微更改了代码.我正在使用 Python-Idle,但我也没有收到任何错误消息,所以我不知道我做错了什么.
I'm new to Python and Pygame and I programmed a little game and tried to implement a pause menu. While I can get to the pause menu I can't get out of it. I followed a tutorial by sentdex and changed the code a bit to my liking. I'm working with Python-Idle but I don't get any error messages there either so I have no idea what I'm doing wrong.
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()
推荐答案
You need to replace type
with key
here: if event.type == pygame.K_ESCAPE:
.
You need to replace type
with key
here: if event.type == pygame.K_ESCAPE:
.
您也不需要全局 paused
变量,只需停留在 pause
函数中,直到用户按下转义键,然后只需 return
来自函数.
You also don't need the global paused
variable, just stay in the pause
function until the user presses escape and then simply return
from the function.
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()
这篇关于蟒蛇&Pygame:无法退出暂停状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!