Step-by-Step Guide to Building a Classic Airplane Shooter Game with Pygame
This tutorial walks you through recreating the classic WeChat "Airplane Battle" game using Python and Pygame, covering environment setup, player and enemy plane implementation, keyboard controls, collision detection, and a complete runnable code example.
This article shows how to recreate the classic WeChat "Airplane Battle" game using Python and the Pygame library, providing a complete, step‑by‑step guide and full source code.
Environment installation : You need Python 3, an IDE such as PyCharm, and the Pygame module. Install Pygame with the following command:
pip install -i https://pypi.douban.com/simple/ pygamePlayer plane : The main() function creates a 480×852 window, loads the background image, and instantiates HeroPlane and EnermyPlane objects. It then enters a loop that displays the background, renders the hero plane, checks for bullet hits, updates the hero image based on the hit count, and breaks after a certain number of hits.
def main():
global is_hit, nums
screen = pygame.display.set_mode((480, 852), 0, 32)
background = pygame.image.load("./feiji/background.png")
hero = HeroPlane(screen)
enermy_plane = EnermyPlane(screen)
while True:
# display background
screen.blit(background, (0, 0))
# display hero
hero.display()
# test hit detection
hero.test(enermy_plane.bullets)
if hero.is_hit:
hero_nums += 1
if hero_nums == 10:
hero.image = pygame.image.load("./feiji/hero_blowup_n1.png")
elif hero_nums == 20:
hero.image = pygame.image.load("./feiji/hero_blowup_n2.png")
# ... other frames ...
elif hero_nums > 50:
breakEnemy plane : Similar logic is used for the enemy plane. Its image changes according to the number of hits, and it moves and fires bullets.
enermy_plane.display()
enermy_plane.test(hero.bullets)
if enermy_plane.is_hit:
enermy_nums += 1
if enermy_nums == 10:
enermy_plane.image = pygame.image.load("./feiji/enemy0_down1.png")
elif enermy_nums == 20:
enermy_plane.image = pygame.image.load("./feiji/enemy0_down2.png")
# ... other frames ...
elif enermy_nums > 50:
enermy_plane = EnermyPlane(screen)
enermy_nums = 0Keyboard control : The key_control(hero) function processes pygame events. It moves the hero left with K_a or K_LEFT , right with K_d or K_RIGHT , fires a bullet on K_SPACE , and exits when the window is closed.
def key_control(hero):
for event in pygame.event.get():
if event.type == QUIT:
print("exit")
exit()
elif event.type == KEYDOWN:
if event.key == K_a or event.key == K_LEFT:
hero.move_left()
print("left")
elif event.key == K_d or event.key == K_RIGHT:
hero.move_right()
print("right")
elif event.key == K_SPACE:
hero.fire()
print("space")Collision detection : A BasePlane class defines common attributes. Its test(self, bullets) method checks each bullet against the plane’s rectangle and sets is_hit flags when a collision occurs.
class BasePlane(object):
def __init__(self, screen, x, y, image):
self.x = x
self.y = y
self.screen = screen
self.image = pygame.image.load(image)
self.is_hit = False
self.bullets = []
def test(self, bullets):
for bullet in bullets:
if (self.x < bullet.x < self.x + self.image.get_width() and
self.y < bullet.y < self.y + self.image.get_height()):
self.is_hit = True
bullet.is_hit = TrueThe main game loop updates the display with pygame.display.update() and pauses briefly with time.sleep(0.01) . Screenshots of the running game are included.
In summary, the provided code implements a simple endless‑flow airplane shooter without power‑ups; further enhancements can be added later.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.