Jun 01, 2021 Article blog
This article was reproduced to Know ID: Charles (Bai Lu) knows his personal column
Download the W3Cschool Mobile App, 0 Foundation Anytime, Anywhere Learning Programming >> Poke this to learn
In this issue we will make a simple ski game.
Don't talk much nonsense, let's start happily
Baidu web download link:
https://pan.baidu.com/s/1F74v1qGVk2NEWMEv-5E25w
Password: 2fcv
The picture footage comes from the Internet, and it's apologetic and deleted.
Python version: 3.6.4
Related modules:
pygame module;
and some Python's own modules.
Environment construction
Install Python and add it to the environment variable, and pip installs the relevant modules that are required.
Take a quick look
Rules of the game:
Players use the "AD" key or "←→" to manipulate the advancing skiers, trying to avoid the trees on the road, as far as possible to pick up the small flag on the road.
If you hit a tree, the score is reduced by 50, and if a small flag is picked up, the score is increased by 10.
Step-by-step:
Step1: Define the elf class
Because the game involves collision detection (a collision between a skier and a tree and a small flag), we define two elf classes that represent skiers and obstacles (i.e. trees and flags):
Among them, the skier should have the ability to shift left and right as he moves forward, and it is more common sense for the skier to move forward at an offset rate. At the same time, skiers should have different postures to show their state of play:
straight line:
To the left:
There's a lot on the left:
One point to the right:
There's a lot on the right:
In addition, although the left and right movement of the skier is achieved by moving the skier himself, the forward movement of the skier is achieved by moving obstacles.
Step2: Randomly create obstacles
Now we need to define a function that randomly creates obstacles so that we can call them in the game's main loop:
Step3: Game Main Loop
First let's initialize some parameters:
The obstacles are created twice to facilitate picture convergence.
Then we can define the main loop:
The main loop includes:
Event monitoring, obstacle updates, collision detection, and the display of scores are all easy to achieve.
Step4: Other
Start, end the interface these, on everyone's own play, I wrote a simple start interface:
effect:
All Done!
Code as of 2018-07-12 is correct.
Welcome to Star on GitHub:
https://github.com/CharlesPikachu/Games