8.5 python日学 飞机大战

这个版本是封装之后的,没有分成多个模块
首先说一下思路:
1.飞机和敌机的生成
2.我机的移动
3.敌机的移动
4.子弹的发射
5.碰撞检测

功能并不完善,只有简单,移动,射击,以及碰撞检测

飞机和敌机是png的图片形式,因此飞机的移动是坐标的加减,该程序没有写飞机的上下移动。
通过遍历列表的形式,来达到飞机‘冒烟’的动画表现。
由于图片的限制,碰撞检测会看起来不是那么美好(即使子弹打到图片的空白处,也会产生碰撞检测),这算一个小毛病吧,多加几行代码也可以解决。
敌机的子弹是通过随机数的判断发射的,因为是永真循环,所以可能会出现敌机突然发射一大堆子弹的情况。
用列表来将产生的子弹遍历绘制,否则会出现飞机只有一颗子弹,或者子弹在你机头来回调动(反复横跳?)
最简单的飞机大战,不能再简陋了。

下面是全部代码:
import pygame
import os
import random
import time
from pygame.locals import *
def getpath(path):
return os.path.join(r’E:\pywork\pygame\img’,path)

class Plane():
def init(self, x, y, windows): # 设置飞机放置的x,y坐标
self.x = x
self.y = y
self.windows = windows
self.normalImageIndex = 0
self.boomimageIndex = 0#通过下标设置显示的飞机图片
self.isboom = False
self.biulist = []
def draw(self):
if not self.isboom:
image=pygame.image.load(getpath(self.normalImageList[self.normalImageIndex]))
self.normalImageIndex=(self.normalImageIndex+1)%len(self.normalImageList)
self.windows.blit(image,(self.x,self.y))
else:
if len(self.boomimagelist)==self.boomimageIndex:
time.sleep(0.5)
exit(0)
image=pygame.image.load(getpath(self.boomimagelist[self.boomimageIndex]))
self.boomimageIndex+=1
self.windows.blit(image,(self.x,self.y))
time.sleep(0.3)

class HeroPlane(Plane):
def init(self,x,y,windows):#设置飞机放置的x,y坐标
self.normalImageList=[‘hero1.png’,‘hero2.png’]#切换显示飞机图片列表
self.boomimagelist=[‘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()
if not self.isboom:
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.typeQUIT:
exit(0)
elif event.type
KEYDOWN:
if event.keyK_LEFT:
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=HeroPlaneFire(self.x+100//2-22//2,self.y-22,self.windows)
self.biulist.append(zd)
def pzjc(self,blist):
hRect=Rect(self.x,self.y,100,124)
for zd in blist:
zdrect=Rect(zd.x,zd.y,9,21)
if hRect.colliderect(zdrect):
self.isboom = True
blist.remove(zd)

class EnemyPlane(Plane):
def init(self,x,y,windows):
self.direc=‘left’
self.normalImageList =[‘enemy1.png’]
self.boomimagelist=[‘enemy1_down1.png’,‘enemy1_down2.png’,‘enemy1_down3.png’,‘enemy1_down4.png’,]
super().init(x,y,windows)
def draw(self):
super().draw()
if not self.isboom:
self.move()
self.fire()
for zd in self.biulist:
zd.draw()
self.biulist.remove(zd) if zd.y>652 else ‘’

def move(self):
    if self.direc == 'left':
        self.x -= 2
        if self.x <= 0:
            self.direc = 'right'
    elif self.direc == 'right':
        self.x += 2
        if self.x >= 480 - 69:
            self.direc = 'left'
def fire(self):
    x=random.randint(1,100)
    if x==3:
        zd=EnemyPlaneFire(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.isboom=True
            blist.remove(zd)

class Fire():
def init(self,x,y,windows):
self.x=x
self.y=y
self.windows=windows
def draw(self):
self.windows.blit(self.image,(self.x,self.y))
self.move()

class HeroPlaneFire(Fire):
def init(self,x,y,windows):
self.image=pygame.image.load(getpath(‘bullet.png’))
super().init(x,y,windows)
def move(self):
self.y-=5
self.windows.blit(self.image,(self.x,self.y))

class EnemyPlaneFire(Fire):
def init(self,x,y,windows):
self.image=pygame.image.load(getpath(‘bullet1.png’))
super().init(x,y,windows)
def move(self):
self.y+=5

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(30,30)
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()
heroplane.dealEvent(pygame.event.get())
enemyplane.draw()
enemyplane.pzjc(heroplane.biulist)
heroplane.pzjc(enemyplane.biulist)

pygame.display.update()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值