C语言-----贪吃蛇实现
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#define up 'w'
#define down 's'
#define right 'd'
#define left 'a'
void Welcome();
void StartGame();
void gotoxy(int x,int y);
void printxy(int x,int y);
void deletexy(int x,int y);
void CreateGraph();
void CreateFood();
int ClickControl();
void ChangeBody();
void MoveBody(int x, int y);
void Eating();
int Judge();
void Finish();
typedef struct Snake
{
int x;
int y;
struct Snake* next;
}snake;
struct Food{
int x;
int y;
}food;
snake* head;
char click;
int score=0;
int main()
{
system("color 0B");
StartGame();
if(ClickControl() == 0) return 0;
return 0;
}
int ClickControl()
{
while(1){ //while循环非常重要
if(Judge() == 0) return 0;
if(_kbhit()) {
click = _getch();
}
ChangeBody();
Eating();
}
return 1;
}
int Judge()
{
int x=head->x, y=head->y;
if(x == 0||x==56||y==0||y==19){
Finish(); //Finish 必须在return前
return 0;
}
snake* p = head->next;
while(p){
if(x==p->x&&y==p->y){
Finish();
return 0;
}
p=p->next;
}
return 1;
}
void Finish()
{
system("cls");
gotoxy(20,10);
printf("你个渣渣,不服再来");
gotoxy(20,12);
printf("Look Look 你的垃圾分%d\n",score);
snake* p = head,* q;
while(p){
q = p->next;
free(p);
p = q;
}
system("pause"); //提示:无此会出现一行线
}
void Eating()
{
int x = head->x, y= head->y;
if(x==food.x&&y==food.y){
score+=10;
gotoxy(68,10);
printf("%d",score);
snake* s = (snake*)malloc(sizeof(snake));
snake* p = head;
while(p->next){
p = p->next;
}
p->next = s;
s->next = NULL;
CreateFood();
}
}
void ChangeBody()
{
int x=head->x, y=head->y;
switch(click){
case up: y--; break;
case down: y++; break;
case right: x+=2; break;
case left: x-=2; break;
default: break;
}
if(x!=head->x||y!=head->y) MoveBody( x, y);
Sleep(150);
}
void MoveBody(int x, int y)
{
snake* _new = (snake*)malloc(sizeof(snake));
snake* p=head;
while(p->next->next){
p = p->next;
}
deletexy(p->next->x,p->next->y);
free(p->next);
p->next =NULL;
_new->x = x;
_new->y = y;
_new->next = head;
head = _new;
printxy(head->x,head->y);
}
void CreateFood()
{
short flag = false; // bool flag = false;为啥不行
while(!flag){
flag = 1;
srand((int)time(NULL)) ;
food.x = rand()%(54-2+1)+2;
food.y = rand()%(18-1+1)+1;
if(food.x%2 == 1) food.x--;
snake* p= head;
while(p!=NULL){
if(p->x==food.x&&p->y==food.y);
flag = 1; break;
p = p->next;
}
}
gotoxy(food.x,food.y);
printf("☆");
}
void CreateGraph()
{
int i;
for(i=0;i<58;i+=2){
printxy(i,0);
printxy(i,19);
}
for(i=0;i<20;i++){
printxy(0,i);
printxy(56,i);
}
gotoxy(60,10);
printf("score");
gotoxy(68,10);
printf("%d",score);
head = (snake*)malloc(sizeof(snake));
snake* p = (snake*)malloc(sizeof(snake));
snake* q = (snake*)malloc(sizeof(snake));
head->x = 20; head->y = 10;
p->x = 18; p->y = 10;
q->x = 16; q->y = 10;
head->next = p;
p->next = q;
q->next = NULL;
p = head;
while(p!=NULL){
printxy(p->x,p->y);
p = p->next;
}
}
void gotoxy(int x,int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
CONSOLE_CURSOR_INFO cursor;
cursor.bVisible = FALSE;
cursor.dwSize = sizeof(cursor);
SetConsoleCursorInfo(hOutput,&cursor);
}
void printxy(int x,int y)
{
gotoxy(x,y);
printf("■");
}
void deletexy(int x,int y)
{
gotoxy(x,y);
printf(" ");
}
void StartGame()
{
Welcome();
CreateGraph();
CreateFood();
gotoxy(0,20);
}
void Welcome()
{
printf("\n\n\n\n\t\t\tWelcome to the game\n");
printf("\n\t\t\tDesigner by me\n");
system("pause");
system("cls");
}