Game Development 8 min read

Creating a Simple “Jump” Game with Python and Pygame

This guide demonstrates how to build a basic “jump” game in Python using the Pygame library, covering installation, game logic, platform generation, jump control, collision detection, game loop, and suggestions for extending the project with scoring, difficulty, sound, and graphics.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Creating a Simple “Jump” Game with Python and Pygame

This tutorial explains how to create a simple “jump” game in Python using the pygame library, providing a complete example from installation to running the game.

Installation : Install pygame via pip install pygame .

Game Logic Overview :

The player jumps from one platform to another.

Jumping is triggered by the space bar or mouse click.

If the player misses a platform, they fall and the game ends.

Code Implementation – the following Python script implements the game:

<code>import pygame
import random
import time

# 初始化 pygame
pygame.init()

# 设置游戏窗口
screen_width = 600
screen_height = 400
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("跳一跳游戏")

# 定义颜色
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# 定义时钟
clock = pygame.time.Clock()

# 定义角色参数
player_width = 50
player_height = 50
player_x = screen_width // 4
player_y = screen_height - player_height - 10
player_velocity = 0  # 垂直速度
gravity = 0.5  # 重力加速度

# 定义平台参数
platform_width = 100
platform_height = 10
platforms = []

# 跳跃控制
jumping = False
jump_force = 10  # 初始跳跃力度

# 游戏循环标志
running = True

def create_platforms():
    """生成随机平台"""
    global platforms
    platforms = []
    for i in range(5):
        platform_x = random.randint(50, screen_width - platform_width - 50)
        platform_y = random.randint(100, screen_height - 50)
        platforms.append(pygame.Rect(platform_x, platform_y, platform_width, platform_height))

def draw_player():
    """绘制玩家"""
    pygame.draw.rect(screen, RED, pygame.Rect(player_x, player_y, player_width, player_height))

def draw_platforms():
    """绘制平台"""
    for platform in platforms:
        pygame.draw.rect(screen, GREEN, platform)

def check_collisions():
    """检查是否与平台碰撞"""
    global player_y, player_velocity
    player_rect = pygame.Rect(player_x, player_y, player_width, player_height)
    on_platform = False
    for platform in platforms:
        if player_rect.colliderect(platform):
            if player_velocity > 0 and player_y + player_height <= platform.top:
                player_y = platform.top - player_height
                player_velocity = 0
                on_platform = True
                break
    if not on_platform:
        player_velocity += gravity

def jump():
    """跳跃控制"""
    global player_velocity, jumping
    if jumping:
        player_velocity = -jump_force

def game_over():
    """游戏结束"""
    font = pygame.font.SysFont("Arial", 40)
    game_over_text = font.render("游戏结束", True, BLACK)
    screen.blit(game_over_text, (screen_width // 3, screen_height // 3))
    pygame.display.flip()
    time.sleep(2)

create_platforms()

while running:
    screen.fill(WHITE)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE and not jumping:
                jumping = True
                jump()
    if player_y + player_height < screen_height:
        player_y += player_velocity
    check_collisions()
    draw_player()
    draw_platforms()
    if player_y > screen_height:
        game_over()
        running = False
    pygame.display.flip()
    clock.tick(60)

pygame.quit()
</code>

Code Explanation :

Initialization and Setup : Calls pygame.init() , sets screen size, defines colors and object dimensions.

Platform Generation : create_platforms() creates random green platforms for the player to jump onto.

Jump Control : jump() sets a negative vertical velocity to make the player rise when the space bar is pressed.

Collision Detection : check_collisions() stops the player on a platform or applies gravity if no collision occurs.

Game Over : Displays a “游戏结束” message when the player falls off the screen.

Game Loop : The while running: loop processes events, updates positions, draws objects, and limits the frame rate to 60 FPS.

Running the Game : Execute the script; a window appears where the player can press the space bar to jump between platforms. Missing a platform ends the game.

Improvements and Extensions :

Add a scoring system to count jumps or platforms passed.

Increase difficulty by adding more or moving platforms.

Include sound effects and animations for jumps and landings.

Replace simple rectangles with images for richer graphics.

This example serves as an introductory tutorial for Python game development and can be expanded with additional features.

PythonGame developmentTutorialPygameJump Game
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.