Exploring the Basics of Game Development: Creating a Simple “Subway Surfers” Game in Python

In the realm of game development, creating a simple yet engaging game can be an excellent way to grasp fundamental programming concepts. Python, with its simplicity and versatility, offers an ideal platform for beginners to delve into game creation. This article aims to guide you through the process of developing a basic version of the popular mobile game “Subway Surfers” using Python.
Setting Up the Environment

To start, ensure you have Python installed on your computer. Additionally, installing Pygame, a cross-platform Python module designed for writing video games, is crucial. You can install Pygame by running pip install pygame in your command prompt or terminal.
Game Concept

“Subway Surfers” is an endless runner game where the player controls a character who runs along a subway track, avoiding obstacles and collecting coins. For our simplified version, we will focus on creating a basic environment, controlling the character’s movement, and implementing obstacle avoidance.
Initializing the Game

First, import the necessary Pygame modules and initialize the game window:

pythonCopy Code
import pygame import sys pygame.init() screen_width = 800 screen_height = 600 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption("Mini Subway Surfers") clock = pygame.time.Clock()

Character Movement

Next, let’s create a basic character that can move left and right. We’ll use the arrow keys for this purpose:

pythonCopy Code
class Player(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface((50, 50)) self.image.fill((0, 128, 0)) self.rect = self.image.get_rect(center=(screen_width // 2, screen_height - 50)) self.speed = 5 def update(self): keys = pygame.key.get_pressed() if keys[pygame.K_LEFT] and self.rect.left > 0: self.rect.x -= self.speed if keys[pygame.K_RIGHT] and self.rect.right < screen_width: self.rect.x += self.speed

Obstacles and Collision Detection

Implementing obstacles and collision detection is crucial for the game’s mechanics. Let’s add a simple obstacle that moves towards the player:

pythonCopy Code
class Obstacle(pygame.sprite.Sprite): def __init__(self): super().__init__() self.image = pygame.Surface((50, 50)) self.image.fill((128, 0, 0)) self.rect = self.image.get_rect(center=(screen_width, screen_height // 2)) self.speed = 7 def update(self): self.rect.x -= self.speed if self.rect.right < 0: self.rect.center = (screen_width, screen_height // 2) # Collision detection def check_collision(player, obstacles): for obstacle in obstacles: if player.rect.colliderect(obstacle.rect): return True return False

Game Loop

Finally, integrate everything into the game loop, handling events, updating the game state, and rendering graphics:

pythonCopy Code
all_sprites = pygame.sprite.Group() player = Player() all_sprites.add(player) obstacles = pygame.sprite.Group() for _ in range(3): obstacle = Obstacle() obstacles.add(obstacle) all_sprites.add(obstacle) running = True while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False all_sprites.update() if check_collision(player, obstacles): print("Game Over!") running = False screen.fill((30, 30, 30)) all_sprites.draw(screen) pygame.display.flip() clock.tick(60) pygame.quit

As I write this, the latest version of Python is 3.12.4