Menu
CoddyTech

Print a Message

Part of the Python in a Game Window section of Coddy's Pygame journey. Lesson 2 of 33.

print() writes a line of text in the Console, the panel beside the game. The text does not appear in the window: print is for you, the programmer, to see what your program is doing.

print("Hello")

The text goes in quotes, and it is printed exactly as it is written.

Where a line goes matters. Every program in this section has the same three marked parts:

# Set things up here, before the game loop.
    # Change things here, once a frame.
    # Draw here, after the fill.

The setup part runs once, at the start. The other two are inside the game loop, the lines under while running:, which run 60 times a second. So a print in the setup part shows once, and a print inside the loop floods the Console. Section 2 explains the loop line by line.

challenge icon

Challenge

Easy

Make the game say something of your own when it starts.

  1. In the setup part, find print("").
  2. Type your own words between the quotes: print("The night begins").

Press Play: your message shows once in the Console. Then Check.

Try it yourself

import pygame

pygame.init()
screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Night Sky")
clock = pygame.time.Clock()

# Set things up here, before the game loop.
comet_x = 0
print("")

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

    # Change things here, once a frame.
    comet_x = comet_x + 3
    if comet_x > 700:
        comet_x = -60

    screen.fill((20, 24, 48))
    # Draw here, after the fill.
    pygame.draw.circle(screen, (240, 230, 160), (500, 110), 80)
    pygame.draw.circle(screen, (255, 255, 255), (comet_x, 300), 6)

    pygame.display.flip()
    clock.tick(60)

pygame.quit()

All lessons in Python in a Game Window

Practice on your own: Pygame playground