Print a Number
Part of the Python in a Game Window section of Coddy's Pygame journey. Lesson 3 of 33.
Text in quotes is printed as it is written. Leave the quotes out and Python works the value out first, then prints the answer:
print(640 * 480)
print("640 * 480")The first line shows 307200. The second shows 640 * 480, because in quotes it is only text. In Python * means times, / means divided by, and + and - are plus and minus.
The window in these programs is 640 pixels wide and 480 tall: those are the two numbers in pygame.display.set_mode((640, 480)).
Challenge
EasyPrint how many pixels the window has.
- Under
print("The night begins"), addprint(640 * 480), without quotes, so Python works the number out. - Leave
set_mode((640, 480))as it is.
Press Play: the Console shows 307200 under your message. 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("The night begins")
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