Complete Python Pygame Code for a Simple Endless‑Runner Game
This article provides a step‑by‑step tutorial and full Python Pygame source code for creating a simple endless‑runner game, covering imports, configuration, character, obstacle and background classes, game initialization, object spawning, logic updates, collision detection, and scoring.
This article invites readers to follow the public account for daily Python learning and presents the complete source code for a simple endless‑runner game built with Pygame.
import pygame, sys<br/>import random
It defines the game configuration variables such as window width, height, score, fonts, and placeholders for objects, background, player, and game state.
class Role:<br/> def __init__(self, surface=None, y=None):<br/> self.surface = surface<br/> self.y = y<br/> self.w = (surface.get_width()) / 12<br/> self.h = surface.get_height() / 2<br/> self.currentFrame = -1<br/> self.state = 0 # 0: running, 1: jumping, 2: double jump<br/> self.g = 1 # gravity<br/> self.vy = 0<br/> self.vy_start = -20 # initial jump speed<br/> def getRect(self):<br/> return (0, self.y + 12, self.w, self.h)
class Object:<br/> def __init__(self, surface, x=0, y=0):<br/> self.surface = surface<br/> self.x = x<br/> self.y = y<br/> self.w = surface.get_width()<br/> self.h = surface.get_height()<br/> self.currentFrame = random.randint(0, 6)<br/> self.w = 100<br/> self.h = 100<br/> def getRect(self):<br/> return (self.x, self.y, self.w, self.h)<br/> def collision(self, rect1, rect2):<br/> if (rect2[0] >= rect1[2] - 20) or (rect1[0] + 40 >= rect2[2]) or (rect1[1] + rect1[3] < rect2[1] + 20) or (rect2[1] + rect2[3] < rect1[1] + 20):<br/> return False<br/> return True
class Bg:<br/> def __init__(self, surface):<br/> self.surface = surface<br/> self.dx = -10<br/> self.w = surface.get_width()<br/> self.rect = surface.get_rect()
The initGame() function initializes the score, object list, fonts, clock, game state, loads images for background, game‑over screen, player, and obstacles, and creates the initial Role and Bg instances.
The addObject() function randomly spawns obstacle objects at varying heights and adds them to the objectList when a random condition is met.
The updateLogic() function processes keyboard events (quit, space for jump or restart), moves the background, updates the player’s animation or jump physics, moves obstacles, removes off‑screen obstacles while increasing the score, and performs collision detection that ends the game on a hit or awards extra points for special objects.
Finally, the author notes that this is the complete code for the “天天酷跑” game and invites readers to leave comments for further assistance, followed by promotional material for a free Python course and related resources.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.