Review past

Python implements the “bunny and Bun” game

Python to achieve eight notes small game

Python implements jigsaw puzzles

Preface:

Today we’re going to make a simple skiing game. Without further ado, let’s begin happily

The results of

As usual, let’s look at the renderings first

The development tools

Python version: 3.6.4

Related modules:

Pygame module;

And some of the modules that come with Python.

Environment set up

Install Python and add it to the environment variable, and the PIP will install the appropriate module.

The principle is introduced

Game rules:

The player controls the skier through the “AD” key or “←→”, tries to avoid the trees on the road, and tries to pick up the flags on the road.

If a tree is touched, the score is reduced by 50, and if a small flag is found, the score is increased by 10.

Step by step:

Step1: Define the Sprite class

Since the game involves collision detection (collision between the skier and the tree and flag), we define two Sprite classes to represent the skier and the obstacle (the tree and flag) :

Class SkierClass(PyGame.Sprite.Sprite): def __init__(self): Sprite.__init__(self) # Skiers' Directions (-2 to 2) self. Direction = 0 self. Imagepaths = CFG. Skier_Image_Paths [:-1] self.image = pygame.image.load(self.imagepaths[self.direction]) self.rect = self.image.get_rect() self.rect.center = [320, 100] self.speed = [self.direction, 6-abs(self.direction)*2] Def turn(self, num) def turn(self, num): self.direction += num self.direction = max(-2, self.direction) self.direction = min(2, self.direction) center = self.rect.center self.image = pygame.image.load(self.imagepaths[self.direction]) self.rect = self.image.get_rect() self.rect.center = center self.speed = [self.direction, 6-abs(self.direction)*2] return self.speed def move(self): self.rect.centerx += self.speed[0] self.rect.centerx = max(20, self.rect.centerx) self.rect.centerx = min(620, Class obstacleClass (pygame.sprite.Sprite): def __init__(self, img_path, location, attribute): pygame.sprite.Sprite.__init__(self) self.img_path = img_path self.image = pygame.image.load(self.img_path) self.location  = location self.rect = self.image.get_rect() self.rect.center = self.location self.attribute = attribute self.passed = Def move(self, num): self.rect.centery = self.location[1] -num

The skier should have the ability to drift left and right as he moves forward, and it would be more logical for the skier to slow down as he moves forward in order for the player to operate. At the same time, skiers should have different positions to show how they are skiing:

A straight line:

A little to the left:

Much to the left:

A little to the right:

Much to the right:

In addition, while the skier’s left/right movement is achieved by moving the skier, the skier’s forward movement is achieved by moving obstacles.

Step2: Create obstacles randomly

Now we need to define a random obstacle creation function that can be called in the main game loop:

The purpose of creating two obstacles is to facilitate the connection of the picture.

Then we can define the main loop:

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: Move () 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: Score += 10 Diction. Remove (hitted_Diction [0]) updateFrame(screen, Diction, Skier, score) clock.tick(cfg.FPS)

The contents of the main loop include:

Event monitoring, obstacle updates, collision detection, and score display are all very easy to implement.

Step4: other

Start, end interface these, rely on everyone to play, I wrote a simple start interface:

Def showStartInterface (screen, screensize): screen.fill((255, 255, 255)) tfont = pygame.font.Font(cfg.FONTPATH, ScreenSize [0]//5) cFONT = pyGame.FONT (cfg. fontPath, screenSize [0]//20) title = tFONT. Render (u' Snoop ', True, (255,) 0, 0)) content = CFONT.render (u' press any key to start game ', True, (0, 0, 255)) trect = title.get_rect() trect.midTop = (screenSize [0]/2, screenSize [0]/2, screenSize [0]/2, screensize[1]/5) crect = content.get_rect() crect.midtop = (screensize[0]/2, screensize[1]/2) screen.blit(title, trect) screen.blit(content, crect) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() elif event.type == pygame.KEYDOWN: return pygame.display.update()

This is the end of the article, thank you for watching, Python24 mini-game series, the next article to share the classic 90 tank war

To thank you readers, I’d like to share with you some of my recent collection of dry programming goodies and give something back to every reader in the hope of helping you out.

Dry goods mainly include:

① More than 2000 Python e-books (mainstream and classic books are available)

(2) The Python Standard Library (Chinese version)

③ project source code (40 or 50 interesting and classic hands-on project and source code)

④Python basic introduction, crawler, web development, big data analysis video (suitable for small white learning)

⑤Python Learning Roadmap (Goodbye to Slow Learning)

⑥ Two days of Python crawler training camp live access

All done~ thumb up + Comments ~ See bio or private message for complete source code.