#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<time.h>
#define Down_Wall 28
#define Right_Wall 58
int Foodx, Foody;
char key;
int speed;
int score = 0,highscore=0;
void GetHighScore(int* highscore)
{
FILE* fp = NULL;
fp = fopen("highscore.txt", "r");
if (fp == NULL)
{
fp = fopen("highscore.txt", "w");
fprintf(fp, "%-10d", *highscore);
}
else
fscanf(fp, "%10d", highscore);
fclose(fp);
}
void StoreHighScore(int highscore)
{
FILE* fp = fopen("highscore.txt", "w");
fprintf(fp, "%-10d", highscore);
fclose(fp);
}
struct Snake
{
int x;
int y;
Snake* Next;
};
Snake* Head, *End,*p;
void gotoxy(int x, int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
HANDLE houtput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(houtput, pos);
}
void Start()
{
gotoxy(20, 5);
printf("■■■■■■■■■■■■■■■■■■■■■■\n");
gotoxy(20, 8);
printf("------------------贪吃蛇-------------------");
gotoxy(20, 11);
printf(" 输入方向键控制方向 按下任意键开始游戏");
gotoxy(20, 14);
printf("■■■■■■■■■■■■■■■■■■■■■■");
getchar();
}
void DrawFrame()
{
system("cls");
for (int i = 0; i < 60; i += 2)
{
gotoxy(i, 0);
printf("■");
gotoxy(i, Down_Wall);
printf("■");
}
for (int i = 1; i < 28; i++)
{
gotoxy(0, i);
printf("■");
gotoxy(Right_Wall, i);
printf("■");
}
}
void InitSnake()
{
int i = 0;
End = (Snake*)malloc(sizeof(struct Snake));
End->Next = NULL;
End->x = 30;
End->y = 14;
for (i = 0; i < 3; i++)
{
Head = (Snake*)malloc(sizeof(struct Snake));
Head->x = End->x + 2;
Head->y = End->y;
Head->Next = End;
End = Head;
}
while (End->Next)
{
gotoxy(End->x, End->y);
printf("■");
End = End->Next;
}
}
void Food()
{
srand(time(0));
flag:
while (1)
{
Foodx = rand() % (Right_Wall-2+1-3)+3;
Foody = rand() % (Down_Wall + 1 - 1 - 1) + 1;
if (Foodx % 2 == 0)
break;
}
p = Head;
while (p)
{
if (Foodx == p->x&&Foody == p->y)
goto flag;
else
p = p->Next;
}
gotoxy(Foodx, Foody);
printf("⊙");
}
int IsHitWall()
{
if (Head->x == Right_Wall || Head->x == 0 || Head->y == 0 || Head->y == Down_Wall)
{
if(score>highscore)
StoreHighScore(score);
GetHighScore(&highscore);
gotoxy(Right_Wall+2, 14);
printf("游戏结束 最高分为:%d", highscore);
gotoxy(Right_Wall, 15);
return 1;
}
return 0;
}
int IsBitYourself()
{
p= Head->Next;
while (p->Next)
{
if (Head->x == p->x&&Head->y == p->y)
{
gotoxy(Right_Wall, 14);
printf("游戏结束");
Sleep(50000);
return 1;
}
p = p->Next;
}
return 0;
}
int Move()
{
int ret;
if (key != 'a' && key != 'w' && key != 's' && key != 'd')
return 0;
ret = IsHitWall();
ret += IsBitYourself();
if (ret == 1)
{
return 1;
}
p = (Snake*)malloc(sizeof(struct Snake));
p->Next = Head;
switch (key)
{
case 'a':
p->x = Head->x - 2;
p->y = Head->y;
break;
case 's':
p->x = Head->x;
p->y = Head->y + 1;
break;
case 'd':
p->x = Head->x + 2;
p->y = Head->y;
break;
case 'w':
p->x = Head->x;
p->y = Head->y - 1;
break;
}
gotoxy(p->x, p->y);
printf("■");
Head = p;
if (p->x == Foodx && p->y == Foody)
{
Food();
score += 20;
gotoxy(Right_Wall+2, 14);
printf("得分:%d", score);
}
else
{
p = Head;
while (p->Next->Next)
p = p->Next;
gotoxy(p->x, p->y);
printf(" ");
free(p->Next);
p->Next = NULL;
p = Head;
}
}
void Play()
{
int mv_ret ;
key = 'd';
while (1)
{
if ((GetAsyncKeyState(VK_UP) && 0x8000) && key != 's')
key = 'w';
else if ((GetAsyncKeyState(VK_DOWN) && 0x8000) && key != 'w')
key = 's';
else if ((GetAsyncKeyState(VK_RIGHT) && 0x8000) && key != 'a')
key = 'd';
else if ((GetAsyncKeyState(VK_LEFT) && 0x8000) && key != 'd')
key = 'a';
else if (GetAsyncKeyState(VK_SPACE) && 0x8000)
{
while (1)
{
Sleep(100);
if (GetAsyncKeyState(VK_SPACE) && 0x8000)
break;
}
}
mv_ret = Move();
if (score >= 0 && score <= 50)
speed = 1000;
else if (score > 50 && score <= 100)
speed = 750;
else if (score > 100 && score <= 150)
speed = 500;
else if (score > 150 && score <= 200)
speed = 250;
else
speed = 180;
if ((GetAsyncKeyState(VK_LEFT) && 0x8000) || (GetAsyncKeyState(VK_RIGHT) && 0x8000) || (GetAsyncKeyState(VK_UP) && 0x8000) || (GetAsyncKeyState(VK_DOWN) && 0x8000))
speed = 100;
gotoxy(Right_Wall + 2, 15);
printf(" ");
gotoxy(Right_Wall + 2, 15);
printf("速度:%d", 1100 - speed);
Sleep(speed);
if (mv_ret == 1)
break;
}
}
int main()
{
Start();
DrawFrame();
InitSnake();
Food();
Play();
}
贪吃蛇小项目
最新推荐文章于 2024-05-08 00:03:50 发布