This article mainly introduces the python programming instructions for whack-a-mole, which has certain reference value. Friends in need can refer to it. I hope you will gain a lot after reading this article. Let the editor take you to understand it together.
Pygame library is an open source module of the Python programming language specifically designed to help make games and other multimedia applications PHP vs. PYTHON: Detailed comparison based on syntax, functionality, applications, etc. This article will use the Pygame module to create a small game of whack-a-mole. Come and learn with the editor. Click to get programming learning materials for free
Preface
Today I will write a mini game of whack-a-mole for everyone. Let’s get started without further ado~
development tools
Python version: 3.6.4
Related modules:
pygame module;
And some modules that come with Python.
Environment build
Install Python and add it to the environment variables, and pip to install the required relevant modules.
Principle introduction
I believe everyone knows the rules of the game of whack-a-mole, so I won’t go into details here. Anyway, it’s just a mole that keeps digging holes with a hammer~
First, let's identify what elements are in the game. To play whack-a-mole, of course there must be a mole, so let’s write a game elf class for the mole:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Obviously, the mole has two states: being hit by the hammer and not being hit by the hammer, so two pictures need to be loaded. When the mole is hit, the state diagram of the mole that has not been hit is switched to the state diagram of the mole after being hit. Gopher status chart (ps: the picture may not look like a gopher).
Then let's define the game elf class of hammer. Similar to the gopher, the hammer also has two states: unhammered and hammered. However, after hammering, it needs to quickly return to the unhammered state. Specifically, the code implementation as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
OK, after defining the game wizard, we can start writing the main program. The first thing is to initialize the game:
1 2 3 4 5 6 7 |
|
Then load the necessary game materials and define the necessary game variables
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
Then comes the main game loop:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
|
I have also commented on each part. The logic is very simple, so I won’t go into too much nonsense. After 60 seconds, the game is over, and we can count the scores and compare them with the highest score in history:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
This concludes this article about the actual Whac-A-Mole game in Python Pygame.