Python 飞机大战游戏

#崔,2019.03.08
设定己方飞机可以同时发射三发导弹且相互独立,轰击敌方血量不同(两侧小子弹为1伤,主导弹为3伤),己方飞机血量设为50。
设定敌方飞机可以左右循环移动,子弹为1伤,飞机血量设为100。
小飞机无血量,不会发射导弹,撞向己方飞机会产生3伤的伤害

import pygame,os
from pygame.locals import *
import random
import time
class HeroBulleto():
    def __init__(self,x1,y1,windows):
        self.x1=x1
        self.y1=y1
        self.windows=windows
        self.pic1=pygame.image.load('feiji\\bullet.png')
        self.pic2= pygame.image.load('feiji\\bullet.png')
        self.pic3 = pygame.image.load('feiji\\bomb2.png')
    def draw(self):
        self.windows.blit(self.pic1,(self.x1,self.y1))
        self.move()
    def move(self):
        self.x1-=1
        self.y1-=3


class HeroBullettr():
    def __init__(self, x3, y3, windows):
        self.x3 = x3
        self.y3 = y3
        self.windows = windows
        self.pic3 = pygame.image.load('feiji\\bomb2.png')

    def draw(self):
        self.windows.blit(self.pic3, (self.x3, self.y3))
        self.move()

    def move(self):
        self.y3 -= 3

class HeroBullettw():
    def __init__(self,  x2, y2, windows):
        self.x2 = x2
        self.y2 = y2
        self.windows = windows
        self.pic2 = pygame.image.load('feiji\\bullet.png')

    def draw(self):
        self.windows.blit(self.pic2, (self.x2, self.y2))
        self.move()
    def move(self):
        self.x2 += 1
        self.y2 -= 3

class EnemyBullet():
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows
        self.pic = pygame.image.load(getpath('enemybiu.png'))
        # self.diji= pygame.image.load(getpath('091534_5027005851268.png'))
    def draw(self):
        self.windows.blit(self.pic, (self.x, self.y))
        self.move()
    def move(self):
        self.y += 1
class Enemyfeiji():
    def __init__(self, x, y, windows):
        self.x = x
        self.y = y
        self.windows = windows
        self.diji= pygame.image.load(getpath('091534_5027005851268.png'))
    def draw(self):
        self.windows.blit(self.diji, (self.x, self.y))
        self.move()
    def move(self):
        x=random.randint(-100,100)
        if x==-2 or x==2:
            self.x+=x
        self.y += 1

def getpath(path):
    return os.path.join('...\workpy\\feiji',path)#获得素材库路径

class Heroplan():
    tima=11
    def __init__(self,x,y,windows):
        self.x=x
        self.y=y
        self.windows=windows
        self.nomalImagelist=['091535_6378052234649.png','091535_6378052234640.png']
        self.namalIndex=0
        self.boomImagelist=['hero_blowup_n2.png','hero_blowup_n3.png','hero_blowup_n4.png']
        self.boomIndex=0
        self.isboom=False
        self.biulist=[]
        self.biulist1=[]
        self.biulist2=[]
        self.live=50

    def draw(self):
        if self.isboom==False:
            pic=pygame.image.load(getpath(self.nomalImagelist[self.namalIndex]))
            self.windows.blit(pic,(self.x-(self.namalIndex),self.y))

            self.namalIndex=(self.namalIndex+1)%len(self.nomalImagelist)
        else:
            if self.boomIndex == len(self.boomImagelist):
                time.sleep(1)
                exit(0)
            pic = pygame.image.load(getpath(self.boomImagelist[self.boomIndex]))
            windows.blit(pic, (self.x, self.y))
            self.boomIndex = (self.boomIndex + 1)
            time.sleep(0.5)
        for biu in self.biulist:
            biu.draw()
            self.biulist.remove(biu) if  biu.y1 < 0 else ''
        for biu1 in self.biulist1:
            biu1.draw()
            self.biulist1.remove(biu1) if biu1.y2 < 0 else ''
        for biu2 in self.biulist2:
            biu2.draw()
            self.biulist2.remove(biu2) if biu2.y3 < 0 else ''
    def dealevent(self,eventlist):
        for event in eventlist:
            if event.type==QUIT:
                exit(0)
            key_pressed = pygame.key.get_pressed()
            if key_pressed[K_w] or key_pressed[K_UP]:
                self.y = self.y - 5 if self.y > 5 else 0
            if key_pressed[K_s] or key_pressed[K_DOWN]:
                self.y = self.y + 5 if self.y < 565 else 570
            if key_pressed[K_a] or key_pressed[K_LEFT]:
                self.x = self.x - 5 if self.x > 5 else 0
            if key_pressed[K_d] or key_pressed[K_RIGHT]:
                self.x = self.x + 5 if self.x < 375 else 380
            if key_pressed[K_SPACE]:
                if Heroplan.tima>10:
                    onebullet  = HeroBulleto (self.x - 2, self.y + 23,windows)
                    onebullet3 = HeroBullettw(self.x + 81, self.y + 23,windows)
                    onebullet2 = HeroBullettr(self.x + 40, self.y - 40,windows)
                    self.biulist.append(onebullet)
                    self.biulist1.append(onebullet3)
                    self.biulist2.append(onebullet2)
                    Heroplan.tima=0
                else:
                    Heroplan.tima+=1
    def pzjc(self, bList,bList1):
        heroRect = Rect(self.x, self.y, 117, 80)
        for biu in bList:
            biuRect1 = Rect(biu.x, biu.y, 22, 22)
            if biuRect1.colliderect(heroRect):
                self.live -= 1
                print("Hero:",self.live)
                if self.live <= 0:
                    self.isboom = True
                enemyplan.biulist.remove(biu)
        for biu1 in bList1:
            biuRect2 = Rect(biu1.x, biu1.y, 50, 38)
            if biuRect2.colliderect(heroRect):
                self.live -= 3
                print("Hero",self.live)
                if self.live <= 0:
                    self.isboom = True
                enemyplan.plan.remove(biu1)

class Enemyplan():
    t=10
    count=1
    def __init__(self, x, y, windows,dic):
        self.x = x
        self.y = y
        self.dic=dic
        self.windows = windows
        self.nomalImagelist = ['091534_3025160074234.png',]
        self.namalIndex = 0
        self.boomImagelist = ['enemy0_down2.png','enemy0_down3.png','enemy0_down4.png']
        self.boomIndex = 0
        self.isboom = False
        self.biulist = []
        self.plan=[]
        self.live=100
        self.m= 0
    def draw(self):
        if self.isboom == False:
            pic = pygame.image.load(getpath(self.nomalImagelist[self.namalIndex]))
            self.windows.blit(pic, (self.x - ( self.namalIndex), self.y))
            self.namalIndex = (self.namalIndex + 1) % len(self.nomalImagelist)
            self.move()
            self.fire()
            self.m=random.randint(0, 440)
            if Enemyplan.t>5:
                self.feiji()
                Enemyplan.t = 0
            else:
                Enemyplan.t += 1
            for biu in self.biulist:
                biu.draw()
                self.biulist.remove(biu) if biu.y < 0  else ''
            for biu1 in self.plan:
                biu1.draw()
                self.plan.remove(biu1) if biu1.y < 0 else ''


        else:
            if self.boomIndex == len(self.boomImagelist):
                time.sleep(1)
                exit(0)
            pic = pygame.image.load(getpath(self.boomImagelist[self.boomIndex]))
            windows.blit(pic, (self.x, self.y))
            self.boomIndex = (self.boomIndex + 1)
            time.sleep(0.5)

    def move(self):
        if self.x <= 0:
            self.dic = '→'
        if self.x >=363:
            self.dic = '←'
        if self.dic == '←':
            self.x -=1
        if self.dic == '→':
            self.x +=1

    def fire(self):
        x = random.randint(0, 120)
        if x == 14 or x == 60:
            onebullet=EnemyBullet(self.x+69//2,self.y+70,windows)
            self.biulist.append(onebullet)
    def feiji(self):
        x=random.randint(0,120)
        if x==30 or x==90:
            twobullet=Enemyfeiji(self.m,0,windows)
            self.plan.append(twobullet)


    def pzjc(self, bList,bList1,bList2):
        dijiRect = Rect(self.x, self.y, 117, 80)
        for biu in bList:
            biuRect1 = Rect(biu.x1, biu.y1, 22, 22)
            if biuRect1.colliderect(dijiRect):
                self.live-= 1
                print(self.live)
                if self.live<= 0:
                    self.isboom = True
                heroplan.biulist.remove(biu)
        for biu1 in bList1:
            biuRect2 = Rect(biu1.x2, biu1.y2, 22, 22)
            if biuRect2.colliderect(dijiRect):
                self.live-= 1
                print(self.live)
                if self.live<= 0:
                    self.isboom = True
                heroplan.biulist1.remove(biu1)
        for biu2 in bList2:
            biuRect3 = Rect(biu2.x3, biu2.y3, 22, 48)
            if biuRect3.colliderect(dijiRect):
                self.live-= 3
                print(self.live)
                if self.live<= 0:
                    self.isboom = True
                heroplan.biulist2.remove(biu2)

windows=pygame.display.set_mode((480,650),0,32)
pygame.display.set_caption('打飞机')
icon=pygame.image.load(getpath('icon72x72.png'))
pygame.display.set_icon(icon)
bj=pygame.image.load(getpath('579b1fd07753b.png'))
heroplan=Heroplan(480//2-100//2,680-110,windows)
enemyplan=Enemyplan(480//2-117//2,0,windows,'←')
pygame.key.set_repeat(20, 30)
while True:
    windows.blit(bj,(0,0))
    heroplan.draw()
    enemyplan.draw()
    enemyplan.pzjc(heroplan.biulist,heroplan.biulist1,heroplan.biulist2)
    heroplan.pzjc(enemyplan.biulist,enemyplan.plan)
    heroplan.dealevent(pygame.event.get())
    pygame.display.update()

己方飞机在下方,敌机boss水平运动,小飞机为敢死队飞机

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
好的,以下是一个简单的 Python 飞机大战游戏示例代码: ```python import pygame import sys import random # 初始化pygame pygame.init() # 设置屏幕大小和标题 screen_width = 480 screen_height = 700 screen = pygame.display.set_mode((screen_width, screen_height)) pygame.display.set_caption('飞机大战') # 加载背景图片 bg_img = pygame.image.load('images/background.png').convert() # 加载飞机图片和位置 player_img = pygame.image.load('images/player.png').convert_alpha() player_rect = player_img.get_rect() player_rect.left = (screen_width - player_rect.width) // 2 player_rect.top = screen_height - player_rect.height - 50 # 加载敌机图片和位置 enemy_img = pygame.image.load('images/enemy.png').convert_alpha() enemy_rect = enemy_img.get_rect() enemy_rect.left = random.randint(0, screen_width - enemy_rect.width) enemy_rect.top = -enemy_rect.height enemy_speed = 1 # 加载子弹图片和位置 bullet_img = pygame.image.load('images/bullet.png').convert_alpha() bullet_rect = bullet_img.get_rect() bullet_speed = 5 bullet_list = [] # 加载字体 font = pygame.font.Font(None, 36) # 设置计分和生命值 score = 0 life = 3 # 游戏主循环 while True: # 处理游戏退出事件 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() sys.exit() # 处理键盘事件 key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_LEFT]: player_rect.left -= 5 elif key_pressed[pygame.K_RIGHT]: player_rect.left += 5 elif key_pressed[pygame.K_SPACE]: bullet_list.append([player_rect.left + player_rect.width // 2 - bullet_rect.width // 2, player_rect.top - bullet_rect.height]) # 移动敌机和子弹 enemy_rect.top += enemy_speed for b in bullet_list: b[1] -= bullet_speed # 检测敌机和子弹是否碰撞 for b in bullet_list: if enemy_rect.colliderect(pygame.Rect(b[0], b[1], bullet_rect.width, bullet_rect.height)): bullet_list.remove(b) enemy_rect.left = random.randint(0, screen_width - enemy_rect.width) enemy_rect.top = -enemy_rect.height enemy_speed += 0.1 score += 10 # 检测敌机是否碰撞到玩家 if enemy_rect.colliderect(player_rect): life -= 1 enemy_rect.left = random.randint(0, screen_width - enemy_rect.width) enemy_rect.top = -enemy_rect.height enemy_speed = 1 # 绘制背景、玩家、敌机、子弹、分数和生命值 screen.blit(bg_img, (0, 0)) screen.blit(player_img, player_rect) screen.blit(enemy_img, enemy_rect) for b in bullet_list: screen.blit(bullet_img, (b[0], b[1])) score_text = font.render('Score: ' + str(score), True, (255, 255, 255)) life_text = font.render('Life: ' + str(life), True, (255, 255, 255)) screen.blit(score_text, (10, 10)) screen.blit(life_text, (screen_width - life_text.get_width() - 10, 10)) # 更新屏幕 pygame.display.update() # 检测生命值是否为0,如果是则结束游戏 if life == 0: gameover_text = font.render('Game Over', True, (255, 0, 0)) screen.blit(gameover_text, ((screen_width - gameover_text.get_width()) // 2, (screen_height - gameover_text.get_height()) // 2)) pygame.display.update() pygame.time.delay(3000) pygame.quit() sys.exit() ``` 在运行之前,需要先准备好游戏所需的图片资源,并将其放在与代码同级的 images 文件夹中。此外,还需要安装 Pygame 库。
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值