|
|
@ -0,0 +1,300 @@
|
|
|
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
Created on Wed May 29 19:18:32 2024
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@author: keshen
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import random
|
|
|
|
|
|
|
|
import pygame
|
|
|
|
|
|
|
|
# 屏幕大小的常量
|
|
|
|
|
|
|
|
SCREEN_RECT = pygame.Rect(0, 0, 480, 700)
|
|
|
|
|
|
|
|
# 刷新的帧率
|
|
|
|
|
|
|
|
FRAME_PER_SEC = 60
|
|
|
|
|
|
|
|
# 创建敌机的定时器常量
|
|
|
|
|
|
|
|
CREATE_ENEMY_EVENT = pygame.USEREVENT
|
|
|
|
|
|
|
|
# 英雄发射子弹事件
|
|
|
|
|
|
|
|
HERO_FIRE_EVENT = pygame.USEREVENT + 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GameSprite(pygame.sprite.Sprite):
|
|
|
|
|
|
|
|
"""飞机大战游戏精灵"""
|
|
|
|
|
|
|
|
def __init__(self, image_name, speedx=1,speedy=1):
|
|
|
|
|
|
|
|
# 调用父类的初始化方法
|
|
|
|
|
|
|
|
super().__init__()
|
|
|
|
|
|
|
|
# 定义对象的属性
|
|
|
|
|
|
|
|
self.image = pygame.image.load(image_name)
|
|
|
|
|
|
|
|
self.rect = self.image.get_rect()
|
|
|
|
|
|
|
|
self.speedx = speedx
|
|
|
|
|
|
|
|
self.speedy = speedy
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
|
|
|
# 在屏幕的垂直和水平方向上移动
|
|
|
|
|
|
|
|
self.rect.y += self.speedy
|
|
|
|
|
|
|
|
self.rect.x += self.speedx
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Background(GameSprite):
|
|
|
|
|
|
|
|
"""游戏背景精灵"""
|
|
|
|
|
|
|
|
def __init__(self, is_alt=False):
|
|
|
|
|
|
|
|
# 1. 调用父类方法实现精灵的创建(image/rect/speed)
|
|
|
|
|
|
|
|
super().__init__("image/background.png")
|
|
|
|
|
|
|
|
# 2. 判断是否是交替图像,如果是,需要设置初始位置
|
|
|
|
|
|
|
|
if is_alt:
|
|
|
|
|
|
|
|
self.rect.y = -self.rect.height
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
|
|
|
# 1. 调用父类的方法实现
|
|
|
|
|
|
|
|
self.rect.y += self.speedy
|
|
|
|
|
|
|
|
# 2. 判断是否移出屏幕,如果移出屏幕,将图像设置到屏幕的上方
|
|
|
|
|
|
|
|
if self.rect.y >= SCREEN_RECT.height:
|
|
|
|
|
|
|
|
self.rect.y = -self.rect.height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Enemy1(GameSprite):
|
|
|
|
|
|
|
|
counted = 0
|
|
|
|
|
|
|
|
#对死亡的Enemy1进行计数
|
|
|
|
|
|
|
|
"""敌机精灵"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
# 1. 调用父类方法,创建敌机精灵,同时指定敌机图片
|
|
|
|
|
|
|
|
super().__init__("image/enemy1.png",0,0)
|
|
|
|
|
|
|
|
# 2. 指定敌机的初始随机速度 1 ~ 3
|
|
|
|
|
|
|
|
self.speedy = random.randint(1, 3)
|
|
|
|
|
|
|
|
# 3. 指定敌机的初始随机位置
|
|
|
|
|
|
|
|
self.rect.bottom = 0
|
|
|
|
|
|
|
|
max_x = SCREEN_RECT.width - self.rect.width
|
|
|
|
|
|
|
|
self.rect.x = random.randint(0, max_x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
|
|
|
# 1.保持垂直方向的飞行,但不能调用父类方法,否则会斜着移动
|
|
|
|
|
|
|
|
self.rect.y += self.speedy
|
|
|
|
|
|
|
|
# 2. 判断是否飞出屏幕,如果是,需要从精灵组删除敌机
|
|
|
|
|
|
|
|
if self.rect.y >= SCREEN_RECT.height:
|
|
|
|
|
|
|
|
# print("飞出屏幕,需要从精灵组删除...")
|
|
|
|
|
|
|
|
# kill方法可以将精灵从所有精灵组中移出,精灵就会被自动销毁
|
|
|
|
|
|
|
|
self.kill()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
|
|
|
print("敌机挂了 %s" % self.rect)
|
|
|
|
|
|
|
|
Enemy1.counted+=1
|
|
|
|
|
|
|
|
#用计数方法记录杀掉的敌机,每print一次count值就加1
|
|
|
|
|
|
|
|
print("enemy1_count=%s"%self.counted)
|
|
|
|
|
|
|
|
if 40>=Enemy1.counted>=20:
|
|
|
|
|
|
|
|
self.speedx = random.randint(4, 7)
|
|
|
|
|
|
|
|
elif 100>=Enemy1.counted>=40:
|
|
|
|
|
|
|
|
self.speedx = random.randint(10, 20)
|
|
|
|
|
|
|
|
elif Enemy1.counted>100:
|
|
|
|
|
|
|
|
self.speedx = random.randint(200,300)
|
|
|
|
|
|
|
|
#对不同的计数程度,赋予不同的难度,为减少玩家的疲劳感,我们特地在计数值达到100后,进入地狱难度
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Enemy2(GameSprite):
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
"""不同于enemy1的敌机精灵"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
# 1. 调用父类方法,创建敌机精灵,同时指定敌机图片
|
|
|
|
|
|
|
|
super().__init__("image/enemy2.png",0,0)
|
|
|
|
|
|
|
|
# 2. 指定敌机的初始随机速度 2 ~ 4
|
|
|
|
|
|
|
|
self.speedy = random.randint(2,4)
|
|
|
|
|
|
|
|
# 3. 指定敌机的初始随机位置
|
|
|
|
|
|
|
|
self.rect.bottom = 0
|
|
|
|
|
|
|
|
max_x = SCREEN_RECT.width - self.rect.width
|
|
|
|
|
|
|
|
self.rect.x = random.randint(0, max_x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
|
|
|
# 1. 保持垂直方向的飞行
|
|
|
|
|
|
|
|
self.rect.y += self.speedy
|
|
|
|
|
|
|
|
# 2. 判断是否飞出屏幕,如果是,需要从精灵组删除敌机
|
|
|
|
|
|
|
|
if self.rect.y >= SCREEN_RECT.height:
|
|
|
|
|
|
|
|
# print("飞出屏幕,需要从精灵组删除...")
|
|
|
|
|
|
|
|
# kill方法可以将精灵从所有精灵组中移出,精灵就会被自动销毁
|
|
|
|
|
|
|
|
self.kill()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
|
|
|
print("敌机挂了 %s" % self.rect)
|
|
|
|
|
|
|
|
Enemy2.count+=1
|
|
|
|
|
|
|
|
#用计数方法记录杀掉的敌机,每print一次count值就加1
|
|
|
|
|
|
|
|
print("enemy2_count=%s"%self.count)
|
|
|
|
|
|
|
|
if 40>=Enemy2.count>=20:
|
|
|
|
|
|
|
|
self.speedx = random.randint(5, 8)
|
|
|
|
|
|
|
|
elif 100>=Enemy2.count>=40:
|
|
|
|
|
|
|
|
self.speedx = random.randint(15, 25)
|
|
|
|
|
|
|
|
elif Enemy2.count>100:
|
|
|
|
|
|
|
|
self.speedx = random.randint(200,300)
|
|
|
|
|
|
|
|
#对不同的计数程度,赋予不同的难度,为减少玩家的疲劳感,我们特地在计数值达到100后,进入地狱难度
|
|
|
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Enemy3(GameSprite):
|
|
|
|
|
|
|
|
count = 0
|
|
|
|
|
|
|
|
"""敌机作者精灵"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
# 1. 调用父类方法,创建敌机精灵,同时指定敌机图片
|
|
|
|
|
|
|
|
super().__init__("image/r.png")#作者的图片
|
|
|
|
|
|
|
|
# 2. 指定作者的初始随机速度 10~20 因为众所周知作者的速度是很快的,并且杀伤力极强
|
|
|
|
|
|
|
|
self.speedy = random.randint(10, 20)
|
|
|
|
|
|
|
|
# 3. 指定作者的初始随机位置
|
|
|
|
|
|
|
|
self.rect.bottom = 0
|
|
|
|
|
|
|
|
max_x = SCREEN_RECT.width - self.rect.width
|
|
|
|
|
|
|
|
self.rect.x = random.randint(0, max_x)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
|
|
|
# 1. 保持垂直方向的飞行
|
|
|
|
|
|
|
|
self.rect.y += self.speedy
|
|
|
|
|
|
|
|
# 2. 判断是否飞出屏幕,如果是,需要从精灵组删除作者
|
|
|
|
|
|
|
|
if self.rect.y >= SCREEN_RECT.height:
|
|
|
|
|
|
|
|
# print("飞出屏幕,需要从精灵组删除...")
|
|
|
|
|
|
|
|
# kill方法可以将精灵从所有精灵组中移出,精灵就会被自动销毁
|
|
|
|
|
|
|
|
self.kill()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
|
|
|
print("作者好帅 %s" % self.rect)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Hero(GameSprite):
|
|
|
|
|
|
|
|
"""英雄精灵"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
# 1. 调用父类方法,设置image&speed
|
|
|
|
|
|
|
|
super().__init__("image/hero1.png", 0,0)
|
|
|
|
|
|
|
|
# 2. 设置英雄的初始位置
|
|
|
|
|
|
|
|
self.rect.centerx = SCREEN_RECT.centerx
|
|
|
|
|
|
|
|
self.rect.bottom = SCREEN_RECT.bottom - 120
|
|
|
|
|
|
|
|
# 3. 创建子弹的精灵组
|
|
|
|
|
|
|
|
self.bullets = pygame.sprite.Group()
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
|
|
|
|
# 英雄在水平和竖直方向移动
|
|
|
|
|
|
|
|
self.rect.x += self.speedx
|
|
|
|
|
|
|
|
self.rect.y += self.speedy
|
|
|
|
|
|
|
|
# 控制英雄不能离开屏幕
|
|
|
|
|
|
|
|
if self.rect.x < 0:
|
|
|
|
|
|
|
|
self.rect.x = 0
|
|
|
|
|
|
|
|
elif self.rect.right > SCREEN_RECT.right:
|
|
|
|
|
|
|
|
self.rect.right = SCREEN_RECT.right
|
|
|
|
|
|
|
|
if self.rect.y < 0:
|
|
|
|
|
|
|
|
self.rect.y = 0
|
|
|
|
|
|
|
|
elif self.rect.height > SCREEN_RECT.height:
|
|
|
|
|
|
|
|
self.rect.height = SCREEN_RECT.height
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def fire(self):
|
|
|
|
|
|
|
|
print("发射子弹...")
|
|
|
|
|
|
|
|
for i in (0, 1, 2):
|
|
|
|
|
|
|
|
# 1. 创建子弹精灵
|
|
|
|
|
|
|
|
bullet = Bullet()
|
|
|
|
|
|
|
|
# 2. 设置精灵的位置
|
|
|
|
|
|
|
|
bullet.rect.bottom = self.rect.y - i * 20
|
|
|
|
|
|
|
|
bullet.rect.centerx = self.rect.centerx
|
|
|
|
|
|
|
|
# 3. 将精灵添加到精灵组
|
|
|
|
|
|
|
|
self.bullets.add(bullet)
|
|
|
|
|
|
|
|
class Bullet(GameSprite):
|
|
|
|
|
|
|
|
"""子弹精灵"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
# 调用父类方法,设置子弹图片,设置初始速度
|
|
|
|
|
|
|
|
super().__init__("image/bullet1.png", 0,-4)#x方向速度为0,经测试该系统y轴方向向下,所以给予y方向初始速度-4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def updates(self):
|
|
|
|
|
|
|
|
# 让子弹沿垂直方向飞行
|
|
|
|
|
|
|
|
self.rect.y += self.speedy
|
|
|
|
|
|
|
|
# 判断子弹是否飞出屏幕
|
|
|
|
|
|
|
|
if self.rect.bottom < 0:
|
|
|
|
|
|
|
|
self.kill()
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
|
|
|
print("子弹被销毁...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlaneGame(object):
|
|
|
|
|
|
|
|
"""飞机大战主游戏"""
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
print("游戏初始化")
|
|
|
|
|
|
|
|
# 1. 创建游戏的窗口
|
|
|
|
|
|
|
|
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
|
|
|
|
|
|
|
|
# 2. 创建游戏的时钟
|
|
|
|
|
|
|
|
self.clock = pygame.time.Clock()
|
|
|
|
|
|
|
|
# 3. 调用私有方法,精灵和精灵组的创建
|
|
|
|
|
|
|
|
self.__create_sprites()
|
|
|
|
|
|
|
|
# 4. 设置定时器事件 - 创建敌机 1s
|
|
|
|
|
|
|
|
pygame.time.set_timer(CREATE_ENEMY_EVENT, 1000)
|
|
|
|
|
|
|
|
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
|
|
|
|
|
|
|
|
def __create_sprites(self):
|
|
|
|
|
|
|
|
# 创建背景精灵和精灵组
|
|
|
|
|
|
|
|
bg1 = Background()
|
|
|
|
|
|
|
|
bg2 = Background(True)
|
|
|
|
|
|
|
|
self.back_group = pygame.sprite.Group(bg1, bg2)
|
|
|
|
|
|
|
|
# 创建敌机的精灵组
|
|
|
|
|
|
|
|
self.enemy_group = pygame.sprite.Group()
|
|
|
|
|
|
|
|
# 创建英雄的精灵和精灵组
|
|
|
|
|
|
|
|
self.hero = Hero()
|
|
|
|
|
|
|
|
self.hero_group = pygame.sprite.Group(self.hero)
|
|
|
|
|
|
|
|
def start_game(self):
|
|
|
|
|
|
|
|
print("游戏开始...")
|
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
# 1. 设置刷新帧率
|
|
|
|
|
|
|
|
self.clock.tick(FRAME_PER_SEC)
|
|
|
|
|
|
|
|
# 2. 事件监听
|
|
|
|
|
|
|
|
self.__event_handler()
|
|
|
|
|
|
|
|
# 3. 碰撞检测
|
|
|
|
|
|
|
|
self.__check_collide()
|
|
|
|
|
|
|
|
# 4. 更新/绘制精灵组
|
|
|
|
|
|
|
|
self.__update_sprites()
|
|
|
|
|
|
|
|
# 5. 更新显示
|
|
|
|
|
|
|
|
pygame.display.update()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __event_handler(self):
|
|
|
|
|
|
|
|
for event in pygame.event.get():
|
|
|
|
|
|
|
|
# 判断是否退出游戏
|
|
|
|
|
|
|
|
if event.type == pygame.QUIT:
|
|
|
|
|
|
|
|
PlaneGame.__game_over()
|
|
|
|
|
|
|
|
elif event.type == CREATE_ENEMY_EVENT:
|
|
|
|
|
|
|
|
print("敌机出场...")
|
|
|
|
|
|
|
|
# 创建敌机精灵
|
|
|
|
|
|
|
|
enemy = Enemy1()
|
|
|
|
|
|
|
|
enemy1 = Enemy2()
|
|
|
|
|
|
|
|
enemy2 = Enemy3()
|
|
|
|
|
|
|
|
# 将敌机精灵添加到敌机精灵组
|
|
|
|
|
|
|
|
self.enemy_group.add(enemy)
|
|
|
|
|
|
|
|
self.enemy_group.add(enemy1)
|
|
|
|
|
|
|
|
self.enemy_group.add(enemy2)
|
|
|
|
|
|
|
|
elif event.type == HERO_FIRE_EVENT:
|
|
|
|
|
|
|
|
self.hero.fire()
|
|
|
|
|
|
|
|
elif event.type == pygame.KEYDOWN and event.key == pygame.K_RIGHT:
|
|
|
|
|
|
|
|
print("向右移动...")
|
|
|
|
|
|
|
|
# 使用键盘提供的方法获取键盘按键 - 按键元组
|
|
|
|
|
|
|
|
keys_pressed = pygame.key.get_pressed()
|
|
|
|
|
|
|
|
# 判断元组中对应的按键索引值 1
|
|
|
|
|
|
|
|
if keys_pressed[pygame.K_RIGHT]:
|
|
|
|
|
|
|
|
self.hero.speedx = 4
|
|
|
|
|
|
|
|
self.hero.speedy = 0
|
|
|
|
|
|
|
|
elif keys_pressed[pygame.K_LEFT]:
|
|
|
|
|
|
|
|
self.hero.speedx = -4
|
|
|
|
|
|
|
|
self.hero.speedy = 0
|
|
|
|
|
|
|
|
elif keys_pressed[pygame.K_DOWN]:
|
|
|
|
|
|
|
|
self.hero.speedx = 0
|
|
|
|
|
|
|
|
self.hero.speedy = 4
|
|
|
|
|
|
|
|
elif keys_pressed[pygame.K_UP]:
|
|
|
|
|
|
|
|
self.hero.speedx = 0
|
|
|
|
|
|
|
|
self.hero.speedy = -4
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
|
|
|
self.hero.speedx = 0
|
|
|
|
|
|
|
|
self.hero.speedy = 0
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __check_collide(self):
|
|
|
|
|
|
|
|
# 1. 子弹摧毁敌机
|
|
|
|
|
|
|
|
pygame.sprite.groupcollide(self.hero.bullets, self.enemy_group, True, True)
|
|
|
|
|
|
|
|
# 2. 敌机撞毁英雄
|
|
|
|
|
|
|
|
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
|
|
|
|
|
|
|
|
# 判断列表时候有内容
|
|
|
|
|
|
|
|
if len(enemies) > 0:
|
|
|
|
|
|
|
|
# 让英雄牺牲
|
|
|
|
|
|
|
|
self.hero.kill()
|
|
|
|
|
|
|
|
# 结束游戏
|
|
|
|
|
|
|
|
PlaneGame.__game_over()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def __update_sprites(self):#屏幕更新
|
|
|
|
|
|
|
|
self.back_group.update()
|
|
|
|
|
|
|
|
self.back_group.draw(self.screen)
|
|
|
|
|
|
|
|
self.enemy_group.update()
|
|
|
|
|
|
|
|
self.enemy_group.draw(self.screen)
|
|
|
|
|
|
|
|
self.hero_group.update()
|
|
|
|
|
|
|
|
self.hero_group.draw(self.screen)
|
|
|
|
|
|
|
|
self.hero.bullets.update()
|
|
|
|
|
|
|
|
self.hero.bullets.draw(self.screen)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
|
|
|
def __game_over():
|
|
|
|
|
|
|
|
print("游戏结束")
|
|
|
|
|
|
|
|
pygame.quit()
|
|
|
|
|
|
|
|
exit()#游戏结束,退出游戏
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
|
|
|
# 创建游戏对象
|
|
|
|
|
|
|
|
game = PlaneGame()
|
|
|
|
|
|
|
|
# 启动游戏
|
|
|
|
|
|
|
|
game.start_game()
|