项目:飞机大战(初始版)
代码:
# -*- coding: utf-8 -*-
# @Time : 2019/11/22 8:47
# @Author : Han lei
# @Email : hanlei5012@163.com
# @File : demo1.py
# @Software: PyCharm
# 飞机大战项目(初始版)
# 控制飞机
import random
from os import path
import pygame
img_dir = path.join(path.dirname(__file__), 'img')
snd_dir = path.join(path.dirname(__file__), 'snd')
WIDTH = 480
HEIGHT = 600
FPS = 60
POWERUP_TIME = 5000
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('Aircraft war')
clock = pygame.time.Clock()
# 绘制文本函数
# 使用pygame.font.match_font()可以在系统中搜索最接近的匹配字体
font_name = pygame.font.match_font('simhei')
def draw_text(surf, text, size, x, y):
"""
绘制文本函数
:param surf: 想要绘制文本的表面
:param text: 想要显示的字符串
:param size: 文本大小
:param x: 要绘制那个地点的横坐标
:param y: 要绘制那个地点的纵坐标
"""
font = pygame.font.Font(font_name, size)
text_surface = font.render(text, True, WHITE)
text_rect = text_surface.get_rect()
text_rect.midtop = (x, y)
surf.blit(text_surface, text_rect)
# 产生一个新暴徒
def newmob():
m = Mob()
all_sprites.add(m)
mobs.add(m)
# 显示血条
def draw_shield_bar(surf, x, y, pct): # pct填充量参数
if pct < 0:
pct = 0
BAR_LENGTH = 100
BAR_HEIGHT = 10
fill = (pct/100) * BAR_LENGTH
outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
pygame.draw.rect(surf, GREEN, fill_rect)
pygame.draw.rect(surf, WHITE, outline_rect, 2) # 2是边框宽度
# 生命计数器
def draw_lives(surf, x, y, lives, img):
for i in range(lives):
img_rect = img.get_rect()
img_rect.x = x + 30*i
img_rect.y = y
surf.blit(img, img_rect)
# 定义开始/结束屏幕
def show_go_screen():
screen.blit(background, background_rect)
draw_text(screen, '飞机大战', 64, WIDTH/2, HEIGHT/4)
draw_text(screen, '方向键移动,空格开火', 22, WIDTH/2, HEIGHT/2)
draw_text(screen, '按任意键开始', 18, WIDTH/2, HEIGHT*3/4)
pygame.display.flip()
waiting = True
while waiting:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
if event.type == pygame.KEYUP: # KEYUP是什么?
waiting = False
# 添加玩家精灵
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# 调整图像大小
self.image = pygame.transform.scale(player_img, (50, 38))
# 设置透明颜色
self.image.set_colorkey(BLACK)
# self.image.fill(GREEN)
self.rect = self.image.get_rect() # 定位矩形
# 设置碰撞圈,为了改进碰撞
self.radius = 20
# 测试碰撞圈
# pygame.draw.circle(self.image, RED, self.rect.center, self.radius)
self.rect.centerx = WIDTH/2
self.rect.bottom = HEIGHT - 10
self.speedx = 0 # 在x方向上的移动速度
self.speedy = 0
self.shield = 100 # 护盾
# 自动开火
self.shoot_delay = 250 # 测量飞船在发射另一颗子弹之前应该等待的时间长度(以毫秒为单位)
self.last_shot = pygame.time.get_ticks() # last_shot将跟踪最后一颗子弹射击的时间,以便知道何时有足够的时间再次射击。
# 多条生命
self.lives = 3 # 生命计数器
self.hidden = False # 隐藏/显示玩家
self.hide_timer = pygame.time.get_ticks() # 控制玩家隐藏时间的计时器
# 子弹增强
self.power = 1
self.power_time = pygame.time.get_ticks()
# 运动/控制
def update(self):
self.speedx = 0
self.speedy = 0
# 键盘控制移动
keystate = pygame.key.get_pressed() # 返回的是一个字典
if keystate[pygame.K_LEFT]:
self.speedx = -8
if keystate[pygame.K_RIGHT]:
self.speedx = 8
if keystate[pygame.K_UP]:
self.speedy = -8
if keystate[pygame.K_DOWN]:
self.speedy = 8
if keystate[pygame.K_SPACE]:
self.shoot()
self.rect.x += self.speedx
self.rect.y += self.speedy
# 边界检查
if self.rect.right > WIDTH:
self.rect.right = WIDTH
if self.rect.left < 0:
self.rect.left = 0
if self.rect.top < 0:
self.rect.top = 0
if self.rect.bottom > HEIGHT:
self.rect.bottom = HEIGHT
# 道具时间
if self.power >= 2 and pygame.time.get_ticks() - self.power_time > POWERUP_TIME:
self.power -= 1
self.power_time = pygame.time.get_ticks()
# 取消死亡后的隐藏
if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000:
self.hidden = False
self.rect.centerx = WIDTH / 2
self.rect.bottom = HEIGHT - 10
def shoot(self):
now = pygame.time.get_ticks()
if now - self.last_shot > self.shoot_delay:
self.last_shot = now
if self.power == 1:
bullet = Bullet(self.rect.centerx, self.rect.top)
all_sprites.add(bullet)
bullets.add(bullet)
shoot_sound.play()
if self.power >= 2:
bullet1 = Bullet(self.rect.left, self.rect.centery)
bullet2 = Bullet(self.rect.right, self.rect.centery)
all_sprites.add(bullet1)
all_sprites.add(bullet2)
bullets.add(bullet1)
bullets.add(bullet2)
shoot_sound.play()
def hide(self):
self.hidden = True
self.hide_timer = pygame.time.get_ticks() # 计时基本都用这个(单位毫秒)
self.rect.center = (WIDTH / 2, HEIGHT + 200)
def powerup(self):
self.power += 1
self.power_time = pygame.time.get_ticks()
# 创建敌人精灵
class Mob(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
# self.image = pygame.Surface((30, 40))
self.image_orig = random.choice(meteor_images)
self.image_orig.set_colorkey(BLACK)
self.image = self.image_orig.copy()
# self.image.fill(RED)
self.rect = self.image.get_rect()
self.radius = int(self.rect.width*.85/2)
# pygame.draw.circle(self.image, RED, self.rect.center, self.radius)
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
self.speedx = random.randrange(-3, 3)
self.rot = 0 # 精灵旋转的度数
self.rot_speed = random.randrange(-8, 8) # 精灵每次旋转多少度
self.last_update = pygame.time.get_ticks() # 控制动画速度,可以获取自时钟启动以来经过了多少毫秒
def update(self):
self.rotate()
self.rect.y += self.speedy
self.rect.x += self.speedx
if self.rect.top > HEIGHT + 10 or self.rect.left < -25 or self.rect.right > WIDTH + 20:
self.rect.x = random.randrange(WIDTH - self.rect.width)
self.rect.y = random.randrange(-100, -40)
self.speedy = random.randrange(1, 8)
# 动画精灵,旋转
def rotate(self):
now = pygame.time.get_ticks()
if now - self.last_update > 50:
self.last_update = now
self.rot = (self.rot + self.rot_speed) % 360
# 将图像旋转
new_image = pygame.transform.rotate(self.image_orig, self.rot)
old_center = self.rect.center
self.image = new_image
self.rect = self.image.get_rect()
self.rect.center = old_center
# 创建子弹精灵
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.image.set_colorkey(BLACK)
# self.image.fill(YELLOW)
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speedy = -10
def update(self):
self.rect.y += self.speedy
if self.rect.bottom < 0:
self.kill()
# 创建爆炸精灵
class Explosion(pygame.sprite.Sprite):
def __init__(self, center, size):
pygame.sprite.Sprite.__init__(self)
self.size = size
self.image = explosion_anim[self.size][0]
self.rect = self.image.get_rect()
self.rect.center = center
self.frame = 0
self.last_update = pygame.time.get_ticks()
self.frame_rate = 50
def update(self):
now = pygame.time.get_ticks()
if now - self.last_update > self.frame_rate:
self.last_update = now
self.frame += 1
if self.frame == len(explosion_anim[self.size]):
self.kill()
else:
center = self.rect.center
self.image = explosion_anim[self.size][self.frame]
self.rect = self.image.get_rect()
self.rect.center = center
# 创建Pow精灵
class Pow(pygame.sprite.Sprite):
def __init__(self, center):
pygame.sprite.Sprite.__init__(self)
self.type = random.choice(['shield', 'gun'])
self.image = powerup_images[self.type]
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.rect.center = center
self.speedy = 2
def update(self):
self.rect.y += self.speedy
if self.rect.top > HEIGHT:
self.kill()
# 加载图片
background = pygame.image.load(path.join(img_dir, 'starfield.png')).convert()
background_rect = background.get_rect()
player_img = pygame.image.load(path.join(img_dir, 'playerShip1_orange.png')).convert()
player_mini_img = pygame.transform.scale(player_img, (25, 19)) # 改变图像尺寸
player_mini_img.set_colorkey(BLACK)
powerup_images = {}
powerup_images['shield'] = pygame.image.load(path.join(img_dir, 'shield_gold.png')).convert()
powerup_images['gun'] = pygame.image.load(path.join(img_dir, 'bolt_gold.png')).convert()
meteor_images = []
meteor_list = ['meteorBrown_big1.png', 'meteorBrown_big2.png', 'meteorBrown_med1.png', 'meteorBrown_med3.png',
'meteorBrown_small1.png', 'meteorBrown_small2.png', 'meteorBrown_tiny1.png']
for img in meteor_list:
meteor_images.append(pygame.image.load(path.join(img_dir, img)).convert())
bullet_img = pygame.image.load(path.join(img_dir, 'laserRed16.png')).convert()
# 加载声音
# 射击声音
shoot_sound = pygame.mixer.Sound(path.join(snd_dir, 'pew.wav'))
# 加道具声音
shield_sound = pygame.mixer.Sound(path.join(snd_dir, 'pow4.wav'))
power_sound = pygame.mixer.Sound(path.join(snd_dir, 'pow5.wav'))
# 爆炸声音
expl_sounds = []
for snd in ['expl3.wav', 'expl6.wav']:
expl_sounds.append(pygame.mixer.Sound(path.join(snd_dir, snd)))
player_die_sound = pygame.mixer.Sound(path.join(snd_dir, 'rumble1.ogg'))
# 背景声音
pygame.mixer.music.load(path.join(snd_dir, 'tgfcoder-FrozenJam-SeamlessLoop.ogg'))
pygame.mixer.music.set_volume(0.4) # 将音量设置为最大音量的40%。
# 加载动画爆炸图形
explosion_anim = {}
explosion_anim['lg'] = []
explosion_anim['sm'] = []
explosion_anim['player'] = []
for i in range(9):
filename = f'regularExplosion0{i}.png'
img = pygame.image.load(path.join(img_dir, filename)).convert()
img.set_colorkey(BLACK)
img_lg = pygame.transform.scale(img, (75, 75))
explosion_anim['lg'].append(img_lg)
img_sm = pygame.transform.scale(img, (32, 32))
explosion_anim['sm'].append(img_sm)
filename = f'sonicExplosion0{i}.png'
img = pygame.image.load(path.join(img_dir, filename)).convert()
img.set_colorkey(BLACK)
explosion_anim['player'].append(img)
# all_sprites = pygame.sprite.Group()
# mobs = pygame.sprite.Group()
# bullets = pygame.sprite.Group()
# powerups = pygame.sprite.Group()
#
# player = Player()
# all_sprites.add(player)
# # 创建一群敌机,加入精灵组
# for i in range(8):
# newmob()
#
# # 分数
# score = 0
pygame.mixer.music.play(loops=-1) # 该loops参数是指定你想要的歌曲重复播放的次数。通过设置loops到-1,把它无限重复。
# 主程序
game_over = True
running = True
while running:
if game_over:
show_go_screen()
# 重置所有内容
game_over = False
all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
powerups = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
newmob()
score = 0
clock.tick(FPS)
# 事件检查
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# elif event.type == pygame.KEYDOWN:
# if event.key == pygame.K_SPACE:
# player.shoot()
all_sprites.update()
# 碰撞检测
hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
for hit in hits: # 这里返回的是字典
score += 70 - hit.radius
random.choice(expl_sounds).play()
expl = Explosion(hit.rect.center, 'lg')
all_sprites.add(expl)
if random.random() > 0.7:
pow = Pow(hit.rect.center)
all_sprites.add(pow)
powerups.add(pow)
newmob()
# 该spritecollide()函数有3个参数:要检查的玩家精灵,要比较的敌人组的名称,以及dokill参数(True / False)。dokill参数设置是否应该在命中对象时删除该对象(删除的是mobs)。
# spritecollide()只比较一个精灵和一个组,而groupcollide()可以比较组与组
hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle)
for hit in hits: # spritecollide()命令的返回值是被击中的精灵列表
player.shield -= hit.radius * 2
expl = Explosion(hit.rect.center, 'sm')
all_sprites.add(expl)
newmob()
if player.shield <= 0:
player_die_sound.play()
death_explosion = Explosion(hit.rect.center, 'player')
all_sprites.add(death_explosion)
player.hide()
player.lives -= 1
player.shield = 100
# 玩家与道具碰撞
hits = pygame.sprite.spritecollide(player, powerups, True)
for hit in hits:
if hit.type == 'shield':
player.shield += random.randrange(10, 30)
shield_sound.play()
if player.shield > 100:
player.shield = 100
if hit.type == 'gun':
player.powerup()
power_sound.play()
# alive()函数只返回特定精灵是否存活
# if not player.alive() and not death_explosion.alive():
if player.lives == 0 and not death_explosion.alive():
game_over = True
screen.fill(BLACK)
screen.blit(background, background_rect)
all_sprites.draw(screen)
draw_text(screen, str(score), 18, WIDTH/2, 10)
draw_shield_bar(screen, 5, 5, player.shield)
draw_lives(screen, WIDTH-100, 5, player.lives, player_mini_img)
pygame.display.flip()
pygame.quit()
游戏画面: