Introduction:
In winter, of course, you should go skiing with your loved one,
Walking in the snow-white world,
Romantic and exciting!Only love and skiing can live up to!
The scenery is not only beautiful, but also super! Will! Play!
It’s not the right time yet but can winter be far behind when autumn is half over?
Since you can’t go skiing now, but xiaobian can first let you experience ~
Take you to do a super fun skiing games, but also can practice skiing posture oh ~
The body of the
Environmental Installation Part (1) :
Python version: **3.6
Pygame module;
pip install pygame
Copy the code
And some modules that come with Python.
Game picture material (2) : can be modified.
Set the number of various music, font, picture path (3) :
SKIER_IMAGE_PATHS = [os.path.join(os.getcwd(), 'resources/images/skier_forward.png'), os.path.join(os.getcwd(), 'resources/images/skier_right1.png'), os.path.join(os.getcwd(), 'resources/images/skier_right2.png'), os.path.join(os.getcwd(), 'resources/images/skier_left2.png'), os.path.join(os.getcwd(), 'resources/images/skier_left1.png'), os.path.join(os.getcwd(), 'resources/images/skier_fall.png') ] OBSTACLE_PATHS = { 'tree': os.path.join(os.getcwd(), 'resources/images/tree.png'), 'flag': OS. Path. Join (OS getcwd (), 'resources/images/flag. PNG')} 'background music path' ' ' ' 'BGMPATH = OS. The path. The join (OS. The getcwd (), 'resources/music/ gm.mp3') 'FONTPATH = os.path.join(os.getcwd(), 'resources/font/ fzstk.ttf ')Copy the code
Rules of the game:
The player controls the skier in progress through the “AD” key or the “←→” key, tries to avoid the trees on the road, and tries to pick up the small flag on the road.
If you touch a tree, you lose 50 points. If you pick up a flag, you gain 10 points.
Skiers: have the ability to shift from left to right, as shown below:
To the left:To the right:A straight line:Left:Right:
Operate the player to ski, have different direction offset ability, that still need what? Obstacles, of course.
"' create obstacles' def createObstacles (s, e, num = 10) : obstacles = pygame. Sprite. Group () locations = [] for I in range (num) : row = random.randint(s, e) col = random.randint(0, 9) location = [col*64+20, row*64+20] if location not in locations: locations.append(location) attribute = random.choice(list(cfg.OBSTACLE_PATHS.keys())) img_path = cfg.OBSTACLE_PATHS[attribute] obstacle = ObstacleClass(img_path, location, attribute) obstacles.add(obstacle) return obstaclesCopy the code
The game scissors on the road of the small flag will certainly add points, met the tree minus points, so set up the score display:
Def showScore(screen, score, pos=(10, 10)): font = pygame.font.Font(cfg.FONTPATH, 30) score_text = font.render("Score: %s" % score, True, (0, 0, 0)) screen.blit(score_text, pos)Copy the code
Attached code game main program:
Def main(): # Game init() PyGame.mixer.init () PyGame.mixer.music.load (cfg.bgmpath) PyGame.mixer.music.set_volume (0.4) Play (-1) # Set screen = Pygame.display.set_mode (cfg.screensize) PyGame.display.set_caption (' Ski-game Source base: #959755565 ') # ShowStartInterface(screen, CFG.SCREENSIZE) # Create obstacles0 = createObstacles(20, 29) obstacles1 = createObstacles(10, 19) obstaclesflag = 0 obstacles = AddObstacles(obstacles0, Obstacles1) clock clock = pygame.time.clock () distance = 0 score = 0 6] # game main loop while True: # -- Event capture for event in Pygame.event.get (): if event.type == pyGame.quit: pygame.quit() sys.exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT or event.key == pygame.K_a: speed = skier.turn(-1) elif event.key == pygame.K_RIGHT or event.key == pygame.K_d: Speed = skier. Turn (1) # -- 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: obstacle.location[1] = obstacle.location[1] - 1280 obstacles1 = createObstacles(10, 19) obstacles = AddObstacles(obstacles0, obstacles1) for obstacle in obstacles: Obstacle. Move (short) # -- collision detection hitted_obstacles = pygame. Sprite. Spritecollide (skier, obstacles, False) if hitted_obstacles: if hitted_obstacles[0].attribute == "tree" and not hitted_obstacles[0].passed: score -= 50 skier.setFall() updateFrame(screen, obstacles, skier, score) pygame.time.delay(1000) skier.setForward() speed = [0, 6] hitted_obstacles[0].passed = True elif hitted_obstacles[0].attribute == "flag" and not hitted_obstacles[0].passed: Remove (hitted_obstacles[0]) # updateFrame(screen, block, skier) score) clock.tick(cfg.FPS)Copy the code
Game renderings:
conclusion
All right! This skiing game is finished, everyone to show their skill pa ~
Source base penguin group: # private letter xiaobian can # complete information more game code group free to share drops!!