Game Development 7 min read

Python Pygame Skiing Game Tutorial with Full Source Code

This tutorial walks through building a simple skiing game in Python using Pygame, explaining the game mechanics, required image assets, and providing the complete source code that handles skier movement, obstacle generation, collision detection, scoring, and rendering in a 640×640 window.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Pygame Skiing Game Tutorial with Full Source Code

This article presents a simple Python pygame skiing game where the player moves the skier left or right with the arrow keys, gains points by passing flags and loses points when colliding with trees.

The game uses several image assets such as skier_down.png, skier_left1.png, skier_left2.png, skier_right1.png, skier_right2.png, skier_tree.png, and skier_flag.png.

Below is the complete source code for the game, including the SkierClass, ObstacleClass, map generation, rendering loop, and collision handling.

import pygame,sys,random
skier_images= ["skier_down.png","skier_right1.png","skier_right2.png","skier_left1.png","skier_left2.png"]
class SkierClass(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("skier_down.png")
        self.rect = self.image.get_rect()
        self.rect.center = [320,100]
        self.angle = 0
        # 创建滑雪者
    def turn(self,direction):
        self.angle = self.angle + direction
        if self.angle < -2 :
            self.angle = -2
        if self.angle > 2 :
            self.angle = 2
        center = self.rect.center
        self.image = pygame.image.load(skier_images[self.angle])
        self.rect = self.image.get_rect()
        self.rect.center = center
        speed = [self.angle , 6-abs(self.angle) * 2]
        return speed
    # 滑雪者转向方法
    def move(self,speed):
        self.rect.centerx = self.rect.centerx + speed[0]
        if self.rect.centerx <20 :
            self.rect.centerx = 20
        if self.rect.centerx >620:
            self.rect.centerx = 620
    # 滑雪者移动方法
class ObstacleClass(pygame.sprite.Sprite):
    def __init__(self,image_file,location,type):
        pygame.sprite.Sprite.__init__(self)
        self.image_file = image_file
        self.image = pygame.image.load(image_file)
        self.rect = self.image.get_rect()
        self.rect.center = location
        self.type = type
        self.passed = False
    # 创建树和小旗子
    def update(self):
        global speed
        self.rect.centery -= speed[1] # 屏幕向上滚动
        if self.rect.centery <-32:
            self.kill() # 删除从屏幕上方滚下的障碍物
def create_map():
    global obstacles
    locations = []
    for i in range(10):
        row = random.randint(0,9)
        col = random.randint(0,9)
        location = [col *64 +20,row*64+20+640]
        if not (location in locations):
            locations.append(location)
        type = random.choice(["tree","flag"])
        if type == "tree":
            img = "skier_tree.png"
        elif type == "flag":
            img = "skier_flag.png"
        obstacle = ObstacleClass(img,location,type)
        obstacles.add(obstacle)
    # 创建窗口,并包含树与小旗子
def animate():
    screen.fill([255,255,255])
    obstacles.draw(screen)
    screen.blit(skier.image,skier.rect)
    screen.blit(score_text,[10,10])
    pygame.display.flip()
    # 重新绘制画面
pygame.init()
screen = pygame.display.set_mode([640,640])
clock = pygame.time.Clock()
skier = SkierClass()
speed = [0,6]
obstacles = pygame.sprite.Group()
map_position = 0
points = 0
create_map()
font = pygame.font.Font(None,50)
# 准备画面
running = True
while running: # 主循环
    clock.tick(30) # 每秒更新30次图形
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                speed = skier.turn(-1)
            elif event.key == pygame.K_RIGHT:
                speed = skier.turn(1)
            # 检索用户输入
    skier.move(speed) # 移动滑雪者
    map_position += speed[1] # 滚动场景
    if map_position >= 640 :
        create_map()
        map_position = 0
        # 创建新场景
    hit = pygame.sprite.spritecollide(skier,obstacles,False)
    if hit:
        if hit[0].type == "tree" and not hit[0].passed:
            points -=100
            skier.image = pygame.image.load("skier_crash.png")
            score_text = font.render("Score:" +str(points),1,(0,0,0))
            pygame.time.delay(100)
            skier.iamge = pygame.image.load("skier_down.png")
            skier.angle = 0
            speed = [0,6]
            hit[0].passed = True
        elif hit[0].type == "flag" and not hit[0].passed:
            points +=10
            hit[0].kill()
            # 检测是否碰到树或者小旗子
    obstacles.update()
    score_text = font.render("Score:" +str(points),1,(0,0,0)) # 显示得分
    animate()
pygame.quit()

The main loop runs at 30 fps, handling quit events, keyboard input for left/right movement, updating the skier’s position, scrolling the map, generating new obstacles, detecting collisions, updating the score, and redrawing the screen until the user closes the window.

*Disclaimer: This article is compiled from online sources; copyright belongs to the original author.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

PythonGame DevelopmentTutorialSkiing 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

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.