#encoding=utf-8
import pygame
import time
import random
from pygame.locals import *
class Base(object):
def __init__(self,x,y,imageName):
self.x=x
self.y=y
self.imageName=imageName
self.image = pygame.image.load(self.imageName).convert()
def display(self):
screen.blit(self.image,(self.x,self.y))
class Plane(Base):
def __init__(self, screen ,x,y,imageName,planeName):
Base.__init__(self,x,y,imageName)
self.screen=screen
self.bulletList = []
self.planeName = planeName
def display(self):
Base.display(self)
class EnemyPlane(Plane):
def __init__(self,screen):
Plane.__init__(self,screen,0,0,"./feiji/enemy-3.gif","Enemy")
self.directioin = "right" # right表示向右 left表示向左
self.speed = random.randint(1, 5)
def move(self):
if self.directioin == "right":
self.x += self.speed
elif self.directioin == "left":
self.x -= self.speed
# 到达另外一个边界时,需要反转方向
if self.x > 480:
self.directioin = "left"
elif self.x < 0:
self.directioin = "right"
def shoot(self):
shootFlagList = [2, 6]
shootFlag = random.randint(1, 100)
if shootFlag in shootFlagList:
self.bulletList.append(Bullet(self.screen, self.planeName, self.x, self.y))
#print("x:%d,y:%d"%(self.x, self.y))
def display(self):
Plane.display(self)
#print(self.bulletList)
for bullett in self.bulletList:
if bullett.y<=700:
bullett.display()
bullett.move()
global hero
# 以中点为心
if ((bullett.x - hero.x - 40) ** 2 + (bullett.y - hero.y - 40) ** 2) ** 0.5 < 40 and bullett.baozhaflag==0:
bullett.baozhaflag = 1
global score
if score>20:
score-=20
else :
score=0
global flagg
flagg = 1
print(hero.x, hero.y)
imageName = "./feiji/hero_blowup_n3.gif"
im = hero.image
hero.image = pygame.image.load(imageName).convert()
print("END")
else :
self.bulletList.remove(bullett)
class playerPlane(Plane):
def __init__(self,screen):
Plane.__init__(self,screen,230,600,"./feiji/hero.gif","player")
self.speed = 20
def display(self):
Plane.display(self)
#print(self.bulletList)
for bullett in self.bulletList:
if bullett.y>=0:
bullett.display()
bullett.move()
global enemy
if ((enemy.x + 40-bullett.x ) ** 2 + ( enemy.y + 80-bullett.y) ** 2) ** 0.5 < 40 and bullett.baozhaflag==0:
bullett.baozhaflag=1
global escore
if escore>0:
escore-=20
global flagge
flagge = 1
print(enemy.x, enemy.y)
imageName = "./feiji/enemy2_down1.gif"
im = enemy.image
enemy.image = pygame.image.load(imageName).convert()
print("END")
else :
self.bulletList.remove(bullett)
def moveRight(self):
if self.x >= 0 and self.x <= 420:
self.x += self.speed
def moveLeft(self):
if self.x <= 480 and self.x >= 20:
self.x -= self.speed
def sheBullet(self):
bui = Bullet(self.screen, "player",self.x+40,self.y-4)
self.bulletList.append(bui)
#导弹类
class Bullet(Base):
def __init__(self,screen,bulletName,x,y):
self.bulletName=bulletName
imageName1 = "./feiji/bullet-1.gif"
self.baozhaflag=0
if bulletName=="player":
imageName1="./feiji/bullet-3.gif"
Base.__init__(self, x, y, imageName1)
def move(self):
if self.bulletName=="player":
self.y -= 2
else :
self.y+=2
def display(self):
Base.display(self)
if __name__=='__main__':
score=200
escore=100
#1.创建一个窗口
screen = pygame.display.set_mode((480,700),0,32)
#2.创建一个图片
background=pygame.image.load("./feiji/background.png").convert()
color_red = (255, 0, 0)
color_green = (0, 255, 0)
color_blue = (0, 0, 255)
print(pygame.font.get_fonts())
pygame.font.init()
#font = pygame.font.SysFont(None, 48)
# 使用系统字体
fontObj3 = pygame.font.SysFont('arial', 20)
# 加粗
fontObj3.set_bold(True)
# 斜体
fontObj3.set_italic(True)
# 文字具有蓝色背景
textSurfaceObj3 = fontObj3.render("敌方血量:"+str(score), True, color_red, color_blue)
textRectObj3 = textSurfaceObj3.get_rect()
textRectObj3.center = (60, 510)
# 文字具有蓝色背景
textSurfaceObj2 = fontObj3.render("我方血量:" + str(escore), True, color_red, color_blue)
textRectObj2 = textSurfaceObj2.get_rect()
textRectObj2.center = (60, 10)
#创建玩家飞机
#hero = pygame.image.load("./feiji/hero.gif").convert()
global flagg
flagg=0
flagge=0
global hero
hero=playerPlane(screen)
global enemy
enemy = EnemyPlane(screen)
#3.图片到背景去
imm = hero.image
imme=enemy.image
while True:
screen.blit(background,(0,0))
textSurfaceObj3 = fontObj3.render("Me :"+str(score), True, color_red, color_blue)
textRectObj3 = textSurfaceObj3.get_rect()
textRectObj3.center = (60, 510)
# 文字具有蓝色背景
textSurfaceObj2 = fontObj3.render("Enemy :" + str(escore), True, color_red, color_blue)
textRectObj2 = textSurfaceObj2.get_rect()
textRectObj2.center = (60, 10)
screen.blit(textSurfaceObj3, textRectObj3)
screen.blit(textSurfaceObj2, textRectObj2)
#screen.blit(hero, (x, 600))
#hero.display()
# 获取事件,比如按键等
for event in pygame.event.get():
# 判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
# 判断是否是按下了键
elif event.type == KEYDOWN:
# 检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero.moveLeft()
# 检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero.moveRight()
# 检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
hero.sheBullet()
# 更新需要显示的内容
hero.display()
if flagg==1:
hero.image=imm
flagg=0
# 让敌机自己移动以及发射子弹
enemy.move()
enemy.shoot()
enemy.display()
if flagge==1:
enemy.image=imme
flsgge=0
pygame.display.update()
time.sleep(0.01)
没有合适的资源?快使用搜索试试~ 我知道了~
python飞机大战素材加源码
共72个文件
png:58个
gif:11个
py:1个
5星 · 超过95%的资源 需积分: 20 29 下载量 162 浏览量
2018-08-08
18:58:25
上传
评论
收藏 1.5MB RAR 举报
温馨提示
搜集了开发小程序能用到的所有的飞机大战图片,包含Python开发源码,希望 对大家学习python能有所帮助
资源推荐
资源详情
资源评论
收起资源包目录
飞机大战 (1).rar (72个子文件)
飞机大战
woplane.py 8KB
feiji
enemy1_down2.png 11KB
resume_nor.png 1KB
gameover.png 58KB
loading.png 154KB
icon72x72.png 12KB
hero2.png 12KB
hero_blowup_n1.png 15KB
background.png 36KB
btn_finish.png 9KB
enemy0_down2.png 4KB
game_resume_pressed.png 2KB
quit_sel.png 2KB
bullet1.png 490B
._.DS_Store 4KB
enemy2_down5.png 74KB
hero_blowup_n3.png 18KB
bomb-2.gif 2KB
bullet.png 23KB
resume_sel.png 1KB
game_loading3.png 6KB
game_pause_pressed.png 2KB
enemy2_down6.png 9KB
enemy2_down1.gif 18KB
restart_nor.png 2KB
hero_blowup_n3.gif 6KB
bullet2.png 498B
enemy0_down3.png 5KB
progress.png 28KB
hero1.png 13KB
enemy1.png 8KB
bg.png 418KB
game_resume_nor.png 2KB
enemy1_down1.png 10KB
enemy2_down1.png 47KB
enemy0_down4.png 2KB
button_nor.png 29KB
prop_type_0.png 7KB
enemy2_down4.png 66KB
game_loading1.png 3KB
restart_sel.png 2KB
enemy0_down4.png.baiduyun.uploading.cfg 3KB
game_pause_nor.png 2KB
enemy1_down4.png 3KB
enemy1_hit.png 9KB
bullet-3.gif 401B
bomb-1.gif 3KB
enemy2_down2.png 52KB
New Atlas.png 7KB
game_loading4.png 96B
bomb.png 5KB
enemy0.png 3KB
bullet-2.gif 151B
plane.png 24KB
quit_nor.png 2KB
name.png 32KB
prop_type_1.png 7KB
enemy-1.gif 2KB
enemy-3.gif 14KB
enemy2.png 42KB
enemy-2.gif 3KB
button_p.png 28KB
hero_blowup_n2.png 16KB
hero.gif 4KB
bullet-1.gif 122B
enemy0_down1.png 3KB
enemy2_hit.png 45KB
hero_blowup_n4.png 3KB
enemy1_down3.png 13KB
game_loading2.png 5KB
enemy2_n2.png 42KB
enemy2_down3.png 59KB
共 72 条
- 1
资源评论
- wangquan_5202020-02-20程序完美,值得学习
~哦~豁~
- 粉丝: 14
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功