Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

python make mini-games (i)


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

Lead

This will be an irregularly updated series

This series will mainly use python's pygame module to teach you to make some small games, one per issue. Finally will be packaged into .exe files to share with you

So, for small partners who just want to play small games, just download the relevant files

As for pygame's development documents, temporarily not classified into the finishing plan, the coming Japanese side long, English bad students can only first look at the online Chinese information will be under the . .


Related documents

Baidu web download link: https://pan.baidu.com/s/1gDLSHDF48xV4Z7z25UjOxw

Password: q4ib


References for this issue

link:

https://www.raywenderlich.com/24252/beginning-game-programming-for-teens-with-python

author:

Julian Meyer

illustrate

python version: 3.5.4

pygame version: 1.9.3 (pip installed)


Start happily

Step 1: Hello, rabbit

 python make mini-games (i)1

effect:

 python make mini-games (i)2

Step2: Add a background

Load the desired picture first, then loop through x and y, and draw the effect of grass based on the values of x and y in the loop; and draw the castle on the screen in the next few lines.

 python make mini-games (i)3

effect:

 python make mini-games (i)4

Step3: Let the rabbit move

Next you need to add some real game elements, such as allowing rabbits to move with the keys.

 python make mini-games (i)5  python make mini-games (i)6

Effect (can move rabbit):

 python make mini-games (i)7

Step4: Turn the rabbit around

Okay, now your rabbit can move when you press the button, but wouldn't it be cooler if you could use the mouse to get the rabbit in the direction you chose? So it won't always go in one direction.

 python make mini-games (i)8

effect:

 python make mini-games (i)9

Step5: Shoot! hare

Getting rabbits to shoot at their enemies with arrows can be a bit complicated because you need to track all the arrows, update them, rotate them, and delete them as they run off the screen.

 python make mini-games (i)10  python make mini-games (i)11  python make mini-games (i)12

effect:

 python make mini-games (i)13

Step6: Come on, get your weapon!

Well, now there's the castle, and you have a hero who can move and shoot arrows. W hat's the difference? Of course it's the enemy who attacked the castle

In this step, you will randomly create some slugs rushing towards the castle. More and more crickets will emerge in the course of the game.

 python make mini-games (i)14  python make mini-games (i)15  python make mini-games (i)16

effect:

 python make mini-games (i)17

Step7: The collision of the dragonfly with the arrow

The dragonflies rush to your castle, but your arrows do nothing for them! How does this allow the rabbit to defend its home?

It's time for the arrows to kill the dragonflies so that the rabbits can protect their castles and win the game

 python make mini-games (i)18

effect:

 python make mini-games (i)19

Step8: Add a display of health values and times

The game works pretty well, with attackers and defenders. Now all you need is a way to show the Rabbitohs' score.

The easiest way is to add a HUD to show the life value of the current castle. You can also add a time to record how long the castle has survived.

 python make mini-games (i)20  python make mini-games (i)21

effect:

 python make mini-games (i)22

Step9: Win or lose

If you play long enough, even if your life value has become 0, the game will continue! N ot only that, you can still use arrows to shoot at these dragonflies. T hat's certainly not quite right. You need to add some win or loss to make your game worth playing.

Then let's add the conditions for victory or failure. Y ou can do this by terminating the main loop and entering the victory/failure cycle. You need to indicate whether the player has won and display it on the screen.

 python make mini-games (i)23  python make mini-games (i)24  python make mini-games (i)25

effect:

 python make mini-games (i)26

Step10: Free music and sound effects

The game is pretty good now, but there's no sound. I t's a little too quiet. With a little sound effect, you can feel the game better.

 python make mini-games (i)27  python make mini-games (i)28  python make mini-games (i)29

effect:

Of course, there will be all kinds of sound effects, the picture is probably not able to express emm.

OK, it's done, you've made a perfect little game!

On this basis, you can expand the game according to your own ideas! For example, you can try to reset the various pictures inside, plus different guns or different monsters!