#include<stdio.h>
#include<Windows.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#define HEAD 1
#define TAIL 2
int score = 0;
bool lastwin = false;
unsigned long speed = 505;
typedef struct Snake
{
int ch;
COORD pos;
Snake *next;
Snake *last;
} *PriSna;
PriSna s;
PriSna tail = NULL;
int sx, sy;
void goto_x_y(int x, int y);
void Creatsnake(int len = 1);
void gamestart();
void Drawseed();
void goto_x_y(int x, int y)
{
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD pt;
pt.X = x;
pt.Y = y;
SetConsoleCursorPosition(hout, pt);
CONSOLE_CURSOR_INFO set = { 1,0 };
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &set); //隐藏光标
}
void Creatsnake(int len) //蛇的身体
{
if (s->next == NULL)
{
tail = s;
}
for (int i = 0; i < len; i++)
{
tail->next = new Snake[1];
tail->next->last = tail;
tail = tail->next;
tail->ch = TAIL;
tail->pos = tail->last->pos;
tail->next = NULL;
}
}
void DrawMap()
{
for (int i = 0; i < 40; i++)
{
printf("□");
}
for (int i = 0; i < 22; i++)
{
goto_x_y(0, i + 1);
printf("□");
goto_x_y(78, i + 1);
printf("□");
}
goto_x_y(0, 23);
for (int i = 0; i < 40; i++)
{
printf("□");
}
goto_x_y(0, 24);
printf("wasd控制反向,z暂停,自动变速; MADE BY TOUYI");
goto_x_y(65, 24);
printf("score:00");
goto_x_y(0, 0);
}
bool judgegame()
{
if (s->pos.X >= 79 || s->pos.X <= 2 || s->pos.Y>22 || s->pos.Y < 1)
return true;
else
{
PriSna t = s->next;
while (t != NULL)
{
if (s->pos.X == t->pos.X&&s->pos.Y == t->pos.Y)
return true;
t = t->next;
}
}
return false;
}
bool Drawsnake(char cha)
{
if (cha == 't')
{
int two = 2;
PriSna t = s;
while (two--)
{
goto_x_y(t->pos.X - 2, t->pos.Y);
if (t->ch == HEAD)
printf("●");
else
printf("■");
goto_x_y(0, 0);
t = t->next;
}
return true;
}
if (cha == 'z' || cha == 'Z')
{
PriSna t = s;
while (t != NULL)
{
goto_x_y(t->pos.X - 2, t->pos.Y);
if (t->ch == HEAD)
printf("●");
else
printf("■");
goto_x_y(0, 0);
t = t->next;
}
}
else
{
PriSna t;
if (cha == 'a' || cha == 'A')
{
goto_x_y(tail->pos.X, tail->pos.Y);
t = tail;
tail = tail->last;
tail->next = NULL;
printf("\b \b");
goto_x_y(s->pos.X, s->pos.Y);
(*t) = (*s);
t->ch = TAIL;
t->next->last = t;
t->last = s;
s->next = t;
s->pos.X -= 2;
printf("\b \b");
Drawsnake('t');
}
else if (cha == 's' || cha == 'S')
{
goto_x_y(tail->pos.X, tail->pos.Y);
t = tail;
tail = tail->last;
tail->next = NULL;
printf("\b \b");
goto_x_y(s->pos.X, s->pos.Y);
(*t) = (*s);
t->ch = TAIL;
t->next->last = t;
t->last = s;
s->next = t;
s->pos.Y++;
printf("\b \b");
Drawsnake('t');
}
else if (cha == 'd' || cha == 'D')
{
goto_x_y(tail->pos.X, tail->pos.Y);
t = tail;
tail = tail->last;
tail->next = NULL;
printf("\b \b");
goto_x_y(s->pos.X, s->pos.Y);
(*t) = (*s);
t->ch = TAIL;
t->next->last = t;
t->last = s;
s->next = t;
s->pos.X += 2;
printf("\b \b");
Drawsnake('t');
}
else if (cha == 'w' || cha == 'W')
{
goto_x_y(tail->pos.X, tail->pos.Y);
t = tail;
tail = tail->last;
tail->next = NULL;
printf("\b \b");
goto_x_y(s->pos.X, s->pos.Y);
(*t) = (*s);
t->ch = TAIL;
t->next->last = t;
t->last = s;
s->next = t;
s->pos.Y--;
printf("\b \b");
Drawsnake('t');
}
}
if (s->pos.X == sx + 2 && s->pos.Y == sy)
{
Drawseed();
Creatsnake();
score += 10;
goto_x_y(71, 24);
speed -= 10;
printf("%d", score);
return true;
}
if (judgegame())
return false;
return true;
}
bool judge(int x, int y)
{
PriSna t = s;
while (t != NULL)
{
if (t->pos.X == x && t->pos.Y == y)
return false;
t = t->next;
}
return true;
}
void Drawseed() //画食物
{
srand((unsigned)time(NULL));
while (1)
{
sx = ((rand() % 38) + 1) * 2;
sy = (rand() % 22) + 1;
if (judge(sx, sy))
{
break;
}
}
goto_x_y(sx, sy);
printf("▲");
goto_x_y(0, 0);
}
void init()
{
s = new Snake[1];
PriSna p = s;
s->pos.X = 16;
s->pos.Y = 1;
s->ch = HEAD;
s->next = NULL;
s->last = NULL;
Creatsnake(3);
p = p->next;
for (int i = 1; i < 4; i++)
{
p->pos.X = (SHORT)(16 - i * 2);
p->pos.Y = 1;
p->ch = TAIL;
p = p->next;
}
Drawsnake('z');
Drawseed();
}
void gamestart()
{
char ch = 'z', tch = 'd';
//unsigned long speed = 1000;
while (1)
{
if (_kbhit())
{
tch = ch;
ch = _getch();
}
/*if (ch <= '5' && ch > '0')
{
speed = (unsigned long)((ch - '0') * 100);
ch = tch;
}
else */
if (ch == 'z' || ch == 'Z') //暂停
{
_getch();
ch = tch;
}
else if (ch == 'a' || ch == 'A' || ch == 'S' || ch == 's' || ch == 'd' || ch == 'D' || ch == 'W' || ch == 'w')
{
if (((ch == 'W' || ch == 'w') && (tch == 's' || tch == 'S')) || ((ch == 'd' || ch == 'D') && (tch == 'a' || tch == 'A')) || ((ch == 's' || ch == 'S') && (tch == 'w' || tch == 'W')) || ((ch == 'a' || ch == 'A') && (tch == 'd' || tch == 'D')))
{
ch = tch;
continue;
}
tch = ch;
if (Drawsnake(ch) == false && lastwin == false)
{
goto_x_y(35, 10);
printf("game over!");
break;
}
else if (lastwin == true)
break;
}
else if (ch == 'q' || ch == 'Q')
break;
else
ch = tch;
Sleep(speed);
}
//此处结束
}
int main()
{
init();
DrawMap();
gamestart();
_getch();
return 0;
}
c语言控制台的一个贪吃蛇小游戏
最新推荐文章于 2024-07-29 12:22:35 发布