import sys
import traceback
from pygame.locals import *
import pygame
import random
from random import *
import math
pygame.init()
pygame.mixer.init()
#载入游戏音乐
pygame.mixer.music.load("sound/game_music.ogg")
pygame.mixer.music.set_volume(0.2)
bullet_sound = pygame.mixer.Sound("sound/bullet.wav")
bullet_sound.set_volume(0.2)
bomb_sound = pygame.mixer.Sound("sound/use_bomb.wav")
bomb_sound.set_volume(0.2)
supply_sound = pygame.mixer.Sound("sound/supply.wav")
supply_sound.set_volume(0.2)
get_bomb_sound = pygame.mixer.Sound("sound/get_bomb.wav")
get_bomb_sound.set_volume(0.2)
get_bullet_sound = pygame.mixer.Sound("sound/get_bullet.wav")
get_bullet_sound.set_volume(0.2)
upgrade_sound = pygame.mixer.Sound("sound/upgrade.wav")
upgrade_sound.set_volume(0.2)
enemy3_fly_sound = pygame.mixer.Sound("sound/enemy3_flying.wav")
enemy3_fly_sound.set_volume(0.2)
enemy1_down_sound = pygame.mixer.Sound("sound/enemy1_down.wav")
enemy1_down_sound.set_volume(0.2)
enemy2_down_sound = pygame.mixer.Sound("sound/enemy2_down.wav")
enemy2_down_sound.set_volume(0.2)
enemy3_down_sound = pygame.mixer.Sound("sound/enemy3_down.wav")
enemy3_down_sound.set_volume(0.2)
me_down_sound = pygame.mixer.Sound("sound/me_down.wav")
me_down_sound.set_volume(0.2)
class Myplane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self) #继承类
self.myplane_1_image = pygame.image.load("image/me1.png").convert_alpha()
self.myplane_2_image = pygame.image.load("image/me2.png").convert_alpha()
self.rect = self.myplane_1_image.get_rect()
self.destroy_image1 = pygame.image.load("image/me_destroy_1.png").convert_alpha()
self.destroy_image2 = pygame.image.load("image/me_destroy_2.png").convert_alpha()
self.destroy_image3 = pygame.image.load("image/me_destroy_3.png").convert_alpha()
self.rect = self.myplane_1_image.get_rect()
self.rect.left,self.rect.top = \
(bg_size[0] - self.rect.width) // 2 ,\
bg_size[1] - self.rect.height
self.screen_width,self.screen_height = bg_size[0],bg_size[1]
self.speed = [0,0]
#如果按下W,A,S,D,表示飞机分别向上下左右移动
self.press_can_K_w = True
self.press_can_K_s = True
self.press_can_K_a = True
self.press_can_K_d = True
def move(self):
if self.rect.left < 0 : #超过左边界,键盘a按下无效
self.rect.left = 0
self.press_can_K_a = False
elif self.rect.right > self.screen_width:
self.rect.right = self.screen_width
self.press_can_K_d = False
elif self.rect.top < 0 :
self.rect.top = 0
self.press_can_K_w = False
elif self.rect.bottom > self.screen_height:
self.rect.bottom = self.screen_height
self.press_can_K_d = False
else:
self.press_can_K_w = True
self.press_can_K_s = True
self.press_can_K_a = True
self.press_can_K_d = True
class Bullet_myplane(pygame.sprite.Sprite):
def __init__(self,bg_size,myplane_position):
pygame.sprite.Sprite.__init__(self) #继承类
self.image = pygame.image.load("image/bullet2.png").convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height =bg_size[0], bg_size[1]
self.myplane_position=myplane_position
self.rect.left,self.rect.top = self.myplane_position[0],self.myplane_position[1]
self.speed = -5
def bullet_move(self):
if self.rect.bottom > 0:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.rect.left,self.rect.top = self.myplane_position[0],self.myplane_position[1]
class Enemyplane(pygame.sprite.Sprite):
def __init__(self,bg_size,position):
pygame.sprite.Sprite.__init__(self) #继承类
self.image = pygame.image.load("image/enemy1.png").convert_alpha()
self.breakdown_image1 = pygame.image.load("image/enemy1_down1.png").convert_alpha()
self.breakdown_image2 = pygame.image.load("image/enemy2_down1.png").convert_alpha()
self.rect = self.image.get_rect()
self.width,self.height =bg_size[0], bg_size[1]
self.rect.left,self.rect.top = position
self.speed = 5
def enemy_move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.rect.left,self.rect.top = \
randint(0,self.width-self.rect.width),\
randint(-2*self.height,0)
def main():
pygame.init()
bg_size = width,height = 480,700
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("飞机大战")
background = pygame.image.load("image/background.png").convert()
BLACK = (0,0,0)
GREEN = (0,255,0)
RED = (255,0,0)
pygame.mixer.music.play(-1) #无限循环背景
clock = pygame.time.Clock()
pygame.key.set_repeat(10,10)
#生成我方飞机
myplane = Myplane(bg_size)
#生成子弹
myplane_bullet = Bullet_myplane(bg_size,myplane.rect) #想把飞机的实时位置传给子弹
group = pygame.sprite.Group()
#在屏幕最上方随机产生小飞机
position = randint(0,width-40),0
you = Enemyplane(bg_size,position)
group.add(you)
#延时
delay = 1000
#用于切换图片
switch_image = True
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
myplane.move()
if event.key == K_w and myplane.press_can_K_w == True :
myplane.rect.top -= 10
if event.key == K_s and myplane.press_can_K_s == True :
myplane.rect.bottom += 10
if event.key == K_a and myplane.press_can_K_a == True :
myplane.rect.left -= 10
if event.key == K_d and myplane.press_can_K_d == True :
myplane.rect.right += 10
screen.blit(background,(0,0))
#绘制子弹
myplane_bullet. bullet_move()
screen.blit(myplane_bullet.image,myplane_bullet.rect)
#绘制我方飞机
switch_image = not switch_image #取反,实现不断切换
if switch_image:
screen.blit(myplane.myplane_1_image,myplane.rect)
else:
screen.blit(myplane.myplane_2_image,myplane.rect)
#绘制敌方飞机
for each in group:
each.enemy_move()
screen.blit(you.image,you.rect)
delay -= 1
if not delay: #如果delay为0
delay == 200
#检测子弹与小飞机的碰撞
#检测和敌人飞机的碰撞
if pygame.sprite.spritecollide(myplane, group, False):
screen.blit(you.breakdown_image1,you.rect)
screen.blit(myplane.destroy_image1,myplane.rect)
screen.blit(myplane.destroy_image2,myplane.rect)
screen.blit(myplane.destroy_image3,myplane.rect)
me_down_sound.play()
pygame.mixer.music.pause()
you.speed = 0
myplane.speed = 0
running = False
pygame.display.flip()
clock.tick(60)
if __name__ =="__main__":
#在双击打开文件时,如果出现异常,将异常显示出来
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()