import pygame,sys
from src.plane import Ourplane
from src.enemyplane import SmallEnemy
from src.enemyplane import MiddleEnemy
from src.bullet import Bullet
bg_size=(480,652)
#申明四种颜色
color_black = (0,0,0)
color_green = (0,255,0)
color_red = (255,0,0)
color_white = (255,255,255)
#设置窗体大小
screen=pygame.display.set_mode(bg_size,0,32)
#设置窗体标题
pygame.display.set_caption("飞机大战")
#加载背景图片
background_1 = pygame.image.load("../material/image/background.png")
background_2 = pygame.image.load("../material/image/background.png")
#申明飞机类的对象
ourplane = Ourplane(bg_size)
#创建敌机方法
def add_small_enemys(group1,group2,num):
for i in range(num):
small_enemy = SmallEnemy(bg_size)
group1.add(small_enemy)
group2.add(small_enemy)
def add_middle_enemys(group1,group2,num):
for i in range(num):
middle_enemy = MiddleEnemy(bg_size)
group1.add(middle_enemy)
group2.add(middle_enemy)
def main():
"核心代码封装在main方法中去,此main方法为自定义方法"
running = True
# 2.3 图片切换的条件
switch_image = False
# 2.4 设置图片切换条件的参数变量
delay=60
y = 0
enemies1 = pygame.sprite.Group()
enemies2 = pygame.sprite.Group()
small_enemies = pygame.sprite.Group()
middle_enemies=pygame.sprite.Group()
# 生成敌机
add_small_enemys(enemies1, small_enemies, 6)
add_middle_enemys(enemies2, middle_enemies, 3)
#定义子弹
bullet_index = 0
#子弹的实例化个数
bullet1=[]
bullet_num=1
passtime = 0
for i in range(bullet_num):
bullet1.append(Bullet(ourplane.rect.midtop))
while running:
#绘制背景图片
screen.blit(background_1,(0,y))
screen.blit(background_2,(0,y-bg_size[1]))
# 2.2 设置动画效果的帧数,(图片切换的时间差)
clock = pygame.time.Clock()
clock.tick(200)
y +=1
if y == bg_size[1]:
y=0
if not delay % 3:
switch_image = not switch_image
for each in small_enemies:
if each.active:
each.move()#敌机下落
screen.blit(each.image,each.rect)#绘制敌机
pygame.draw.line(screen, color_black, (each.rect.left, each.rect.top - 5),
(each.rect.right, each.rect.top - 5))
energy_remain = each.energy / SmallEnemy.energy
if energy_remain > 0.2:
energy_color = color_green
else:
energy_color = color_red
pygame.draw.line(screen, energy_color, (each.rect.left, each.rect.top - 5),
(each.rect.left + each.rect.width * energy_remain, each.rect.top - 5), 2)
for each in middle_enemies:
if each.active:
each.move()#敌机下落
screen.blit(each.image,each.rect)#绘制敌机
pygame.draw.line(screen, color_black, (each.rect.left, each.rect.top - 5),
(each.rect.right, each.rect.top - 5))
energy_remain = each.energy / SmallEnemy.energy
if energy_remain > 0.2:
energy_color = color_green
else:
energy_color = color_red
pygame.draw.line(screen, energy_color, (each.rect.left, each.rect.top - 5),
(each.rect.left + each.rect.width * energy_remain, each.rect.top - 5), 2)
if ourplane.active:
if switch_image:
screen.blit(ourplane.image_one,ourplane.rect)
else:
screen.blit(ourplane.image_two,ourplane.rect)
for b in bullet1:
screen.blit(b.image, b.rect)
b.move()
if(not b.active):
b.Reset(ourplane.rect.midtop)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if delay == 0:
delay = 60
delay -= 1
# 键盘事件监听,通过键盘控制飞机移动方法
key_pressed = pygame.key.get_pressed()
if key_pressed[pygame.K_w] or key_pressed[pygame.K_UP]:
ourplane.moveup()
if key_pressed[pygame.K_s] or key_pressed[pygame.K_DOWN]:
ourplane.movedown()
if key_pressed[pygame.K_a] or key_pressed[pygame.K_LEFT]:
ourplane.moveleft()
if key_pressed[pygame.K_d] or key_pressed[pygame.K_RIGHT]:
ourplane.moveright()
if(passtime>=10):
if(bullet1.__len__()<4):
bullet1.append(Bullet(ourplane.rect.midtop))
passtime = 0
else:
passtime +=1
pygame.display.flip()
if __name__ == "__main__":
main()