我的程序遇到一个问题,当游戏进入新的while循环时,它冻结(或至少看上去冻结)。
当我在窗口外单击时,游戏停止响应,这使我相信游戏很可能由于没有检测到输入而冻结/崩溃。
在玩我的游戏时,用户可以选择四个选项(目前只有两个选项有些用),其中一个允许他们访问库存。当变量“ inv” == True时,游戏将进入一个类似于游戏主循环的循环,在该循环中将发生仅限于清单屏幕的过程。
以下是与“ inv == True” while循环相关的代码:
def textChange(end, inv, spin, fight, run): #code for changing text
integ = random.randint(0,4) # for choosing a random escape quote from the runWords variable
if end == True:
display_box.draw(screen) # draws the main box to the screen.
textDisplay("", 487, 312, fBold)
textDisplayMessage(run[integ],12,297)
time.sleep(2)
pygame.quit() #quit game
sys.exit()
gameRun = False #break the loop.
elif inv == True: #if the inventory has been selected
while inv == True: #if user in the inventory menu:
key = pygame.key.get_pressed()
inv_window = pygame.transform.scale(pygame.image.load("bigbox.png"), (449, 289)) #loads display box
screen.fill(bg_colour) #fills screen
screen.blit(inv_window, (75,0)) #blits the display box
display_box.drawsmall(screen) #draws smaller box
pygame.display.update() #updates display
if key[pygame.K_ESCAPE]: #if down
inv = False
print("howdy")
else:
display_box.draw(screen) #display larger message box
display_box.drawsmall(screen) #display smaller selection box
textDisplay("fight", 487, 312, fBold) #display fight option
textDisplay("items", 561, 312, iBold) #item option
textDisplay("spin",487,376, sBold) #spin option
textDisplay("run",561,376, rBold) #run
textDisplayMessage(message, 12, 297)
这是上下文的完整代码:
#inventory
#display items in 2 x 4 at right of box
#next page option if more items needed
#items type listed down left side
#select item: use option and back option appear in lower right box
#back option will be present in lower right box otherwise.
import time, random, sys
import pygame
pygame.init()
(width, height) = (600, 400) #specify width, height
bg_colour = (100, 20, 156) #specify bgcolour
#pre-defining some variables so they can be changed later.
fBold = False
iBold = False
sBold = False
rBold = False
down = False
right = False
endFight = False
inven = False
fighty = False
spinner = False
runWords = ["gotta dash.","better skidaddle.","I left the tap running.","nice seein' you.","later."] #for when the player escapes battle
##class inventory(): #inventory items stored in class????
## def __init__(self):
## print("hi")
playerInv = ["1","2","3","4","5","6","7","8","9"]
fps = 60
class infoBox(object):
def __init__(self, surface):
self.box = pygame.image.load("bigbox.png") # loads text/info boxes
self.sbox = pygame.image.load("smallbox.png")
def draw(self, surface):
surface.blit(self.box, (0, 289)) #for blitting the big and small boxes to the screen
def drawsmall(self, surface):
surface.blit(self.sbox, (449, 289))
def textDisplay(text, x, y, bold): #function for displaying text
if bold == True:
font = "courbd.ttf" #code for bold text, red and bold font
colour = (225, 0, 0)
elif bold == False:
font = "cour.ttf" #otherwise the font is normal and white
colour = (255,255,255)
textSize = pygame.font.Font(font,14) #font size and font is specified
textSurf = textSize.render(text, True, colour) # code for rendering text
textRect = textSurf.get_rect()
textRect.center = (x, y) #coords of text
screen.blit(textSurf, textRect) #blits text for battle options to screen
def textDisplayMessage(msg, x, y): #function for displaying textbox message
font2 = "cour.ttf" # separate font variable for option descriptions
colour2 = (255,255,255)
textSize2 = pygame.font.Font(font2,14)
textSurf2 = textSize2.render(msg, True, colour2)
textRect2 = textSurf2.get_rect()
textRect2.topleft = (x, y) #specifies x and y from top left instead ofcenter
screen.blit(textSurf2, textRect2)
pygame.display.update() # updates display
def textChange(end, inv, spin, fight, run): #code for changing text
integ = random.randint(0,4) # for choosing a random escape quote from the runWords variable
if end == True:
display_box.draw(screen) # draws the main box to the screen.
textDisplay("", 487, 312, fBold)
textDisplayMessage(run[integ],12,297)
time.sleep(2)
pygame.quit() #quit game
sys.exit()
gameRun = False #break the loop.
elif inv == True: #if the inventory has been selected
while inv == True: #if user in the inventory menu:
key = pygame.key.get_pressed()
inv_window = pygame.transform.scale(pygame.image.load("bigbox.png"), (449, 289)) #loads display box
screen.fill(bg_colour) #fills screen
screen.blit(inv_window, (75,0)) #blits the display box
display_box.drawsmall(screen) #draws smaller box
pygame.display.update() #updates display
if key[pygame.K_ESCAPE]: #if down
inv = False
print("howdy")
else:
display_box.draw(screen) #display larger message box
display_box.drawsmall(screen) #display smaller selection box
textDisplay("fight", 487, 312, fBold) #display fight option
textDisplay("items", 561, 312, iBold) #item option
textDisplay("spin",487,376, sBold) #spin option
textDisplay("run",561,376, rBold) #run
textDisplayMessage(message, 12, 297)
screen = pygame.display.set_mode((width, height)) #specify screen size
pygame.display.set_caption("battle EduGame") #window name
clock = pygame.time.Clock()
pygame.display.flip()
gameRun = True
display_box = infoBox(screen)
while gameRun:
screen.fill(bg_colour) #fill screen
event = pygame.event.poll()
if event.type == pygame.QUIT: #if the "x" is pressed
pygame.quit() #quit game
sys.exit()
gameRun = False #break the loop.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT and right == False: #only allows right arrow to be pressed if the variable for the right arrow hasn't been triggered:
right = True
elif event.key == pygame.K_LEFT and right == True: #if the right arrow variable has been triggered, then the left button can be pressed to turn it off again.
right = False
elif event.key == pygame.K_UP and down == True: #same, but for down and up.
down = False
elif event.key == pygame.K_DOWN and down == False:
down = True
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: #if press return...
if fBold == True:
print("yo!")
fighty = True #trigger fight option
elif sBold == True:
print("howdy")
spinner = True #trigger spinner
elif iBold == True:
print("hello")
inven = True #trigger inventory
elif rBold == True:
print("good evening")
endFight = True #trigger run/end the fight
textChange(endFight, inven, spinner, fighty, runWords) #change the text.
if inven == True:
print("nice one") #placeholder
else:
if down == False and right == False: #if pointer in the top left:
fBold = True
sBold = False
iBold = False
rBold = False
message = "strike the opponent!" #change the message
if down == True and right == False: #if in bottom left
sBold = True
fBold = False
iBold = False
rBold = False
message = "spin the question wheel!" #change
if down == False and right == True: #if in top right
iBold = True
sBold = False
fBold = False
rBold = False
message = "access your inventory!"
if down == True and right == True: #if bottom right
rBold = True
sBold = False
iBold = False
fBold = False
message = "run like a coward!"
#print (fBold,sBold,iBold,rBold)
textChange(endFight, inven, spinner, fighty, runWords) #update text
pygame.display.update() #update screen
clock.tick(fps)
答案 0 :(得分:1)
我的猜测是,当您检查库存是否开放时,您会中断游戏循环。当程序处于“清单循环”中时,它无法再检索事件。
pygame.event.poll() #Is defined only at the beginning of the game loop
您应该在一个满足特定条件的函数中定义广告资源,而不会破坏初始游戏循环,如
if inv == True:
此外,我认为您应该使用事件机制来检查是否按下了按键
if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN:
代替
key = pygame.key.get_pressed()