一、需求
设计一个简单的《飞机大战》游戏,实现我方飞机和敌方飞机均可左右移动,并且可以射击子弹,而且被击中后会退出游戏
二、效果图
被击中后:
三、代码
import os
import time
import random
import pygame
from pygame.locals import *
def getPath(path):# 创建一个传入路径的函数,返回路径,然后避免以后传东西的时候路径太长
return os.path.join("C:\\Users\\Administrator\Desktop\IT研究院-Python\\New_Stydy\\img\\", path)
#飞机和子弹共同类
class Father():
def __init__(self, x, y, windows):
self.x = x
self.y = y
self.windows = windows
#子弹父类
class BiuFather(Father):
def draw(self):
self.windows.blit(self.image, (self.x, self.y))
self.move()
#飞机父类
class Plane(Father):
def __init__(self, x, y, windows):
super().__init__(x,y,windows)
self.normalImageListIndex = 0
self.bombImageIndex = 0
self.isBomb = False
self.biuList = []
def draw(self):
if not self.isBomb:
image = pygame.image.load(getPath(self.normalImageList[self.normalImageListIndex]))
self.normalImageListIndex = (self.normalImageListIndex + 1) % len(self.normalImageList) # 切换飞机显示图片
self.windows.blit(image, (self.x, self.y))
else:
# 敌方飞机爆炸
if len(self.bombImageList) == self.bombImageIndex:
time.sleep(0.5)
exit(0)
image = pygame.image.load(getPath(self.bombImageList[self.bombImageIndex]))
self.bombImageIndex += 1
self.windows.blit(image, (self.x, self.y))
time.sleep(0.3)
#我方子弹
class Biu(BiuFather):
def __init__(self, x, y, windows):
super().__init__(x, y, windows)
self.image = pygame.image.load(getPath("bullet.png"))
def move(self):
self.y -= 5
#敌方子弹
class EnemyBiu(BiuFather):
def __init__(self, x, y, windows):
super().__init__(x, y, windows)
self.image = pygame.image.load(getPath("bullet1.png"))
def move(self):
self.y += 5 # 敌机子弹速度
class EnemyPlane(Plane):
def __init__(self, x, y, windows):
self.normalImageList = ['enemy1.png']
self.bombImageList = ['enemy1_down1.png', 'enemy1_down2.png', 'enemy1_down3.png', 'enemy1_down4.png'] # 敌机爆炸图片
super().__init__(x, y, windows)
self.direct = "左"
def draw(self):
super().draw()
if not self.isBomb:
self.move()
self.fire()
for zd in self.biuList:
zd.draw()
def move(self):
if self.direct == '左':
self.x -= 2
if self.x <= 0:
self.direct = " 右"
else:
self.x += 2
if self.x >= 480 - 69:
self.direct = "左"
def fire(self):
x = random.randint(1, 100)
if x == 3:
zd = EnemyBiu(self.x + 69 // 2 - 9 // 2, self.y + 89, self.windows)
self.biuList.append(zd)
def pzjc(self, bList):
eRect = Rect(self.x, self.y, 69, 89)
for zd in bList:
zdRect = Rect(zd.x, zd.y, 22, 22)
if zdRect.colliderect(eRect):
self.isBomb = True
bList.remove(zd)
class HeroPlane(Plane):
def __init__(self, x, y, windows):
self.normalImageList = ['hero1.png', 'hero2.png']
self.bombImageList = ['hero_blowup_n1.png', 'hero_blowup_n2.png', 'hero_blowup_n3.png',
'hero_blowup_n4.png'] # 我方飞机机爆炸图片
super().__init__(x, y, windows)
def draw(self):
super().draw()
for zd in self.biuList:
zd.draw()
self.biuList.remove(zd) if zd.y < 0 else '' # 子弹越界删除子弹
# 我方飞机碰撞检测
def pzjc(self, bList):
eRect = Rect(self.x, self.y, 100, 124)
for zd in bList:
zdRect = Rect(zd.x, zd.y, 9, 21)
if zdRect.colliderect(eRect):
self.isBomb = True
bList.remove(zd)
def dealEvent(self, eventList):
# 事件处理
for event in eventList:
if event.type == QUIT:
exit(0)
elif event.type == KEYDOWN:
if event.key == K_LEFT:
self.x = self.x - 5 if self.x > 5 else 0
elif event.key == K_RIGHT:
self.x = self.x + 5 if self.x < 480 - 100 - 5 else 480 - 100
elif event.key == K_SPACE:
zd = Biu(self.x + 100 // 2 - 22 // 2, self.y - 22, self.windows)
self.biuList.append(zd)
windows = pygame.display.set_mode((480, 652), 0, 32)
pygame.display.set_caption("飞机大战")
backGround = pygame.image.load(getPath("background.png"))
icon = pygame.image.load(getPath("icon72x72.png"))
pygame.display.set_icon(icon)
pygame.key.set_repeat(50, 10) # 第一个参数是键盘按下30ms后开始反应,第二个参数是按下30ms为抬起再触发一次新的按键事件
heroPlane = HeroPlane(480 // 2 - 100 // 2, 652 - 124, windows) # 贴上我方飞机图片
enemyPlane = EnemyPlane(480 // 2 - 69 // 2, 0, windows) # 贴上敌方飞机图片
while True:
windows.blit(backGround, (0, 0))
heroPlane.draw()
enemyPlane.draw()
enemyPlane.pzjc(heroPlane.biuList)
heroPlane.pzjc(enemyPlane.biuList)
heroPlane.dealEvent(pygame.event.get())
pygame.display.update()
四、图片素材包
https://download.csdn.net/download/qq_40558166/11474415?spm=1001.2014.3001.5503