飞机大战

本文详细介绍了一个使用Python和Pygame库开发的飞机大战游戏。游戏包括英雄飞机和敌机的创建,子弹发射,碰撞检测,以及游戏循环等核心功能。通过控制英雄飞机躲避敌机和子弹,增加游戏的趣味性和挑战性。

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

import pygame,random,time,os
from pygame.locals import *
def getPic(path):
    #path只是文件名称,在这里是图片名称。加入到绝对路径上,写此函数抑郁不同环境下的修改
    return os.path.join('d:\\Desktop\\img',path)
class PlaneBase():
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows
        self.normalIndex = 0
        self.bombImageIndex = 0
        self.isBomb = False
        self.biuList = []
    def draw(self):
        if self.isBomb == False:
            pic = pygame.image.load(getPic(self.normalImageList[self.normalIndex]))
            self.windows.blit(pic, (self.x, self.y))
            self.normalIndex = (self.normalIndex + 1) % len(self.normalImageList)
        else:
            if self.bombImageIndex == len(self.bombImageList):
                time.sleep(1)
                exit(0)
            pic = pygame.image.load(getPic(self.bombImageList[self.bombImageIndex]))
            self.windows.blit(pic, (self.x, self.y))
            self.bombImageIndex = (self.bombImageIndex + 1)
            time.sleep(1)
class HeroPlane(PlaneBase):
    def __init__(self, x, y, windows):
        super().__init__(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']
    def draw(self):
        super().draw()
        for biu in self.biuList:
            biu.draw()
            self.biuList.remove(biu) if biu.y < 0 else ''
    def dealEvent(self,eventList):
        for event in eventList:
            if event.type==QUIT:
                exit(0)
            if 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<=375 else 380
                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<=640-124 else 650-124
                elif event.key==K_SPACE:
                    oneBiu=HeroBullet(self.x+39,self.y-22,windows)
                    self.biuList.append(oneBiu)
    def pzjc(self, bList):
        eRect = Rect(self.x, self.y, 100, 124)  # 我机
        for biu in bList:
            bRect = Rect(biu.x, biu.y, 9, 21)
            if bRect.colliderect(eRect):
                self.isBomb = True
class EnemyPlane(PlaneBase):
    def __init__(self,x,y,windows):
        super().__init__(x,y,windows)
        self.normalImageList = ['enemy0.png']
        self.bombImageList = ['enemy0_down1.png', 'enemy0_down2.png', 'enemy0_down3.png', 'enemy0_down4.png']
        self.isBomb = False
        self.direct='左'
    def draw(self):
        super().draw()
        self.move()
        self.fire()
        for biu in self.biuList:
            biu.draw()
            self.biuList.remove(biu) if biu.y>650 else ''
    def move(self):
        if self.direct == '左':
             self.x-=1
             if self.x <= 0:
                self.direct = '右'
        elif self.direct == '右':
            self.x += 1
            if self.x >= 480 - 51:
                self.direct = '左'
    def fire(self):
        x=random.randint(0,100)
        if x==7 or x==70:
            oneBiu=enemyBullet(self.x+51//2-9//2,self.y+39,windows)
            self.biuList.append(oneBiu)
    def pzjc(self,bList):
        eRect=Rect(self.x,self.y,51,39)#敌机
        for biu in bList:
            bRect=Rect(biu.x,biu.y,22,22)
            if bRect.colliderect(eRect):
                self.isBomb=True
class BulletBase():
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
    def draw(self):
        self.windows.blit(self.pic,(self.x,self.y))
        self.move()
class HeroBullet(BulletBase):
    def __init__(self, x, y, windows):
        super().__init__(x,y,windows)
        self.pic=pygame.image.load(getPic('bullet.png'))
    def move(self):
        self.y-=5
class enemyBullet(BulletBase):
    def __init__(self, x, y, windows):
        super().__init__(x, y, windows)
        self.pic=pygame.image.load(getPic('bullet1.png'))
    def move(self):
        self.y+=2
windows=pygame.display.set_mode((480,650),0,32)
pygame.display.set_caption('飞机大战')
icon=pygame.image.load(getPic('icon72x72.png'))
pygame.display.set_icon(icon)
bg=pygame.image.load(getPic('background.png'))
heroPlane=HeroPlane(480//2-100//2,650-124,windows)
enemyPlane=EnemyPlane(480//2-51//2,0,windows)
pygame.key.set_repeat(20,30)
while True:
    windows.blit(bg,(0,0))
    enemyPlane.pzjc(heroPlane.biuList)
    heroPlane.pzjc(enemyPlane.biuList)
    heroPlane.draw()
    enemyPlane.draw()
    heroPlane.dealEvent(pygame.event.get())
    pygame.display.update()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值