飞机大战(二)

本文介绍了一个简单的二维飞机大战游戏实现,游戏使用Python的Pygame库构建。玩家可以控制英雄飞机发射子弹,同时游戏会生成敌方飞机进行攻击。文章详细展示了游戏的类结构,包括子弹类和飞机类的设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

子弹类:

import pygame,os
def getImage(image):
    return os.path.join("D:\\下载课件\\讲义\\IT研究院-Python\\New_Stydy\\img",image)
class BaseBiu():
    def __init__(self,x,y,windows,image):
        self.x=x
        self.y=y
        self.windows=windows
        self.image=pygame.image.load(getImage(image))
    def draw(self):
        self.windows.blit(self.image,(self.x,self.y))
        self.move()
class EnemyBiu(BaseBiu):
    def __init__(self,x,y,windows):
        super().__init__(x,y,windows,"bullet1.png")
    def move(self):
        self.y+=5
class HeroBiu(BaseBiu):
    def __init__(self,x,y,windows):
        super().__init__(x,y,windows,"bullet.png")
    def move(self):
        self.y-=5

飞机部件类

import pygame,time,random
from pygame.locals import *
from BiuClass import *
class BasePlane():
    def __init__(self,x,y,windows,normalImageList,bombImageList):
        self.x=x
        self.y=y
        self.windows=windows
        self.normalImageList=normalImageList
        self.normalImageIndex=0
        self.bombImageList=bombImageList
        self.bombImageIndex=0
        self.biuList=[]
        self.isBomb=False
    def draw(self):
        if self.isBomb==False:
            image=pygame.image.load(getImage(self.normalImageList[self.normalImageIndex]))
            self.normalImageIndex=(self.normalImageIndex+1)%len(self.normalImageList)
            self.windows.blit(image,(self.x,self.y))
        else:
            if self.bombImageIndex==len(self.bombImageList):
                time.sleep(0.8)
                exit(0)
            image=pygame.image.load(getImage(self.bombImageList[self.bombImageIndex]))
            self.bombImageIndex+=1
            self.windows.blit(image,(self.x,self.y))
            time.sleep(0.5)
class HeroPlane(BasePlane):
    def __init__(self,x,y,windows):
        normalImageList=["hero1.png","hero2.png"]
        bombImageList=["hero_blowup_n1.png","hero_blowup_n2.png","hero_blowup_n3.png","hero_blowup_n4.png"]
        super().__init__(x,y,windows,normalImageList,bombImageList)
    def draw(self):
        super().draw()
        for zd in self.biuList:
            zd.draw()
            self.biuList.remove(zd) if zd.y<0 else ""
    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_UP:
                    self.y=self.y-5 if self.y>5 else 0
                elif event.key==K_DOWN:
                    self.y=self.y+5 if self.y<=(520-124-5) else 520-124
                elif event.key==K_SPACE:
                    zd=HeroBiu(self.x+50-22//2,self.y-22,self.windows)
                    self.biuList.append(zd)
    def pzjc(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.isBomb=True
class EnemyPlane(BasePlane):
    def __init__(self,x,y,windows):
        normalImageList=["enemy1.png"]
        bombImageList=["enemy1_down1.png","enemy1_down2.png","enemy1_down3.png","enemy1_down4.png",]
        self.direct="左"
        super().__init__(x,y,windows,normalImageList,bombImageList)
    def draw(self):
        super().draw()
        for zd in self.biuList:
            zd.draw()
            self.biuList.remove(zd) if zd.y>520 else ""
        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.windows)
            self.biuList.append(zd)
    def pzjc(self,zdList):
        fjRect=Rect(self.x,self.y,69,89)
        for zd in zdList:
            zdRect=Rect(zd.x,zd.y,22,22)
            if zdRect.colliderect(fjRect):
                self.isBomb=True

运行类

import pygame
from BiuClass import *
from PlaneClass import *
window=pygame.display.set_mode((480,520),0,32)
pygame.display.set_caption("飞机大战")
icon=pygame.image.load(getImage("icon72x72.png"))
pygame.display.set_icon(icon)
bg=pygame.image.load(getImage("background.png"))
wofang=HeroPlane(480/2-100/2,520-124,window)
difang=EnemyPlane(480/2-69/2,0,window)
pygame.key.set_repeat(30,30)
while True:
    window.blit(bg,(0,0))
    wofang.draw()
    difang.draw()
    wofang.pzjc(difang.biuList)
    difang.pzjc(wofang.biuList)
    wofang.dealEvent(pygame.event.get())
    pygame.display.update()
    pass

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值