先上效果图,COPY到VS上,运行不起直播吃
封面图
记得COPY的时候要注意,先创一个头文件叫snake.h的头文件,因为我这里的头文件就叫snake.h,然后把相关头文件放在你创建的这个头文件里面。第二步创建一个任意名称的源文件,把源代码放在源文件里,直接F5运行。
单人模式
双人模式
废话不多说直接上代码
记得COPY的时候要注意,先创一个头文件叫snake.h的头文件,因为我这里的头文件就叫snake.h,然后把相关头文件放在你创建的这个头文件里面。第二步创建一个任意名称的源文件,把源代码放在源文件里,直接F5运行。
先是相关头文件
#pragma once
#pragma warning(disable : 4996)
#include
#include
#include
#include
#include
#include
#pragma comment(lib,"Winmm.lib")
#define snake 64
#define inity 4
#define initx 20
#define inity2 20
#define initx2 4
typedef struct Snake
{
int x;
int y;
struct Snake* Next;
}SNAKE, * PSNAKE;
int sta = 0;
int sta2 = 0;
int score = 0;
PSNAKE pHead;
PSNAKE pHead2;
PSNAKE food;
int flag = 0;
int HighScore = 0;
void HideCursor();
void ShowCursor();
void gotoxy(int x, int y);
int changecolor(int a);
void ZHTsnake();
void startmenu_options();
void instruction();
void startmenu();
void createmap();
void initsnake();
void initsnakePT();
void initsnake2();
void createfood();
void createfood2();
void keystatus();
void keystatus2();
void keystatusjs();
bool movesnake();
bool movesnakejs();
bool movesnake2();
void startgame();
void shuangren();
void jisumosi();
int cannotcrossthewall();
int cannotcrossthewall2();
int cannotbiteyourself();
int cannotbiteyourself2();
int cannotbitethenselves();
void gameover();
void write_file();
void read_file();
void highestscore();
void ConsoleTitle();
void youximoshi();
void choices_1(int a);
void choices_2(int a);
void laotie();
void menu_word1();
void menu_word2();
void yinyue();
接着是源代码
#include "snake.h"
int main()
{
startmenu();
return 0;
}
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
void ShowCursor()
{
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = TRUE;
cursor.dwSize = 100;
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorInfo(handle, &cursor);
}
void gotoxy(int x, int y)
{
COORD c;
c.X = x;
c.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), c);
}
int changecolor(int a)
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
return 0;
}
void choices_1(int a)
{
int ch = getch();
while (ch != 0x0d || ch == 0xE0)
{
ch = getch();
if (ch == 0x48 && a > 20)
{
a -= 2;
gotoxy(6, a);
}
if (ch == 0x50 && a < 26)
{
a += 2;
gotoxy(6, a);
}
}
switch (a)
{
case 20: youximoshi(); break;
case 22: instruction(); break;
case 24: highestscore(); break;
case 26: gotoxy(2, 30); exit(0); break;
}
}void choices_2(int a)
{
ShowCursor();
int ch = getch();
while (ch != 0x0d || ch == 0xE0)
{
ch = getch();
if (ch == 0x48 && a > 20)
{
a -= 2;
gotoxy(5, a);
}
if (ch == 0x50 && a < 26)
{
a += 2;
gotoxy(5, a);
}
}
switch (a)
{
case 20: startgame(); break;
case 22: jisumosi(); break;
case 24: shuangren(); break;
case 26: startmenu(); break;
}
}
void youximoshi()
{
system("cls");
char chCmd[34];
sprintf_s(chCmd, "mode con cols=%d lines=%d", 117, 34);
system(chCmd);
ShowCursor();
ZHTsnake();
menu_word1();
laotie();
changecolor(9);
gotoxy(6, 20);
printf("◆普通模式◆\n");
gotoxy(6, 22);
printf("◆急速模式◆\n");
gotoxy(6, 24);
printf("◆双人模式◆\n");
gotoxy(6, 26);
printf("◆返回上一级◆\n");
gotoxy(5, 20);
choices_2(20);
}
void laotie()
{
gotoxy(25, 15);
changecolor(15);
printf(" ●●●●●●●●●●●●●●● \n");
gotoxy(25, 16);
printf(" ●●●●●●●●●●●●●●● \n");
gotoxy(25, 17);
printf(" ●● \n");
gotoxy(25, 18);
printf(" ●● \n");
gotoxy(25, 19);
printf(" ●● ●●●●●●●●●●●●● \n");
gotoxy(25, 20);
printf(" ●● ●\n");
gotoxy(25, 21);
printf(" ●● ● \n");
gotoxy(25, 22);
printf(" ●● ● ●●●●●●●● \n");
gotoxy(25, 23);
printf(" ●●●●●●●●●●●●●●●● ● ●\n");
gotoxy(25, 24);
printf(" ●●●●●●●●●●●●●●●● ●●●●●●●●●●●●● ● \n");
gotoxy(25, 25);
printf(" ●● ●● ● ● ● \n");
gotoxy(25, 26);
printf(" ●● ●● ● ● ●\n");
gotoxy(25, 27);
printf(" ●● ●● ● ● ●●●●●●●● \n");
gotoxy(25, 28);
printf(" ●● ●● ● ● ● ●\n");
gotoxy(25, 29);
printf(" ●● ●● ● ● ● ●\n");
gotoxy(25, 30);
printf(" ●● ●● ● ● ● ●\n");
gotoxy(25, 31);
printf(" ●●●●●●●●●●●●●●●● ● ● ● ●\n");
gotoxy(25, 32);
printf(" ●●●●●●●●●●●●●●●● ●●●●●●●●●●●●● ●●●●●●●●\n");
}
void ZHTsnake()
{
changecolor(11);
printf(" \n");
printf(" @@@ @@ @ @ ● ● \n");
printf(" @@@@@@@@@@@@@@@@@@@ @@ ◆ @@ @@@@@@@@@@@@@@@@@@@@ \n");
printf(" @ @@ @@ & & @@ @@ \n");
printf(" ~ ~ @@ @@ # # @@ @@ \n");
printf(" # # @@ @@ @@ @@ \n");
printf(" @@ @ @@@@@@@@@@@@@@@@@ @@ \n");
printf(" @ @@ @@ @ @ @@ @@ \n");
printf(" @@ @ @@ @@ @@ \n");
printf(" @@ @@ @@ @@ \n");
printf(" @@@@@@@@@@@@@@@@@@@ @@ @@ @@ \n");
printf(" @@ @@ @ @ \n");
printf(" \n");
gotoxy(snake, 2);
changecolor(5);
printf(" @ @@@@@@ \n");
gotoxy(snake, 3);
printf(" @ @ \n");
gotoxy(snake, 4);
printf(" @ @ \n");
gotoxy(snake, 5);
printf(" @ @@@@ ## @ \n");
gotoxy(snake, 6);
printf(" @ @@ @ @ \n");
gotoxy(snake, 7);
printf(" @ @@ @@@@ \n");
gotoxy(snake, 8);
printf(" @ @@ \n");
gotoxy(snake, 9);
printf(" @ @@ \n");
gotoxy(snake, 10);
printf(" @ @@ \n");
gotoxy(snake, 11);
printf(" @@@@@@ @ @@ \n");
gotoxy(snake, 12);
printf(" @ @@@ @ @@ \n");
gotoxy(snake, 13);
printf("@ @@ @ @ @@@@@@ @ @ @ @@@@@\n");
gotoxy(snake, 14);
printf("@ @@@@@ @ @ @ @ @ @ @ @ @ \n");
gotoxy(snake, 15);
printf(" @ @@ @ @ @@@@@ @@ @@@@ \n");
gotoxy(snake, 16);
printf(" @ @@ @ @ @ @ @ @ @ \n");
gotoxy(snake, 17);
printf(" @ @@@@@@@@@ @ @@ @@ @ @@@@@\n");
}
void menu_word1()
{
gotoxy(3, 18);
changecolor(6);
printf(" 请开始你的表演!");
}
void menu_word2()
{
gotoxy(3, 18);
changecolor(14);
printf(" 请做出你的选择!");
}
void startmenu_options()
{
changecolor(9);
gotoxy(6, 20);
printf("◆游戏模式◆\n");
gotoxy(6, 22);
printf("◆游戏介绍◆\n");
gotoxy(6, 24);
printf("◆得分记录◆\n");
gotoxy(6, 26);
printf("◆退出游戏◆\n");
gotoxy(6, 20);
choices_1(20);
}
void startmenu()
{
mciSendString("open a.mp3 ", 0, 0, 0);
mciSendString("play a.mp3 repeat", 0, 0, 0);
ConsoleTitle();
ShowCursor();
flag = 0;
char chCmd[32];
sprintf_s(chCmd, "mode con cols=%d lines=%d", 117, 34);
system(chCmd);
read_file();
ZHTsnake();
menu_word2();
laotie();
startmenu_options();
}
void instruction()
{
system("cls");
changecolor(3);
printf("----------------------Hi,Snake!~o(*= ▽=*)ブ----------------------\n");
gotoxy(0, 17);
changecolor(3);
printf("------------------------------------------------------------------\n");
gotoxy(2, 2);
changecolor(14);
printf("◇ 操作说明 ◇\n");
gotoxy(2, 4);
changecolor(3);
printf("※按↑↓←→键分别控制贪吃蛇向上、下、左、右移动\n");
gotoxy(2, 6);
printf("※按空格键暂停游戏\n");
gotoxy(2, 8);
printf("※按ESC键于游戏中途退出游戏\n");
changecolor(14);
gotoxy(2, 10);
printf("◇ 游戏规则 ◇\n");
gotoxy(2, 12);
changecolor(3);
printf("※肥肠简单,只要不撞到自己或撞到墙就行了(笑)\n");
gotoxy(1, 16);
printf("Press space back to the menu......");
gotoxy(35, 16);
int cha = getch();
while (cha == 0x0d)
{
system("cls");
startmenu();
}
}
void startgame()
{
createmap();
initsnakePT();
Sleep(100);
createfood();
keystatus();
write_file();
score = 0;
}
void jisumosi()
{
createmap();
initsnake();
Sleep(100);
createfood();
keystatusjs();
write_file();
score = 0;
}
void createmap()
{
HideCursor();
system("cls");
char chCmd[32];
sprintf_s(chCmd, "mode con cols=%d lines=%d", 51, 26);
system(chCmd);
int i, j;
changecolor(5);
for (i = 0; i < 49; i += 2)
{
gotoxy(i, 0);
printf("〓");
gotoxy(i, 24);
printf("〓");
}
for (i = 1; i < 25; i++)
{
gotoxy(0, i);
printf("〓");
gotoxy(48, i);
printf("〓");
}
changecolor(11);
for (i = 1; i < 24; i++)
{
for (j = 2; j < 48; j += 2)
{
gotoxy(j, i);
printf("■");
}
printf("\n");
}
printf("\n");
}
void initsnake()
{
pHead = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE pTail = (PSNAKE)malloc(sizeof(SNAKE));
pTail = pHead;
if (pTail != NULL && pHead != NULL)
{
pHead->Next = NULL;
int i, val = initx;
for (i = 0; i < 4; i++)
{
PSNAKE isnake = (PSNAKE)malloc(sizeof(SNAKE));
if (isnake != NULL)
{
isnake->x = val;
isnake->y = inity;
val -= 2;
pTail->Next = isnake;
isnake->Next = NULL;
pTail = isnake;
}
}
PSNAKE p = pHead->Next;
gotoxy(p->x, p->y);
changecolor(6);
printf("●");
p = p->Next;
while (p != NULL)
{
changecolor(6);
gotoxy(p->x, p->y);
printf("◆");
p = p->Next;
}
}
}
void initsnakePT()
{
pHead = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE pTail = (PSNAKE)malloc(sizeof(SNAKE));
pTail = pHead;
if (pTail != NULL && pHead != NULL)
{
pHead->Next = NULL;
int i, val = initx;
for (i = 0; i < 4; i++)
{
PSNAKE isnake = (PSNAKE)malloc(sizeof(SNAKE));
if (isnake != NULL)
{
isnake->x = val;
isnake->y = inity;
val -= 2;
pTail->Next = isnake;
isnake->Next = NULL;
pTail = isnake;
}
}
PSNAKE p = pHead->Next;
gotoxy(p->x, p->y);
changecolor(8);
printf("●");
p = p->Next;
while (p != NULL)
{
changecolor(8);
gotoxy(p->x, p->y);
printf("◆");
p = p->Next;
}
}
}
void createfood2()
{
srand((unsigned)time(NULL));
PSNAKE ffood = (PSNAKE)malloc(sizeof(SNAKE));
food = (PSNAKE)malloc(sizeof(SNAKE));
int we = rand() % 42 + 2;
while (we % 2 == 1)
we = rand() % 42 + 2;
ffood->x = we;
ffood->y = rand() % 19 + 1;
PSNAKE p = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE remhead = (PSNAKE)malloc(sizeof(SNAKE));
p = pHead->Next;
if (ffood != NULL && food != NULL)
{
while (p != NULL)
{
if (p->x == ffood->x && p->y == ffood->y)
{
srand((unsigned)time(NULL));
int we = rand() % 42 + 2;
while (we % 2 == 1)
we = rand() % 42 + 2;
ffood->x = we;
ffood->y = rand() % 19 + 1;
ffood->Next = NULL;
}
else
p = p->Next;
}
PSNAKE q = (PSNAKE)malloc(sizeof(SNAKE));
q = pHead2->Next;
while (q != NULL)
{
if (q->x == ffood->x && q->y == ffood->y)
{
int we = rand() % 42 + 2;
while (we % 2 == 1)
we = rand() % 42 + 2;
ffood->x = we;
ffood->y = rand() % 19 + 1;
ffood->Next = NULL;
}
q = q->Next;
}
food = ffood;
changecolor(4);
gotoxy(food->x, food->y);
printf("●");
changecolor(5);
}
}
void createfood()
{
srand((unsigned)time(NULL));
PSNAKE ffood = (PSNAKE)malloc(sizeof(SNAKE));
food = (PSNAKE)malloc(sizeof(SNAKE));
int we = rand() % 42 + 2;
while (we % 2 == 1)
we = rand() % 42 + 2;
ffood->x = we;
ffood->y = rand() % 19 + 1;
PSNAKE p = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE remhead = (PSNAKE)malloc(sizeof(SNAKE));
p = pHead->Next;
if (ffood != NULL && food != NULL)
{
while (p != NULL)
{
if (p->x == ffood->x && p->y == ffood->y)
{
srand((unsigned)time(NULL));
int we = rand() % 42 + 2;
while (we % 2 == 1)
we = rand() % 42 + 2;
ffood->x = we;
ffood->y = rand() % 19 + 1;
ffood->Next = NULL;
}
else
p = p->Next;
}
food = ffood;
changecolor(4);
gotoxy(food->x, food->y);
printf("●");
changecolor(5);
}
}
void keystatus()
{
sta = 4;
while (1)
{
if (GetAsyncKeyState(VK_UP) && sta != 2)
sta = 1;
else
{
if (GetAsyncKeyState(VK_DOWN) && sta != 1)
sta = 2;
else
{
if (GetAsyncKeyState(VK_LEFT) && sta != 4)
sta = 3;
else
{
if ((GetAsyncKeyState(VK_RIGHT) && sta != 3))
sta = 4;
}
}
}
if (GetAsyncKeyState(VK_SPACE))
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
break;
}
}
if (GetAsyncKeyState(VK_ESCAPE))
{
system("cls");
startmenu();
break;
}
if (movesnake() == false)
{
gameover();
break;
}
}
}
void keystatusjs()
{
sta = 4;
while (1)
{
if (GetAsyncKeyState(VK_UP) && sta != 2)
sta = 1;
else
{
if (GetAsyncKeyState(VK_DOWN) && sta != 1)
sta = 2;
else
{
if (GetAsyncKeyState(VK_LEFT) && sta != 4)
sta = 3;
else
{
if ((GetAsyncKeyState(VK_RIGHT) && sta != 3))
sta = 4;
}
}
}
if (GetAsyncKeyState(VK_SPACE))
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
break;
}
}
if (GetAsyncKeyState(VK_ESCAPE))
{
system("cls");
startmenu();
break;
}
if (movesnakejs() == false)
{
gameover();
break;
}
}
}
bool movesnake()
{
HideCursor();
changecolor(8);
PSNAKE qq = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE pp = (PSNAKE)malloc(sizeof(SNAKE));
pp = pHead->Next;
if (sta == 1)
{
qq->x = pp->x;
qq->y = pp->y - 1;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(200);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
changecolor(5);
Sleep(200);
}
return true;
}
}
if (sta == 2)
{
qq->x = pp->x;
qq->y = pp->y + 1;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(200);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(200);
changecolor(5);
}
return true;
}
}
if (sta == 3)
{
qq->x = pp->x - 2;
qq->y = pp->y;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(200);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(200);
changecolor(5);
}
return true;
}
}
if (sta == 4)
{
qq->x = pp->x + 2;
qq->y = pp->y;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(200);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(200);
changecolor(5);
}
return true;
}
}
}
bool movesnakejs()
{
HideCursor();
changecolor(6);
PSNAKE qq = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE pp = (PSNAKE)malloc(sizeof(SNAKE));
pp = pHead->Next;
if (sta == 1)
{
qq->x = pp->x;
qq->y = pp->y - 1;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
changecolor(5);
Sleep(50);
}
return true;
}
}
if (sta == 2)
{
qq->x = pp->x;
qq->y = pp->y + 1;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(50);
changecolor(5);
}
return true;
}
}
if (sta == 3)
{
qq->x = pp->x - 2;
qq->y = pp->y;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(50);
changecolor(5);
}
return true;
}
}
if (sta == 4)
{
qq->x = pp->x + 2;
qq->y = pp->y;
qq->Next = pp;
pHead->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead->Next;
if (cannotbiteyourself() == 1 || cannotcrossthewall() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(50);
changecolor(5);
}
return true;
}
}
}
int cannotcrossthewall()
{
if (pHead->Next->x == 0 || pHead->Next->x == 48 || pHead->Next->y == 0 || pHead->Next->y == 24)
flag = 1;
return flag;
}
int cannotcrossthewall2()
{
if (pHead2->Next->x == 0 || pHead2->Next->x == 48 || pHead2->Next->y == 0 || pHead2->Next->y == 24)
flag = 1;
return flag;
}
int cannotbiteyourself()
{
PSNAKE bite = (PSNAKE)malloc(sizeof(SNAKE));
bite = pHead->Next->Next;
while (bite != NULL)
{
if (bite->x == pHead->Next->x && bite->y == pHead->Next->y)
flag = 1;
bite = bite->Next;
}
return flag;
}
int cannotbitethenselves()
{
PSNAKE bite = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE bite2 = (PSNAKE)malloc(sizeof(SNAKE));
bite = pHead->Next;
bite2 = pHead2->Next;
while (bite != NULL)
{
while (bite2)
{
if (bite->x == bite2->x && bite->y == bite2->y)
flag = 1;
bite2 = bite2->Next;
}
bite = bite->Next;
}
return flag;
}
void gameover()
{
if (score > HighScore)
write_file();
system("cls");
changecolor(11);
char chCmd[32];
sprintf_s(chCmd, "mode con cols=%d lines=%d", 60, 25);
system(chCmd);
gotoxy(5, 5);
printf(" /\\________/\\\n");
gotoxy(5, 6);
printf(" | Q Q |\n");
gotoxy(5, 7);
printf(" | ⌒ | ---你死的好惨啊呜呜呜(;′⌒`) \n");
gotoxy(5, 8);
printf(" | |\n");
gotoxy(5, 9);
printf(" \\____________/ \n");
gotoxy(5, 10);
printf(" \n");
gotoxy(5, 11);
changecolor(9);
printf(" Your score is %d \n", score);
gotoxy(5, 12);
printf("Highest score : %d \n", HighScore);
gotoxy(1, 15);
printf("Press space back to the menu......");
gotoxy(35, 15);
int ch = getchar();
while (ch == 0x0d)
exit(0);
system("cls");
startmenu();
}
void read_file()
{
FILE* fp;
fp = fopen("Desktop.txt", "a+");
fscanf_s(fp, "%d", &HighScore);
fclose(fp);
}
void write_file()
{
FILE* fp;
fp = fopen("Desktop.txt", "w+");
fprintf(fp, "%d", score);
fclose(fp);
}
void highestscore()
{
system("cls");
char chCmd[32];
sprintf_s(chCmd, "mode con cols=%d lines=%d", 50, 25);
system(chCmd);
changecolor(14);
gotoxy(5, 5);
printf("Your highest score is %d.\n\n", HighScore);
gotoxy(1, 9);
printf("Press space back to the menu......");
gotoxy(35, 9);
int cha = getch();
while (cha == 0x0d)
{
system("cls");
startmenu();
}
}
void ConsoleTitle()
{
char Title[200];
SetConsoleTitleA(" 赵海涛的贪吃蛇");
GetConsoleTitleA(Title, 200);
}
void shuangren()
{
createmap();
initsnake();
initsnake2();
Sleep(100);
createfood2();
keystatus2();
write_file();
score = 0;
}
void initsnake2()
{
pHead2 = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE pTail = (PSNAKE)malloc(sizeof(SNAKE));
pTail = pHead2;
if (pTail != NULL && pHead2 != NULL)
{
pHead2->Next = NULL;
int i, val = initx2;
for (i = 0; i < 2; i++)
{
PSNAKE isnake = (PSNAKE)malloc(sizeof(SNAKE));
{
isnake->x = val;
isnake->y = inity2;
val -= 2;
pTail->Next = isnake;
isnake->Next = NULL;
pTail = isnake;
}
}
PSNAKE p = pHead2->Next;
gotoxy(p->x, p->y);
changecolor(5);
printf("●");
p = p->Next;
while (p != NULL)
{
changecolor(5);
gotoxy(p->x, p->y);
printf("◆");
p = p->Next;
}
}
}
void keystatus2()
{
sta = 3;
sta2 = 4;
while (1)
{
if (GetAsyncKeyState(0x57) && sta2 != 2)
sta2 = 1;
else
{
if (GetAsyncKeyState(0x53) && sta2 != 1)
sta2 = 2;
else
{
if (GetAsyncKeyState(0x41) && sta2 != 4)
sta2 = 3;
else
{
if ((GetAsyncKeyState(0x44) && sta2 != 3))
sta2 = 4;
else {
if (GetAsyncKeyState(VK_UP) && sta != 2)
sta = 1;
else
{
if (GetAsyncKeyState(VK_DOWN) && sta != 1)
sta = 2;
else
{
if (GetAsyncKeyState(VK_LEFT) && sta != 4)
sta = 3;
else
{
if ((GetAsyncKeyState(VK_RIGHT) && sta != 3))
sta = 4;
}
}
}
}
}
}
}
if (GetAsyncKeyState(VK_SPACE))
{
while (1)
{
Sleep(300);
if (GetAsyncKeyState(VK_SPACE))
break;
}
}
if (GetAsyncKeyState(VK_ESCAPE))
{
system("cls");
startmenu();
break;
}
if (movesnakejs() == false || movesnake2() == false)
{
gameover();
break;
}
}
}
bool movesnake2()
{
HideCursor();
changecolor(5);
PSNAKE qq = (PSNAKE)malloc(sizeof(SNAKE));
PSNAKE pp = (PSNAKE)malloc(sizeof(SNAKE));
pp = pHead2->Next;
if (sta2 == 1)
{
qq->x = pp->x;
qq->y = pp->y - 1;
qq->Next = pp;
pHead2->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead2->Next;
if (cannotbiteyourself2() == 1 || cannotcrossthewall2() == 1 || cannotbitethenselves() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
changecolor(5);
Sleep(50);
}
return true;
}
}
if (sta2 == 2)
{
qq->x = pp->x;
qq->y = pp->y + 1;
qq->Next = pp;
pHead2->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead2->Next;
if (cannotbiteyourself2() == 1 || cannotcrossthewall2() == 1 || cannotbitethenselves() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(50);
changecolor(5);
}
return true;
}
}
if (sta2 == 3)
{
qq->x = pp->x - 2;
qq->y = pp->y;
qq->Next = pp;
pHead2->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead2->Next;
if (cannotbiteyourself2() == 1 || cannotcrossthewall2() == 1 || cannotbitethenselves() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(50);
changecolor(5);
}
return true;
}
}
if (sta2 == 4)
{
qq->x = pp->x + 2;
qq->y = pp->y;
qq->Next = pp;
pHead2->Next = qq;
PSNAKE a = (PSNAKE)malloc(sizeof(SNAKE));
a = pHead2->Next;
if (cannotbiteyourself2() == 1 || cannotcrossthewall2() == 1 || cannotbitethenselves() == 1)
return false;
else
{
if (a->x == food->x && a->y == food->y)
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
Sleep(50);
createfood();
score += 5;
}
else
{
gotoxy(a->x, a->y);
printf("●");
a = a->Next;
while (a->Next->Next != NULL)
{
gotoxy(a->x, a->y);
printf("◆");
a = a->Next;
}
gotoxy(a->x, a->y);
printf("◆");
PSNAKE b = (PSNAKE)malloc(sizeof(SNAKE));
b = a->Next;
a->Next = NULL;
changecolor(11);
gotoxy(b->x, b->y);
printf("■");
free(b);
b = NULL;
Sleep(50);
changecolor(5);
}
return true;
}
}
}
int cannotbiteyourself2()
{
PSNAKE bite = (PSNAKE)malloc(sizeof(SNAKE));
bite = pHead2->Next->Next;
while (bite != NULL)
{
if (bite->x == pHead2->Next->x && bite->y == pHead2->Next->y)
flag = 1;
bite = bite->Next;
}
return flag;
}