標題:
新人報道,第一帖就發(fā)個C語言程序吧,用C語言做的貪吃蛇
[打印本頁]
作者:
啦啦啦keep
時間:
2019-6-25 14:14
標題:
新人報道,第一帖就發(fā)個C語言程序吧,用C語言做的貪吃蛇
感興趣的可以下載共同學習學習
0.png
(3.62 KB, 下載次數: 73)
下載附件
2019-6-25 21:46 上傳
單片機源程序如下:
#include<stdio.h>
#include<windows.h>
#include<dos.h>
#include<time.h>
#include<stdlib.h>
#include <conio.h>
#define WIDTH 20
#define HEIGTH 20
#define SX 20//初始x坐標
#define SY 10//初始y坐標
#define LENGTH 1
#define LEFT 75
#define RIGHT 77
#define UP 72
#define BUTTOM 80
#define DIC UP//初始方向;
#define STEP 1
#define SL 1//每增加多少積分提升難度
#define RED FOREGROUND_RED
#define BLUE FOREGROUND_BLUE
#define GREEN FOREGROUND_GREEN
#define COLOR RED //蛇顏色 可通過RGB相加得出
#define BGCOLOR RED+BLUE+GREEN//背景顏色 可通過RGB相加得出
struct snake
{
int x;
int y;
};
struct node
{
snake point;
node *next;
};
char control;
int total=0,nd;
snake food;
void HideCursor()
{
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void movexy(int x, int y)
{
COORD coord;
HANDLE hscr;
coord.X = x;
coord.Y = y;
hscr = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hscr, coord);
}
void bgcolr()
{
HANDLE hscr = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hscr,BGCOLOR);
}
void showsnake(int x,int y)
{
HANDLE hscr = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hscr,FOREGROUND_INTENSITY|COLOR);
movexy(x,y);
printf("%s","■") ;
bgcolr();
}
void dig(node *v,node *n)//蛇向前走
{
if(n->next!=NULL)
dig(n,n->next);
n->point=v->point;
}
void update(node *head )//更新蛇位置
{
int step=STEP;
node *ol=head;
snake v=head->point;
if(control!=LEFT&&control!=RIGHT&&control!=BUTTOM&&control!=UP)return;
while(ol->next!=NULL)
{
ol=ol->next;
}
movexy(ol->point.x,ol->point.y);
printf("%s","□");
dig(head,head->next);
switch(control)
{
case LEFT:step=-1*step;
case RIGHT:v.x=v.x+2*step;v.y=v.y;head->point=v;break;
case UP:step=-1*step;
case BUTTOM:v.y=v.y+step;v.x=v.x;head->point=v;break;
}
}
node* newlist()//用鏈表存蛇并顯示出來
{
node *head=(node*)malloc(sizeof(node)),*ol;
int step=STEP;
ol=head;
ol->next=NULL;
snake v={SX+12,SY+6};
ol->point=v;
for(int i=0;i<LENGTH;i++)
{
node *t=(node*)malloc(sizeof(node));
ol->next=t;
t->next=NULL;
v=ol->point;
switch(DIC)
{
case LEFT:step=-1*STEP;
case RIGHT:v.x=v.x+2*step;v.y=v.y;break;
case UP:step=-1*STEP;
case BUTTOM:v.y=v.y+step;v.x=v.x;break;
}
t->point=v;
showsnake(v.x,v.y);
ol=t;
}
return head;
}
void add(node *t)//增加蛇身體
{
node *ol=(node*)malloc(sizeof(node));
ol->next=NULL;
while(t->next!=NULL)
{
t=t->next;
}
ol->point=t->point;
t->next=ol;
}
void show(node *node)//顯示鏈表是否正常;
{
while(node!=NULL)
{
printf("%d,%d\n",node->point.x,node->point.y);
node=node->next;
}
}
void init()//初始化背景,并去掉無關信息
{
bgcolr();
srand(time(0));
movexy(SX+WIDTH-WIDTH/2,SY+HEIGTH+5);
printf(" ");
HideCursor();
for(int i=0;i<WIDTH;i++)
{
movexy(SX,SY+i);
for(int j=0;j<HEIGTH;j++)
{
printf("%s","□");
}
}
}
void run(node *node)//顯示蛇頭
{
//snake v=node->point;
showsnake(node->point.x,node->point.y);
char *s[]={"簡單","困難","噩夢"};
movexy(SX+2*WIDTH+2,SY+HEIGTH-HEIGTH/2);
printf("難度:%s ",s[nd]);
movexy(SX+2*WIDTH+2,SY+HEIGTH-HEIGTH/2+2);
if(total%5==9)add(node);
printf("積分:%d ",total);
}
void over()
{
movexy(SX+2*WIDTH-20,SY+HEIGTH+5);
printf("游戲結束按esc退出或方向鍵重新游戲");
}
int eatf(node *node)//判斷蛇吃食物
{
if(node->point.x==food.x&&node->point.y==food.y)return 1;
return 0;
}
int cf(node *node)//食物合法性檢查
{
while(node!=NULL)
{
if(node->point.x!=food.x&&node->point.y!=food.y&&food.x%2==0)return 1;
node=node->next;
}
return 0;
}
void addfood(node *head)//添加食物并顯示
{
int x,y;
while(1)
{
x=rand()%(2*WIDTH)+SX;
y=rand()%(HEIGTH)+SY;
food.x=x;
food.y=y;
if(cf(head))break;
}
movexy(x,y);
printf("%s","★");
}
int main(void)
{
movexy(SX+WIDTH,SY-4);
printf("貪吃蛇");
movexy(SX+WIDTH-WIDTH/2,SY-3);
printf("可隨時按ESC終止游戲");
movexy(SX+WIDTH-WIDTH/2,SY-2);
printf("請按方向鍵開始游戲");
int sd[]={500,250,100};
while(1)
{
nd=0;
init();
node *head=newlist();
control=0;
total=0;
addfood(head);
//show(head);
while(1)
{
while(kbhit())
{
movexy(SX+WIDTH-WIDTH/2,SY-2);
char c;
if((c=getch())==LEFT||c==RIGHT||c==UP||c==BUTTOM||c==27)
{
printf(" ");
if(c==LEFT&&control==RIGHT||control==LEFT&&c==RIGHT)continue;
if(c==UP&&control==BUTTOM||control==UP&&c==BUTTOM)continue;
control=c;
}
}
if(control==27)break;
update(head);
if(head->point.x<SX||head->point.y<SY||head->point.x>=SX+2*WIDTH||head->point.y>=SY+HEIGTH)break;
if(eatf(head))
{
addfood(head);
total++;//積分增加
add(head);
if(nd<2&&total%SL==0)nd++;//難度增加
}
run(head);
Sleep(sd[nd]);
}
over() ;
if(getch()==27)break;
}
return 0;
}
復制代碼
所有資料51hei提供下載:
貪吃蛇.rar
(1.12 MB, 下載次數: 10)
2019-6-25 14:14 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1