Menu
CoddyTech

Your First Program

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

Press Run. A window opens beside the editor: a night sky with a moon, and a comet flying past. This is a game made with pygame, a library for writing games in Python. In this section you build one like it, a piece at a time, and learn Python as you go.

Python reads a program from the top line down. You do not need to understand every line yet. Find the line that draws the moon:

pygame.draw.circle(screen, (240, 230, 160), (500, 110), 40)

The last number, 40, is how big the moon is. Change it, press Run again, and the moon changes. Every game is built from lines like this one.

Lines that start with # are comments: notes for people, which Python skips. The ones in this program mark where you will add your own code in the lessons ahead.

challenge icon

Challenge

Easy

Make the moon twice as big: change its size from 40 to 80, then press Run.

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

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), 40)
    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