SoFunction
Updated on 2024-11-21

Python implementation of the skiing game

In this article, we share the example of Python to achieve the specific code of the skiing game for your reference, the details are as follows

Source code sharing:

import sys
import cfg
import pygame
import random
 
 
'''Skier class'''
class SkierClass():
    def __init__(self):
        .__init__(self)
        # Skier orientation (-2 to 2)
         = 0
         = cfg.SKIER_IMAGE_PATHS[:-1]
         = ([])
         = .get_rect()
         = [320, 100]
         = [, 6-abs()*2]
    '''Change the skier's facing direction . Negative numbers are to the left, positive numbers are to the right, and 0 is forward'''
    def turn(self, num):
         += num
         = max(-2, )
         = min(2, )
        center = 
         = ([])
         = .get_rect()
         = center
         = [, 6-abs()*2]
        return 
    '''Mobile Skier'''
    def move(self):
         += [0]
         = max(20, )
         = min(620, )
    '''Set to fall status'''
    def setFall(self):
         = (cfg.SKIER_IMAGE_PATHS[-1])
    '''Set to standing'''
    def setForward(self):
         = 0
         = ([])
 
 
'''
Function.
    Obstacle Class
Input.
    img_path: Obstacle image path
    location: Obstacle location
    attribute: Obstacle class attribute
'''
class ObstacleClass():
    def __init__(self, img_path, location, attribute):
        .__init__(self)
        self.img_path = img_path
         = (self.img_path)
         = location
         = .get_rect()
         = 
         = attribute
         = False
    '''Move'''
    def move(self, num):
         = [1] - num
 
 
'''Creating obstacles'''
def createObstacles(s, e, num=10):
    obstacles = ()
    locations = []
    for i in range(num):
        row = (s, e)
        col = (0, 9)
        location  = [col*64+20, row*64+20]
        if location not in locations:
            (location)
            attribute = (list(cfg.OBSTACLE_PATHS.keys()))
            img_path = cfg.OBSTACLE_PATHS[attribute]
            obstacle = ObstacleClass(img_path, location, attribute)
            (obstacle)
    return obstacles
 
 
'''Consolidation of obstacles'''
def AddObstacles(obstacles0, obstacles1):
    obstacles = ()
    for obstacle in obstacles0:
        (obstacle)
    for obstacle in obstacles1:
        (obstacle)
    return obstacles
 
 
'''Show game start screen'''
def ShowStartInterface(screen, screensize):
    ((255, 255, 255))
    tfont = (, screensize[0]//5)
    cfont = (, screensize[0]//20)
    title = (u'Ski game', True, (255, 0, 0))
    content = (u'Press any key to start the game', True, (0, 0, 255))
    trect = title.get_rect()
     = (screensize[0]/2, screensize[1]/5)
    crect = content.get_rect()
     = (screensize[0]/2, screensize[1]/2)
    (title, trect)
    (content, crect)
    while True:
        for event in ():
            if  == :
                ()
                ()
            elif  == :
                return
        ()
 
 
'''Show scores'''
def showScore(screen, score, pos=(10, 10)):
    font = (, 30)
    score_text = ("Score: %s" % score, True, (0, 0, 0))
    (score_text, pos)
 
 
'''Update the current frame of the game'''
def updateFrame(screen, obstacles, skier, score):
    ((255, 255, 255))
    (screen)
    (, )
    showScore(screen, score)
    ()
 
 
'''Main program'''
def main():
    # Game initialization
    ()
    ()
    ()
    .set_volume(0.4)
    (-1)
    # Setting up the screen
    screen = .set_mode()
    .set_caption('Ski game -- Nine Songs')
    # Game start screen
    ShowStartInterface(screen, )
    # Instantiate game sprites
    # -- skier
    skier = SkierClass()
    # --Creating obstacles
    obstacles0 = createObstacles(20, 29)
    obstacles1 = createObstacles(10, 19)
    obstaclesflag = 0
    obstacles = AddObstacles(obstacles0, obstacles1)
    # Gameclock
    clock = ()
    # Record the distance skied
    distance = 0
    # Record the current score
    score = 0
    # Record the current speed
    speed = [0, 6]
    # The main game loop
    while True:
        # --Event Capture
        for event in ():
            if  == :
                ()
                ()
            if  == :
                if  == pygame.K_LEFT or  == pygame.K_a:
                    speed = (-1)
                elif  == pygame.K_RIGHT or  == pygame.K_d:
                    speed = (1)
        # --Update the data of the current game frame
        ()
        distance += speed[1]
        if distance >= 640 and obstaclesflag == 0:
            obstaclesflag = 1
            obstacles0 = createObstacles(20, 29)
            obstacles = AddObstacles(obstacles0, obstacles1)
        if distance >= 1280 and obstaclesflag == 1:
            obstaclesflag = 0
            distance -= 1280
            for obstacle in obstacles0:
                [1] = [1] - 1280
            obstacles1 = createObstacles(10, 19)
            obstacles = AddObstacles(obstacles0, obstacles1)
        for obstacle in obstacles:
            (distance)
        # -- collision detection
        hitted_obstacles = (skier, obstacles, False)
        if hitted_obstacles:
            if hitted_obstacles[0].attribute == "tree" and not hitted_obstacles[0].passed:
                score -= 50
                ()
                updateFrame(screen, obstacles, skier, score)
                (1000)
                ()
                speed = [0, 6]
                hitted_obstacles[0].passed = True
            elif hitted_obstacles[0].attribute == "flag" and not hitted_obstacles[0].passed:
                score += 10
                (hitted_obstacles[0])
        # --update screen
        updateFrame(screen, obstacles, skier, score)
        ()
 
 
'''run'''
if __name__ == '__main__':
    main();

This is the whole content of this article.