马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
# main.py
import pygame
import sys
import traceback
import myplane
import enemy
import bullet
from pygame.locals import *
from random import *
pygame.init()
pygame.mixer.init()
bg_size =width, height = 480, 800
screen =pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background =pygame.image.load("images/bg_01.jpg").convert()
#载入游戏声音
pygame.mixer.music.load("music/BGM.mp3")
pygame.mixer.music.set_volume(0.2)
def add_Plane1_enemies(group1,group2,num):
for i in range(num):
e1 =enemy.Plane1(bg_size)
group1.add(e1)
group2.add(e1)
def add_Plane2_enemies(group1,group2,num):
for i in range(num):
e2 =enemy.Plane1(bg_size)
group1.add(e2)
group2.add(e2)
def add_Plane3_enemies(group1,group2,num):
for i in range(num):
e3 =enemy.Plane1(bg_size)
group1.add(e3)
group2.add(e3)
def add_Plane4_enemies(group1,group2,num):
for i in range(num):
e4 =enemy.Plane1(bg_size)
group1.add(e4)
group2.add(e4)
def main():
pygame.mixer.music.play(-1)
#生成我方飞机
me = myplane.MyPlane(bg_size)
enemies =pygame.sprite.Group()
#生成敌方飞机1
Plane1_enemies =pygame.sprite.Group()
add_Plane1_enemies(Plane1_enemies,enemies,15)
#生成敌方飞机2
Plane2_enemies =pygame.sprite.Group()
add_Plane2_enemies(Plane2_enemies,enemies,6)
#生成敌方飞机3
Plane3_enemies =pygame.sprite.Group()
add_Plane3_enemies(Plane3_enemies,enemies,3)
#生成敌方飞机4
Plane4_enemies =pygame.sprite.Group()
add_Plane4_enemies(Plane4_enemies,enemies,1)
#生成普通子弹
bullet1 = []
bullet1_index = 0
BULLET1_NUM = 6
for i in range(BULLET1_NUM):
bullet1.append(bullet.Bullet1(me.rect.midtop))
clock =pygame.time.Clock()
#用于切换图片
switch_image = True
#用于延迟
delay = 99
running =True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#检测用户的键盘操作
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.moveUp()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.moveDown()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.moveLeft()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.moveRight()
screen.blit(background,(0, 0))
#发射子弹
if not (delay % 10):
bullet1[bullet1_index].reset(me.rect.midtop)
bullet1_index =(bullet1_index+1) % BULLET1_NUM
#检测子弹是否击中敌机
for b in bullet1:
if b.active:
b.move()
screen.blit(b.image, b.rect)
enemy_hit = pygame.sprite.spritecollide(b, enemies, False)
if enemy_hit:
b.active = False
for e in enemy_hit:
e.active = False
#绘制敌方飞机4
for each in Plane4_enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
#毁灭
each.reset()
#绘制敌方飞机3
for each in Plane3_enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
#绘制敌方飞机2
for each in Plane2_enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
#绘制敌方飞机1
for each in Plane1_enemies:
if each.active:
each.move()
screen.blit(each.image, each.rect)
else:
each.reset()
#检测我方飞机是否碰撞
enemies_down = pygame.sprite.spritecollide(me,enemies,False,pygame.sprite.collide_mask)
if enemies_down:
#me.active =False
for e in enemies_down:
e.active = False
# 绘制我方飞机
if me.active:
screen.blit(me.image, me.rect)
else:
#毁灭
print("Gameover!")
running = False
#切换图片
if not(delay % 5):
switch_image = not switch_image
delay-=1
if not delay:
delay =99
pygame.display.flip()
clock.tick(60)
if __name__ == '__main__':
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()