赞
踩
目录
应用到的库:
import pygame
import random一、框架
1、创建界面宽度
2、背景图片
3、标题
4、背景音乐
二、键盘控制
1、接收键盘控制-自己的方向和射击控制
三、创建类
1、飞机基类
a、初始化
屏幕、图片装载、子弹列表
b、显示
图片、子弹越界问题
2、子弹基类
a、初始化
屏幕、飞机类型、图片装载
b、移动
判断飞机类型移动
c、显示
图片
d、判断
是否越界3、Hero飞机类-继承飞机、子弹基类
a、初始化
xy坐标,继承基类
b、左移动
c、右移动
d、发射子弹
引用子弹基类实例对象,通过列表保存4、Enemy飞机类-继承飞机、子弹基类
a、初始化
xy坐标,继承基类,初始方向-右
b、移动
c、发射子弹
通过random随机发射
引用子弹基类实例对象,通过列表保存
- def main():
- #界面
- screen=pygame.display.set_mode((350,500),depth=32)
- background=pygame.image.load('./feiji/background.png')
- pygame.display.set_caption('飞机大作战')
- #背景音乐
- pygame.mixer.init()
- pygame.mixer.music.load('./feiji/background.mp3')
- pygame.mixer.music.play(-1)
- #实例化对象
- hero=HeroPlane(screen)
- enemy=EnemyPlane(screen)
- while True:
- screen.blit(background,(0,0))
- hero.display()
- enemy.display()
- enemy.move()
- enemy.sheBullet()
- keyControl(hero)
- pygame.display.update()
- pygame.time.Clock().tick(120)
-
- pass
- def keyControl(HeroObj):
- eventList=pygame.event.get()
- for event in eventList:
- if event.type==QUIT:
- print('退出!')
- exit()
- pass
- elif event.type==KEYDOWN:
- if event.key==K_a or event.key==K_LEFT:
- print('left')
- HeroObj.moveleft()
- elif event.key==K_d or event.key==K_RIGHT:
- print('right')
- HeroObj.moveright()
- elif event.key==K_SPACE:
- print('空格-发射')
- HeroObj.sheBullet()
- pass
- pass
- pass
- pass
- class BasePlane(object):
- def __init__(self,screen,imagePath):
- self.screen=screen
- self.image=pygame.image.load(imagePath)
- self.bullentList=[]
- pass
- def display(self):
- self.screen.blit(self.image,(self.x,self.y))
- #子弹越界删除
- needDelItemList=[]
- for item in self.bullentList:
- if item.judge():
- needDelItemList.append(item)#先保存
- for i in needDelItemList:
- self.bullentList.remove(i)#再删除
- #发射子弹
- for bullet in self.bullentList:
- bullet.display()
- bullet.move()
- pass
- class CommonBullet(object):
- def __init__(self,x,y,screen,bulletType):
- self.screen=screen
- self.type=bulletType
- #判断飞机类型-再初始化子弹坐标
- if self.type=='hero':
- self.x=x+13
- self.y=y-20
- self.imagePath='./feiji/bullet.png'
- elif self.type=='enemy':
- self.x=x
- self.y=y+10
- self.imagePath='./feiji/bullet1.png'
- pass
- self.image=pygame.image.load(self.imagePath)
- pass
- #判断飞机类型-再根据类型决定子弹走向
- def move(self):
- if self.type=='hero':
- self.y-=2
- elif self.type=='enemy':
- self.y+=2
- pass
- def display(self):
- self.screen.blit(self.image,(self.x,self.y))
- pass
- def judge(self):
- if self.y>=500 or self.y<=0:
- return True
- else:
- return False
- pass
- class HeroPlane(BasePlane):
- def __init__(self,screen):
- super().__init__(screen,'./feiji/hero.png')
- self.x=150
- self.y=450
- pass
- def moveleft(self):
- if self.x>=0:
- self.x-=10
- pass
- def moveright(self):
- if self.x<=310:
- self.x+=10
- pass
- def sheBullet(self):
- newBullet=CommonBullet(self.x,self.y,self.screen,'hero')
- self.bullentList.append(newBullet)
- pass
- pass
- class EnemyPlane(BasePlane):
- def __init__(self,screen):
- super().__init__(screen,'./feiji/enemy0.png')
- self.direction='right'
- self.x=0
- self.y=0
- pass
- def move(self):
- if self.direction=='right':
- self.x+=1
- elif self.direction=='left':
- self.x-=1
- if self.x>=330:
- self.direction='left'
- elif self.x<=0:
- self.direction='right'
- pass
- def sheBullet(self):
- num=random.randint(1,60)
- if num==3:
- newBullet=CommonBullet(self.x,self.y,self.screen,'enemy')
- self.bullentList.append(newBullet)
- pass
- pass
代码及其素材: 提取码:vwk0https://pan.baidu.com/s/12QaRLi7-Ogh1Oh93HcCH4A
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。