Build a Simple Python Airplane Shooter with Pygame – Step‑by‑Step Tutorial
This tutorial walks you through creating a basic airplane‑shooting game in Python using Pygame, covering window setup, image loading, sprite movement, event handling, collision detection, audio playback, and provides complete source code and explanations for each function.
Overview
This guide demonstrates how to build a minimal airplane‑shooting game with Python and the Pygame library, illustrating window creation, sprite handling, user input, collision detection, and sound effects.
Environment
Python 3.6+
Windows/Linux
Installation
pip install pygameCreating the Game Window
import os
import time
import pygame
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 250
IMG_PATH = os.path.join(os.getcwd(), "img")
def main():
pygame.display.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("python小demo 飞机大战")
pygame.display.set_icon(pygame.image.load(os.path.join(IMG_PATH, "icon.jpg")))
background = pygame.image.load(os.path.join(IMG_PATH, "test_bj.png"))
while True:
screen.blit(background, (0, 0))
pygame.display.update()
time.sleep(0.02)
if __name__ == "__main__":
main()Moving the Hero Plane
class HeroPlane(object):
def __init__(self):
self.image = pygame.image.load(os.path.join(IMG_PATH, "hero_show_1.png"))
self.rect = self.image.get_rect()
self.rect.x = 0
self.rect.y = int(SCREEN_HEIGHT - self.rect.height) / 2
self.is_running = True
def move_level(self, level):
# keep within screen bounds
if 0 <= level <= SCREEN_WIDTH - self.rect.width:
self.rect.x = level
elif level < 0:
self.rect.x = 0
else:
self.rect.x = SCREEN_WIDTH - self.rect.width
def move_vertical(self, vertical):
if 0 <= vertical <= SCREEN_HEIGHT - self.rect.height:
self.rect.y = vertical
elif vertical < 0:
self.rect.y = 0
else:
self.rect.y = SCREEN_HEIGHT - self.rect.heightEvent Handling and User Control
def event_check():
global MOUSE_MOVE, KEYDOWN
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEBUTTONDOWN:
MOUSE_MOVE = True
elif event.type == pygame.MOUSEBUTTONUP:
MOUSE_MOVE = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
KEYDOWN = 1
elif event.key == pygame.K_UP:
KEYDOWN = 2
elif event.key == pygame.K_DOWN:
KEYDOWN = 3
elif event.key == pygame.K_RIGHT:
KEYDOWN = 4
elif event.type == pygame.KEYUP:
KEYDOWN = 0
if MOUSE_MOVE:
x, y = pygame.mouse.get_pos()
hero.move_level(x)
hero.move_vertical(y)
if KEYDOWN:
if KEYDOWN == 1:
hero.move_level(hero.rect.x - 5)
elif KEYDOWN == 2:
hero.move_vertical(hero.rect.y - 5)
elif KEYDOWN == 3:
hero.move_vertical(hero.rect.y + 5)
else:
hero.move_level(hero.rect.x + 5)Collision Detection
def check_crash():
is_crash = pygame.sprite.collide_rect(hero, enemy)
if is_crash:
screen.blit(pygame.image.load(os.path.join(IMG_PATH, "ex_2.png")), (hero.rect.x, hero.rect.y))
screen.blit(pygame.image.load(os.path.join(IMG_PATH, "ex_2.png")), (enemy.rect.x, enemy.rect.y))
pygame.mixer.Sound(os.path.join(MUSIC_PATH, "enemy1_down.wav")).play()
game_over = pygame.font.Font(None, 100)
game_over = game_over.render("game over", True, (220, 20, 60))
screen.blit(game_over, (int(SCREEN_WIDTH/2 - game_over.get_width()/2), int(SCREEN_HEIGHT/2)))Audio Playback
pygame.mixer.init()
pygame.mixer.music.load(os.path.join(MUSIC_PATH, "game_music.ogg"))
pygame.mixer.music.play(loops=-1)Full Example
The complete source combines window initialization, hero and enemy sprites, event handling, collision detection, and background music into a functional airplane‑shooting game.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
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.
