#include
#include
#include
#include
#include
#define HIGH 45
#define WIDTH 48
struct Gamer {
int gamer_x;
int gamer_y;
int score;
int death;
int monsterDeath;
int flag;
int bullet;
char recordRe;
};
struct Gamer gamer = {
22,
20,
0,
3,
0,
0,
0,
0,
};
struct FoodPos {
int food_x;
int food_y;
};
struct FoodPos food1 = {
94,
1
};
struct FoodPos food2 = {
66,
30
};
struct FoodPos food3 = {
6,
36
};
struct FoodPos food4 = {
66,
4
};
struct FoodPos food5 = {
8,
5
};
struct FoodPos food6 = {
94,
28
};
struct Bullet {
int bullet_x;
int bullet_y;
int breaker;
int flag;
};
struct Bullet bullet[4] = {
{
0, 0, 0, 0},
{
0, 0, 0, 0},
{
0, 0, 0, 0},
{
0, 0, 0, 0}
};
struct AppleTree {
int appletree_x;
int appletree_y;
int appletree_xPre;
int appletree_yPre;
int health;
int appletime;
int flag;
};
struct AppleTree appletree = {
46,
28,
46,
28,
1,
5,
0
};
struct MonsterPos {
int monster_x;
int monster_y;
};
struct MonsterPos monster1 = {
6,
4
};
struct MonsterPos monster2 = {
6,
6
};
struct MonsterPos monster3 = {
6,
8
};
struct MonsterPos monster4 = {
6,
10
};
struct Boss {
int boss_x;
int boss_y;
int boss_death;
int boss_ATK;
};
void goto_XY(int x, int y)
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pos = {
x, y };
SetConsoleCursorPosition(hOut, pos);
}
void hide_cursor()
{
CONSOLE_CURSOR_INFO cci;
cci.bVisible = FALSE;
cci.dwSize = sizeof(cci);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cci);
}
void Show() {
int i, j;
for (i = 0; i < 48; i += 1) {
for (j = 0; j < 120; j += 2) {
if (i != 0 && i != 47 && (j == 0 || j == 98 || j == 118)) {
hide_cursor();
goto_XY(j, i);
printf("▉");
}
else if (i == 0 || i == 47) {
hide_cursor();
goto_XY(j, i);
hide_cursor();
printf("▉");
}
}
printf("\n");
}
goto_XY(102, 5);
hide_cursor();
printf("游戏说明:");
goto_XY(107, 6);
hide_cursor();
printf("●");
goto_XY(101, 7);
hide_cursor();
printf("苹果树│ 吃后可");
goto_XY(101, 8);
hide_cursor();
printf("以回血,而回血");
goto_XY(101, 9);
hide_cursor();
printf("量则不确定。遇");
goto_XY(101, 10);
hide_cursor();
printf("到怪物记得逃跑");
goto_XY(101, 11);
hide_cursor();
printf(",否则,减血。");
goto_XY(101, 12);
hide_cursor();
printf("吃小草▲可以加");
goto_XY(101, 13);
hide_cursor();
printf("分。当分数为10");
goto_XY(101, 14);
hide_cursor();
printf("时,通关成功!");
goto_XY(101, 16);
hide_cursor();
printf("注意事项:");
goto_XY(101, 18);
hide_cursor();
printf("苹果树每隔一段");
goto_XY(101, 19);
hide_cursor();
printf("时间会消失。怪");
goto_XY(101, 20);
hide_cursor();
printf("物可追杀你,可");
goto_XY(101, 21);
hide_cursor();
printf("躲避子弹。空格");
goto_XY(101, 22);
hide_cursor();
printf("为召唤魔法球,");
goto_XY(101, 23);
hide_cursor();
printf("魔球可控,但四");
goto_XY(101, 24);
hide_cursor();
printf("个魔法球不可控");
goto_XY(102, 27);
hide_cursor();
printf("分数:%d", gamer.score);
goto_XY(102, 29);
hide_cursor();
printf("杀敌数:%d", gamer.monsterDeath);
goto_XY(102, 31);
hide_cursor();
printf("血量:%d", gamer.death);
if (gamer.flag == 1) {
goto_XY(101, 42);
hide_cursor();
printf("怪物:菜鸡,你");
goto_XY(101, 43);
hide_cursor();
printf("斗不赢我的,哈哈.");
}
else if (gamer.flag == 2) {
goto_XY(101, 42);
hide_cursor();
printf("怪物:小伙子,你");
goto_XY(101, 43);
hide_cursor();
printf("不行噢,哈哈.");
}
else if (gamer.flag == 3) {
goto_XY(101, 42);
hide_cursor();
printf("怪物:我想让你");
goto_XY(101, 43);
hide_cursor();
printf("一条胳膊,啧啧.");
}
else if (gamer.flag == 4) {
goto_XY(101, 42);
hide_cursor();
printf("我想睡觉~~~~,好");
goto_XY(101, 43);
hide_cursor();
printf("想回家 @&@!");
}
}
void GameOver()
{
if (gamer.score >= 10) {
system("cls");
printf(" 恭 喜 通 关 !\n");
exit(0);
}
if (gamer.death <= 0) {
system("cls");
printf(" 过 关 失 败 !\n");
exit(0);
}
}
int IsDeathBullet(int bulletnum)
{
if (bullet[bulletnum].breaker == 1) {
if ((bullet[bulletnum].bullet_y <= 46 && bullet[bulletnum].bullet_y >= 1) && bullet[bulletnum].bullet_x < 2)
{
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
return 1;
}
else if ((bullet[bulletnum].bullet_y <= 46 && bullet[bulletnum].bullet_y >= 1) && bullet[bulletnum].bullet_x > 96) {
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
return 1;
}
else if ((bullet[bulletnum].bullet_x >= 2 && bullet[bulletnum].bullet_x <= 96) && bullet[bulletnum].bullet_y > 46) {
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
return 1;
}
else if ((bullet[bulletnum].bullet_x >= 2 && bullet[bulletnum].bullet_x <= 96) && bullet[bulletnum].bullet_y < 1) {
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
return 1;
}
else if (bullet[bulletnum].bullet_x == monster1.monster_x && bullet[bulletnum].bullet_y == monster1.monster_y) {
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
++gamer.monsterDeath;
++gamer.score;
monster1.monster_x = rand() % 94 + 2;
monster1.monster_y = rand() % 45 + 1;
while (monster1.monster_x % 2 == 0) {
monster1.monster_x = rand() % 94 + 2;
}
goto_XY(monster1.monster_x, monster1.monster_y);
hide_cursor();
printf("▉");
return 2;
}
else if (bullet[bulletnum].bullet_x == monster2.monster_x && bullet[bulletnum].bullet_y == monster2.monster_y) {
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
++gamer.monsterDeath;
++gamer.score;
monster2.monster_x = rand() % 94 + 2;
monster2.monster_y = rand() % 45 + 1;
while (monster2.monster_x % 2 == 0) {
monster2.monster_x = rand() % 94 + 2;
}
goto_XY(monster2.monster_x, monster2.monster_y);
hide_cursor();
printf("▉");
return 2;
}
else if (bullet[bulletnum].bullet_x == monster3.monster_x && bullet[bulletnum].bullet_y == monster3.monster_y) {
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
++gamer.monsterDeath;
++gamer.score;
monster3.monster_x = rand() % 94 + 2;
monster3.monster_y = rand() % 45 + 1;
while (monster3.monster_x % 2 == 0) {
monster3.monster_x = rand() % 94 + 2;
}
goto_XY(monster3.monster_x, monster3.monster_y);
hide_cursor();
printf("▉");
return 2;
}
else if (bullet[bulletnum].bullet_x == monster4.monster_x && bullet[bulletnum].bullet_y == monster4.monster_y) {
bullet[bulletnum].breaker = 0;
bullet[bulletnum].flag = 0;
bullet[bulletnum].bullet_x = 0;
bullet[bulletnum].bullet_y = 0;
++gamer.monsterDeath;
++gamer.score;
monster4.monster_x = rand() % 94 + 2;
monster4.monster_y = rand() % 45 + 1;
while (monster4.monster_x % 2 == 0) {
monster4.monster_x = rand() % 94 + 2;
}
goto_XY(monster4.monster_x, monster4.monster_y);
hide_cursor();
printf("▉");
return 2;
}
return 0;
}
}
void BulletGo()
{
if (bullet[0].breaker == 1 && IsDeathBullet(0) == 0) {
if (gamer.recordRe == 'w') {
--bullet[0].bullet_y;
--bullet[0].bullet_y;
goto_XY(bullet[0].bullet_x, bullet[0].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 's') {
++bullet[0].bullet_y;
++bullet[0].bullet_y;
goto_XY(bullet[0].bullet_x, bullet[0].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'a') {
--bullet[0].bullet_x;
goto_XY(bullet[0].bullet_x, bullet[0].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'd') {
++bullet[0].bullet_x;
goto_XY(bullet[0].bullet_x, bullet[0].bullet_y);
hide_cursor();
printf("◆");
}
}
if (bullet[1].breaker == 1 && IsDeathBullet(1) == 0) {
if (gamer.recordRe == 'w') {
--bullet[1].bullet_y;
--bullet[1].bullet_y;
goto_XY(bullet[1].bullet_x, bullet[1].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 's') {
++bullet[1].bullet_y;
++bullet[1].bullet_y;
goto_XY(bullet[1].bullet_x, bullet[1].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'a') {
--bullet[1].bullet_x;
goto_XY(bullet[1].bullet_x, bullet[1].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'd') {
++bullet[1].bullet_x;
goto_XY(bullet[1].bullet_x, bullet[1].bullet_y);
hide_cursor();
printf("◆");
}
}
if (bullet[2].breaker == 1 && IsDeathBullet(2) == 0) {
if (gamer.recordRe == 'w') {
--bullet[2].bullet_y;
--bullet[2].bullet_y;
goto_XY(bullet[2].bullet_x, bullet[2].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 's') {
++bullet[2].bullet_y;
++bullet[2].bullet_y;
goto_XY(bullet[2].bullet_x, bullet[2].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'a') {
--bullet[2].bullet_x;
goto_XY(bullet[2].bullet_x, bullet[2].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'd') {
++bullet[2].bullet_x;
goto_XY(bullet[2].bullet_x, bullet[2].bullet_y);
hide_cursor();
printf("◆");
}
}
if (bullet[3].breaker == 1 && IsDeathBullet(3) == 0) {
if (gamer.recordRe == 'w') {
--bullet[3].bullet_y;
--bullet[3].bullet_y;
goto_XY(bullet[3].bullet_x, bullet[3].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 's') {
++bullet[3].bullet_y;
++bullet[3].bullet_y;
goto_XY(bullet[3].bullet_x, bullet[3].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'a') {
--bullet[3].bullet_x;
goto_XY(bullet[3].bullet_x, bullet[3].bullet_y);
hide_cursor();
printf("◆");
}
else if (gamer.recordRe == 'd') {
++bullet[3].bullet_x;
goto_XY(bullet[3].bullet_x, bullet[3].bullet_y);
hide_cursor();
printf("◆");
}
}
}
void Bullet_XY()
{
if (bullet[gamer.bullet - 1].breaker == 1 && bullet[gamer.bullet - 1].flag == 0) {
if (gamer.recordRe == 'w') {
bullet[gamer.bullet - 1].bullet_x = gamer.gamer_x;
bullet[gamer.bullet - 1].bullet_y = gamer.gamer_y - 1;
}
else if (gamer.recordRe == 's') {
bullet[gamer.bullet - 1].bullet_x = gamer.gamer_x;
bullet[gamer.bullet - 1].bullet_y = gamer.gamer_y + 1;
}
else if (gamer.recordRe == 'a') {
bullet[gamer.bullet - 1].bullet_y = gamer.gamer_y;
bullet[gamer.bullet - 1].bullet_x = gamer.gamer_x - 1;
}
else if (gamer.recordRe == 'd') {
bullet[gamer.bullet - 1].bullet_y = gamer.gamer_y;
bullet[gamer.bullet - 1].bullet_x = gamer.gamer_x + 1;
}
bullet[gamer.bullet - 1].flag = 1;
}
BulletGo();
}
int AppleEated()
{
if (gamer.gamer_x == appletree.appletree_xPre && gamer.gamer_y == appletree.appletree_yPre) {
gamer.death += appletree.health;
appletree.flag = 1;
return 1;
}
else {
return 0;
}
}
void Appletree()
{
srand((unsigned)time(NULL));
if (appletree.appletime >= 0 && appletree.appletime <= 75) {
if (appletree.flag == 1) {
appletree.appletree_x = rand() % 94 + 2;
appletree.appletree_y = rand() % 45 + 1;
appletree.appletime = 76;
appletree.flag = 0;
}
else {
goto_XY(appletree.appletree_xPre, appletree.appletree_yPre);
hide_cursor();
printf("●");
goto_XY(appletree.appletree_xPre, appletree.appletree_yPre + 1);
printf("│");
while (appletree.appletree_xPre % 2 != 0) {
appletree.appletree_xPre = rand() % 94 + 2;
appletree.appletree_yPre = rand() % 45 + 1;
goto_XY(appletree.appletree_xPre, appletree.appletree_yPre);
hide_cursor();
printf("●");
goto_XY(appletree.appletree_xPre, appletree.appletree_yPre + 1);
printf("│");
}
}
}
else if (appletree.appletime > 75 && appletree.appletime <= 100) {
if (appletree.appletree_x != 0 && appletree.appletree_y != 0) {
appletree.appletree_xPre = appletree.appletree_x;
appletree.appletree_yPre = appletree.appletree_y;
}
appletree.appletree_x = 0;
appletree.appletree_y = 0;
goto_XY(appletree.appletree_x, appletree.appletree_y);
}
else {
appletree.appletime = 0;
}
}
void Food()
{
srand((unsigned)time(NULL));
goto_XY(food1.food_x, food1.food_y);
hide_cursor();
while (food1.food_x % 2 != 0) {
food1.food_x = rand() % 94 + 2;
food1.food_y = rand() % 45 + 1;
goto_XY(food1.food_x, food1.food_y);
}
printf("▲");
goto_XY(food2.food_x, food2.food_y);
hide_cursor();
while (food2.food_x % 2 != 0) {
food2.food_x = rand() % 94 + 2;
food2.food_y = rand() % 45 + 1;
goto_XY(food2.food_x, food2.food_y);
}
printf("▲");
goto_XY(food3.food_x, food3.food_y);
hide_cursor();
while (food3.food_x % 2 != 0) {
food3.food_x = rand() % 94 + 2;
food3.food_y = rand() % 45 + 1;
goto_XY(food3.food_x, food3.food_y);
}
printf("▲");
goto_XY(food4.food_x, food4.food_y);
hide_cursor();
while (food4.food_x % 2 != 0) {
food4.food_x = rand() % 94 + 2;
food4.food_y = rand() % 45 + 1;
goto_XY(food4.food_x, food4.food_y);
}
printf("▲");
goto_XY(food5.food_x, food5.food_y);
hide_cursor();
while (food5.food_x % 2 != 0) {
food5.food_x = rand() % 94 + 2;
food5.food_y = rand() % 45 + 1;
goto_XY(food5.food_x, food5.food_y);
}
printf("▲");
goto_XY(food6.food_x, food6.food_y);
hide_cursor();
while (food6.food_x % 2 != 0) {
food6.food_x = rand() % 94 + 2;
food6.food_y = rand() % 45 + 1;
goto_XY(food6.food_x, food6.food_y);
}
printf("▲");
}
int FoodEated()
{
if (gamer.gamer_x == food1.food_x && gamer.gamer_y == food1.food_y) {
food1.food_x = rand() % 94 + 2;
food1.food_y = rand() % 45 + 1;
Food();
return 1;
}
else if (gamer.gamer_x == food2.food_x && gamer.gamer_y == food2.food_y) {
food2.food_x = rand() % 94 + 2;
food2.food_y = rand() % 45 + 1;
Food();
return 1;
}
else if (gamer.gamer_x == food3.food_x && gamer.gamer_y == food3.food_y) {
food3.food_x = rand() % 94 + 2;
food3.food_y = rand() % 45 + 1;
Food();
return 1;
}
else if (gamer.gamer_x == food4.food_x && gamer.gamer_y == food4.food_y) {
food4.food_x = rand() % 94 + 2;
food4.food_y = rand() % 45 + 1;
Food();
return 1;
}
else if (gamer.gamer_x == food5.food_x && gamer.gamer_y == food5.food_y) {
food5.food_x = rand() % 94 + 2;
food5.food_y = rand() % 45 + 1;
Food();
return 1;
}
else if (gamer.gamer_x == food6.food_x && gamer.gamer_y == food6.food_y) {
food6.food_x = rand() % 94 + 2;
food6.food_y = rand() % 45 + 1;
Food();
return 1;
}
else {
return 0;
}
}
void PrintMonster()
{
if ((monster1.monster_y <= 46 && monster1.monster_y >= 1) && monster1.monster_x < 2)
{
monster1.monster_x = 96;
goto_XY(monster1.monster_x, monster1.monster_y);
hide_cursor();
printf("▉");
}
else if ((monster1.monster_y <= 46 && monster1.monster_y >= 1) && monster1.monster_x > 96) {
monster1.monster_x = 2;
goto_XY(monster1.monster_x, monster1.monster_y);
hide_cursor();
printf("▉");
}
else if ((monster1.monster_x >= 2 && monster1.monster_x <= 96) && monster1.monster_y > 46) {
monster1.monster_y = 1;
goto_XY(monster1.monster_x, monster1.monster_y);
hide_cursor();
printf("▉");
}
else if ((monster1.monster_x >= 2 && monster1.monster_x <= 96) && monster1.monster_y < 1) {
monster1.monster_y = 46;
goto_XY(monster1.monster_x, monster1.monster_y);
hide_cursor();
printf("▉");
}
if ((monster2.monster_y <= 46 && monster2.monster_y >= 1) && monster2.monster_x < 2)
{
monster2.monster_x = 96;
goto_XY(monster2.monster_x, monster2.monster_y);
hide_cursor();
printf("▉");
}
else if ((monster2.monster_y <= 46 && monster2.monster_y >= 1) && monster2.monster_x > 96) {
monster2.monster_x = 2;
goto_XY(monster2.monster_x, monster2.monster_y);
hide_cursor();
printf("▉");
}
else if ((monster2.monster_x >= 2 && monster2.monster_x <= 96) && monster2.monster_y > 46) {
monster2.monster_y = 1;
goto_XY(monster2.monster_x, monster2.monster_y);
hide_cursor();
printf("▉");
}
else if ((monster2.monster_x >= 2 && monster2.monster_x <= 96) && monster2.monster_y < 1) {
monster2.monster_y = 46;
goto_XY(monster2.monster_x, monster2.monster_y);
hide_cursor();
printf("▉");
}
if ((monster3.monster_y <= 46 && monster3.monster_y >= 1) && monster3.monster_x < 2)
{
monster3.monster_x = 96;
hide_cursor();
goto_XY(monster3.monster_x, monster3.monster_y);
printf("▉");
}
else if ((monster3.monster_y <= 46 && monster3.monster_y >= 1) && monster3.monster_x > 96) {
monster3.monster_x = 2;
hide_cursor();
goto_XY(monster3.monster_x, monster3.monster_y);
printf("▉");
}
else if ((monster3.monster_x >= 2 && monster3.monster_x <= 96) && monster3.monster_y > 46) {
monster3.monster_y = 1;
hide_cursor();
goto_XY(monster3.monster_x, monster3.monster_y);
printf("▉");
}
else if ((monster3.monster_x >= 2 && monster3.monster_x <= 96) && monster3.monster_y < 1) {
monster3.monster_y = 46;
hide_cursor();
goto_XY(monster3.monster_x, monster3.monster_y);
printf("▉");
}
if ((monster4.monster_y <= 46 && monster4.monster_y >= 1) && monster4.monster_x < 2)
{
monster4.monster_x = 96;
hide_cursor();
goto_XY(monster4.monster_x, monster4.monster_y);
printf("▉");
}
else if ((monster4.monster_y <= 46 && monster4.monster_y >= 1) && monster4.monster_x > 96) {
monster4.monster_x = 2;
hide_cursor();
goto_XY(monster4.monster_x, monster4.monster_y);
printf("▉");
}
else if ((monster4.monster_x >= 2 && monster4.monster_x <= 96) && monster4.monster_y > 46) {
monster4.monster_y = 1;
hide_cursor();
goto_XY(monster4.monster_x, monster4.monster_y);
printf("▉");
}
else if ((monster4.monster_x >= 2 && monster4.monster_x <= 96) && monster4.monster_y < 1) {
monster4.monster_y = 46;
hide_cursor();
goto_XY(monster4.monster_x, monster4.monster_y);
printf("▉");
}
if ((gamer.gamer_x == monster1.monster_x) && (gamer.gamer_y == monster1.monster_y)) {
gamer.gamer_x = 22;
gamer.gamer_y = 20;
--gamer.score;
--gamer.death;
gamer.flag = 1;
if (gamer.score <= 0)
gamer.score = 0;
GameOver();
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else if ((gamer.gamer_x == monster2.monster_x) && (gamer.gamer_y == monster2.monster_y)) {
gamer.gamer_x = 22;
gamer.gamer_y = 20;
--gamer.score;
--gamer.death;
gamer.flag = 2;
if (gamer.score <= 0)
gamer.score = 0;
GameOver();
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else if ((gamer.gamer_x == monster3.monster_x) && (gamer.gamer_y == monster3.monster_y)) {
gamer.gamer_x = 22;
gamer.gamer_y = 20;
--gamer.score;
--gamer.death;
gamer.flag = 3;
if (gamer.score <= 0)
gamer.score = 0;
GameOver();
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else if ((gamer.gamer_x == monster4.monster_x) && (gamer.gamer_y == monster4.monster_y)) {
gamer.gamer_x = 22;
gamer.gamer_y = 20;
--gamer.score;
--gamer.death;
gamer.flag = 4;
if (gamer.score <= 0)
gamer.score = 0;
GameOver();
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else {
hide_cursor();
goto_XY(monster1.monster_x, monster1.monster_y);
printf("▉");
hide_cursor();
goto_XY(monster2.monster_x, monster2.monster_y);
printf("▉");
hide_cursor();
goto_XY(monster3.monster_x, monster3.monster_y);
printf("▉");
hide_cursor();
goto_XY(monster4.monster_x, monster4.monster_y);
printf("▉");
}
}
int MonsterIndex()
{
int flag = 0;
if (monster1.monster_x == gamer.gamer_x && monster1.monster_y > gamer.gamer_y) {
--monster1.monster_y;
PrintMonster();
flag = 1;
}
else if (monster1.monster_x == gamer.gamer_x && monster1.monster_y < gamer.gamer_y) {
++monster1.monster_y;
PrintMonster();
flag = 1;
}
else if (monster1.monster_y == gamer.gamer_y && monster1.monster_x > gamer.gamer_x) {
monster1.monster_x -= -2;
PrintMonster();
flag = 1;
}
else if (monster1.monster_y == gamer.gamer_y && monster1.monster_x < gamer.gamer_x) {
monster1.monster_x += 2;
PrintMonster();
flag = 1;
}
if (monster2.monster_x == gamer.gamer_x && monster2.monster_y > gamer.gamer_y) {
--monster2.monster_y;
PrintMonster();
flag = 1;
}
else if (monster2.monster_x == gamer.gamer_x && monster2.monster_y < gamer.gamer_y) {
++monster2.monster_y;
PrintMonster();
flag = 1;
}
else if (monster2.monster_y == gamer.gamer_y && monster2.monster_x > gamer.gamer_x) {
monster2.monster_x -= 2;
PrintMonster();
flag = 1;
}
else if (monster2.monster_y == gamer.gamer_y && monster2.monster_x < gamer.gamer_x) {
monster2.monster_x += 2;
PrintMonster();
flag = 1;
}
if (monster3.monster_x == gamer.gamer_x && monster3.monster_y > gamer.gamer_y) {
--monster3.monster_y;
PrintMonster();
flag = 1;
}
else if (monster3.monster_x == gamer.gamer_x && monster3.monster_y < gamer.gamer_y) {
++monster3.monster_y;
PrintMonster();
flag = 1;
}
else if (monster3.monster_y == gamer.gamer_y && monster3.monster_x > gamer.gamer_x) {
monster3.monster_x -= 2;
PrintMonster();
flag = 1;
}
else if (monster3.monster_y == gamer.gamer_y && monster3.monster_x < gamer.gamer_x) {
monster3.monster_x += 2;
PrintMonster();
flag = 1;
}
if (monster4.monster_x == gamer.gamer_x && monster4.monster_y > gamer.gamer_y) {
--monster4.monster_y;
PrintMonster();
flag = 1;
}
else if (monster4.monster_x == gamer.gamer_x && monster4.monster_y < gamer.gamer_y) {
++monster4.monster_y;
PrintMonster();
flag = 1;
}
else if (monster4.monster_y == gamer.gamer_y && monster4.monster_x > gamer.gamer_x) {
monster4.monster_x -= 2;
PrintMonster();
flag = 1;
}
else if (monster4.monster_y == gamer.gamer_y && monster4.monster_x < gamer.gamer_x) {
monster4.monster_x += 2;
PrintMonster();
flag = 1;
}
if (flag == 0) {
return 1;
}
else {
return 0;
}
}
void MonsterMovement()
{
srand((unsigned)time(NULL));
if (MonsterIndex()) {
int direction1 = rand() % 7;
if (direction1 == 0) {
monster1.monster_x -= 2;
}
else if (direction1 == 1) {
monster1.monster_x += 2;
}
else if (direction1 == 2) {
monster1.monster_y -= 1;
}
else if (direction1 == 3) {
monster1.monster_y += 1;
}
PrintMonster();
int direction2 = rand() % 7;
if (direction2 == 0) {
monster2.monster_x -= 2;
}
else if (direction2 == 1) {
monster2.monster_x += 2;
}
else if (direction2 == 2) {
monster2.monster_y -= 1;
}
else if (direction2 == 3) {
monster2.monster_y += 1;
}
PrintMonster();
int direction3 = rand() % 7;
if (direction3 == 0) {
monster3.monster_x -= 2;
}
else if (direction3 == 1) {
monster3.monster_x += 2;
}
else if (direction3 == 2) {
monster3.monster_y -= 1;
}
else if (direction3 == 3) {
monster3.monster_y += 1;
}
PrintMonster();
int direction4 = rand() % 7;
if (direction4 == 0) {
monster4.monster_x -= 2;
}
else if (direction4 == 1) {
monster4.monster_x += 2;
}
else if (direction4 == 2) {
monster4.monster_y -= 1;
}
else if (direction4 == 3) {
monster4.monster_y += 1;
}
PrintMonster();
}
}
void Monster()
{
srand((unsigned)time(NULL));
monster1.monster_x = rand() % 94 + 2;
monster1.monster_y = rand() % 45 + 1;
hide_cursor();
goto_XY(monster1.monster_x, monster1.monster_y);
while (monster1.monster_x % 2 != 0) {
monster1.monster_x = rand() % 94 + 2;
monster1.monster_y = rand() % 45 + 1;
goto_XY(monster1.monster_x, monster1.monster_y);
}
printf("▉");
srand((unsigned)time(NULL));
monster2.monster_x = rand() % 94 + 2;
monster2.monster_y = rand() % 45 + 1;
hide_cursor();
goto_XY(monster2.monster_x, monster2.monster_y);
while (monster2.monster_x % 2 != 0) {
monster2.monster_x = rand() % 94 + 2;
monster2.monster_y = rand() % 45 + 1;
goto_XY(monster2.monster_x, monster2.monster_y);
}
printf("▉");
srand((unsigned)time(NULL));
monster3.monster_x = rand() % 94 + 2;
monster3.monster_y = rand() % 45 + 1;
hide_cursor();
goto_XY(monster3.monster_x, monster3.monster_y);
while (monster3.monster_x % 2 != 0) {
monster3.monster_x = rand() % 94 + 2;
monster3.monster_y = rand() % 45 + 1;
goto_XY(monster3.monster_x, monster3.monster_y);
}
printf("▉");
srand((unsigned)time(NULL));
monster4.monster_x = rand() % 94 + 2;
monster4.monster_y = rand() % 45 + 1;
hide_cursor();
goto_XY(monster4.monster_x, monster4.monster_y);
while (monster4.monster_x % 2 != 0) {
monster4.monster_x = rand() % 94 + 2;
monster4.monster_y = rand() % 45 + 1;
goto_XY(monster4.monster_x, monster4.monster_y);
}
printf("▉");
}
void PrintGamer()
{
if ((gamer.gamer_y <= 46 && gamer.gamer_y >= 1) && gamer.gamer_x < 2)
{
gamer.gamer_x = 96;
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else if ((gamer.gamer_y <= 46 && gamer.gamer_y >= 1) && gamer.gamer_x > 96) {
gamer.gamer_x = 2;
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else if ((gamer.gamer_x >= 2 && gamer.gamer_x <= 96) && gamer.gamer_y > 46) {
gamer.gamer_y = 1;
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else if ((gamer.gamer_x >= 2 && gamer.gamer_x <= 96) && gamer.gamer_y < 1) {
gamer.gamer_y = 46;
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
else {
hide_cursor();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
}
}
void Judgement(char ch)
{
int bulletnum = gamer.bullet;
if (bulletnum >= 4) {
bulletnum = 0;
}
if (ch == 'w' && bullet[bulletnum].flag == 0) {
gamer.recordRe = 'w';
}
else if (ch == 's' && bullet[bulletnum].flag == 0) {
gamer.recordRe = 's';
}
else if (ch == 'a' && bullet[bulletnum].flag == 0) {
gamer.recordRe = 'a';
}
else if (ch == 'd' && bullet[bulletnum].flag == 0) {
gamer.recordRe = 'd';
}
}
void BulletState()
{
if (gamer.bullet >= 5) {
gamer.bullet = 1;
}
bullet[gamer.bullet - 1].breaker = 1;
}
void Move()
{
if (_kbhit())
{
fflush(stdin);
char key = _getch();
switch (key)
{
case 'd':gamer.gamer_x += 2; Judgement('d'); break;
case 's':++gamer.gamer_y; Judgement('s'); break;
case 'a':gamer.gamer_x -= 2; Judgement('a'); break;
case 'w':--gamer.gamer_y; Judgement('w'); break;
case ' ':++gamer.bullet; BulletState(); Bullet_XY(); break;
}
}
PrintGamer();
goto_XY(food1.food_x, food1.food_y);
printf("▲");
goto_XY(food2.food_x, food2.food_y);
printf("▲");
goto_XY(food3.food_x, food3.food_y);
printf("▲");
goto_XY(food4.food_x, food4.food_y);
printf("▲");
goto_XY(food5.food_x, food5.food_y);
printf("▲");
goto_XY(food6.food_x, food6.food_y);
printf("▲");
}
int main()
{
int g, i, ch;
Show();
goto_XY(gamer.gamer_x, gamer.gamer_y);
printf("▉");
Monster();
while (1) {
system("cls");
Show();
PrintGamer();
Food();
Appletree();
MonsterMovement();
BulletGo();
Move();
ch = IsDeathBullet(0);
ch = IsDeathBullet(1);
ch = IsDeathBullet(2);
ch = IsDeathBullet(3);
if (FoodEated())
++gamer.score;
if (AppleEated())
appletree.appletime = 76;
GameOver();
appletree.appletime++;
Sleep(100);
}
}