为什么我的 pygame 屏幕在等待用户输入时没有响应?

问题描述

我是 Python 编程的新手,正在尝试为课堂创建游戏。每次我运行程序时,在用户将输入放入表面屏幕之前,它都会无响应。我做错了什么?

#import 需要的模块 导入 pygame,系统,时间,随机 从 pygame.locals 导入 *

定义 main():

#Assigns Colors
ORANGE = (255,127,80)         #Background
BLACK = (0,0 )                      #Mountains
WHITE = (255,255,255)             #SNow  & Trees
broWN = (61,16,16)               #moose & Mountains
GREEN = (0,153,0)                  #Trees
L_broWN=(181,101,29)         #Tree Trunks
YELLOW =(255,204)         #Sky
BLUE = (67,111,181)               #Lake
LIME = (57,20)


#initiate modules
pygame.init()
done=False
#assign screen values and display captions
screen = pygame.display.set_mode((1000,600))
pygame.display.set_caption("Welcome To Michelle Era's Final Project - Moose Scene")

#start clock
clock=pygame.time.Clock()
fps = (60)

#Assign Variables
drink = " "
name = " "
welcome = " "
water= " "
quit = " "
i = 0
Moose_img = pygame.image.load('moose.png')
Beer_img = pygame.image.load('beer.png')
mikey_img=pygame.image.load('Mikey.jpg')
Water_img = pygame.image.load('water.png')
font = pygame.font.SysFont('Calibri',25,False,False)
text=font.render("My text",True,YELLOW)
player_num = 0
moose_num = random.randrange(1,26)

while not done:
    pygame.event.pump()
    for event in pygame.event.get():
            if event.type == pygame.QUIT:
                    done = True
               
    screen.fill(ORANGE) #fills background orange

     #Draw Scene       
    def draw_mountains(screen,x,y):
        pygame.draw.rect(screen,BLACK,(1,200,1000,100)) # Base of mountain
        x_offset=0
        while x_offset<1000:
            pygame.draw.polygon(screen,WHITE,[[100+x_offset,100],[0+x_offset,225],[200+x_offset,225]],0) # sNow caps on mountain
            pygame.draw.polygon(screen,broWN,120],230]],0) 
            x_offset=x_offset+120#tells how many pixels to move over until you reached 1000
        
    def draw_trees(screen,y):
        x_offset=0
        while x_offset<1000:
            pygame.draw.ellipse(screen,GREEN,[27+x_offset,158,40,50],0) #draws leaves starting at x27 to x 1000 every 50 pixels
            x_offset=x_offset +50 #tells how many pixels to move over until you reached 1000
            pygame.draw.line(screen,L_broWN,[x_offset,200],[x_offset +10,250],8) #draws trunk starting at x0 to x 1000 every 50 pixels
            pygame.draw.line(screen,207],1) #draws sNow starting at x0 to x 1000 every 50 pixels
            x_offset=x_offset +50

    def draw_lake(screen,BLUE,[0,300,0)# draws the lake
        pygame.draw.rect(screen,500,0) #draws grass

    def gameover():
        screen.fill(BLACK) #fills play surface
        pygame.display.flip() #updates the display
        gameOverFont=pygame.font.Font('freesansbold.ttf',17)
        gameOversurf = gameOverFont.render("A Big thank you to my brother Michael Era who originally painted the mural that was the  inspiration for this project",YELLOW)
        gameOverRect = gameOversurf.get_rect()
        gameOverRect.midtop = (500,40)
        screen.blit(gameOversurf,gameOverRect)
        screen.blit(mikey_img,(200,100))
        pygame.display.flip()
        time.sleep(30)


             
    draw_mountains(screen,0)
    draw_trees(screen,0)
    draw_lake(screen,400)
    screen.blit(Moose_img,(600,400))      
    welcome = font.render("WELCOME TO MOOSEVILLE",YELLOW)
    screen.blit(welcome,[300,50])
    pygame.display.update()

#
    
#GET user input        
        
    name=(input('What is your Moose named ? '))
    name=font.render("I like The Name " + str(name),YELLOW)

    draw_mountains(screen,400))
    screen.blit(name,(550,355))
    pygame.display.update()


    num_game=font.render("I am thinking of a number between 1 and 25,can you guess it? ",LIME)
    screen.blit(num_game,(25,325))


    player_num=(input(" What is your guess 1 to 10: "))
    player_num = font.render('You Choose The Number: ' +str(player_num),LIME)
    screen.blit(player_num,350))
    pygame.display.update()

    moose_num = random.randrange(1,11)
    moose_num=font.render('My number choice was : ' + str(moose_num),LIME)
    screen.blit(moose_num,376))
    pygame.display.update()
       
    if player_num == moose_num:
        won=font.render('You Won!!!',YELLOW)
        screen.blit(won,400))
        pygame.display.update()
    else:
        lose=font.render('You Lose!',YELLOW)
        screen.blit(lose,400))
        pygame.display.update()

    quit = input('Do you want to try again? y or n ')
    if quit == "n":
        gameover()
    else:
        done=False
                     
               

    pygame.quit()

main()

解决方法

首先,您不需要同时使用 event.pump()event.get(),只需像现在一样在循环中使用 event.get。此外,您通常不会在 while 循环内定义函数。他们将在循环之外并从循环内部调用。您还同时调用 event.pumpevent.get 并通过“input”获取输入。

您还可以在 while 循环内调用 pygame.quit()。将其分解为一系列函数。

main() - this calls all the others
get_input()
draw_screen()
etc

还有数字游戏说:

num_game=font.render("I am thinking of a number between 1 and 25,can you guess it? "

然后要求:

player_num=(input(" What is your guess 1 to 10: "))

,

您不能在应用程序循环中使用 inputinput 等待输入。当系统等待输入时,应用程序循环将停止并且游戏将不会响应。使用 KEYDOWN 事件代替 input

while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        if event.type == pygame.KEYDOWN:
            # [...]

要么实现一个文本框(见How to create a text input box with pygame?
或在单独的线程中获取输入(参见 Why is my display not responding while waiting for input?)。