Introduction:

This will be an occasional series ~~~

This series will use python’s PyGame module to teach you how to make small games, one per installment. Finally will be packaged into an.exe file to share with everyone ~~~

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

As for the development documents of PyGame, they have not been included in the sorting plan for the time being. Students who are not good at English can only read the Chinese materials on the Internet

Preparations: Install Python

Python version: 3.5.4

Pygame version: 1.9.3 (PIP install)

Start off happy

Step1: Hello, rabbit

Effect:

Step2: Add background first load the required picture; Secondly, cycle through X and Y in turn, and draw the grass effect according to the value of x and Y in the cycle; The next few lines draw the castle on the screen.

Effect:

Step3: make the rabbit move next you need to add some real game elements, such as making the rabbit move with the buttons.

Effect (can move rabbit) :

Step4: Let the rabbit turn

Ok, now your rabbit can move when you press a button, but wouldn’t it be cool if you could use your mouse to point the rabbit in the direction you choose? So it’s not always pointing in the same direction.

Effect:

Step5: Shoot! The rabbit

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

Effect:

Step6: Badger, take the weapon!

Well, now you have a castle, and you have a hero who can move and shoot arrows. What do we need? In this step, you will randomly create badgers to charge into the castle. More and more badgers emerge as the game progresses.

Effect:

Step7: Badger and arrow collision

Badgers charge into your castle, but your arrows have no effect on them! How does this leave the rabbit to defend its home? It’s time for arrows to kill badgers so rabbits can defend their castles and win the game

Effect:

Step8: Add health value and time display game runs quite well, there are attackers, defenders. Now, all you need is a way to show the rabbit’s score. The easiest way to do this is to add a HUD that displays the health of the current castle. You can also add a timer to keep track of how long the castle survived.

Effect:

Step9: win or lose if you play long enough, even if your health has become 0, the game still continues! Not only that, you can still shoot the badgers with arrows. That can’t be right. You need to add some wins and losses to make your game worth playing. So let’s add victory or defeat conditions. You can do this by terminating the main loop and entering the win/lose loop. You need to indicate whether the player has won or not and show it on the screen.

Effect:

Step10: Free music and sound effects

The game is pretty good now, but there’s no sound. It’s a little too quiet. Add a little sound effect to give you a better feel for the game.

Effect:

Of course there are all kinds of sound effects ~~~ pictures probably can’t express emM.

OK, you’re done, you’ve made a perfect little game!

That’s the end of this article, thanks for watching Python24 Mini-games, next article sharing 8-note mini-games

To thank you readers, I’d like to share some of my recent programming favorites to give back to each and every one of you in the hope that they can help you.

Dry goods mainly include:

① Over 2000 Python ebooks (both mainstream and classic books should be available)

②Python Standard Library (Most Complete Chinese version)

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

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

⑤ A Roadmap for Learning Python

⑥ Two days of Python crawler boot camp live access

All done~ ~ see personal profile or private letter to obtain the complete source code.