#include"stdafx.h"
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>


// 引用 Windows Multimedia API
#pragma comment(lib,"Winmm.lib")

#define Width 800
#define High 600

int map_num = 1;
int Hero_x, Hero_y;//主角坐标
int board_x[13], board_y[13];//平台坐标
int super_board_x, super_board_y;//弹跳平台
int eraser_x, eraser_y;//橡皮擦
int fruit_x[10], fruit_y[10];//水果
int lock_x, lock_y;//锁的坐标
int bullet_x, bullet_y;//子弹坐标
int boss_bullet_x, boss_bullet_y;
int key_x, key_y;//钥匙坐标
int key;//判断
int pill_x, pill_y;//药物坐标
int pill, fruit[10];//水果坐标
int ghost_x[10], ghost_y[10];//怪物坐标

int bean[12][3];//1表示有豆子,0表示没有,坐标可以通过平台来实现
int fire_direction;
int is_eraser;//判断橡皮擦
int is_ghost[10];//判断是否显示有怪物
bool can_jump;//判断跳跃
bool invincible;//无敌状态
bool is_onboard;//状态判断
bool is_weak;//是否可被消灭
bool startup;//是否需要初始化
bool enter;//是否过关
bool invisible;//隐身
bool is_fire;//开火
bool is_fire_boss;
bool boss_hurt;

int first_board_length;//平台长度
int board_length;


int vy_up, vy_down;//升降的增量
int v_ghost;

int Ghost_size;//怪物大小
int Hero_size;//人物大小
int Bean_size;//豆子大小


int board_move_x, board_move_y;//地图移动增量

int direction;//标记方向(主角)
int ghost_direction;

int play_times;//实现动画
int weak_times;
int death_times;
int ghost_play_times;
int tip_time;

int invincible_time;//无敌时间

int life;//血量
int boss_life;

int score;//得分

IMAGE img_Hero, img_Hero_mask, img_bk1, img_bk2, img_bk3, img_board, img_board_mask;
IMAGE img_bean, img_bean_mask, img_bean_plus, img_bean_plus_mask;
IMAGE img_hero_death, img_hero_death_mask;
IMAGE img_key, img_key_mask, img_lock, img_lock_mask;
IMAGE img_fruit, img_fruit_mask, img_pill, img_pill_mask;
IMAGE img_healthEmpty, img_healthFull, img_healthLittle, img_healthMuch, img_health_mask;
IMAGE img_super_board, img_super_board_mask;
IMAGE img_eraser, img_eraser_empty, img_eraser_mask;
IMAGE img_ghost, img_ghost_mask, img_Board, img_Board_mask, img_ghost_weak, img_ghost_weak_mask;//图片
IMAGE img_bullet, img_bullet_mask, img_boss_bullet, img_boss_bullet_mask;
IMAGE img_boss, img_boss_mask;
IMAGE img_logo, img_start;
IMAGE img_health10, img_health20, img_health30, img_health40, img_health50, img_health60, img_health70, img_health80, img_health90;
IMAGE img_tip1, img_tip1_mask, img_tip2, img_tip2_mask;
//声明功能函数

void gameover();
void is_hurt(int a, int b, int *c);
void jump();
void drop();
bool is_score(int a, int b);
bool is_hit(int a, int b, int L, int H, int size);
bool is_dead();
void ghost_move(int a, int b, int c, int d);
void show_life();
void map1();
void map2();
void map3();
void show_life_boss();
void show_life_hero();


void show_life()
{
if (life == 75)
{
putimage(300, 500, &img_health_mask, SRCAND);
putimage(300, 500, &img_healthFull, SRCPAINT);
}
if (life == 50)
{
putimage(300, 500, &img_health_mask, SRCAND);
putimage(300, 500, &img_healthMuch, SRCPAINT);
}
if (life == 25)
{
putimage(300, 500, &img_health_mask, SRCAND);
putimage(300, 500, &img_healthLittle, SRCPAINT);
}
if (life == 0)
{
putimage(300, 500, &img_health_mask, SRCAND);
putimage(300, 500, &img_healthEmpty, SRCPAINT);
}
}

void ghost_move(int a, int b, int c, int d)//a,b是怪物坐标,c用来判断怪物是否被消灭,d时平台横坐标,此函数用来显示怪物
{
a += board_move_x;
b += board_move_y;
if (c)
{
if (is_weak)//此时怪物可以被消灭
{
putimage(a, b, 50, 50, &img_ghost_weak_mask, 0, 0, SRCAND);
putimage(a, b, 50, 50, &img_ghost_weak, 0, 0, SRCPAINT);
}
else//正常怪物
{
putimage(a, b, 50, 50, &img_ghost_mask, 50 * ghost_play_times, ghost_direction * 50, SRCAND);
putimage(a, b, 50, 50, &img_ghost, 50 * ghost_play_times, ghost_direction * 50, SRCPAINT);
}

if (Ghost_size + a >= d + board_length)//平台左右边界判定实现自主移动
{
v_ghost = -10;
ghost_direction = 1;
}
else if (a <= d)
{
v_ghost = 10;
ghost_direction = 0;
}
}
}

void is_hurt(int a, int b, int *c)//a,b是怪物坐标
{
if (*c)
{
if (is_hit(a, b, Ghost_size, Ghost_size, Hero_size))//受伤判断
{
if (!invincible)
{
life -= 25;
invincible = true;
mciSendString("stop hurtmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close hurtmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_Injured.mp3 alias hurtmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play hurtmusic", NULL, 0, NULL); // 仅播放一次
}
}
}
if (is_hit(a, b, Ghost_size, Ghost_size, Hero_size) && is_weak)//消灭怪物判断
{
*c = 0;
mciSendString("stop getmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close getmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetGhost.mp3 alias getmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play getmusic", NULL, 0, NULL); // 仅播放一次
}

}

bool is_score(int a, int b)//判断是否得分,a,b是豆子的坐标,再写一个是因为is_hit的变量太多了
{
if (is_hit(a, b, Bean_size, Bean_size, Hero_size))
{
return true;
}
return false;
}

bool is_dead()//判断是否主角死亡
{
if ((Hero_y >= High - 50) || (life == 0))
{
life = 0;
mciSendString("stop deadmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close deadmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_Death.mp3 alias deadmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play deadmusic", NULL, 0, NULL); // 仅播放一次
return true;
}
return false;
}

void jump()//实现跳跃
{
if (can_jump)//跳跃
{
board_move_y = Hero_y;
Hero_y -= vy_up;
board_move_y = board_move_y - Hero_y;
vy_up--;
}
else//不跳时恢复初始化
{
vy_up = 15;
}
}

void drop()//实现自由下落
{
board_move_y = Hero_y;//相对运动
if (!can_jump)//不在跳时下落
{
Hero_y += vy_down;
vy_down++;
}
if (!is_onboard)
board_move_y = -Hero_y + board_move_y;

if (can_jump)//恢复初始化不然跳不动
vy_down = 0;
}

bool is_hit(int a, int b, int L, int H, int size)//a,b是物体的左端坐标,L是长度,H是高度,判断是否接触
{
if (Hero_x + size < a || Hero_x > a + L || Hero_y > b + H || Hero_y + size < b)
{
return false;
}
return true;
}

void logo()
{
loadimage(&img_logo,"素材\\blingbling.jpg" , 800, 600);//LOGO
mciSendString("open 音乐音效\\logo.mp3 alias logomusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play logomusic", NULL, 0, NULL); // 仅播放一次
int r, g, b; // 获取绘图窗口和 img 对象的显存指针
DWORD* pbWnd = GetImageBuffer();
DWORD* pbImg = GetImageBuffer(&img_logo);
for (int n = 0; n <= 40; n++)
{
for (int i = 0; i < 800 * 600; i++)
{
r = GetRValue(pbImg[i]);
r = (255 - r)*n / (80 - n) + r;
b = GetBValue(pbImg[i]);
b = (255 - b)*n / (80 - n) + b;
g = GetGValue(pbImg[i]);
g = (255 - g)*n / (80 - n) + g;
pbWnd[i] = RGB(r, g, b);
}
FlushBatchDraw();
Sleep(100);
}
Sleep(1000);
}


void show_life_hero()
{
if (life == 75)
{
putimage(10, 50, &img_health_mask, SRCAND);
putimage(10, 50, &img_healthFull, SRCPAINT);
}
if (life == 50)
{
putimage(10, 50, &img_health_mask, SRCAND);
putimage(10, 50, &img_healthMuch, SRCPAINT);
}
if (life == 25)
{
putimage(10, 50, &img_health_mask, SRCAND);
putimage(10, 50, &img_healthLittle, SRCPAINT);
}
if (life == 0)
{
putimage(10, 50, &img_health_mask, SRCAND);
putimage(10, 50, &img_healthEmpty, SRCPAINT);
}
}


void show_life_boss()
{
if (boss_life == 250)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_healthFull, SRCPAINT);
}
if (boss_life == 225)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health90, SRCPAINT);
}
if (boss_life == 200)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health80, SRCPAINT);
}
if (boss_life == 175)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health70, SRCPAINT);
}
if (boss_life == 150)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health60, SRCPAINT);
}
if (boss_life == 125)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health50, SRCPAINT);
}
if (boss_life == 100)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health40, SRCPAINT);
}
if (boss_life == 75)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health30, SRCPAINT);
}
if (boss_life == 50)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health20, SRCPAINT);
}
if (boss_life == 25)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_health10, SRCPAINT);
}
if (boss_life == 0)
{
putimage(600, 50, &img_health_mask, SRCAND);
putimage(600, 50, &img_healthEmpty, SRCPAINT);
}
}


void Startup()
{

mciSendString("stop startmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close startmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
initgraph(Width, High);

setbkmode(TRANSPARENT);
Hero_x = 0;
Hero_y = High / 5 * 3 + 50;
Hero_size = 50;
Bean_size = 20;
Ghost_size = 50;
first_board_length = 100;
board_length = 240;
vy_up = 15;
vy_down = 0;
v_ghost = 2;
can_jump = false;
invincible = false;
is_weak = false;
is_onboard = true;
startup = true;
enter = false;
life = 75;
direction = 0;//0向右,1向左
ghost_direction = 0;
play_times = 0;
weak_times = 0;
death_times = 0;
ghost_play_times = 0;
invincible_time = 50;
pill = 1;
key = 1;

//加载图片
loadimage(&img_bk1, "素材\\Scene 1.jpg", 800, 600);
loadimage(&img_bk2, "素材\\Scene 2.jpg", 800, 600);
loadimage(&img_bk3, "素材\\Scene 3.jpg", 800, 600);
loadimage(&img_Hero, "素材\\hero.bmp");
loadimage(&img_Hero_mask, "素材\\hero_mask.bmp");
loadimage(&img_board, "素材\\Beginning.bmp", 100, 10);
loadimage(&img_board_mask, "素材\\Beginning_mask.bmp", 100, 10);
loadimage(&img_bean, "素材\\bean.bmp", 20, 20);
loadimage(&img_bean_mask, "素材\\bean_mask.bmp", 20, 20);
loadimage(&img_ghost, "素材\\ghost.jpg");
loadimage(&img_ghost_mask, "素材\\ghost_mask.jpg");
loadimage(&img_Board, "素材\\Board.bmp", 240, 30);
loadimage(&img_Board_mask, "素材\\Board_mask.bmp", 240, 30);
loadimage(&img_ghost_weak, "素材\\ghostWeak.bmp");
loadimage(&img_ghost_weak_mask, "素材\\ghostWeak_mask.bmp");
loadimage(&img_bean_plus, "素材\\beanplus.bmp", 30, 30);
loadimage(&img_bean_plus_mask, "素材\\beanplus_mask.bmp", 30, 30);
loadimage(&img_hero_death, "素材\\heroDeath.bmp");
loadimage(&img_hero_death_mask, "素材\\heroDeath_mask.bmp");
loadimage(&img_healthEmpty, "素材\\healthEmpty.bmp");
loadimage(&img_healthFull, "素材\\healthFull.bmp");
loadimage(&img_healthLittle, "素材\\healthLittle.bmp");
loadimage(&img_healthMuch, "素材\\healthMuch.bmp");
loadimage(&img_health_mask, "素材\\health_mask.bmp");
loadimage(&img_fruit, "素材\\fruit_cherry.bmp");
loadimage(&img_fruit_mask, "素材\\fruit_mask.bmp");
loadimage(&img_pill, "素材\\aid.bmp");
loadimage(&img_pill_mask, "素材\\aid_mask.bmp");
loadimage(&img_key, "素材\\key.bmp");
loadimage(&img_key_mask, "素材\\key_mask.bmp");
loadimage(&img_lock, "素材\\lock_upright.bmp", 30, 400);
loadimage(&img_lock_mask, "素材\\lock_upright_mask.bmp", 30, 400);
loadimage(&img_bullet, "素材\\heroBullet.bmp");
loadimage(&img_bullet_mask, "素材\\heroBullet_mask.bmp");
loadimage(&img_boss, "素材\\Boss.bmp");
loadimage(&img_boss_mask, "素材\\Boss_mask.bmp");
loadimage(&img_boss_bullet, "素材\\bossBullet.bmp");
loadimage(&img_boss_bullet_mask, "素材\\bossBullet_mask.bmp");
loadimage(&img_health10, "素材\\health10.bmp");
loadimage(&img_health20, "素材\\health20.bmp");
loadimage(&img_health30, "素材\\health30.bmp");
loadimage(&img_health40, "素材\\health40.bmp");
loadimage(&img_health50, "素材\\health50.bmp");
loadimage(&img_health60, "素材\\health60.bmp");
loadimage(&img_health70, "素材\\health70.bmp");
loadimage(&img_health80, "素材\\health80.bmp");
loadimage(&img_health90, "素材\\health90.bmp");
loadimage(&img_tip1, "素材\\tips1.bmp");
loadimage(&img_tip1_mask, "素材\\tips1_mask.bmp");
loadimage(&img_tip2, "素材\\tips2.bmp");
loadimage(&img_tip2_mask, "素材\\tips2_mask.bmp");
loadimage(&img_start, "素材\\Startup.jpg");




for (int i = 0; i < 12; i++)//豆子显示初始化
{
for (int j = 0; j < 3; j++)
bean[i][j] = 1;
}
for (int i = 0; i < 3; i++)
{
fruit[i] = 1;
}
//部分豆子被水果等替代
bean[4][1] = 0;
bean[6][1] = 0;
bean[7][1] = 0;
bean[9][1] = 0;
bean[11][1] = 0;

is_ghost[1] = 1;
//平台坐标
board_x[1] = 0;
board_y[1] = 460;
board_x[2] = 250;
board_y[2] = 360;
board_x[3] = 650;
board_y[3] = 260;
board_x[4] = 1090;
board_y[4] = 260;
board_x[5] = 1530;
board_y[5] = 260;
board_x[6] = 1930;
board_y[6] = 160;
board_x[7] = 2060;
board_y[7] = 0;
board_x[8] = 2330;
board_y[8] = 80;
board_x[9] = 2600;
board_y[9] = -50;
board_x[10] = 2850;
board_y[10] = 180;
board_x[11] = 2840;
board_y[11] = 490;
board_x[12] = 3150;
board_y[12] = 340;

fruit_x[0] = board_x[4] + 25 + 85 * 1;//水果坐标
fruit_y[0] = board_y[4] - 45;
fruit_x[1] = board_x[7] + 25 + 85 * 1;
fruit_y[1] = board_y[7] - 45;
fruit_x[2] = board_x[9] + 25 + 85 * 1;
fruit_y[2] = board_y[9] - 45;

pill_x = board_x[6] + 25 + 85 * 1;//药物坐标
pill_y = board_y[6] - 45;

key_x = board_x[11] + 25 + 85 * 1;//钥匙坐标
key_y = board_y[11] - 45;

lock_x = board_x[12] + board_length / 2 - 50;//锁的坐标
lock_y = board_y[12] - 400;

ghost_x[1] = board_x[2] + 10;//怪物坐标
ghost_y[1] = board_y[2] - 50;

BeginBatchDraw();
}



void map1()
{

startup = false;
mciSendString("open 音乐音效\\bkmusic_Scene1.mp3 alias bkmusic", NULL, 0, NULL);//打开背景音乐
mciSendString("play bkmusic repeat", NULL, 0, NULL); // 循环播放
mciSendString("stop beginmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close beginmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_LevelBegin.mp3 alias beginmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play beginmusic", NULL, 0, NULL); // 仅播放一次
//显示
{
putimage(0, 0, &img_bk1);

show_life();//显示生命值
char s[5];
sprintf(s, "%d", score);
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
outtextxy(400, 100, s);

if (is_dead())//死亡画面
{
if (death_times >= 21)
{
outtextxy(335, 300, "GAME OVER!");
gameover();
}
else
{
putimage(Hero_x, Hero_y, 50, 50, &img_hero_death_mask, 50 * death_times, 0, SRCAND);//死亡动画
putimage(Hero_x, Hero_y, 50, 50, &img_hero_death, 50 * death_times, 0, SRCPAINT);

putimage(board_x[1], board_y[1], &img_board_mask, SRCAND);
putimage(board_x[1], board_y[1], &img_board, SRCPAINT);

for (int i = 2; i <= 12; i++)
{
putimage(board_x[i], board_y[i], &img_Board_mask, SRCAND);
putimage(board_x[i], board_y[i], &img_Board, SRCPAINT);
}
//相对移动坐标同步
fruit_x[0] = board_x[4] + 25 + 85 * 1;
fruit_y[0] = board_y[4] - 45;
fruit_x[1] = board_x[7] + 25 + 85 * 1;
fruit_y[1] = board_y[7] - 45;
fruit_x[2] = board_x[9] + 25 + 85 * 1;
fruit_y[2] = board_y[9] - 45;

pill_x = board_x[6] + 25 + 85 * 1;
pill_y = board_y[6] - 45;

key_x = board_x[11] + 25 + 85 * 1;
key_y = board_y[11] - 45;

lock_x = board_x[12] + board_length / 2 - 50;
lock_y = board_y[12] - 400;


ghost_move(ghost_x[1], ghost_y[1], is_ghost[1], board_x[2]);//怪物自主移动

if (key == 1)//显示钥匙
{
putimage(key_x, key_y, &img_key_mask, SRCAND);
putimage(key_x, key_y, &img_key, SRCPAINT);

}

if (enter)//显示锁
{
putimage(lock_x, lock_y - 100, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y - 100, &img_lock, SRCPAINT);
}
else
{
putimage(lock_x, lock_y, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y, &img_lock, SRCPAINT);
}

if (pill == 1)//显示药物
{
putimage(pill_x, pill_y, &img_pill_mask, SRCAND);
putimage(pill_x, pill_y, &img_pill, SRCPAINT);

}

for (int i = 0; i < 3; i++)//显示水果
{
if (fruit[i] == 1)
{
putimage(fruit_x[i], fruit_y[i], &img_fruit_mask, SRCAND);
putimage(fruit_x[i], fruit_y[i], &img_fruit, SRCPAINT);

}


}
for (int i = 2; i <= 12; i++)//显示豆子
{
for (int j = 0; j < 3; j++)
{
if (bean[i][j] == 1)
{
if (i != 3 || j != 1)
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean, SRCPAINT);
}
else
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus, SRCPAINT);

}
}
}
}

death_times++;
}
}
else//正常画面
{
if (tip_time < 100&&Hero_x<board_x[2])
{

putimage(0, 0, &img_tip1_mask, SRCAND);
putimage(0, 0, &img_tip1, SRCPAINT);
tip_time++;
}
if (invincible)//无敌动画
{
putimage(Hero_x, Hero_y, 50, 50, &img_Hero_mask, 50 * play_times, (direction + 2) * 50, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_Hero, 50 * play_times, (direction + 2) * 50, SRCPAINT);

}
else//正常动画
{
putimage(Hero_x, Hero_y, 50, 50, &img_Hero_mask, 50 * play_times, direction * 50, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_Hero, 50 * play_times, direction * 50, SRCPAINT);
}

putimage(board_x[1], board_y[1], &img_board_mask, SRCAND);
putimage(board_x[1], board_y[1], &img_board, SRCPAINT);

for (int i = 2; i <= 12; i++)
{
putimage(board_x[i], board_y[i], &img_Board_mask, SRCAND);
putimage(board_x[i], board_y[i], &img_Board, SRCPAINT);
}

//相对移动坐标同步
fruit_x[0] = board_x[4] + 25 + 85 * 1;
fruit_y[0] = board_y[4] - 45;
fruit_x[1] = board_x[7] + 25 + 85 * 1;
fruit_y[1] = board_y[7] - 45;
fruit_x[2] = board_x[9] + 25 + 85 * 1;
fruit_y[2] = board_y[9] - 45;

pill_x = board_x[6] + 25 + 85 * 1;
pill_y = board_y[6] - 45;

key_x = board_x[11] + 25 + 85 * 1;
key_y = board_y[11] - 45;

lock_x = board_x[12] + board_length / 2 - 50;
lock_y = board_y[12] - 400;


ghost_move(ghost_x[1], ghost_y[1], is_ghost[1], board_x[2]);//怪物自主移动

if (key == 1)//显示钥匙
{
putimage(key_x, key_y, &img_key_mask, SRCAND);
putimage(key_x, key_y, &img_key, SRCPAINT);

}

if (enter)//显示锁
{
putimage(lock_x, lock_y - 100, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y - 100, &img_lock, SRCPAINT);
mciSendString("stop lockmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close lockmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_LockOpen.mp3 alias lockmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play lockmusic", NULL, 0, NULL); // 仅播放一次
}
else
{
putimage(lock_x, lock_y, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y, &img_lock, SRCPAINT);
}

if (pill == 1)//显示药物
{
putimage(pill_x, pill_y, &img_pill_mask, SRCAND);
putimage(pill_x, pill_y, &img_pill, SRCPAINT);

}

for (int i = 0; i < 3; i++)//显示水果
{
if (fruit[i] == 1)
{
putimage(fruit_x[i], fruit_y[i], &img_fruit_mask, SRCAND);
putimage(fruit_x[i], fruit_y[i], &img_fruit, SRCPAINT);

}


}
for (int i = 2; i <= 12; i++)//显示豆子
{
for (int j = 0; j < 3; j++)
{
if (bean[i][j] == 1)
{
if (i != 3 || j != 1)
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean, SRCPAINT);
}
else
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus, SRCPAINT);
}
}
}
}

}

FlushBatchDraw();
}



//与输入无关的更新
{



play_times++;//显示小球动画
ghost_play_times++;//显示怪物动画
weak_times++;//可被消灭时间计时
//恢复初始化
if (play_times == 12)
play_times = 0;

if (ghost_play_times == 4)
ghost_play_times = 0;

if (weak_times == 500)
{
weak_times = 0;
is_weak = false;
}
if (invincible)
invincible_time--;//无敌设置
if (invincible_time == 0)
{
invincible = false;
invincible_time = 50;
}


for (int i = 1; i <= 12; i++)//判断是否在平台上
{
if (i >= 2)
{
if (is_hit(board_x[i], board_y[i], board_length, 30, Hero_size))
{
is_onboard = true;

}
}
if (i == 1)
{
if (is_hit(board_x[i], board_y[i], first_board_length, 10, Hero_size))
{
is_onboard = true;
}
}

}

ghost_x[1] += v_ghost;//怪物自主移动
ghost_y[1] = board_y[2] - 50;//同步相对运动


if (!is_onboard && !can_jump)//如果碰不到平台就下降(空中也要下降)
{
drop();
}

if (vy_up <= 0)//到顶不可以再跳了
{
vy_down = 0;
can_jump = false;
}

jump();

for (int i = 0; i < 3; i++)//吃水果判断
{
if (is_hit(fruit_x[i], fruit_y[i], 30, 30, Hero_size) && fruit[i])
{
score += 100;
fruit[i] = 0;
mciSendString("stop fruitmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close fruitmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetFruit.mp3 alias fruitmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play fruitmusic", NULL, 0, NULL); // 仅播放一次
}
}

if (is_hit(pill_x, pill_y, 30, 30, Hero_size) && pill)//吃药物判断
{
mciSendString("stop pillmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close pillmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_Aid.mp3 alias pillmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play pillmusic", NULL, 0, NULL); // 仅播放一次
if (life <= 50)
life = 75;
pill = 0;
}

if (is_hit(key_x, key_y, 260, 15, Hero_size))//拿钥匙判断
{
mciSendString("stop keymusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close keymusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetKey.mp3 alias keymusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play keybeanmusic", NULL, 0, NULL); // 仅播放一次
key = 2;
}

if (key == 2 && is_hit(lock_x, lock_y, 30, 400, Hero_size))//闯关判断
enter = true;

for (int i = 2; i <= 12; i++)//判断是否加分
{
for (int j = 0; j < 3; j++)
{
if (is_score(board_x[i] + 25 + 85 * j, board_y[i] - 45) && (bean[i][j] == 1))
{
mciSendString("stop eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetBean.mp3 alias eatbeanmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play eatbeanmusic", NULL, 0, NULL); // 仅播放一次
bean[i][j] = 0;
score += 10;
}
}
}

if (bean[3][1] == 0)//强力豆消灭怪物判断
{
is_weak = true;
bean[3][1] = 2;
mciSendString("stop eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetBeanPlus.mp3 alias eatbeanplusmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play eatbeanplusmusic", NULL, 0, NULL); // 仅播放一次
}
if (!invincible)
is_hurt(ghost_x[1], ghost_y[1], &is_ghost[1]);//判断是否受伤

for (int i = 1; i <= 12; i++)//平台相对移动
{
if ((Hero_x > 50 && board_move_x == -10) || (board_move_x == 10 && Hero_x >= 50))
{
if (Hero_x>100 && Hero_x <= 200)
board_x[i] += 2 * board_move_x;
else if (Hero_x>200 && Hero_x <= 300)
board_x[i] += 3 * board_move_x;
else if (Hero_x>300 && Hero_x <= 400)
board_x[i] += 4 * board_move_x;
else if (Hero_x>400 && Hero_x <= 700)
board_x[i] += 6 * board_move_x;
else
board_x[i] += board_move_x;
}

board_y[i] += board_move_y;
}

board_move_x = board_move_y = 0;//初始化
}


//与输入有关的更新
{
if (kbhit())
{
char input = getch();//按键操作

if ((GetAsyncKeyState(0x41) & 0x8000) && Hero_x > 0)//a
{
direction = 1;
Hero_x -= 10;
board_move_x = 10;
}

if ((GetAsyncKeyState(0x44) & 0x8000) && Hero_x < Width)//d
{
direction = 0;
if (Hero_x + 2 * Hero_size >= lock_x)
{
if (enter)
{
Hero_x += 10;
board_move_x = -10;
}
}
else
{
Hero_x += 10;
board_move_x = -10;
}
}

if ((GetAsyncKeyState(0x57) & 0x8000) && is_onboard)//按下了w并且在平台上就可以跳了
{
can_jump = true;
}
}
is_onboard = false;//重新判断
}


}



void map2()//第二张地图
{

//初始化
if (startup)
{
mciSendString("stop bkmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close bkmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("stop beginmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close beginmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_LevelBegin.mp3 alias beginmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play beginmusic", NULL, 0, NULL); // 仅播放一次

initgraph(Width, High);

setbkmode(TRANSPARENT);
Hero_x = 0;
Hero_y = High / 5 * 3 + 50;
Hero_size = 50;
Bean_size = 20;
Ghost_size = 50;
first_board_length = 100;
board_length = 240;
vy_up = 15;
vy_down = 0;
v_ghost = 2;
can_jump = false;
invincible = false;
is_weak = false;
is_onboard = true;
startup = true;
enter = false;
invisible = false;
life = 75;
direction = 0;//0向右,1向左
ghost_direction = 0;
play_times = 0;
weak_times = 0;
death_times = 0;
ghost_play_times = 0;
invincible_time = 50;
pill = 1;
key = 1;
is_eraser = 1;

//加载图片
loadimage(&img_bk1, "素材\\Scene 1.jpg", 800, 600);
loadimage(&img_bk2, "素材\\Scene 2.jpg", 800, 600);
loadimage(&img_bk3, "素材\\Scene 3.jpg", 800, 600);
loadimage(&img_Hero, "素材\\hero.bmp");
loadimage(&img_Hero_mask, "素材\\hero_mask.bmp");
loadimage(&img_board, "素材\\Beginning.bmp", 100, 10);
loadimage(&img_board_mask, "素材\\Beginning_mask.bmp", 100, 10);
loadimage(&img_bean, "素材\\bean.bmp", 20, 20);
loadimage(&img_bean_mask, "素材\\bean_mask.bmp", 20, 20);
loadimage(&img_ghost, "素材\\ghost.jpg");
loadimage(&img_ghost_mask, "素材\\ghost_mask.jpg");
loadimage(&img_Board, "素材\\Board.bmp", 240, 30);
loadimage(&img_Board_mask, "素材\\Board_mask.bmp", 240, 30);
loadimage(&img_ghost_weak, "素材\\ghostWeak.bmp");
loadimage(&img_ghost_weak_mask, "素材\\ghostWeak_mask.bmp");
loadimage(&img_bean_plus, "素材\\beanplus.bmp", 30, 30);
loadimage(&img_bean_plus_mask, "素材\\beanplus_mask.bmp", 30, 30);
loadimage(&img_hero_death, "素材\\heroDeath.bmp");
loadimage(&img_hero_death_mask, "素材\\heroDeath_mask.bmp");
loadimage(&img_healthEmpty, "素材\\healthEmpty.bmp");
loadimage(&img_healthFull, "素材\\healthFull.bmp");
loadimage(&img_healthLittle, "素材\\healthLittle.bmp");
loadimage(&img_healthMuch, "素材\\healthMuch.bmp");
loadimage(&img_health_mask, "素材\\health_mask.bmp");
loadimage(&img_fruit, "素材\\fruit_cherry.bmp");
loadimage(&img_fruit_mask, "素材\\fruit_mask.bmp");
loadimage(&img_pill, "素材\\aid.bmp");
loadimage(&img_pill_mask, "素材\\aid_mask.bmp");
loadimage(&img_key, "素材\\key.bmp");
loadimage(&img_key_mask, "素材\\key_mask.bmp");
loadimage(&img_lock, "素材\\lock_upright.bmp", 30, 400);
loadimage(&img_lock_mask, "素材\\lock_upright_mask.bmp", 30, 400);
loadimage(&img_super_board, "素材\\BoardHighJump.bmp");
loadimage(&img_super_board_mask, "素材\\BoardHighJump_mask.bmp");
loadimage(&img_eraser, "素材\\eraser.bmp");
loadimage(&img_eraser_empty, "素材\\eraserEmpty.bmp");
loadimage(&img_eraser_mask, "素材\\eraser_mask.bmp");



for (int i = 0; i < 12; i++)//豆子显示初始化
{
for (int j = 0; j < 3; j++)
bean[i][j] = 1;
}
for (int i = 0; i < 3; i++)
{
fruit[i] = 1;
}
//部分豆子被水果等替代
bean[4][1] = 0;
bean[6][1] = 0;
bean[7][1] = 0;
bean[9][0] = 0;
bean[9][1] = 0;
bean[9][2] = 0;
bean[11][1] = 0;
bean[11][0] = 0;
bean[11][2] = 0;

for (int i = 1; i <= 4; i++)
{
is_ghost[i] = 1;
}
//平台坐标
board_x[1] = 0;
board_y[1] = 460;
board_x[2] = 250;
board_y[2] = 360;
board_x[3] = 650;
board_y[3] = 460;
board_x[4] = 1090;
board_y[4] = 60;
board_x[0] = 1090;
board_y[0] = 560;
board_x[5] = 1530;
board_y[5] = 260;
board_x[6] = 1930;
board_y[6] = 160;
board_x[7] = 2260;
board_y[7] = 300;
board_x[8] = 2530;
board_y[8] = 80;
board_x[9] = 2850;
board_y[9] = -50;
board_x[10] = 3100;
board_y[10] = 180;
board_x[11] = 3090;
board_y[11] = 490;
board_x[12] = 3400;
board_y[12] = 340;

fruit_x[0] = board_x[4] + 25 + 85 * 1;//水果坐标
fruit_y[0] = board_y[4] - 45;
fruit_x[1] = board_x[7] + 25 + 85 * 1;
fruit_y[1] = board_y[7] - 45;
fruit_x[2] = board_x[9] + 25 + 85 * 1;
fruit_y[2] = board_y[9] - 45;

pill_x = board_x[6] + 25 + 85 * 1;//药物坐标
pill_y = board_y[6] - 45;

key_x = board_x[11] + 25 + 85 * 1;//钥匙坐标
key_y = board_y[11] - 45;

lock_x = board_x[12] + board_length / 2 - 50;//锁的坐标
lock_y = board_y[12] - 400;

is_ghost[1] = 0;
ghost_x[2] = board_x[11] + 10;
ghost_y[2] = board_y[11] - 50;
ghost_x[3] = board_x[11] + 80;
ghost_y[3] = board_y[11] - 50;
ghost_x[4] = board_x[11] + 150;
ghost_y[4] = board_y[11] - 50;

eraser_x = board_x[9] + 25;//橡皮坐标
eraser_y = board_y[9] - 45;


BeginBatchDraw();
}
startup = false;
mciSendString("open 音乐音效\\bkmusic_Scene2.mp3 alias bkmusic", NULL, 0, NULL);//打开背景音乐
mciSendString("play bkmusic repeat", NULL, 0, NULL); // 循环播放
//显示
{
putimage(0, 0, &img_bk2);

show_life();//显示生命值
char s[5];
settextcolor(BLACK);
settextstyle(20, 0, _T("黑体"));
sprintf(s, "%d", score);
outtextxy(300, 100, "得分: ");
outtextxy(400, 100, s);

if (is_dead())//死亡画面
{
if (death_times >= 21)
{
outtextxy(335, 300, "GAME OVER!");
gameover();
}
else
{
putimage(board_x[0], board_y[0], &img_super_board_mask, SRCAND);
putimage(board_x[0], board_y[0], &img_super_board, SRCPAINT);//跳台

if (is_eraser)
{
putimage(eraser_x, eraser_y, &img_eraser_mask, SRCAND);
putimage(eraser_x, eraser_y, &img_eraser, SRCPAINT);
}
else
{
putimage(eraser_x, eraser_y, &img_eraser_mask, SRCAND);
putimage(eraser_x, eraser_y, &img_eraser_empty, SRCPAINT);
}
putimage(Hero_x, Hero_y, 50, 50, &img_hero_death_mask, 50 * death_times, 0, SRCAND);//死亡动画
putimage(Hero_x, Hero_y, 50, 50, &img_hero_death, 50 * death_times, 0, SRCPAINT);

putimage(board_x[1], board_y[1], &img_board_mask, SRCAND);
putimage(board_x[1], board_y[1], &img_board, SRCPAINT);

for (int i = 2; i <= 12; i++)
{
putimage(board_x[i], board_y[i], &img_Board_mask, SRCAND);
putimage(board_x[i], board_y[i], &img_Board, SRCPAINT);
}
//相对移动坐标同步
fruit_x[0] = board_x[4] + 25 + 85 * 1;
fruit_y[0] = board_y[4] - 45;
fruit_x[1] = board_x[7] + 25 + 85 * 1;
fruit_y[1] = board_y[7] - 45;
fruit_x[2] = board_x[9] + 25 + 85 * 1;
fruit_y[2] = board_y[9] - 45;

pill_x = board_x[6] + 25 + 85 * 1;
pill_y = board_y[6] - 45;

key_x = board_x[11] + 25 + 85 * 1;
key_y = board_y[11] - 45;

lock_x = board_x[12] + board_length / 2 - 50;
lock_y = board_y[12] - 400;

eraser_x = board_x[9] + 25;
eraser_y = board_y[9] - 45;

for (int i = 2; i <= 4; i++)
{
ghost_move(ghost_x[i], ghost_y[i], is_ghost[i], board_x[11]);
}//怪物自主移动

if (key == 1)//显示钥匙
{
putimage(key_x, key_y, &img_key_mask, SRCAND);
putimage(key_x, key_y, &img_key, SRCPAINT);

}

if (enter)//显示锁
{
putimage(lock_x, lock_y - 100, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y - 100, &img_lock, SRCPAINT);
}
else
{
putimage(lock_x, lock_y, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y, &img_lock, SRCPAINT);
}

if (pill == 1)//显示药物
{
putimage(pill_x, pill_y, &img_pill_mask, SRCAND);
putimage(pill_x, pill_y, &img_pill, SRCPAINT);

}

for (int i = 0; i < 3; i++)//显示水果
{
if (fruit[i] == 1)
{
putimage(fruit_x[i], fruit_y[i], &img_fruit_mask, SRCAND);
putimage(fruit_x[i], fruit_y[i], &img_fruit, SRCPAINT);

}


}
for (int i = 2; i <= 12; i++)//显示豆子
{
for (int j = 0; j < 3; j++)
{
if (bean[i][j] == 1)
{
if (i != 3 || j != 1)
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean, SRCPAINT);
}
else
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus, SRCPAINT);
}
}
}
}

death_times++;
}
}
else//正常画面
{
putimage(board_x[0], board_y[0], &img_super_board_mask, SRCAND);
putimage(board_x[0], board_y[0], &img_super_board, SRCPAINT);//跳台

if (is_eraser)
{
putimage(eraser_x, eraser_y, &img_eraser_mask, SRCAND);
putimage(eraser_x, eraser_y, &img_eraser, SRCPAINT);
}
else
{
putimage(eraser_x, eraser_y, &img_eraser_mask, SRCAND);
putimage(eraser_x, eraser_y, &img_eraser_empty, SRCPAINT);
}
if (invincible || invisible)//无敌或者隐身动画
{
putimage(Hero_x, Hero_y, 50, 50, &img_Hero_mask, 50 * play_times, (direction + 2) * 50, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_Hero, 50 * play_times, (direction + 2) * 50, SRCPAINT);

}
else//正常动画
{
putimage(Hero_x, Hero_y, 50, 50, &img_Hero_mask, 50 * play_times, direction * 50, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_Hero, 50 * play_times, direction * 50, SRCPAINT);
}

putimage(board_x[1], board_y[1], &img_board_mask, SRCAND);
putimage(board_x[1], board_y[1], &img_board, SRCPAINT);

for (int i = 2; i <= 12; i++)
{
putimage(board_x[i], board_y[i], &img_Board_mask, SRCAND);
putimage(board_x[i], board_y[i], &img_Board, SRCPAINT);
}

//相对移动坐标同步
fruit_x[0] = board_x[4] + 25 + 85 * 1;
fruit_y[0] = board_y[4] - 45;
fruit_x[1] = board_x[7] + 25 + 85 * 1;
fruit_y[1] = board_y[7] - 45;
fruit_x[2] = board_x[9] + 25 + 85 * 1;
fruit_y[2] = board_y[9] - 45;

pill_x = board_x[6] + 25 + 85 * 1;
pill_y = board_y[6] - 45;

key_x = board_x[11] + 25 + 85 * 1;
key_y = board_y[11] - 45;

lock_x = board_x[12] + board_length / 2 - 50;
lock_y = board_y[12] - 400;

eraser_x = board_x[9] + 25;
eraser_y = board_y[9] - 45;

for (int i = 2; i <= 4; i++)
{
ghost_move(ghost_x[i], ghost_y[i], is_ghost[i], board_x[11]);
}//怪物自主移动

if (key == 1)//显示钥匙
{
putimage(key_x, key_y, &img_key_mask, SRCAND);
putimage(key_x, key_y, &img_key, SRCPAINT);

}

if (enter)//显示锁
{
putimage(lock_x, lock_y - 100, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y - 100, &img_lock, SRCPAINT);
mciSendString("stop lockmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close lockmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_LockOpen.mp3 alias lockmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play lockmusic", NULL, 0, NULL); // 仅播放一次
}
else
{
putimage(lock_x, lock_y, &img_lock_mask, SRCAND);
putimage(lock_x, lock_y, &img_lock, SRCPAINT);
}

if (pill == 1)//显示药物
{
putimage(pill_x, pill_y, &img_pill_mask, SRCAND);
putimage(pill_x, pill_y, &img_pill, SRCPAINT);

}

for (int i = 0; i < 3; i++)//显示水果
{
if (fruit[i] == 1)
{
putimage(fruit_x[i], fruit_y[i], &img_fruit_mask, SRCAND);
putimage(fruit_x[i], fruit_y[i], &img_fruit, SRCPAINT);

}


}
for (int i = 2; i <= 12; i++)//显示豆子
{
for (int j = 0; j < 3; j++)
{
if (bean[i][j] == 1)
{
if (i != 3 || j != 1)
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 35, &img_bean, SRCPAINT);
}
else
{
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus_mask, SRCAND);
putimage(board_x[i] + 25 + 85 * j, board_y[i] - 40, &img_bean_plus, SRCPAINT);
}
}
}
}

}

FlushBatchDraw();
}



//与输入无关的更新
{



play_times++;//显示小球动画
ghost_play_times++;//显示怪物动画
weak_times++;//可被消灭时间计时
//恢复初始化
if (play_times == 12)
play_times = 0;

if (ghost_play_times == 4)
ghost_play_times = 0;

if (weak_times == 500)
{
weak_times = 0;
is_weak = false;
}
if (invincible)
invincible_time--;//无敌设置
if (invincible_time == 0)
{
invincible = false;
invincible_time = 50;
invisible = false;
}


for (int i = 0; i <= 12; i++)//判断是否在平台上
{
if (i >= 2)
{
if (is_hit(board_x[i], board_y[i], board_length, 30, Hero_size))
{
is_onboard = true;

}
}
if (i == 1)
{
if (is_hit(board_x[i], board_y[i], first_board_length, 10, Hero_size))
{
is_onboard = true;
}
}

}
for (int i = 1; i <= 4; i++)
{
ghost_x[i] += v_ghost;//怪物自主移动
}
ghost_y[1] = board_y[2] - 50;//同步相对运动
for (int i = 2; i <= 4; i++)
{
ghost_y[i] = board_y[11] - 50;
}
if (is_hit(board_x[0], board_y[0], board_length, 30, Hero_size))//判断跳台
{
can_jump = true;
vy_up = 25;
mciSendString("stop jpmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close jpmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_HighJump.mp3 alias jpmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play jpmusic", NULL, 0, NULL); // 仅播放一次
}

if (is_hit(eraser_x, eraser_y, 30, 30, Hero_size))//吃橡皮擦隐身
{
mciSendString("stop erasermusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close erasermusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetEraser.mp3 alias erasermusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play erasermusic", NULL, 0, NULL); // 仅播放一次
is_eraser = 0;
invisible = true;
}

if (!is_onboard && !can_jump)//如果碰不到平台就下降(空中也要下降)
{
drop();
}

if (vy_up <= 0)//到顶不可以再跳了
{
vy_down = 0;
can_jump = false;
}

jump();

for (int i = 0; i < 3; i++)//吃水果判断
{
if (is_hit(fruit_x[i], fruit_y[i], 30, 30, Hero_size) && fruit[i])
{
score += 100;
fruit[i] = 0;
mciSendString("stop fruitmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close fruitmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetFruit.mp3 alias fruitmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play fruitmusic", NULL, 0, NULL); // 仅播放一次
}
}

if (is_hit(pill_x, pill_y, 30, 30, Hero_size) && pill)//吃药物判断
{
mciSendString("stop pillmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close pillmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_Aid.mp3 alias pillmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play pillmusic", NULL, 0, NULL); // 仅播放一次
if (life <= 50)
life = 75;
pill = 0;
}

if (is_hit(key_x, key_y, 260, 15, Hero_size))//拿钥匙判断
{
mciSendString("stop keymusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close keymusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetKey.mp3 alias keymusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play keybeanmusic", NULL, 0, NULL); // 仅播放一次
key = 2;
}

if (key == 2 && is_hit(lock_x, lock_y, 30, 400, Hero_size))//闯关判断
enter = true;

for (int i = 2; i <= 12; i++)//判断是否加分
{
for (int j = 0; j < 3; j++)
{
if (is_score(board_x[i] + 25 + 85 * j, board_y[i] - 45) && (bean[i][j] == 1))
{
mciSendString("stop eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetBean.mp3 alias eatbeanmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play eatbeanmusic", NULL, 0, NULL); // 仅播放一次
bean[i][j] = 0;
score += 10;
}
}
}

if (bean[3][1] == 0)//强力豆消灭怪物判断
{
mciSendString("stop eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close eatbeanmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetBeanPlus.mp3 alias eatbeanplusmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play eatbeanplusmusic", NULL, 0, NULL); // 仅播放一次
is_weak = true;
bean[3][1] = 2;

}
if (!invisible && !invincible)
{
for (int i = 1; i <= 4; i++)
is_hurt(ghost_x[i], ghost_y[i], &is_ghost[i]);//判断是否受伤
}

for (int i = 0; i <= 12; i++)//平台相对移动
{
if ((Hero_x > 50 && board_move_x == -10) || (board_move_x == 10 && Hero_x >= 50))
{
if (Hero_x > 100 && Hero_x <= 200)
{
board_x[i] += 2 * board_move_x;
}
else if (Hero_x > 200 && Hero_x <= 300)
{
board_x[i] += 3 * board_move_x;
}
else if (Hero_x > 300 && Hero_x <= 400)
{
board_x[i] += 4 * board_move_x;
}
else if (Hero_x > 400 && Hero_x <= 700)
{
board_x[i] += 6 * board_move_x;
}
else
{
board_x[i] += board_move_x;
}
}

board_y[i] += board_move_y;
}

board_move_x = board_move_y = 0;//初始化
}


//与输入有关的更新
{
if (kbhit())
{
char input = getch();//按键操作

if ((GetAsyncKeyState(0x41) & 0x8000) && Hero_x > 0)//a
{
direction = 1;
Hero_x -= 10;
board_move_x = 10;
}

if ((GetAsyncKeyState(0x44) & 0x8000) && Hero_x < Width)//d
{
direction = 0;
if (Hero_x + Hero_size >= lock_x)
{
if (enter)
{
Hero_x += 10;
board_move_x = -10;
}
}
else
{
Hero_x += 10;
board_move_x = -10;
}
}

if ((GetAsyncKeyState(0x57) & 0x8000) && is_onboard)//按下了w并且在平台上就可以跳了
{
can_jump = true;
}
}
is_onboard = false;//重新判断
}

}



void map3()
{
//初始化
if (startup)
{
mciSendString("stop bkmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close bkmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("stop beginmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close beginmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_LevelBegin.mp3 alias beginmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play beginmusic", NULL, 0, NULL); // 仅播放一次

initgraph(Width, High);

setbkmode(TRANSPARENT);
Hero_x = 0;
Hero_y = High / 5 * 3 + 50;
Hero_size = 50;
Bean_size = 20;
Ghost_size = 50;
board_length = 240;
vy_up = 15;
vy_down = 0;
v_ghost = 2;
can_jump = false;
invincible = false;
is_onboard = true;
is_fire = false;
is_fire_boss = true;
boss_hurt = false;

life = 75;
boss_life = 250;
direction = 0;//0向右,1向左
ghost_direction = 0;
play_times = 0;
death_times = 0;
ghost_play_times = 0;
invincible_time = 50;
bullet_x = 0;
bullet_y = 900;


for (int i = 1; i <= 3; i++)
{
is_ghost[i] = 1;
}
ghost_x[1] = Width / 2;
ghost_y[1] = High / 2;
ghost_x[2] = ghost_x[1] + 200;
ghost_y[2] = ghost_y[1] + 250;
ghost_x[3] = 620;
ghost_y[3] = 100;

board_x[1] = 105;
board_y[1] = 200;
board_x[2] = 335;
board_y[2] = 330;
board_x[3] = 170;
board_y[3] = 460;

//地面
board_x[0] = 0;
board_y[0] = 550;




}
startup = false;
mciSendString("open 音乐音效\\bkmusic_Scene3.mp3 alias bkmusic", NULL, 0, NULL);//打开背景音乐
mciSendString("play bkmusic repeat", NULL, 0, NULL); // 循环播放
//显示
{
putimage(0, 0, &img_bk3);
char s[5];
settextcolor(WHITE);
settextstyle(20, 0, _T("黑体"));
sprintf(s, "%d", score);
outtextxy(300, 100, "得分: ");
outtextxy(400, 100, s);
if (is_dead())//死亡画面
{
if (death_times >= 21)
{
outtextxy(335, 300, "GAME OVER!");
gameover();
}
else
{
show_life_hero();
show_life_boss();
putimage(Hero_x, Hero_y, 50, 50, &img_hero_death_mask, 50 * death_times, 0, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_hero_death, 50 * death_times, 0, SRCPAINT);
for (int i = 1; i <= 2; i++)//小怪图片
{
if (is_ghost[i])
{
putimage(ghost_x[i], ghost_y[i], 50, 50, &img_ghost_mask, 50 * ghost_play_times, ghost_direction * 50, SRCAND);
putimage(ghost_x[i], ghost_y[i], 50, 50, &img_ghost, 50 * ghost_play_times, ghost_direction * 50, SRCPAINT);
if (ghost_x[i] > Hero_x)
ghost_x[i] -= 10;
if (ghost_x[i] <= Hero_x)
ghost_x[i] += 10;
if (ghost_y[i] > Hero_y)
ghost_y[i] -= 10;
if (ghost_y[i] <= Hero_y)
ghost_y[i] += 10;

}
}
//BOSS图片
putimage(ghost_x[3], ghost_y[3], 180, 180, &img_boss_mask, 180 * play_times, 0, SRCAND);
putimage(ghost_x[3], ghost_y[3], 180, 180, &img_boss, 180 * play_times, 0, SRCPAINT);

death_times++;
}
}
else if (boss_life<=0)
{
mciSendString("stop bkmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close bkmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\bkmusic_FinalWin.mp3 alias winmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play winmusic", NULL, 0, NULL); // 仅播放一次

putimage(Hero_x, Hero_y, 50, 50, &img_Hero_mask, 50 * play_times, direction * 50, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_Hero, 50 * play_times, direction * 50, SRCPAINT);

outtextxy(335, 300, "YOU WIN!");
outtextxy(275, 400, "PRESS SPACE TO EXIT");
}
else//正常画面
{
if (tip_time < 100 && Hero_x<board_x[1])
{

putimage(0, 0, &img_tip2_mask, SRCAND);
putimage(0, 0, &img_tip2, SRCPAINT);
tip_time++;
}
show_life_hero();
if (invincible)
{
putimage(Hero_x, Hero_y, 50, 50, &img_Hero_mask, 50 * play_times, (direction + 2) * 50, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_Hero, 50 * play_times, (direction + 2) * 50, SRCPAINT);

}
else
{
putimage(Hero_x, Hero_y, 50, 50, &img_Hero_mask, 50 * play_times, direction * 50, SRCAND);
putimage(Hero_x, Hero_y, 50, 50, &img_Hero, 50 * play_times, direction * 50, SRCPAINT);
}
//子弹图片
putimage(bullet_x, bullet_y, &img_bullet_mask, SRCAND);
putimage(bullet_x, bullet_y, &img_bullet, SRCPAINT);


//怪物跟随
for (int i = 1; i <= 2; i++)
{
if (is_ghost[i])
{
putimage(ghost_x[i], ghost_y[i], 50, 50, &img_ghost_mask, 50 * ghost_play_times, ghost_direction * 50, SRCAND);
putimage(ghost_x[i], ghost_y[i], 50, 50, &img_ghost, 50 * ghost_play_times, ghost_direction * 50, SRCPAINT);
if (ghost_x[i] > Hero_x)
ghost_x[i] -= 2;
if (ghost_x[i] <= Hero_x)
ghost_x[i] += 2;
if (ghost_y[i] > Hero_y)
ghost_y[i] -= 2;
if (ghost_y[i] <= Hero_y)
ghost_y[i] += 2;

}
}
//BOSS
if (boss_life > 0)
{
show_life_boss();
if (is_fire_boss)//开火
{
putimage(boss_bullet_x, boss_bullet_y, &img_boss_bullet_mask, SRCAND);
putimage(boss_bullet_x, boss_bullet_y, &img_boss_bullet, SRCPAINT);
}
putimage(ghost_x[3], ghost_y[3], 180, 180, &img_boss_mask, 180 * play_times, 0, SRCAND);
putimage(ghost_x[3], ghost_y[3], 180, 180, &img_boss, 180 * play_times, 0, SRCPAINT);
}
}

FlushBatchDraw();
}



//与输入无关的更新
{
play_times++;//显示小球动画
ghost_play_times++;//显示怪物动画

//恢复初始化
if (play_times == 12)
{
play_times = 0;
}

if (ghost_play_times == 4)
ghost_play_times = 0;

if (invincible)
invincible_time--;//无敌设置
if (invincible_time == 0)
{
invincible = false;
invincible_time = 50;
}


if (ghost_y[3] > Hero_y&&ghost_y[3] > 0)//同步移动
ghost_y[3] = Hero_y - 90;
if (ghost_y[3] <= Hero_y && ghost_y[3] + 200 < High)
ghost_y[3] = Hero_y - 90;

for (int i = 0; i <= 3; i++)//判断是否在平台上
{
if (i != 0)
{
if (is_hit(board_x[i], board_y[i], board_length, 30, Hero_size))
{
is_onboard = true;

}
}
else
{
if (is_hit(board_x[i], board_y[i], 600, 50, Hero_size))
{
is_onboard = true;
}
}

}
for (int i = 1; i <= 3; i++)//子弹发射
{
if (bullet_x >= Width || !(bullet_x + 35 <ghost_x[i] || bullet_x > ghost_x[i] + 180 || bullet_y > ghost_y[i] + 180 || bullet_y + 35 < ghost_y[i]))
{
is_fire = false;
if (!(bullet_x + 35 < ghost_x[i] || bullet_x > ghost_x[i] + 180 || bullet_y > ghost_y[i] + 180 || bullet_y + 35 < ghost_y[i]))
{
if (i == 3)
boss_hurt = true;
else
{
mciSendString("stop getmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close getmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetGhost.mp3 alias getmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play getmusic", NULL, 0, NULL); // 仅播放一次
is_ghost[i] = 0;
}
}
}
else
{
if (boss_hurt&&bullet_x >= ghost_x[3] + 170)//BOSS掉血
{
boss_hurt = false;
boss_life -= 25;
mciSendString("stop getmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close getmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_GetGhost.mp3 alias getmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play getmusic", NULL, 0, NULL); // 仅播放一次

}
if (fire_direction == 1)
bullet_x -= 10;
else
bullet_x += 10;
}
}
//怪物子弹
if ((boss_bullet_x <= 0 || !(boss_bullet_x + 35 <Hero_x || boss_bullet_x > Hero_x + 50 || boss_bullet_y > Hero_y + 50 || boss_bullet_y + 35 < Hero_y))&&boss_life>0)
{
is_fire_boss = false;

if (!(boss_bullet_x + 35 <Hero_x || boss_bullet_x > Hero_x + 50 || boss_bullet_y > Hero_y + 50 || boss_bullet_y + 35 < Hero_y))
{
life -= 25;
mciSendString("stop hurtmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close hurtmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_Injured.mp3 alias hurtmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play hurtmusic", NULL, 0, NULL); // 仅播放一次
}
boss_bullet_x = ghost_x[3] - 50;
boss_bullet_y = ghost_y[3] + 90;
}
else
{
is_fire_boss = true;
boss_bullet_x -= 10;
}

if (!is_onboard && !can_jump)//如果碰不到平台就下降(空中也要下降)
{
drop();
}

if (vy_up <= 0)//到顶不可以再跳了
{
vy_down = 0;
can_jump = false;
}

jump();

for (int i = 1; i <= 2; i++)
{
if (!invincible)
{
is_hurt(ghost_x[i], ghost_y[i], &is_ghost[i]);
}
}//判断是否受伤

}


//与输入有关的更新
{
if (kbhit())
{
char input = getch();//按键操作

if ((GetAsyncKeyState(0x41) & 0x8000) && Hero_x > 0)//a
{
direction = 1;
Hero_x -= 10;
}

if ((GetAsyncKeyState(0x44) & 0x8000) && Hero_x < Width)//d
{
direction = 0;
Hero_x += 10;
}

if ((GetAsyncKeyState(0x57) & 0x8000) && is_onboard)//按下了w并且在平台上就可以跳了
{
can_jump = true;
}
if (input == ' ')
{
if (boss_life <= 0)
{
gameover();
}
else
{
is_fire = true;
mciSendString("stop shootmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close shootmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_Shoot.mp3 alias shootmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play shootmusic", NULL, 0, NULL); // 仅播放一次
fire_direction = direction;
if (direction % 2)//子弹初始化位置
bullet_x = Hero_x - 15;
else
bullet_x = Hero_x + 50;
bullet_y = Hero_y + 18;
}
}
}
is_onboard = false;//重新判断
}
}





void gameover()
{
EndBatchDraw();
getch();
closegraph();
}


int main()
{
Startup();
logo();
while (true)//开始菜单
{
BeginBatchDraw();
putimage(0, 0, &img_start);
mciSendString("stop logomusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close logomusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\bkmusic_Startup.mp3 alias startmusic", NULL, 0, NULL); //背景音乐
mciSendString("play startmusic repeat", NULL, 0, NULL); // 循环播放

MOUSEMSG mouse;
mouse = GetMouseMsg();
if (mouse.mkLButton&&mouse.x<450 && mouse.x>325 && mouse.y >245 && mouse.y <280 )//点击按钮
{
FlushMouseMsgBuffer();
break;
}

if (mouse.mkLButton&&mouse.x<435 && mouse.x>340 && mouse.y >325 && mouse.y <360 )
{
FlushMouseMsgBuffer();
closegraph();
exit(0);
}

FlushBatchDraw();

}
mciSendString("stop startmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close startmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
while (true)
{

Sleep(50);
for (int i = 1; i <= 12; i++)//判断是否在平台上
{
if (i >= 2)
{
if (is_hit(board_x[i], board_y[i], board_length, 30, Hero_size))
{
is_onboard = true;

}
}
if (i == 1)
{
if (is_hit(board_x[i], board_y[i], first_board_length, 10, Hero_size))
{
is_onboard = true;
}
}

}
if ((Hero_x >= board_x[12] + board_length / 2) && map_num < 3 && is_onboard)//判断是否进入下一关卡
{
mciSendString("stop endmusic", NULL, 0, NULL); // 先把前面一次的音乐停止
mciSendString("close endmusic", NULL, 0, NULL); // 先把前面一次的音乐关闭
mciSendString("open 音乐音效\\sound_LevelEnd.mp3 alias endmusic", NULL, 0, NULL); // 打开跳动音乐
mciSendString("play endmusic", NULL, 0, NULL); // 仅播放一次
map_num++;
startup = true;
}
switch (map_num)//地图选择
{
case 1:map1(); break;
case 2:map2(); break;
case 3:map3(); break;
default:
break;
}
}
gameover();
}