前言
大家之前用python编写过飞机大战的部分代码,只能够展示英雄飞机,背景,敌机和发射子弹,今天把背景音乐,击毁敌机,爆
炸特效,得分等等相关功能一并加入进来,代码有点长,三百多行,你们要的代码来了哦?
编程思路
主要使用pygame库,类的创建,函数的调用等等来实现,话不多说,上程序。
编程实现
Python学习交流Q群:906715085####
import pygame # 导入动态模块(.dll .pyd .so) 不需要在包名后边跟模块名
from pygame.locals import *
import time
import random
import sys
# 定义常量(定义后,不再改值)
WINDOW_HEIGHT = 768
WINDOW_WIDTH = 512
enemy_list = []
score = 0
is_restart = False
class Map:
def __init__(self, img_path, window):
self.x = 0
self.bg_img1 = pygame.image.load(img_path)
self.bg_img2 = pygame.image.load(img_path)
self.bg1_y = - WINDOW_HEIGHT
self.bg2_y = 0
self.window = window
def move(self):
# 当地图1的 y轴移动到0,则重置
if self.bg1_y >= 0:
self.bg1_y = - WINDOW_HEIGHT
# 当地图2的 y轴移动到 窗口底部,则重置
if self.bg2_y >= WINDOW_HEIGHT:
self.bg2_y = 0
# 每次循环都移动1个像素
self.bg1_y += 3
self.bg2_y += 3
def display(self):
"""贴图"""
self.window.blit(self.bg_img1, (self.x, self.bg1_y))
self.window.blit(self.bg_img2, (self.x, self.bg2_y))
class HeroBullet:
"""英雄子弹类"""
def __init__(self, img_path, x, y, window):
self.img = pygame.image.load(img_path)
self.x = x
self.y = y
self.window = window
def display(self):
self.window.blit(self.img, (self.x, self.y))
def move(self):
"""子弹向上飞行距离"""
self.y -= 6
def is_hit_enemy(self, enemy):
if pygame.Rect.colliderect(
pygame.Rect(self.x, self.y, 20, 31),
pygame.Rect(enemy.x, enemy.y, 100, 68)
): # 判断是否交叉
return True
else:
return False
class EnemyPlane:
"""敌人飞机类"""
def __init__(self, img_path, x, y, window):
self.img = pygame.image.load(img_path) # 图片对象
self.x = x # 飞机坐标
self.y = y
self.window = window # 飞机所在的窗口
self.is_hited = False
self.anim_index = 0
self.hit_sound = pygame.mixer.Sound("E:/飞机大战/baozha.ogg")
def move(self):
self.y += 10
# 到达窗口下边界,回到顶部
if self.y >= WINDOW_HEIGHT:
self.x = random.randint(0, random.randint(0, WINDOW_WIDTH - 100))
self.y = 0
def plane_down_anim(self):
"""敌机被击中动画"""
if self.anim_index >= 21: # 动画执行完
self.anim_index = 0
self.img = pygame.image.load(
"E:/飞机大战/img-plane_%d.png" % random.randint(1, 7))
self.x = random.randint(0, WINDOW_WIDTH - 100)
self.y = 0
self.is_hited = False
return
elif self.anim_index == 0:
self.hit_sound.play()
self.img = pygame.image.load(
"E:/飞机大战/bomb-%d.png" % (self.anim_index // 3 + 1))
self.anim_index += 1
def display(self):
"""贴图"""
if self.is_hited:
self.plane_down_anim()
self.window.blit(self.img, (self.x, self.y))
class HeroPlane:
def __init__(self, img_path, x, y, window):
self.img = pygame.image.load(img_path) # 图片对象
self.x = x # 飞机坐标
self.y = y
self.window = window # 飞机所在的窗口
self.bullets = [] # 记录该飞机发出的所有子弹
self.is_hited = False
self.is_anim_down = False
self.anim_index = 0
def is_hit_enemy(self, enemy):
if pygame.Rect.colliderect(
pygame.Rect(self.x, self.y, 120, 78),
pygame.Rect(enemy.x, enemy.y, 100, 68)
): # 判断是否交叉
return True
else:
return False
def plane_down_anim(self):
"""敌机被击中动画"""
if self.anim_index >= 21: # 动画执行完
self.is_hited = False
self.is_anim_down = True
return
self.img = pygame.image.load(
"E:/飞机大战/bomb-%d.png" % (self.anim_index // 3 + 1))
self.anim_index += 1
def display(self):
"""贴图"""
for enemy in enemy_list:
if self.is_hit_enemy(enemy):
enemy.is_hited =