1.教程
百度雲鏈接:
鏈接:https://pan.baidu.com/s/1x6WkRs9L5yuXH8fsz1iMmA
提取碼:tp4x
2.代碼(親測DVC++可運行)
#include "stdio.h"
#include "time.h"
#include "windows.h"
#include "stdlib.h"
#include "conio.h"
#define U 1
#define D 2
#define L 3
#define R 4 //蛇的狀態,U:上 ;D:下;L:左 R:右
void gotoxy(int x,int y);//設置光標位置
int color(int c); //更改文字顏色
void printfsnake(); //輸出字符蛇畫
void welcometogame(); //開始界面
void createMap(); //繪製地圖
void scoreandtips(); //遊戲界面右側的得分和小提示
void initsnake(); //初始化蛇身,畫蛇身
int createfood(); //創造食物
int biteself(); //判斷是否咬到了自己
void cantcrosswall(); //蛇是否撞牆
void speedup(); //加速
void speeddown(); //減速
void snakemove(); //控制蛇前進的方向
void keyboardControl(); //控制鍵盤按鍵
void Lostdraw(); //遊戲結束界面
void endgame(); //遊戲結束
void choose(); //遊戲失敗後的選擇
void File_out(); //在文件中讀取最高分
void File_in(); //儲存最高分進文件
void explation(); //遊戲說明
typedef struct snake//結構體
{
int x,y;
struct snake *next;
}snake;
int score=0,add=10;
int HighScore=0;
int status,sleeptime=200;//蛇的狀態,運行時間間隔
snake *head,*food,*q;
int endgamestatus=0;//結束時蛇的狀態(1撞牆,2咬到自己,3按Ese)
HANDLE h0ut; //控制檯句柄
//system("color xx")
int color(int c) //更改文字顏色
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
void gotoxy(int x,int y) //設置光標位置
{
COORD xy;
xy.X=x;
xy.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),xy);
}
void printfsnake() //畫蛇
{
gotoxy(35,1);
color(6);
printf("/^\\/^\\"); //蛇眼睛
gotoxy(34,2);
printf("|__| O|"); //蛇眼睛
gotoxy(33,2);
color(2);
printf("_");
gotoxy(25,3);
color(12);
printf("\\/"); //蛇信
gotoxy(31,3);
color(2);
printf("/");
gotoxy(37,3);
color(6);
printf(" \\_/"); //蛇眼睛
gotoxy(41,3);
color(10);
printf(" \\");
gotoxy(26,4);
color(12);
printf("\\____"); //舌頭
gotoxy(32,4);
printf("_________/");
gotoxy(31,4);
color(2);
printf("|");
gotoxy(43,4);
color(10);
printf("\\");
gotoxy(32,5);
color(2);
printf("\\_______"); //蛇嘴
gotoxy(44,5);
color(10);
printf("\\");
gotoxy(39,6);
printf("| | \\"); //下面都是畫蛇身
gotoxy(38,7);
printf("/ / \\");
gotoxy(37,8);
printf("/ / \\ \\");
gotoxy(35,9);
printf("/ / \\ \\");
gotoxy(34,10);
printf("/ / \\ \\");
gotoxy(33,11);
printf("/ / _----_ \\ \\");
gotoxy(32,12);
printf("/ / _-~ ~-_ | |");
gotoxy(31,13);
printf("( ( _-~ _--_ ~-_ _/ |");
gotoxy(32,14);
printf("\\ ~-____-~ _-~ ~-_ ~-_-~ /");
gotoxy(33,15);
printf("~-_ _-~ ~-_ _-~");
gotoxy(35,16);
printf("~--______-~ ~-___-~");
}
void welcometogame() //開始界面
{
int n;
int i,j = 1;
gotoxy(43,18);
color(11);
printf("貪 吃 蛇 大 作 戰");
color(14); //黃色邊框
for (i = 20; i <= 26; i++) //輸出上下邊框┅
{
for (j = 27; j <= 74; j++) //輸出左右邊框┇
{
gotoxy(j, i);
if (i == 20 || i == 26)
{
printf("-");
}
else if (j == 27 || j == 74)
{
printf("|");
}
}
}
color(12);
gotoxy(35, 22);
printf("1.開始遊戲");
gotoxy(55, 22);
printf("2.遊戲說明");
gotoxy(35, 24);
printf("3.退出遊戲");
gotoxy(29,27);
color(3);
printf("請選擇[1 2 3]:[ ]\b\b"); //\b爲退格,使得光標處於[]中間
color(14);
scanf("%d", &n); //輸入選項
switch(n)
{
case(1):
system("cls");//清屏
createMap();
initsnake();
createfood();break;
case(2):explation();break; //遊戲說明
case(3):exit(0);break;//退出遊戲
default:
color(12);
gotoxy(40,28);
printf("請輸入1~3之間的數!");
getch(); //輸入任意鍵
system("cls");
printfsnake();
welcometogame();
keyboardControl();
endgame();
}
}
void createMap() //繪製地圖
{
int i,j;
for(i=0;i<=26;i++)
{
for(j=0;j<58;j+=2)
{
if(j==0||i==0||j==56||i==26)
{
color(5);
gotoxy(j,i);
printf("□");
}
else
{
color(3);
printf("■");
}
}
}
}
void scoreandtips() //遊戲界面右側的得分和小提示
{ //該函數在鍵盤控制函數中調用
File_out();
color(11);
gotoxy(64,4);
printf("☆最高紀錄☆:%d",HighScore);
color(14);
gotoxy(64,8);
printf("得分:%d",score);
color(13);
gotoxy(73,11);
printf("小 提 示");
color(6);
gotoxy(60,13);
printf("卍-----------------------卍");
gotoxy(60,25);
printf("卍-----------------------卍");
color(3);
gotoxy(64,14);
printf("每個食物得分:%d分",add);
gotoxy(64,16);
printf("不能穿牆,不能咬到自己");
gotoxy(64,18);
printf("用↑↓←→分別控制蛇的移動");
gotoxy(64,20);
printf("F1爲加速,F2爲減速");
gotoxy(64,22);
printf("space:暫停遊戲");
gotoxy(64,24);
printf("Esc退出遊戲") ;
}
void File_out() //在文件中讀取最高分
{
FILE *fp;
fp=fopen("save.txt","a+");
fscanf(fp,"%d",&HighScore);
fclose(fp);
}
void File_in() //寫入最高分
{
FILE *fp;
fp=fopen("save.txt","w+");
fprintf(fp,"%d",score);
}
void initsnake() //初始化蛇身
{ //該函數放入welcometogame()和choose()的case 1中
snake *tail;
int i;
tail=(snake*)malloc(sizeof(snake));
tail->x=24;
tail->y=5;
tail->next=NULL;
for(i=1;i<=4;i++)
{
head=(snake*)malloc(sizeof(snake));
head->next=tail;
head->x=24+2*i;
head->y=5;
tail=head;
}
while(tail)
{
gotoxy(tail->x,tail->y);
color(14);
printf("★");
tail=tail->next;
}
}
int createfood()//食物,不能出現在牆上和蛇身上,x座標要爲偶數
{
snake *food_1;
food_1=(snake*)malloc(sizeof(snake));
srand((unsigned)time(NULL));
while((food_1->x= (rand()%53+2) )%2 != 0); //選出2<=x<55中的偶數
food_1->y= (rand()%25+1);
q=head;
while(q)
{
if(q->x==food_1->x && q->y==food_1->y)
{
free(food_1);
createfood();
return 0;
}
q=q->next;
}
gotoxy(food_1->x,food_1->y);
color(12);
printf("●");
food=food_1;
}
int biteself() //判斷是否咬到了自己
{
snake *self;
self=head->next;
while(self)
{
if(self->x==head->x && self->y==head->y)
{
endgamestatus=2;
return 1;
}
self=self->next;
}
return 0;
}
void cantcrosswall() //蛇是否撞牆
{
if(head->x==0||head->x==56||head->y==26||head->y==0)
endgamestatus=1;
}
void speedup() //加速
{
if(sleeptime>=50)
{
sleeptime=sleeptime-10;
add=add+2;
}
}
void speeddown() //減速
{
if(sleeptime<350)
{
sleeptime+=30;
add-=2;
if(sleeptime==350)
add=1;
}
}
void snakemove() //不按鍵時蛇前進的方向UDLR
{
snake *nexthead;
nexthead=(snake*)malloc(sizeof(snake));
if(status==U)
{
nexthead->x=head->x;
nexthead->y=(head->y)-1;
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q)
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
score+=add;
speedup();
createfood();
}
else
{
while(q->next->next)
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
if(status==D)
{
nexthead->x=head->x;
nexthead->y=(head->y)+1;
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q)
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
score+=add;
speedup();
createfood();
}
else
{
while(q->next->next)
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
if(status==L)
{
nexthead->x=(head->x)-2;
nexthead->y=(head->y);
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q)
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
score+=add;
speedup();
createfood();
}
else
{
while(q->next->next)
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
if(status==R)
{
nexthead->x=(head->x)+2;
nexthead->y=(head->y);
nexthead->next=head;
head=nexthead;
q=head;
if(nexthead->x==food->x && nexthead->y==food->y)
{
while(q)
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
score+=add;
speedup();
createfood();
}
else
{
while(q->next->next) //跳出循環時,q指在鏈表的倒數第二個位置
{
gotoxy(q->x,q->y);
color(14);
printf("★");
q=q->next;
}
gotoxy(q->next->x,q->next->y);
color(3);
printf("■");
free(q->next);
q->next=NULL;
}
}
cantcrosswall(); //判斷蛇是否撞牆
biteself(); //是否咬到自己
}
void keyboardControl() //控制鍵盤按鍵
{
status=R;
while(1)
{
scoreandtips();
if(GetAsyncKeyState(VK_UP) && status!=D)
{
status=U;
}
else if(GetAsyncKeyState(VK_DOWN) && status!=U)
{
status=D;
}
else if(GetAsyncKeyState(VK_LEFT) && status!=R)
{
status=L;
}
else if(GetAsyncKeyState(VK_RIGHT) && status!=L)
{
status=R;
}
if(GetAsyncKeyState(VK_SPACE)) //空格暫停
{
while(1)
{
Sleep(300);
if(GetAsyncKeyState(VK_SPACE))
break;
}
}
else if(GetAsyncKeyState(VK_ESCAPE))
{
endgamestatus=3;
break;
}
else if(GetAsyncKeyState(VK_F1))
speedup();
else if(GetAsyncKeyState(VK_F2))
speeddown();
Sleep(sleeptime);
snakemove();
if(endgamestatus!=0)
{
break;
}
}
}
void explation()
{
int i,j;
system("cls");
color(13);
gotoxy(44,3);
printf("遊戲說明");
color(2);
for (i = 6; i <= 22; i++) //輸出上下邊框===
{
for (j = 20; j <= 75; j++) //輸出左右邊框||
{
gotoxy(j, i);
if (i == 6 || i == 22) printf("=");
else if (j == 20 || j == 75) printf("||");
}
}
color(3);
gotoxy(30,8);
printf("tip1: 不能穿牆,不能咬到自己");
color(10);
gotoxy(30,11);
printf("tip2: 用↑.↓.←.→分別控制蛇的移動");
color(14);
gotoxy(30,14);
printf("tip3: F1 爲加速,F2 爲減速");
color(11);
gotoxy(30,17);
printf("tip4: 按空格鍵暫停遊戲,再按空格鍵繼續");
color(4);
gotoxy(30,20);
printf("tip5: ESC :退出遊戲.space:暫停遊戲");
getch(); //按任意鍵返回主界面
system("cls");
printfsnake();
welcometogame();
}
void endgame() //遊戲結束 要更新最高分
{
system("cls");
if(endgamestatus==1)
{
Lostdraw();
gotoxy(35,9);
color(12);
printf("對不起,您撞到牆了。遊戲結束!");
}
else if(endgamestatus==2)
{
Lostdraw();
gotoxy(35,9);
color(12);
printf("對不起,您咬到自己了。遊戲結束!");
}
else if(endgamestatus==3)
{
Lostdraw();
gotoxy(40,9);
color(12);
printf("您已經結束了遊戲。");
}
gotoxy(43,12);
color(13);
printf("您的得分是 %d",score);
if(score >= HighScore)
{
color(10);
gotoxy(33,16);
printf("創紀錄啦!最高分被你刷新啦,真棒!!!");
File_in(); //把最高分寫進文件
}
else
{
color(10);
gotoxy(33,16);
printf("繼續努力吧~ 你離最高分還差:%d",HighScore-score);
}
choose();
}
void choose()
{
int n;
gotoxy(25,23);
color(12);
printf("我要重新玩一局-------1");
gotoxy(52,23);
printf("不玩了,退出吧-------2");
gotoxy(46,25);
color(11);
printf("選擇:");
scanf("%d", &n);
switch(n)
{
case 1:
system("cls"); //數據重置
add=10;
sleeptime=200;
score=0;
endgamestatus=0;
createMap();
initsnake();
createfood();
keyboardControl();
endgame();
break;
case 2:
exit(0);
break;
default:
gotoxy(35,27);
color(12);
printf("※※您的輸入有誤,請重新輸入※※");
system("pause >nul");
endgame();
break;
}
}
void Lostdraw()
{
system("cls");
int i;
gotoxy(45,2);
color(6);
printf("\\\\\\|///");
gotoxy(43,3);
printf("\\\\");
gotoxy(47,3);
color(15);
printf(".-.-");
gotoxy(54,3);
color(6);
printf("//");
gotoxy(44,4);
color(14);
printf("(");
gotoxy(47,4);
color(15);
printf(".@.@");
gotoxy(54,4);
color(14);
printf(")");
gotoxy(17,5);
color(11);
printf("+------------------------");
gotoxy(35,5);
color(14);
printf("oOOo");
gotoxy(39,5);
color(11);
printf("----------");
gotoxy(48,5);
color(14);
printf("(_)");
gotoxy(51,5);
color(11);
printf("----------");
gotoxy(61,5);
color(14);
printf("oOOo");
gotoxy(65,5);
color(11);
printf("-----------------+");
for(i = 6;i<=19;i++) //豎邊框
{
gotoxy(17,i);
printf("|");
gotoxy(82,i);
printf("|");
}
gotoxy(17,20);
printf("+---------------------------------");
gotoxy(52,20);
color(14);
printf("☆☆☆〃");
gotoxy(60,20);
color(11);
printf("----------------------+");
}
int main()
{
system("mode con cols=100 lines=30");
printfsnake();
welcometogame();
keyboardControl();
endgame();
return 0;
}