import pygame
import sys
from pygame.locals import *
from random import *
import traceback
import plane
import bullet
import enemy
def main():
pygame.init()
pygame.mixer.init()
life_image = 'image/life.png'
bg_music = 'music/game_music.ogg'
background = "image/background.png"
bomb_music = 'music/use_bomb.wav'
about_me = 'image/about_me.png'
about_me_image = pygame.image.load(about_me)
about_me_rect = about_me_image.get_rect()
MyLife = pygame.image.load(life_image)
Mybackground1 = pygame.image.load(background)
life_rect = MyLife.get_rect()
bg_size = 480, 700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战(主页获取源码)")
MyBullet = []
Enemy = []
kill = []
score = 0
score_font = pygame.font.Font("font/font.ttf", 36)
num = 5
num1 = 0
num2 = 0
num3 = 0
harm = 1
pygame.key.set_repeat(100, 100)
clock = pygame.time.Clock()
MyPlane = plane.Plane(bg_size)
Enemy_group = pygame.sprite.Group()
bg_music = pygame.mixer.Sound(bg_music)
bomb_sound = pygame.mixer.Sound(bomb_music)
bg_music.play(-1)
super_Bullet = False
running = True
again = False
My_Protection_cover = plane.Protection_cover(bg_size)
paused = False
pause_nor_image = pygame.image.load("image/pause_nor.png").convert_alpha()
pause_pressed_image = pygame.image.load("image/pause_pressed.png").convert_alpha()
resume_nor_image = pygame.image.load("image/resume_nor.png").convert_alpha()
resume_pressed_image = pygame.image.load("image/resume_pressed.png").convert_alpha()
again_image = pygame.image.load("image/again.png").convert_alpha()
gameover_image = pygame.image.load("image/gameover.png").convert_alpha()
me_image = pygame.image.load('image/me.png').convert_alpha()
me_rect = me_image.get_rect()
gameover_rect = gameover_image.get_rect()
again_rect = again_image.get_rect()
paused_rect = pause_nor_image.get_rect()
paused_rect.left, paused_rect.top = bg_size[0] - paused_rect.width - 10, 10
paused_image = pause_nor_image
tnt_num = 10
while running:
if MyPlane.dead == True:
MyPlane = plane.Plane(bg_size)
MyPlane.not_dead = not MyPlane.not_dead
MyPlane.life_num = oldLife
again = True
num4 = 0
for event in pygame.event.get():
if event.type == QUIT:
bg_music.stop()
pygame.quit()
sys.exit()
if event.type == KEYDOWN and MyPlane.dead == False:
if event.key == K_TAB:
MyPlane.not_dead = not MyPlane.not_dead
if event.key == K_RETURN:
if harm < 11:
harm += 1
if harm >= 11:
harm = 1
if event.key == K_SPACE:
if tnt_num > 0:
bomb_sound.play()
for each in Enemy:
kill.append(each)
score += each.score
Enemy.clear()
Enemy_group = pygame.sprite.Group()
tnt_num -= 1
if event.type == MOUSEBUTTONDOWN:
if event.button == 1 and paused_rect.collidepoint(event.pos):
paused = not paused
if paused:
bg_music.stop()
else:
bg_music.play(-1)
if event.type == MOUSEMOTION:
if paused_rect.collidepoint(event.pos) == True:
if paused == True:
paused_image = resume_pressed_image
if paused == False:
paused_image = pause_pressed_image
else:
if paused == True:
paused_image = resume_nor_image
if paused == True:
paused_image = pause_nor_image
screen.blit(Mybackground1, (0, 0))
key = pygame.key.get_pressed()
if MyPlane.dead == False and paused == False:
if key[K_w] or key[K_UP]:
MyPlane.move([0, -1])
if key[K_s] or key[K_DOWN]:
MyPlane.move([0, 1])
if key[K_a] or key[K_LEFT]:
MyPlane.move([-1, 0])
if key[K_d] or key[K_RIGHT]:
MyPlane.move([1, 0])
if key[K_j] or key[K_1]:
if num1 == num:
temp = bullet.Bullet(MyPlane.rect.centerx, MyPlane.rect.top, harm)
music = pygame.mixer.Sound(temp.music)
music.play()
MyBullet.insert(0, temp)
num1 = 0
num1 += 1
for each in Enemy:
if each.disappear == True:
Enemy.remove(each)
elif paused == False:
each.move()
if each.Enemytype == 'BigEnemy' and each.hit == False:
if num3 >= 5:
screen.blit(each.default_image[0], each.rect)
elif num3 <= 5:
screen.blit(each.default_image[1], each.rect)
elif each.hit == False:
screen.blit(each.default_image[0], each.rect)
else:
screen.blit(each.hit_image[0], each.rect)
for each in MyBullet:
if each.disappear == True:
MyBullet.remove(each)
elif paused == False:
each.move()
screen.blit(each.image, each.rect)
for x in MyBullet:
for y in Enemy:
if pygame.sprite.collide_rect(x, y):
MyBullet.remove(x)
y.life -= harm
break
if MyPlane.not_dead == False:
enemies_down = pygame.sprite.spritecollide \
(MyPlane, Enemy_group, False, pygame.sprite.collide_mask)
else:
enemies_down = pygame.sprite.spritecollide \
(My_Protection_cover, Enemy_group, False, pygame.sprite.collide_mask)
if enemies_down:
if MyPlane.not_dead == False and MyPlane.dead == False:
MyPlane.life_num -= 1
oldLife = MyPlane.life_num
kill.append(MyPlane)
MyPlane.dead = True
tnt_num = 10
for each in enemies_down:
Enemy_group.remove(each)
score += each.score
try:
Enemy.remove(each)
except:
pass
each.dead()
kill.append(each)
if (num2 % 30) == 0 and num2 != 0:
x = randint(0, bg_size[0])
temp = enemy.SmallEnemy(bg_size)
temp.init_image()
temp.init_pos(randint(0, bg_size[0]), 0)
Enemy.append(temp)
Enemy_group.add(temp)
if (num2 % 90) == 0 and num2 != 0:
x = randint(0, bg_size[0])
temp = enemy.MidEnemy(bg_size)
temp.init_image()
temp.init_pos(randint(0, bg_size[0]), 0)
Enemy.insert(0, temp)
Enemy_group.add(temp)
if (num2 % 250) == 0 and num2 != 0:
x = randint(0, bg_size[0])
temp = enemy.BigEnemy(bg_size)
temp.init_image()
temp.init_pos(randint(0, bg_size[0]), 0)
Enemy.append(temp)
Enemy_group.add(temp)
num2 = 0
for k in kill:
if len(k.dead_image) <= 0:
kill.remove(k)
continue
else:
screen.blit(k.dead_image[0], k.rect)
del k.dead_image[0]
for each in Enemy:
if each.life <= 0:
评论0