程式設計實現貪吃蛇小程式
阿新 • • 發佈:2019-01-06
#include<stdio.h>
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#define Key_Up 'w' // 向上方向鍵
#define Key_Down 's' // 向下方向鍵
#define Key_Right 'd' // 向右方向鍵
#define Key_Left 'a' // 向左方向鍵
#define Key_Space ' '
#define R 1 //向右的狀態量
#define L 2 //向左的狀態量
#define U 3 //向上的狀態量
#define D 4 //向下的狀態量
typedef struct node
{
int x;
int y;
struct node*next;
}snake;
//////////全域性變數
int score=0;
int endgamestatus=0;
int food_x,food_y;
snake*head;//蛇的頭結點
snake*p;//遍歷蛇身用的指標
int status=R;//蛇狀態變數
int key;
/////////
void endgame();//退出遊戲函式
void Pos(int x,int y);//游標定位函式
void crosswall();//判斷蛇是否撞到牆壁
void Creat_Food();//生成食物
int Bit_Self();//判斷蛇頭是否與蛇身有接觸
void Crat_Map();//生成地圖
void Snake_Moving();//蛇身移動
void gamecircle();// 遊戲迴圈
void pause();//遊戲暫停
////////
void Pos(int x,int y)
{
COORD pos;
HANDLE hOutput;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
}
int Bit_Self()
{
p=head->next;
while(p)
{
if(p->x==head->x&&p->y==head->y)
return 1;
p=p->next;
}
return 0;
}
void Creat_Map()
{
int i,j;
for(i=0;i<=54;i++)
for(j=0;j<=26;j++)
{
Pos(i,0);
printf("@");
Pos(i,26);
printf("@");
Pos(0,j);
printf("@");
Pos(54,j);
printf("@");
}
}
void crosswall()
{
if(head->x==0||head->y==0||head->x==54||head->y==26)
{
endgamestatus=1;
endgame();
}
}
void Creat_Food()
{
srand(time(NULL));
food_x=rand()%50+2;
food_y=rand()%24+2;
Pos(food_x,food_y);
printf("@");
}
void Init_Snake()
{
int i;
snake*tail;
head=(snake*)malloc(sizeof(snake));
head->x=25;
head->y=5;
head->next=NULL;
for(i=1;i<4;i++)
{
tail=(snake*)malloc(sizeof(snake));
tail->x=25+i*1;
tail->y=5;
tail->next=head;
head=tail;
}
while(tail)
{
Pos(tail->x,tail->y);
printf("@");
tail=tail->next;
}
}
void Snake_Moving()
{
snake*newhead;
newhead=(snake*)malloc(sizeof(snake));
crosswall();
if(Bit_Self())
{
endgamestatus=2;
endgame();
}
if(status==R)//向右走
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x+1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x+1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
if(status==L)
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x-1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x-1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
if(status==D)
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x+1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x;
newhead->y=head->y+1;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
if(status==U)
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x;
newhead->y=head->y-1;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x;
newhead->y=head->y-1;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
}
void gamecircle()
{
Pos(57,4);
printf("操作說明");
Pos(57,5);
printf("w a s d分別對應上 左 下 右 ");
Pos(57,6);
printf("按空格鍵暫停");
while(1)
{
Pos(57,7);
printf("遊戲分數:%d",score);
if(kbhit())
key=getch();
switch(key)
{
case Key_Right:
if(status!=L)
status=R;
break;
case Key_Left:
if(status!=R)
status=L;
break;
case Key_Up:
if(status!=D)
status=U;
break;
case Key_Down:
if(status!=U)
status=D;
break;
case Key_Space:
pause();
break;
default:
break;
}
Sleep(300);
Snake_Moving();
}
}
void pause()
{
while(1)
{
if(key=getch()==' ')
break;
}
}
void endgame()
{
system("cls");
Pos(27,13);
if(endgamestatus==1)
printf("您撞到牆了");
if(endgamestatus==2)
printf("您咬到了自己");
Pos(27,14);
printf("您的得分為%d",score);
exit(0);
}
void welcome()
{
Pos(27,13);
printf("歡迎來到貪吃蛇遊戲");
system("pause");
system("cls");
Pos(50,9);
printf("歡迎大家對原始碼進行修改");
Pos(50,11);
printf("開發出更多好玩的玩法");
Pos(50,12);
system("pause");
system("cls");
}
int main()
{
welcome();
Creat_Map();
Creat_Food();
Init_Snake();
gamecircle();
return 0;
}
#include<conio.h>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#define Key_Up 'w' // 向上方向鍵
#define Key_Down 's' // 向下方向鍵
#define Key_Right 'd' // 向右方向鍵
#define Key_Left 'a' // 向左方向鍵
#define Key_Space ' '
#define R 1 //向右的狀態量
#define L 2 //向左的狀態量
#define U 3 //向上的狀態量
#define D 4 //向下的狀態量
typedef struct node
{
int x;
int y;
struct node*next;
}snake;
//////////全域性變數
int score=0;
int endgamestatus=0;
int food_x,food_y;
snake*head;//蛇的頭結點
snake*p;//遍歷蛇身用的指標
int status=R;//蛇狀態變數
int key;
/////////
void endgame();//退出遊戲函式
void Pos(int x,int y);//游標定位函式
void crosswall();//判斷蛇是否撞到牆壁
void Creat_Food();//生成食物
int Bit_Self();//判斷蛇頭是否與蛇身有接觸
void Crat_Map();//生成地圖
void Snake_Moving();//蛇身移動
void gamecircle();// 遊戲迴圈
void pause();//遊戲暫停
////////
void Pos(int x,int y)
{
COORD pos;
HANDLE hOutput;
pos.X=x;
pos.Y=y;
hOutput=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hOutput,pos);
}
int Bit_Self()
{
p=head->next;
while(p)
{
if(p->x==head->x&&p->y==head->y)
return 1;
p=p->next;
}
return 0;
}
void Creat_Map()
{
int i,j;
for(i=0;i<=54;i++)
for(j=0;j<=26;j++)
{
Pos(i,0);
printf("@");
Pos(i,26);
printf("@");
Pos(0,j);
printf("@");
Pos(54,j);
printf("@");
}
}
void crosswall()
{
if(head->x==0||head->y==0||head->x==54||head->y==26)
{
endgamestatus=1;
endgame();
}
}
void Creat_Food()
{
srand(time(NULL));
food_x=rand()%50+2;
food_y=rand()%24+2;
Pos(food_x,food_y);
printf("@");
}
void Init_Snake()
{
int i;
snake*tail;
head=(snake*)malloc(sizeof(snake));
head->x=25;
head->y=5;
head->next=NULL;
for(i=1;i<4;i++)
{
tail=(snake*)malloc(sizeof(snake));
tail->x=25+i*1;
tail->y=5;
tail->next=head;
head=tail;
}
while(tail)
{
Pos(tail->x,tail->y);
printf("@");
tail=tail->next;
}
}
void Snake_Moving()
{
snake*newhead;
newhead=(snake*)malloc(sizeof(snake));
crosswall();
if(Bit_Self())
{
endgamestatus=2;
endgame();
}
if(status==R)//向右走
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x+1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x+1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
if(status==L)
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x-1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x-1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
if(status==D)
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x+1;
newhead->y=head->y;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x;
newhead->y=head->y+1;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
if(status==U)
{
if(head->x==food_x&&head->y==food_y)
{
score=score+10;
newhead->x=head->x;
newhead->y=head->y-1;
newhead->next=head;
head=newhead;
p=head;
while(p)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Creat_Food();
}
else
{
newhead->x=head->x;
newhead->y=head->y-1;
newhead->next=head;
head=newhead;
p=head;
while(p->next->next)
{
Pos(p->x,p->y);
printf("@");
p=p->next;
}
Pos(p->next->x,p->next-> y);
printf(" ");
free(p->next);
p->next=NULL;
}
}
}
void gamecircle()
{
Pos(57,4);
printf("操作說明");
Pos(57,5);
printf("w a s d分別對應上 左 下 右 ");
Pos(57,6);
printf("按空格鍵暫停");
while(1)
{
Pos(57,7);
printf("遊戲分數:%d",score);
if(kbhit())
key=getch();
switch(key)
{
case Key_Right:
if(status!=L)
status=R;
break;
case Key_Left:
if(status!=R)
status=L;
break;
case Key_Up:
if(status!=D)
status=U;
break;
case Key_Down:
if(status!=U)
status=D;
break;
case Key_Space:
pause();
break;
default:
break;
}
Sleep(300);
Snake_Moving();
}
}
void pause()
{
while(1)
{
if(key=getch()==' ')
break;
}
}
void endgame()
{
system("cls");
Pos(27,13);
if(endgamestatus==1)
printf("您撞到牆了");
if(endgamestatus==2)
printf("您咬到了自己");
Pos(27,14);
printf("您的得分為%d",score);
exit(0);
}
void welcome()
{
Pos(27,13);
printf("歡迎來到貪吃蛇遊戲");
system("pause");
system("cls");
Pos(50,9);
printf("歡迎大家對原始碼進行修改");
Pos(50,11);
printf("開發出更多好玩的玩法");
Pos(50,12);
system("pause");
system("cls");
}
int main()
{
welcome();
Creat_Map();
Creat_Food();
Init_Snake();
gamecircle();
return 0;
}