How to Use Python to Write a Simple Game? Let Me Teach You!

Click the blue text above to follow us

We hope that through this game programming, more parents and children can easily access youth programming, and further understand programming. Parents can learn and practice together with their children, observing their interest in programming, thus understanding whether their child has an interest in this area. We always believe that interest is the best teacher for children. Now, let’s learn how this game is written.

01

Classes and Functions Used

The game project is built using the pygame library framework for the airplane battle game, involving issues such as the airplane, bullets, enemy planes, and the logic between them. The game project involves four types of objects: our plane, enemy planes, bullets, and bullet supplies, requiring the creation of related classes and methods. In total, we designed four classes: myplane, enemy, bullet, and supply, and completed their respective attributes and methods.

Complete Game Code Video

myplane class

The myplane class mainly completes the design task of our plane. It initializes our plane’s settings, including image, position, and movement speed, and defines movement and destruction reset functions, completing the movement and destruction reset of our plane.

How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!

enemy class

The enemy class mainly completes the design task of enemy planes. The enemy class includes three types of planes: SmallEnemy, MidEnemy, and LargeEnemy. The three types of plane classes initialize the plane’s image, position, movement speed, and the medium and large planes also set up health bars. Each of the three plane classes defines movement and destruction reset functions, completing the movement and destruction reset of enemy planes.

How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!

bullet class

The bullet class mainly completes the design task of airplane bullets. The bullet class includes two types of bullets: Bullet1 and Bullet2. The two types of bullet classes initialize the bullet’s image, position, and movement speed. Each bullet class defines movement and destruction reset functions, completing the movement and destruction reset actions of the bullets.

How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!

supply class

The supply class mainly completes the design task of bullet enhancements and bombs. The supply class includes two types: Bullet_Supply and Bomb_Supply. Each type defines the images, positions, and movement speeds of the enhanced bullets and bombs. Each supply class defines movement and destruction reset functions, completing the movement and destruction reset of the supply equipment.

How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!

plane_war class

The plane_war class is the main class of our game project, mainly used to initialize the game interface, load configuration music and images, create our plane, enemy planes, bullets, and bullet supply objects, and complete the game running logic. The game running logic includes the following contents:

① Create the game running interface and load configuration music; ② Create our plane object and bullet object, which appear on the game interface; ③ Create enemy plane objects (including small, medium, and large plane objects), which appear on the game interface at a certain quantity and frequency; ④ Create bullet supply objects, which appear on the game interface at intervals; ⑤ Control our plane’s movement up, down, left, and right using the keyboard, and use the spacebar to control bomb usage; ⑥ Collision between enemy planes and bullets, enemy planes are removed, and our score increases; ⑦ Collision between enemy planes and our plane, our plane loses a life, and if it loses three times, the game ends.

How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!02

Game Operation

Operating Environment: Install Python3 and the pygame module. To run: Enter the airplane battle folder and type python plane_war.py in cmd.

I believe that after reading this, everyone has a further understanding of programming, and you can try writing it yourself. If you have any questions or want the source files, you can add the editor’s WeChat in the lower right corner, and I will answer and provide them.

How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!How to Use Python to Write a Simple Game? Let Me Teach You!

Scan to follow us,

More consultations will be known early!

Technological innovation helps me grow

How to Use Python to Write a Simple Game? Let Me Teach You!

How to Use Python to Write a Simple Game? Let Me Teach You!

Remember to click the “Looking” button below

Leave a Comment