text displays on screen only when I click to exit the program

Mirko mirkok.lists at googlemail.com
Fri Apr 30 15:11:42 EDT 2021


Am 30.04.2021 um 20:55 schrieb Quentin Bock:
> code with comments for context:
> 
> #Create a text based game where the user must find 3 items before
> completing a level
> #imports and variables for game
> import pygame
> from pygame import mixer
> running = True
> #initializes pygame
> pygame.init()
> 
> #creates the pygame window
> screen = pygame.display.set_mode((1200, 800))
> 
> #Title and Icon of window
> pygame.display.set_caption("3.02 Project")
> icon = pygame.image.load('3.02 icon.png')
> pygame.display.set_icon(icon)
> 
> #setting up font
> pygame.font.init()
> font = pygame.font.Font('C:\Windows\Fonts\OCRAEXT.ttf', 16)
> font_x = 10
> font_y = 40
> items_picked_up = 0
> items_left = 3
> 
> #functions to be called later in program
> def display_instruction(x, y):
>     instructions = font.render("Each level contains 3 items you must pick
> up in each room."
>                                "When you have picked up 3 items, you will
> advance to the next room, there are 3 rooms.", True, (255, 255, 255))
>     screen.blit(instructions, (10, 40))
> 
> def main():
>     global running
> 
>     #Game Loop
>     while running:
>         #sets screen color to black
>         screen.fill((0, 0, 0))
> 
>         #checks if the user quits or exits the window
>         for event in pygame.event.get():
>             if event.type == pygame.QUIT:
>                 running = False
> 
>     display_instruction(font_x, font_y)
>     pygame.display.update()
> 
> 
> main()
> 
> 
> 
> please excuse the def main() thing, this doesn't follow a tutorial this is
> all on my own, and I don't know why the text displays when I close the
> python window.
> I appreciate all help :)


Because you are calling display_instruction() and
pygame.display.update() outside of the game loop. Try to indent
those two lines one level more.


More information about the Python-list mailing list