---
title: pygame Basic Setup
lang: en-GB
author: Maggie Harley
date: 22/06/2025
---

```python
#!/usr/bin/env python

import pygame, sys
from pygame import Rect

pygame.init()

screen = pygame.display.set_mode((640, 480))
pygame.display.set_caption("Hello, world!")

clock = pygame.time.Clock()

x = 100

while True:
    delta = clock.tick(120)
    x += delta * 0.3

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            sys.exit()

    screen.fill("black")
    pygame.draw.rect(screen, "white", Rect(x % 600, 100, 100, 100))
    pygame.display.update()
```

I'm struggling to find a simple, "hello world" getting-started template for
[pygame][pygame], so I figured I should make one.

Running the above code while you have pygame installed will hopefully give you
a black-filled window with a white square moving across it:

![A window containing a white square on a black background.](./assets/pygame-square.png)

All of this is quite similar to what I'm used to with SDL, it was mainly the
names of the functions that I couldn't find. The biggest difference is the
[`Clock` class and its `tick` method][tick], which can be used to manage the
framerate of the application (in the above example, the `120` being passed to
`clock.tick` is limiting the framerate to 120 frames per second).

# For nix Users

I've created a flake template for pygame projects in [my templates repo][git].

[pygame]: https://pygame.org
[tick]: https://www.pygame.org/docs/ref/time.html#pygame.time.Clock.tick
[git]: https://git.edina.st/mag/flake-templates/src/commit/1af36e0eb1ae979edb86c2e863a83f71de602a64
