python飞机大战简单实现项目

#导入 游戏模块, 系统模块 ,时间模块 ,随机模块
import pygame,os,time,random
from pygame.locals import *
SCREEN_WIDE = 480                                                               #宽
SCREEN_HIGH = 852                                                               #高


#我方子弹
class Biu():
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.image = pygame.image.load(getImage('bullet2.png'))

    def draw(self):
        window.blit(self.image,(self.x,self.y))                                 #子弹坐标
        self.move()                                                             #移动

    def move(self):
        self.y -=10                                                             #移动步长


#敌机子弹
class EnemyBiu():
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.image = pygame.image.load(getImage('bullet1.png'))

    def draw(self):
        window.blit(self.image,(self.x,self.y))
        self.move()

    def move(self):
        self.y += 5


#敌方飞机
class EnemyPlane():
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.zhengchang = ['enemy1.png']  # 地方飞机图片列表
        self.zhengchang_Index = 0  # 列表内下标

        self.biuList = []  # 在hero内创建子弹回收列表

        self.direct = '左'

    def draw(self):
        image = pygame.image.load(getImage(self.zhengchang[self.zhengchang_Index]))         #通过下标打开图片
        self.zhengchang_Index = (self.zhengchang_Index+1)%len(self.zhengchang)              #下标余列表长度 0+1 % 列表长度2 =1  1+1 %列表长度 =0  一零一零奇偶循环
        window.blit(image,(self.x,self.y))

        for zd in self.biuList:  # 从列表取出子弹
            zd.draw()
            self.biuList.remove(zd) if zd.y > 852 else None  # 如果子弹超出屏幕范围则删除子弹
        self.move()
        self.fire()


#敌方飞机左右移动

    def move(self):
        if self.direct == '左':
            self.x -= 5
            if self.x <= 0:
                self.direct = '右'
        else:
            self.x += 5
            if self.x >= 480-69:
                self.direct = '左'

#敌方飞机随机开火

    def fire(self):
        x = random.randint(0,100)
        if x == 3 or x == 67:
            zd = EnemyBiu(self.x+69/2-9/2,self.y+89)
            self.biuList.append(zd)


#我方飞机
class HeroPlane():
    def __init__(self,x,y):
        self.x = x
        self.y = y
        self.zhengchang = ['hero1.png','hero2.png']                           #正常飞机图片列表
        self.zhengchang_Index = 0                                               #列表内下标

#我方爆炸图片

        self.boomImageList = ['hero_blowup_n1.png','hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png']
        self.boomImageIndex = 0
        self.isBoom = False

#子弹
        self.biuList = []                                                       #在hero内创建子弹回收列表

#画
    def draw(self):

        #如果没有爆炸就正常 图片循环

        if self.isBoom == False:
            image = pygame.image.load(getImage(self.zhengchang[self.zhengchang_Index]))         #通过下标打开图片
            self.zhengchang_Index = (self.zhengchang_Index+1)%len(self.zhengchang)              #下标余列表长度 0+1 % 列表长度2 =1  1+1 %列表长度 =0  一零一零奇偶循环
            window.blit(image,(self.x,self.y))                                                  #定位图标位置

        #否则就 循环 爆炸图片

        else:
            if self.boomImageIndex == len(self.boomImageList):
                time.sleep(0.8)
                exit(0)
            image = pygame.image.load(getImage(self.boomImageList[self.boomImageIndex]))
            self.boomImageIndex +=  1
            window.blit(image,(self.x,self.y))
            time.sleep(0.5)

        for zd in self.biuList:                                                             #从列表取出子弹
            zd.draw()
            self.biuList.remove(zd) if zd.y<0 else None                                     #如果子弹超出屏幕范围则删除子弹


    def dealEvent(self,evenList):                                               #处理按键反馈
        for event in evenList:                                                  #从下面传过来
            if event.type == QUIT:                                              #如果是退出
                exit(0)
            elif event.type == KEYDOWN:                                         #如果键盘按键
                if event.key == K_LEFT:                                         #如果是左键
                    self.x = self.x-10 if self.x>10 else 0
                elif event.key == K_RIGHT:                                      #如果是右键
                    self.x = self.x+10 if self.x<480-100-10 else 480-100
                elif event.key == K_UP:                                         #如果是上键
                    self.y = self.y - 10 if self.y >= 10 else 0
                elif event.key == K_DOWN:
                    self.y = self.y + 10 if self.y <= 852-124 else 852-124

                elif event.key == K_SPACE:                                      #如果按空格键  发射子弹
                    zd = Biu(self.x + 50 - 3.9,self.y - 22)                     #子弹在飞机头部的发射位置
                    self.biuList.append(zd)                                     #在子弹回收列表内添加子弹

#敌方子弹击中我方飞机
    def pen(self,dfZdList):
        jtRect = Rect(self.x+36,self.y,28,42)
        jsRect = Rect(self.x,self.y+42,100,124-42)
        for zd in dfZdList:
            zdRect = Rect(zd.x,zd.y,9,21)
            if zdRect.colliderect(jtRect) or zdRect.colliderect(jsRect):
                self.isBoom = True



def getImage(image):                                                            #传入图片的文件名
    return os.path.join('F:\\飞机大战\\飞机大战2\\新建文件夹\\img',image)    #os . path img 为图片目录的绝对路径  os.join 自动为图片前添加双斜杠 \\所以直接添加图片名即可

(此飞机大战项目只简单实现到 英雄飞机发射子弹 击中 地方飞机 爆炸 ,地方boos 飞机击爆我方飞机)

(图片为微信飞机大战image 图片)

注:本博客为学习中记录要点个人摘录及记录学习点滴,如有纰漏还请多多指教 :D

猜你喜欢

转载自blog.csdn.net/weixin_44689392/article/details/88806280