pygame display not changing

Viewed 77

I'm trying to write my first game in pygame and successfully made a title screen, but can't find a way to make the 'play' button take the user to the actual gameplay. I have a function dedicated to the title screen, and when the user clicks the play button it stops the title screen loop and starts the gameplay loop, although the gameplay loop code doesn't work. The title screen just freezes and the game doesn't start. I also have never used Stack overflow so I'll just paste my code here I guess:

import sys
import random
pygame.init()

# title
game_title = 'GAME-TITLE'

# set display
win = pygame.display.set_mode((750, 500))
pygame.display.set_caption(game_title)

# load images
cloud = pygame.image.load('999-cloud-clipart-free-download-transparent-png-cloud-clipart-cloud-clipart-transparent-1044_592.png')
cloud = pygame.transform.scale(cloud, (128, 72))

# clock
clock = pygame.time.Clock()

# font
pygame.font.init() 
font = pygame.font.SysFont('verdanaboldttf', 60)
font_2 = pygame.font.SysFont('timesnewromanttf', 30)

# colors
red = (255, 0, 0)
blue = (0, 0, 255)
green = (0, 255, 0)
white = (255, 255, 255)
light_blue = (173, 216, 230)
blue = (48, 131, 159)
navy = (0, 0, 200)
black = (0, 0, 0)

# clouds
cloud_values = []
i = 0
while i < 10:
    cloud_values.append([random.randint(-750, -80), random.randint(-50, 550)])
    i += 1

def title_screen():
    run_title = True
    run = True
    show_help = False
    play_game = False
    
    while run_title:
        
        clock.tick(10)
        
        pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))
        play_button = pygame.draw.rect(win, blue, pygame.Rect(150, 175, 450, 75))
        help_button = pygame.draw.rect(win, blue, pygame.Rect(150, 275, 450, 75))
        quit_button = pygame.draw.rect(win, blue, pygame.Rect(150, 375, 450, 75))
        text = font_2.render('PLAY', True, white)
        text_2 = font_2.render('HELP', True, white)
        text_3 = font_2.render('QUIT', True, white)
        title = font.render(game_title, True, navy)
        win.blit(text, (340, 197))
        win.blit(text_2, (340, 297))
        win.blit(text_3, (340, 397))
        win.blit(title, (165, 60))
        
        for i in range(len(cloud_values)):
            win.blit(cloud, (cloud_values[i][0], cloud_values[i][1]))
            cloud_values[i][0] += 10
            if cloud_values[i][0] > 760:
                cloud_values[i][0] = random.randint(-750, -80)
        
        keys = pygame.key.get_pressed()
        
        if keys[pygame.K_ESCAPE]:
            run = False
        
        pos = pygame.mouse.get_pos()
        
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
            if event.type == pygame.MOUSEBUTTONUP:
                pos = pygame.mouse.get_pos()
                if pos[0] > 150 and pos[0] < 600 and pos[1] > 175 and pos[1] < 250:
                    play_game = True
                elif pos[0] > 150 and pos[0] < 600 and pos[1] > 275 and pos[1] < 375:
                    show_help = True
                elif pos[0] > 150 and pos[0] < 600 and pos[1] > 375 and pos[1] < 450:
                    run = False

        if pos[0] > 150 and pos[0] < 600 and pos[1] > 175 and pos[1] < 250:
            pygame.draw.rect(win, blue, pygame.Rect(145, 170, 460, 85))
            win.blit(text, (340, 197))
        elif pos[0] > 150 and pos[0] < 600 and pos[1] > 275 and pos[1] < 375:
            pygame.draw.rect(win, blue, pygame.Rect(145, 270, 460, 85))
            win.blit(text_2, (340, 297))
        elif pos[0] > 150 and pos[0] < 600 and pos[1] > 375 and pos[1] < 450:
            pygame.draw.rect(win, blue, pygame.Rect(145, 370, 460, 85))
            win.blit(text_3, (340, 397))
        
        if play_game or show_help or not run:
            run_title = False
        
        pygame.display.flip()
    
    return run_title, play_game, run, show_help

def game_play():
    run_game = True
    run = True
    x = 10
    while run_game:
        
        # set new background
        pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))
        
        # run gameplay here
        
    return run

def show_help_screen():
    show_help = True
    while show_help:
        pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))
        
        # show help_screen

def show_results_screen():
    run = False
    show_results = True
    while show_results:
        pygame.draw.rect(win, light_blue, pygame.Rect(-100, -100, 1000, 1000))
        
        # show results
        
    return run

def run_game(run_title, play_game, run, show_help):
    
    run = True
    while run:
        
        if play_game:
            game_play()
            show_results = True
        
        elif show_help:
            show_help_screen()
            run_title = True
        
        elif show_results:
            run = show_results_screen()

    pygame.quit()
    sys.exit()

run_title, play_game, run, show_help = title_screen()
run_game(run_title, play_game, run, show_help)```
1 Answers

I went through your code, and I haven't seen an update once.

So since it's your first here's a quick lesson: when there is any movement, any changes, anything that is changing on your window it won't show up unless you update it, using:

pygame.display.update()

or

pygame.display.flip()

When outputting things, think of them as layers, if you output one image, and then a second image. The first image won't show. (That's if they are the same size but you know what I mean.)

Related