Game Development 6 min read

Building a Simple Airplane Shooting Game with Pygame (Python)

This tutorial walks through creating a classic‑style 2D airplane shooting game in Python using Pygame, covering environment setup, player and enemy plane implementation, keyboard controls, bullet collision detection, and includes full source code and visual results.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Building a Simple Airplane Shooting Game with Pygame (Python)

Remember the classic WeChat "Airplane Battle" game? This article shows how to recreate a similar 2D shooter using Python's Pygame library, providing a complete, though simple, version of the game.

1. Environment Installation

Prepare Python 3, PyCharm, and install the Pygame module.

pip install -i https://pypi.douban.com/simple/ pygame<br/>

2. Player Plane

def main():<br/>    global is_hit<br/>    global nums<br/>    screen = pygame.display.set_mode((480, 852), 0, 32)<br/>    background = pygame.image.load("./feiji/background.png")<br/>    # 创建飞机<br/>    hero = HeroPlane(screen)<br/>    enermy_plane = EnermyPlane(screen)<br/>    while True:<br/>        global hero_nums<br/>        global enermy_nums<br/>        # 显示背景<br/>        screen.blit(background, (0, 0))<br/>        # 显示我方飞机<br/>        hero.display()<br/>        # 测试是否被子弹击中<br/>        hero.test(enermy_plane.bullets)<br/>        if hero.is_hit:<br/>            hero_nums += 1<br/>        if hero_nums == 10:<br/>            hero.image = pygame.image.load("./feiji/hero_blowup_n1.png")<br/>        elif hero_nums == 20:<br/>            hero.image = pygame.image.load("./feiji/hero_blowup_n2.png")<br/>        elif hero_nums == 30:<br/>            hero.image = pygame.image.load("./feiji/hero_blowup_n3.png")<br/>        elif hero_nums == 40:<br/>            hero.image = pygame.image.load("./feiji/hero_blowup_n4.png")<br/>        elif hero_nums > 50:<br/>            break<br/>

3. Enemy Plane

# 显示敌飞机<br/>enermy_plane.display()<br/>enermy_plane.test(hero.bullets)<br/>if enermy_plane.is_hit:<br/>    enermy_nums += 1<br/>if enermy_nums == 10:<br/>    enermy_plane.image = pygame.image.load("./feiji/enemy0_down1.png")<br/>elif enermy_nums == 20:<br/>    enermy_plane.image = pygame.image.load("./feiji/enemy0_down2.png")<br/>elif enermy_nums == 30:<br/>    enermy_plane.image = pygame.image.load("./feiji/enemy0_down3.png")<br/>elif enermy_nums == 40:<br/>    enermy_plane.image = pygame.image.load("./feiji/enemy0_down4.png")<br/>elif enermy_nums > 50:<br/>    enermy_plane = EnermyPlane(screen)<br/>    enermy_nums = 0<br/>else:<br/>    enermy_plane.move()<br/>    enermy_plane.fire()<br/># 控制飞机左右<br/>key_control(hero)<br/># 刷新屏幕<br/>pygame.display.update()<br/># 程序休眠0.01秒<br/>time.sleep(0.01)<br/>

4. Keyboard Control

def key_control(hero):<br/>    for event in pygame.event.get():<br/>        if event.type == QUIT:<br/>            print("exit")<br/>            exit()<br/>        elif event.type == KEYDOWN:<br/>            if event.key == K_a or event.key == K_LEFT:<br/>                hero.move_left()<br/>                print("left")<br/>            elif event.key == K_d or event.key == K_RIGHT:<br/>                hero.move_right()<br/>                print("right")<br/>            elif event.key == K_SPACE:<br/>                hero.fire()<br/>                print("space")<br/>

5. Bullet Collision Detection

class BasePlane(object):<br/>    def __init__(self, screen, x, y, image):<br/>        self.x = x<br/>        self.y = y<br/>        self.screen = screen<br/>        self.image = pygame.image.load(image)<br/>        self.is_hit = False  # 此标志用来表示飞机是否被击中了<br/>        self.bullets = []<br/><br/>    def test(self, bullets):<br/>        for bullet in bullets:<br/>            if self.x < bullet.x < self.x + self.image.get_width() and \
                self.y < bullet.y < self.y + self.image.get_height():<br/>                self.is_hit = True<br/>        # 检测子弹碰撞<br/>        for item in self.bullets:<br/>            for bullet in bullets:<br/>                if item.x < bullet.x < item.x + item.image.get_width() and \
                    item.y < bullet.y < item.y + item.image.get_height():<br/>                    item.is_hit = True<br/>                    bullet.is_hit = True<br/>

6. Visual Results

Conclusion

The simple version runs infinitely without power‑ups or varied aircraft, but it demonstrates the core mechanics of a 2D shooter; further enhancements can be added later.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Game Development2D Shooter
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.