Game Dev⏱️ 6 min read• Published on 2026-09-20

How to Code a Jumping Character in Pygame (Gravity & Velocity Explained)

Nilesh Bind
Nilesh Bind (Founder & Principal Educator)
7+ Years Experience • 1,892+ Live Classes Delivered
View Bio →

One of the most thrilling milestones for my students in our Pygame track is making their game character jump. While it seems simple, jumping actually introduces real-world physics: velocity, gravity, and ground collision. Here is how to implement smooth, natural jumping in Python.

1. The Math Behind Jumping: Y-Axis in Computer Graphics

Remember: In 2D game programming (Pygame), the Y-axis is inverted! Y = 0 is at the very top of your window. Moving DOWN increases Y, while moving UP decreases Y. Therefore, when your player jumps, their vertical velocity must be negative (e.g. -15), and gravity adds positive velocity every frame to pull them back down.

2. The Core 3 Variables Every Platformer Needs

You only need three simple variables to handle realistic jumping physics: • is_jumping (boolean flag so the player cannot double-jump mid-air) • y_velocity (current speed moving up or down) • gravity (downward acceleration applied every single frame)
🐍 Python Example
# Pygame Jump Physics Logic
GRAVITY = 1.0
JUMP_FORCE = -16.0

# When Spacebar is pressed:
if keys[pygame.K_SPACE] and not is_jumping:
    y_velocity = JUMP_FORCE
    is_jumping = True

# Inside 60FPS Game Loop:
y_velocity += GRAVITY  # Gravity pulls player down
player_y += y_velocity

# Floor Collision Check:
if player_y >= FLOOR_Y:
    player_y = FLOOR_Y
    y_velocity = 0
    is_jumping = False

3. Why Fixed Jump vs Physics Jump Matters

Beginners often try to do `player_y -= 50` instantly. That creates an ugly teleport! By applying velocity that slowly diminishes to zero at the peak of the arc before gravity pulls downward, you get the satisfying parabolic arc seen in games like Mario and Flappy Bird.

📌 Key Takeaway

When students realize that video games are just visual physics and algebra brought to life, math stops being abstract homework and becomes a creative tool.

Tags:#Pygame#Python#Game Physics#2D Games
Nilesh Bind
About Nilesh Bind
Coding Educator & Founder @ CodeUrDream

Independent coding educator with 7+ years of experience delivering 1,892+ live 1-on-1 coding sessions. Passionate about turning passive screen time into creative game engineering.

🚀 Live 1-on-1 Mentorship

Learn This Topic 1-on-1 with Nilesh Bind

Want your child to build their own 2D arcade games from scratch? Join Nilesh Bind for live 1-on-1 Pygame classes!

More Guides from Nilesh Bind

Python Tips

How to Fix "IndentationError: unexpected indent" in Python (Beginner Guide)

⏱️ 4 min read
Parent Advice

Scratch vs Python for Kids: At What Age Should Your Child Switch?

⏱️ 5 min read