資源簡介
使用C語言編寫貪吃蛇程序,實(shí)現(xiàn)計(jì)分,選擇游戲難度等功能,用鍵盤方向鍵操作。

代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?MAXWIDTH?30
#define?MAXHEIGHT?30
int?xCenter?=?MAXHEIGHT%2==0???MAXHEIGHT/2?:?MAXHEIGHT/2+1;
int?yCenter?=?MAXWIDTH%2==0???MAXWIDTH/2?:?MAXWIDTH/2+1;
int?callspeed;
//程序中用到的各種字符,以及它們的顏色和類型(以數(shù)字表示)
struct{
????char?*ch;
????int?color;
????char?type;}
charBorder?=?{“□“?4?1}??//邊框
charBg?=?{“■“?2?2}??//背景
charSnake?=?{“★“?0xe?3}??//貪吃蛇節(jié)點(diǎn)
charFood?=?{“●“?0xc?4};??//食物
?
//用一個(gè)結(jié)構(gòu)體數(shù)組保存地圖中的各個(gè)點(diǎn)
struct{
????char?type;
????int?index;}
globalMap[MAXWIDTH][MAXHEIGHT];
?
//貪吃蛇有效活動(dòng)范圍地圖的索引
struct{
????int?x;
????int?y;
}?snakeMap[?(MAXWIDTH-2)*(MAXHEIGHT-2)?]?scoresPostion;
?
int?scores?=?0;??//得分
int?snakeMapLen?=?(MAXWIDTH-2)*(MAXHEIGHT-2);
int?headerIndex?tailIndex;??//蛇頭蛇尾對(duì)應(yīng)的snakeMap中的索引(下標(biāo))
HANDLE?hStdin;??//控制臺(tái)句柄
?
void?setPosition(int?x?int?y){//?設(shè)置光標(biāo)位置,x為行,y為列
????COORD?coord;
????coord.X?=?2*y;
????coord.Y?=?x;
????SetConsoleCursorPosition(hStdin?coord);}
void?setColor(int?color){//?設(shè)置顏色
????SetConsoleTextAttribute(hStdin?color);}
void?createFood(){//創(chuàng)建食物
????int?index?rang?x?y;
????//產(chǎn)生隨機(jī)數(shù),確定?snakeMap?數(shù)組的索引?
????srand((unsigned)time(NULL));
????if(tailIndex ????????rang?=?headerIndex-tailIndex-1;
????????index?=?rand()%rang?+?tailIndex?+?1;
????}else{
????????rang?=?snakeMapLen?-?(tailIndex?-?headerIndex+1);
????????index?=?rand()%rang;
????????if(index>=headerIndex){
????????????index?+=?(tailIndex-headerIndex+1);
????????????if(index>28*28){index=tailIndex+1;}?
????????}
????}
????x?=?snakeMap[index].x;
????y?=?snakeMap[index].y;
????setPosition(x?y);
????setColor(charFood.color);
????printf(“%s“?charFood.ch);
????globalMap[x][y].type=charFood.type;
}
?
//死掉
void?die(){
????setPosition(xCenter?yCenter-5);
????setColor(0xC);
????printf(“You?die!?Game?Over!“);
????setPosition(xCenter+1?yCenter-5);
????setColor(0xC);
????getch();
????exit(0);
}
?
//?蛇移動(dòng)
void?move(char?direction){
????int?newHeaderX?newHeaderY;??//新蛇頭的坐標(biāo)
????int?newHeaderPreIndex;??//新蛇頭坐標(biāo)以前對(duì)應(yīng)的索引
????int?newHeaderPreX?newHeaderPreY;??//新蛇頭的索引以前對(duì)應(yīng)的坐標(biāo)
????int?newHeaderPreType;??//新蛇頭以前的類型
????int?oldTailX?oldTailY;??//老蛇尾坐標(biāo)
????//新蛇頭的坐標(biāo)
????switch(direction){
????????case?‘w‘:newHeaderX?=?snakeMap[headerIndex].x-1;newHeaderY?=?snakeMap[headerIndex].y;break;
????????case?‘s‘:newHeaderX?=?snakeMap[headerIndex].x+1;newHeaderY?=?snakeMap[headerIndex].y;break;
????????case?‘a(chǎn)‘:newHeaderX?=?snakeMap[headerIndex].x;newHeaderY?=?snakeMap[headerIndex].y-1;break;
????????case?‘d‘:newHeaderX?=?snakeMap[headerIndex].x;newHeaderY?=?snakeMap[headerIndex].y+1;break;}
????headerIndex?=?headerIndex==0???snakeMapLen-1?:?headerIndex-1;????//新蛇頭的索引
????newHeaderPreIndex?=?globalMap[newHeaderX][newHeaderY].index;????//新蛇頭坐標(biāo)以前對(duì)應(yīng)的索引
????newHeaderPreX?=?snakeMap[headerIndex].x;????//新蛇頭的索引以前對(duì)應(yīng)的坐標(biāo)
????newHeaderPreY?=?snakeMap[headerIndex
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件????????8018??2018-05-03?16:22??貪吃蛇代碼.cpp
評(píng)論
共有 條評(píng)論