“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

Review past

Python to achieve “little rabbit and Bun” game

Python eight notes to achieve small games

Python to achieve the puzzle game

Python to achieve ski games

Python for classic 90 tank wars

Python FlappyBird small game

Python dinosaur jump a small game

Python to achieve tower defense games

preface

Use Python to make a fruit and gold coins of the small game, the whole is relatively simple, forget it, I still tell the truth, I just want to share the recent love of Das Beste this song. Let’s get started happily ~

rendering

The development tools

Python version: 3.6.4

Related modules:

Pygame module;

And some modules that come with Python.

Environment set up

Install Python and add it to the environment variables. PIP installs the required modules.

Introduction of the principle

Game Rules:

The player controls the movement of the farmer by “wasd” or “↑, ↓, ←→”, so that the farmer can receive all the fruit and gold coins dropped from the sky.

The game ends when too many fruits are lost (or not received).

Step by step:

Step1: Define the Sprite class

Obviously, collision detection is needed to determine if a farmer has received fruit/gold, so let’s define the two sprites: farmer and fruit/gold. First, farmers:

The only technical point here is the need to constantly change the farmer’s state map to achieve the farmer’s walking effect.

Then there’s the fruit/gold category:

There is no technical point here, just make the fruit/gold pieces fall.

Step2: implement the main game loop

With the above two Sprite classes defined, you can start implementing the main game loop. The first is initialization:

Then it’s time to enter the main loop:

The main loop does a few things:

Using key detection to control farmers’ actions;

Constantly produce fruit/coins and drop them;

Use collision detection to determine whether the farmer has received fruit/gold coins;

Display current score and health in real time.

Step3: make the end of the game interface

After Game Over, enter the Game Over screen and display the current score and the all-time high score:

That’s the end of this article, thank you for watching Python24 mini games series, the next article to share the plane wars mini games

To thank the readers, I’d like to share some of my recent programming gems to give back to each and every one of you.

Dry goods are mainly:

① More than 2000 Python ebooks (both mainstream and classic)

Python Standard Library (Chinese version)

(3) project source code (forty or fifty interesting and classic practice projects and source code)

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

⑤Python Learning Roadmap

⑥ Two days of Python crawler boot camp live permissions

See profile or private message for complete source code.