Coordinates and Moving Objects in Pygame – Lesson 3

pygame tutorial

In the previous lesson, you learned how to draw:

✅ Rectangles

✅ Circles

✅ Lines

But games become interesting only when objects start moving.

Today, you will learn:

  • What coordinates are
  • What X and Y positions mean
  • How to move objects
  • How simple game animations work

By the end of this lesson, you will make a rectangle move across the screen.


What Are Coordinates?

Every object in a game has a position. Pygame uses a coordinate system to decide where to draw objects.

Think of the game screen like graph paper.

(0,0)
  +---------------------> X
  |
  |
  |
  |
  V
  Y

The top-left corner is always:

(0,0)

Understanding X Position

The X value controls:

➡ Left and Right movement

Example:

x = 100

The object appears near the left side.

x = 400

The object moves toward the right.


Understanding Y Position

The Y value controls:

⬆ Up and Down movement

Example:

y = 50

Object appears near the top.

y = 400

Object moves downward.


Example: Draw Rectangle at Different Positions

pygame.draw.rect(screen, (255,0,0), (100,100,100,100))

Here:

100 = X Position
100 = Y Position
100 = Width
100 = Height

Try:

pygame.draw.rect(screen, (255,0,0), (300,200,100,100))

Notice how the rectangle changes position.


Program 1: Move a Rectangle

Create a new file:

lesson3_movement.py

Type the following code:

import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))

pygame.display.set_caption("Moving Rectangle")

x = 50

running = True

while running:

    screen.fill((255,255,255))

    pygame.draw.rect(screen, (255,0,0), (x,250,100,100))

    x = x + 1

    pygame.display.update()

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

pygame.quit()

Run the program.

Amazing!

The rectangle starts moving automatically.


How Does Movement Work?

Look at this line:

x = x + 1

Every time the game loop runs:

50
51
52
53
54
55

The X position keeps increasing.

As a result, the rectangle moves right.


Faster Movement

Try:

x = x + 5

Now run the program again.

The rectangle moves much faster.


Slower Movement

Try:

x = x + 0.5

Movement becomes slower and smoother.


Program 2: Move a Circle

import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))

x = 50

running = True

while running:

    screen.fill((255,255,255))

    pygame.draw.circle(screen, (0,0,255), (x,300), 50)

    x += 2

    pygame.display.update()

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

pygame.quit()

This time a blue circle moves across the screen.


Understanding Animation

Animation is simply:

Draw
Move
Draw Again
Move Again
Repeat

This happens many times every second.

Because it happens so quickly, our eyes see smooth movement.

This is the secret behind game animation.


Program 3: Move Object Downward

import pygame

pygame.init()

screen = pygame.display.set_mode((800,600))

y = 50

running = True

while running:

    screen.fill((255,255,255))

    pygame.draw.circle(screen, (0,255,0), (400,y), 50)

    y += 2

    pygame.display.update()

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

pygame.quit()

Now the circle moves downward because we are changing:

y += 2

instead of X.


Real Game Example

Think about a racing game.

Car Position = X, Y
Move Right  → X Increases
Move Left   → X Decreases
Move Down   → Y Increases
Move Up     → Y Decreases

Almost every game uses this concept.


Mini Project: Flying Rocket

Try creating a blue rectangle.

Move it from:

Left → Right

using:

x += 3

Give the game title:

pygame.display.set_caption("Flying Rocket")

Challenge Time

Challenge 1

Create a moving red circle.


Challenge 2

Create a moving green square.


Challenge 3

Create two moving objects.

Example:

  • Circle
  • Rectangle

Move both together.


Common Errors

Object Not Moving

Check:

x += 1

Make sure this line is inside the game loop.


Object Disappears

The object may have moved outside the screen.

Try reducing speed:

x += 1

instead of:

x += 20

What You Learned Today

✅ What coordinates are

✅ Understanding X position

✅ Understanding Y position

✅ How movement works

✅ How animation works

✅ How games create motion


Quick Revision

Answer these questions:

1. Which position controls left and right movement?

2. Which position controls up and down movement?

3. What does this line do?

x += 1

4. What is animation?

If you can answer these questions, you are ready for the next lesson.


What’s Next?

In Lesson 4: Keyboard Controls in Pygame, you will learn:

✅ How to detect key presses

✅ Move objects using arrow keys

✅ Create your first controllable game character

✅ Make your game interactive

This is where your game starts responding to the player’s actions. 🎮


FAQ

Why is the top-left corner (0,0)?

Pygame starts counting positions from the top-left corner of the screen.

Can I use negative coordinates?

Yes, but the object may move outside the visible screen.

What does x += 1 mean?

It increases the X position by 1 pixel every loop.

What is the first step to making a game?

Understanding coordinates and movement is one of the most important first steps.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.