#include <stdio.h>
#include <graphics.h>
#include <time.h>
#include <mmsystem.h>
#define _CRT_SECURE_NO_WARNINGS
#pragma comment(lib,"winmm.lib")
#define WIN_WIDTH 480
#define WIN_HEIGHT 700
#define PLAYER_WIDTH 80
#define PLAYER_HEIGHT 100
#define BULLET_NUM 10
#define ENEMY_NUM 10
#define PLAYER_SPEED 5
#define BULLET_SPEED 6
#define ENEMY_SPEED_SMALL 4
#define ENEMY_SPEED_MEDIUM 3
#define ENEMY_SPEED_BIG 2
#define PLAYER_HP 3
#define SMALL_HP 1
#define MEDIUM_HP 2
#define BIG_HP 3
int bk1 = 0, bk2 = WIN_HEIGHT;
struct Image
{
IMAGE background;
IMAGE player, playerY;
IMAGE bullet, bulletY;
IMAGE enemy1, enemy1Y;
IMAGE enemy2, enemy2Y;
IMAGE enemy3, enemy3Y;
}image;
enum TYPE
{
SAMLL,
MEDIUM,
BIG,
};
struct Plane
{
int x;
int y;
bool flag;
union
{
int type;
int score;
}Myunion;
int hp;
}player, bullet[BULLET_NUM], enemy[ENEMY_NUM];
DWORD t1, t2, t3, t4;
void init();
void drawImage();
void drawText();
void createBullet();
void userKey();
void BulletMove();
void createEnemy();
void enemyMove();
void colliDetec();
void bkRemove();
void playGame();
int main()
{
srand((unsigned int)time(NULL));
playGame();
getchar();
closegraph();
return 0;
}
void init()
{
mciSendString("open BGM.mp3 alias bgm", 0, 0, 0);
mciSendString("play bgm repeat", 0, 0, 0);
loadimage(&image.background, "background.png");
loadimage(&image.player, "player.png", PLAYER_WIDTH, PLAYER_HEIGHT);
loadimage(&image.playerY, "playerY.png", PLAYER_WIDTH, PLAYER_HEIGHT);
player.x = WIN_WIDTH / 2 - PLAYER_WIDTH / 2;
player.y = WIN_HEIGHT - PLAYER_HEIGHT * 2;
player.flag = true;
player.hp = PLAYER_HP;
player.Myunion.score = 0;
loadimage(&image.bullet, "bullet.png");
loadimage(&image.bulletY, "bulletY.png");
for (int i = 0; i < BULLET_NUM; i++)
{
bullet[i].flag = false;
}
loadimage(&image.enemy1, "enemy1.png");
loadimage(&image.enemy1Y, "enemy1Y.png");
loadimage(&image.enemy2, "enemy2.png");
loadimage(&image.enemy2Y, "enemy2Y.png");
loadimage(&image.enemy3, "enemy3.png", 100, 153);
loadimage(&image.enemy3Y, "enemy3Y.png", 100, 153);
for (int i = 0; i < ENEMY_NUM; i++)
{
enemy[i].flag = false;
}
t1 = GetTickCount();
t3 = t1;
}
void drawImage()
{
putimage(0, bk1, &image.background);
putimage(0, bk2, &image.background);
putimage(player.x, player.y, &image.playerY, SRCAND);
putimage(player.x, player.y, &image.player, SRCPAINT);
for (int i = 0; i < BULLET_NUM; i++)
{
if (bullet[i].flag)
{
putimage(bullet[i].x, bullet[i].y, &image.bulletY, SRCAND);
putimage(bullet[i].x, bullet[i].y, &image.bullet, SRCPAINT);
}
}
for (int i = 0; i < ENEMY_NUM; i++)
{
if (enemy[i].flag)
{
if (enemy[i].Myunion.type == SAMLL)
{
putimage(enemy[i].x, enemy[i].y, &image.enemy1Y, SRCAND);
putimage(enemy[i].x, enemy[i].y, &image.enemy1, SRCPAINT);
}
else if (enemy[i].Myunion.type == MEDIUM)
{
putimage(enemy[i].x, enemy[i].y, &image.enemy2Y, SRCAND);
putimage(enemy[i].x, enemy[i].y, &image.enemy2, SRCPAINT);
}
else
{
putimage(enemy[i].x, enemy[i].y, &image.enemy3Y, SRCAND);
putimage(enemy[i].x, enemy[i].y, &image.enemy3, SRCPAINT);
}
}
}
}
void drawText()
{
char str[20] = "";
sprintf_s(str, "剩余生命值:%d", player.hp);
settextstyle(30, 0, "楷体");
settextcolor(BROWN);
setbkmode(TRANSPARENT);
outtextxy(20, 20, str);
sprintf_s(str, "已获分数:%d", player.Myunion.score);
outtextxy(WIN_WIDTH / 2 + 20, 20, str);
}
void createBullet()
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (!bullet[i].flag)
{
bullet[i].flag = true;
bullet[i].x = player.x + PLAYER_WIDTH / 2 - 2;
bullet[i].y = player.y - 11;
break;
}
}
}
DWORD _stdcall playMusic(LPVOID lpParameter)
{
mciSendString("open Fire_1.mp3 alias fire", 0, 0, 0);
mciSendString("play fire wait", 0, 0, 0);
mciSendString("close fire", 0, 0, 0);
return 0;
}
void userKey()
{
t2 = GetTickCount();
if (GetAsyncKeyState(VK_UP))
{
player.y -= PLAYER_SPEED;
if (player.y < 0)
{
player.y = 0;
}
}
else if (GetAsyncKeyState(VK_DOWN))
{
player.y += PLAYER_SPEED;
if (player.y>WIN_HEIGHT - PLAYER_HEIGHT)
{
player.y = WIN_HEIGHT - PLAYER_HEIGHT;
}
}
else if (GetAsyncKeyState(VK_LEFT))
{
player.x -= PLAYER_SPEED;
if (player.x < -PLAYER_WIDTH / 2)
{
player.x = -PLAYER_WIDTH / 2;
}
}
else if (GetAsyncKeyState(VK_RIGHT))
{
player.x += PLAYER_SPEED;
if (player.x> WIN_WIDTH - PLAYER_WIDTH / 2)
{
player.x = WIN_WIDTH - PLAYER_WIDTH / 2;
}
}
if (GetAsyncKeyState(VK_SPACE) && t2 - t1>300)
{
createBullet();
HANDLE hThread = CreateThread(NULL, 0, playMusic, NULL, NULL, NULL);
t1 = t2;
}
}
void BulletMove()
{
for (int i = 0; i < BULLET_NUM; i++)
{
if (bullet[i].flag)
{
bullet[i].y -= BULLET_SPEED;
if (bullet[i].y <= -11)
{
bullet[i].flag = false;
}
}
}
}
void createEnemy()
{
t4 = GetTickCount();
if (t4 - t3 > 2000)
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (!enemy[i].flag)
{
enemy[i].flag = true;
int result = rand() % 10;
if (result == 0)
{
enemy[i].Myunion.type = BIG;
enemy[i].hp = BIG_HP;
enemy[i].x = rand() % (WIN_WIDTH - 100);
enemy[i].y = -153;
}
else if (result == 1 || result == 2)
{
enemy[i].Myunion.type = MEDIUM;
enemy[i].hp = MEDIUM_HP;
enemy[i].x = rand() % (WIN_WIDTH - 69);
enemy[i].y = -99;
}
else
{
enemy[i].Myunion.type = SAMLL;
enemy[i].hp = SMALL_HP;
enemy[i].x = rand() % (WIN_WIDTH - 57);
enemy[i].y = -43;
}
break;
}
}
t3 = t4;
}
}
void enemyMove()
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (enemy[i].flag)
{
if (enemy[i].Myunion.type == SAMLL)
{
enemy[i].y += ENEMY_SPEED_SMALL;
}
else if (enemy[i].Myunion.type == MEDIUM)
{
enemy[i].y += ENEMY_SPEED_MEDIUM;
}
else
{
enemy[i].y += ENEMY_SPEED_BIG;
}
if (enemy[i].y >= WIN_HEIGHT)
{
enemy[i].flag = false;
}
}
}
}
void colliDetec()
{
for (int i = 0; i < ENEMY_NUM; i++)
{
if (enemy[i].flag)
{
for (int j = 0; j < BULLET_NUM; j++)
{
if (bullet[j].flag)
{
if ((enemy[i].y + 43 >= bullet[j].y&&enemy[i].Myunion.type == SAMLL&&enemy[i].x <= bullet[j].x&&enemy[i].x + 57 >= bullet[j].x + 5) ||
(enemy[i].y + 99 >= bullet[j].y&&enemy[i].Myunion.type == MEDIUM&&enemy[i].x <= bullet[j].x&&enemy[i].x + 69 >= bullet[j].x + 5) ||
(enemy[i].y + 153 >= bullet[j].y&&enemy[i].Myunion.type == BIG&&enemy[i].x <= bullet[j].x&&enemy[i].x + 100 >= bullet[j].x + 5))
{
bullet[j].flag = false;
enemy[i].hp--;
if (enemy[i].hp == 0)
{
if (enemy[i].Myunion.type == SAMLL)
{
player.Myunion.score += 10 * SMALL_HP;
}
else if (enemy[i].Myunion.type == MEDIUM)
{
player.Myunion.score += 10 * MEDIUM_HP;
}
else
{
player.Myunion.score += 10 * BIG_HP;
}
enemy[i].flag = false;
}
}
}
}
if (enemy[i].y <= player.y + PLAYER_HEIGHT&&enemy[i].x <= player.x + PLAYER_WIDTH)
{
if ((enemy[i].x + 57 >= player.x&&enemy[i].y + 43 >= player.y&&enemy[i].Myunion.type == SAMLL&&enemy[i].x + 57 >= player.x&&enemy[i].x <= player.x + PLAYER_WIDTH) ||
(enemy[i].x + 69 >= player.x&&enemy[i].y + 99 >= player.y&&enemy[i].Myunion.type == MEDIUM&&enemy[i].x + 69 >= player.x&&enemy[i].x <= player.x + PLAYER_WIDTH) ||
(enemy[i].x + 100 >= player.x&&enemy[i].y + 153 >= player.y&&enemy[i].Myunion.type == BIG&&enemy[i].x + 100 >= player.x&&enemy[i].x <= player.x + PLAYER_WIDTH))
{
player.hp -= enemy[i].hp;