Python Basics
Before we build drifting stars and bouncing colors, we need a tiny bit of Python vocabulary. Not theory — just the pieces you’ll actually touch when you tweak your projects.
A variable is just a name that remembers a value. In Python, you don’t declare types — you just assign.
color = (255, 200, 50)
message = “Hello, sandbox!”
You’ll tweak variables constantly: star speed, ball size, color values, gravity strength, chaos slider defaults — everything fun lives here.
A list is a container that holds multiple values. You’ll use them for stars, balls, particles, and anything that moves in groups.
colors = [(255,0,0), (0,255,0), (0,0,255)]
In Drifting Stars, every star is stored in a list. The game loop updates each one every frame.
Every Pygame project runs inside a loop. This loop updates positions, checks events, and redraws the screen.
star.x += star.speed
star.y += star.drift
If HTML was “refresh the page,” Python is “refresh the window 60 times per second.”
Functions let you group steps together and reuse them. You’ll use them for drawing stars, updating motion, and handling collisions.
star.x += star.speed
star.y += star.drift
You don’t need to write many functions yourself — AI will generate most of them. You’ll just tweak numbers inside.
If statements let your project respond to conditions — bouncing off walls, changing colors, triggering chaos.
star.x = 0
You’ll use these constantly in Bouncing Colors and the Chaos Equalizer.
This tiny script uses everything above: variables, loops, and drawing. It’s your warm‑up before Drifting Stars.
pygame.init()
screen = pygame.display.set_mode((800, 600))
x = 100
speed = 3
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
x += speed
if x > 800:
x = 0
screen.fill((10,10,30))
pygame.draw.circle(screen, (255,200,50), (x,300), 20)
pygame.display.flip()
pygame.quit()
If you see a glowing dot sliding across the screen — congratulations. You now know enough Python to build every project in this book.