0

I am new to python programming and am trying to create a game for class. Every time I run the program, before the user gets to put input in the surface screen goes non responsive. What am I doing wrong?

#import modules needed import pygame, sys, time, random from pygame.locals import *

def main():

#Assigns Colors
ORANGE = (255, 127, 80)         #Background
BLACK = (0, 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, 255, 204)         #Sky
BLUE = (67, 111, 181)               #Lake
LIME = (57, 255, 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, [[100+x_offset,120], [0+x_offset ,225], [200+x_offset, 230]], 0) 
            x_offset=x_offset+120#tells how many pixels to move over until you reached 1000
        
    def draw_trees(screen, x,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,WHITE,[x_offset,207],[x_offset +10,250],1) #draws snow starting at x0 to x 1000 every 50 pixels
            x_offset=x_offset +50

    def draw_lake(screen, x,y):
        pygame.draw.rect(screen,BLUE, [0,300,1000,200],0)# draws the lake
        pygame.draw.rect(screen,L_BROWN,[0,500,1000,100],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",True, 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,0)
    draw_trees(screen,0,0)
    draw_lake(screen,300,400)
    screen.blit(Moose_img, (600,400))      
    welcome = font.render("WELCOME TO MOOSEVILLE", True, 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), True, YELLOW)

    draw_mountains(screen, 0,0)
    draw_trees(screen,0,0)
    draw_lake(screen,300,400)
    screen.blit(Moose_img, (600,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? ", True, 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), True, LIME)
    screen.blit(player_num,(25,350))
    pygame.display.update()

    moose_num = random.randrange(1,11)
    moose_num=font.render('My number choice was : ' + str(moose_num), True, LIME)
    screen.blit(moose_num,(25,376))
    pygame.display.update()
       
    if player_num == moose_num:
        won=font.render('You Won!!!', True, YELLOW)
        screen.blit(won,(25,400))
        pygame.display.update()
    else:
        lose=font.render('You Lose!' , True, YELLOW)
        screen.blit(lose,(25,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()

  • Your screen will not update until it gets to the end of the loop, but `input` is preventing the code from proceeding. You will have to display everything and ask for the user input in two separate steps. It is also a little unconventional to define a function inside a while loop. You should move that outside the loop – Orbital May 16 '21 at 03:08

2 Answers2

0

Firstly, you don't need both event.pump() and event.get(), just use event.get in the loop as now. Also, you don't usually define functions inside a while loop. They would be outside the loop and called from inside it. You are also calling both event.pump and event.get and getting input via "input."

You also call pygame.quit() inside the while loop. Break this up into a bunch of functions.

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

Also the numbers game says:

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

and then asks for:

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

marienbad
  • 1,461
  • 1
  • 9
  • 19
0

You cannot use input in the application loop. input waits for an input. While the system is waiting for input, the application loop will halt and the game will not respond. Use the KEYDOWN event instead of input:

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

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

Either implement a textbox (see How to create a text input box with pygame?)
or get the input in a separate thread (see Why is my display not responding while waiting for input?).

Rabbid76
  • 202,892
  • 27
  • 131
  • 174