“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

Python to achieve fruit and gold coins small games

Python simple version of the aircraft war games

Python tetris small game implementation

Python to achieve a small game push box

Preface:

Today to share the alien invasion game let us happily begin

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.

The effect

The principle is introduced

Game Introduction:

Players through the mouse control of the ship action and shooting, if the enemy to reach the bottom of the game interface before the destruction of all the enemies, the game victory, otherwise the game failed.

Step by step:

Step1: define the game Sprite class

First, let’s figure out which characters the game needs.

① Our spaceship

Our ship is controlled by the mouse and needs to be able to shoot, move and explode when hit by the enemy.

Therefore, the game Sprite class code implementation is as follows:

(2) the enemy

The enemy is computer-controlled and needs to be able to shoot, move and explode when hit.

Therefore, the game Sprite class code implementation is as follows:

(3) the UFO

The UFO is controlled by a computer and can be seen as an Easter egg. When you hit this Easter egg, you will receive a large number of bonus points, and when the bonus points accumulate to a certain amount, you will receive an additional health bonus.

The UFO class only requires the ability to move and explode when hit.

Therefore, the game Sprite class code implementation is as follows:

(4) the bullet class

Both your ship and the enemy will need bullets to fire, and they should be able to move forward.

Our bullet class code implementation is as follows:

Enemy bullet class code implementation is as follows:

Step2: implement the main game loop

Initialization, including loading the necessary game materials, instantiating the predefined game Sprite classes, and so on:

Then you can enter the main game loop

The first is button detection to enable the user to interact with the game:

The next step is according to the results of user operations and computer automatic operations to achieve the results of the game screen update. Of course, some sprites need to be collision detected according to the rules of the game, including: enemy bullets and our ship, and our bullets and enemies, etc.

Concrete code implementation is as follows:

Finally, some game-related data is displayed on the game interface in real time for players’ reference:

Step3: define the end game interface

We just need to show the end of the game interface based on whether the player has won the game:

That’s the end of this article, thank you for watching Python24 games series, the next article will share the classic Pac-Man 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.