SoFunction
Updated on 2024-11-10

python implementation of the ski game

In this article, examples for you to share the specific code for python to realize the ski game, for your reference, the details are as follows

# coding: utf-8
# Skiing games
import sys
import pygame
import random
from  import *
 
 
# Skier class
class SkierClass():
 def __init__(self):
 .__init__(self)
 # Skier orientation (-2 to 2)
  = 0
  = ["./images/skier_forward.png", "./images/skier_right1.png", "./images/skier_right2.png", "./images/skier_left2.png", "./images/skier_left1.png"]
  = ([])
  = .get_rect()
  = [320, 100]
  = [, 6-abs()*2]
 # Change the skier's orientation
 # 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 skiers
 def move(self):
  += [0]
  = max(20, )
  = min(620, )
 
 
# Obstacle class
# Input:
# -img_path: obstacle image path
# -location: obstacle location
# -attribute: obstacle category 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
 
 
# Create obstacles
def create_obstacles(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 = (["tree", "flag"])
 img_path = './images/' if attribute=="tree" else './images/'
 obstacle = ObstacleClass(img_path, location, attribute)
 (obstacle)
 return obstacles
 
 
# Merging obstacles
def AddObstacles(obstacles0, obstacles1):
 obstacles = ()
 for obstacle in obstacles0:
 (obstacle)
 for obstacle in obstacles1:
 (obstacle)
 return obstacles
 
 
# Display the game start screen
def Show_Start_Interface(Demo, width, height):
 ((255, 255, 255))
 tfont = ('./font/', width//4)
 cfont = ('./font/', width//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()
  = (width/2, height/10)
 crect = content.get_rect()
  = (width/2, height/2.2)
 (title, trect)
 (content, crect)
 ()
 while True:
 for event in ():
 if  == QUIT:
 ()
 elif  == :
 return
 
 
# Main program
def main():
 '''
 Initialization
 '''
 ()
 # Voices
 ()
 ("./music/bg_music.mp3")
 .set_volume(0.4)
 (-1)
 # Screen
 screen = .set_mode([640, 640])
 .set_caption('Ski Games - Public:Charles' Pikachu')
 # mains frequency
 clock = ()
 # Skiers
 skier = SkierClass()
 # Record the distance skied
 distance = 0
 # Create obstacles
 obstacles0 = create_obstacles(20, 29)
 obstacles1 = create_obstacles(10, 19)
 obstaclesflag = 0
 obstacles = AddObstacles(obstacles0, obstacles1)
 # Scores
 font = (None, 50)
 score = 0
 score_text = ("Score: "+str(score), 1, (0, 0, 0))
 # Speed
 speed = [0, 6]
 Show_Start_Interface(screen, 640, 640)
 '''
 Main loop
 '''
 # Update the screen
 def update():
 ([255, 255, 255])
 ((screen))
 (, )
 (score_text, [10, 10])
 ()
 
 while True:
 # Left and right keys to control the direction of the character
 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)
 ()
 distance += speed[1]
 if distance >= 640 and obstaclesflag == 0:
 obstaclesflag = 1
 obstacles0 = create_obstacles(20, 29)
 obstacles = AddObstacles(obstacles0, obstacles1)
 if distance >= 1280 and obstaclesflag == 1:
 obstaclesflag = 0
 distance -= 1280
 for obstacle in obstacles0:
 [1] = [1] - 1280
 obstacles1 = create_obstacles(10, 19)
 obstacles = AddObstacles(obstacles0, obstacles1)
 # For collision detection
 for obstacle in obstacles:
 (distance)
 # Collision detection
 is_hit = (skier, obstacles, False)
 if is_hit:
 if is_hit[0].attribute == "tree" and not is_hit[0].passed:
 score -= 50
  = ("./images/skier_fall.png")
 update()
 # Pause for a moment after you fall and then get back up
 (1000)
  = ("./images/skier_forward.png")
  = 0
 speed = [0, 6]
 is_hit[0].passed = True
 elif is_hit[0].attribute == "flag" and not is_hit[0].passed:
 score += 10
 (is_hit[0])
 score_text = ("Score: "+str(score), 1, (0, 0, 0))
 update()
 (40)
 
 
if __name__ == '__main__':
 main()

This is the whole content of this article.