求c语言的贪吃蛇的代码 能运行的
// ConsoleApplication1.cpp: 定义控制台应用程序的入口点。//#include "stdafx.h"#include #include #include #include #define frame_height 20 //define map size#define frame_width 40#define UP 'w' //define operate key#define DOWN 's'#define LEFT 'a'#define RIGHT 'd'int i, j, k;char ch = UP; //initial directionint grow = 0; //flag: if snake growstruct Food {int x;int y;}food;struct Snake {int x[50];int y[50];int len;int speed;}snake; //snake[0] is headvoid init_map(void);void update_food(void);void move_snake(void);int is_alive(void);void get_speed(void);void gotoxy(int x, int y);int main(){init_map(); //初始化地图while (1){update_food();//是否产生食物get_speed(); //获取速度move_snake(); //移动蛇身Sleep(snake.speed);//移动速度if (!(is_alive()))//蛇的死活(撞墙或自食)break;}printf("Game Over!");_getch();return 0;}//initializevoid init_map(void){//initial foodsrand(time(NULL));food.x = rand() % (frame_height - 2) + 1;food.y = rand() % (frame_width - 2) + 1;gotoxy(food.x, food.y);printf("!");//initial snakesnake.x[0] = frame_height / 2;snake.y[0] = frame_width / 2;gotoxy(snake.x[0], snake.y[0]);printf("@");snake.len = 3;snake.speed = 200;for (k = 1; k0; k--){snake.x[k] = snake.x[k - 1];snake.y[k] = snake.y[k - 1];}switch (ch){case UP: snake.x[0]--; break;case DOWN: snake.x[0]++; break;case LEFT: snake.y[0]--; break;case RIGHT: snake.y[0]++; break;default: break;}gotoxy(snake.x[0], snake.y[0]);printf("@");grow = 0;gotoxy(frame_height, 0);}//is aliveint is_alive(void){if (snake.x[0] == 0 || snake.x[0] == frame_height - 1 || snake.y[0] == frame_width - 1 || snake.y[0] == 0)return 0;for (k = 1; k<snake.len; k++)if (snake.x[k] == snake.x[0] && snake.y[k] == snake.y[0])return 0;return 1;}//speed upvoid get_speed(void){if (snake.len <= 6)snake.speed = 200;else if (snake.len <= 10)snake.speed = 100;else if (snake.len <= 20)snake.speed = 50;else if (snake.len <= 30)snake.speed = 30;else snake.speed = 20;}//move cursorvoid gotoxy(int x, int y){HANDLE hout;COORD cor;hout = GetStdHandle(STD_OUTPUT_HANDLE);cor.X = y;cor.Y = x;SetConsoleCursorPosition(hout, cor);}
求贪吃蛇C语言代码,有一定功能要求
以下是代码/* 贪吃蛇程序 by champking */#define N 200#include #include #include #define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bint i,key;int score = 0;/*得分*/int gamespeed = 100000;/*游戏速度自己调整*/struct Food{ int x;/*食物的横坐标*/ int y;/*食物的纵坐标*/ int yes;/*判断是否要出现食物的变量*/}food;/*食物的结构体*/struct Snake{ int x[N]; int y[N]; int node;/*蛇的节数*/ int direction;/*蛇移动方向*/ int life;/* 蛇的生命,0活着,1死亡*/}snake;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){ Init();/*图形驱动*/ DrawK();/*开始画面*/ GamePlay();/*玩游戏具体过程*/ Close();/*图形结束*/}/*图形驱动*/void Init(void){ int gd = DETECT, gm; initgraph(&gd, &gm, "c:\\tc"); cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/void DrawK(void){/*setbkcolor(LIGHTGREEN);*/ setcolor(11); setlinestyle(SOLID_LINE, 0, THICK_WIDTH);/*设置线型*/ for(i = 50; i 0; i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/ { snake.x[i] = snake.x[i-1]; snake.y[i] = snake.y[i-1]; } /*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/ switch(snake.direction) { case 1: snake.x[0] += 10; break; case 2: snake.x[0] -= 10; break; case 3: snake.y[0] -= 10; break; case 4: snake.y[0] += 10; break; } for(i = 3; i 595||snake.y[0]455)/*蛇是否撞到墙壁*/ { GameOver();/*本次游戏结束*/ snake.life=1; /*蛇死*/ } if(snake.life == 1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/ break; if(snake.x[0] == food.x && snake.y[0] == food.y)/*吃到食物以后*/ { setcolor(0);/*把画面上的食物东西去掉*/ rectangle(food.x, food.y, food.x + 10, food.y - 10); snake.x[snake.node] =- 20; snake.y[snake.node] =- 20; /*新的一节先放在看不见的位置,下次循环就取前一节的位置*/ snake.node++;/*蛇的身体长一节*/ food.yes = 1;/*画面上需要出现新的食物*/ score += 10; PrScore();/*输出新得分*/ } setcolor(4);/*画出蛇*/ for(i = 0; i < snake.node; i++) rectangle(snake.x[i], snake.y[i], snake.x[i] + 10, snake.y[i] - 10); delay(gamespeed); setcolor(0);/*用黑色去除蛇的的最后一节*/ rectangle(snake.x[snake.node-1], snake.y[snake.node-1], snake.x[snake.node-1] + 10, snake.y[snake.node - 1] - 10); } /*endwhile(!kbhit)*/ if(snake.life == 1)/*如果蛇死就跳出循环*/ break; key = bioskey(0);/*接收按键*/ if(key == ESC)/*按ESC键退出*/ break; else if(key == UP&&snake.direction!=4) /*判断是否往相反的方向移动*/ snake.direction=3; else if(key == RIGHT &&snake.direction != 2) snake.direction=1; else if(key == LEFT && snake.direction != 1) snake.direction = 2; else if(key == DOWN && snake.direction != 3) snake.direction = 4; }/*endwhile(1)*/}/*游戏结束*/void GameOver(void){ cleardevice(); PrScore(); setcolor(RED); settextstyle(0, 0, 4); outtextxy(200, 200, "GAME OVER"); getch();}/*输出成绩*/void PrScore(void){ char str[10]; setfillstyle(SOLID_FILL, YELLOW); bar(50, 15, 220, 35); setcolor(6); settextstyle(0,0,2); sprintf(str, "score:%d", score); outtextxy(55, 20, str);}/*图形结束*/void Close(void){ getch(); closegraph();}
C语言的贪吃蛇源代码
//******友情提示:如想速度快点,请改小_sleep(500)函数中参数***** #include #include #include #include #include const int H = 8; //地图的高 const int L = 16; //地图的长 char GameMap[H][L]; //游戏地图 int key; //按键保存 int sum = 1, over = 0; //蛇的长度, 游戏结束(自吃或碰墙) int dx[4] = {0, 0, -1, 1}; //左、右、上、下的方向 int dy[4] = {-1, 1, 0, 0}; struct Snake //蛇的每个节点的数据类型 { int x, y; //左边位置 int now; //保存当前节点的方向, 0,1,2,3分别为左右上下 }Snake[H*L]; const char Shead = '@'; //蛇头 const char Sbody = '#'; //蛇身 const char Sfood = '*'; //食物 const char Snode = '.'; //'.'在地图上标示为空 void Initial(); //地图的初始化 void Create_Food(); //在地图上随机产生食物 void Show(); //刷新显示地图 void Button(); //取出按键,并判断方向 void Move(); //蛇的移动 void Check_Border(); //检查蛇头是否越界 void Check_Head(int x, int y); //检查蛇头移动后的位置情况 int main() { Initial(); Show(); return 0; } void Initial() //地图的初始化 { int i, j; int hx, hy; system("title 贪吃蛇"); //控制台的标题 memset(GameMap, '.', sizeof(GameMap)); //初始化地图全部为空'.' system("cls"); srand(time(0)); //随机种子 hx = rand()%H; //产生蛇头 hy = rand()%L; GameMap[hx][hy] = Shead; Snake[0].x = hx; Snake[0].y = hy; Snake[0].now = -1; Create_Food(); //随机产生食物 for(i = 0; i _= H || Snake[0].y = L) over = 1; } void Check_Head(int x, int y) //检查蛇头移动后的位置情况 { if(GameMap[ Snake[0].x ][ Snake[0].y ] == '.') //为空 GameMap[ Snake[0].x ][ Snake[0].y ] = '@'; else if(GameMap[ Snake[0].x ][ Snake[0].y ] == '*') //为食物 { GameMap[ Snake[0].x ][ Snake[0].y ] = '@'; Snake[sum].x = x; //新增加的蛇身为蛇头后面的那个 Snake[sum].y = y; Snake[sum].now = Snake[0].now; GameMap[ Snake[sum].x ][ Snake[sum].y ] = '#'; sum++; Create_Food(); //食物吃完了马上再产生一个食物 } else over = 1; }
c语言贪吃蛇代码,有分
2.3程序源代码及注释#define N 200#include#include#include#define LEFT 0x4b00#define RIGHT 0x4d00#define DOWN 0x5000#define UP 0x4800#define ESC 0x011bint i,key;int score=0;/*得分*/int gamespeed=50000;/*游戏速度自己调整*/struct Food{int x;/*食物的横坐标*/int y;/*食物的纵坐标*/int yes;/*判断是否要出现食物的变量*/}food;/*食物的结构体*/struct Snake{int x[N];int y[N];int node;/*蛇的节数*/int direction;/*蛇移动方向*/int life;/* 蛇的生命,0活着,1死亡*/}snake;void Init(void);/*图形驱动*/void Close(void);/*图形结束*/void DrawK(void);/*开始画面*/void GameOver(void);/*结束游戏*/void GamePlay(void);/*玩游戏具体过程*/void PrScore(void);/*输出成绩*//*主函数*/void main(void){Init();/*图形驱动*/DrawK();/*开始画面*/GamePlay();/*玩游戏具体过程*/Close();/*图形结束*/}/*图形驱动*/void Init(void){int gd=DETECT,gm;registerbgidriver(EGAVGA_driver);initgraph(&gd,&gm,"c:\\program files\\winyes\\tc20h\\bgi");cleardevice();}/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/void DrawK(void){/*setbkcolor(LIGHTGREEN);*/setcolor(11);setlinestyle(SOLID_LINE,0,THICK_WIDTH);/*设置线型*/for(i=50;i<=600;i+=10)/*画围墙*/{rectangle(i,40,i+10,49);/*上边*/rectangle(i,451,i+10,460);/*下边*/}for(i=40;i<=450;i+=10){rectangle(50,i,59,i+10);/*左边*/rectangle(601,i,610,i+10);/*右边*/}}/*玩游戏具体过程*/void GamePlay(void){randomize();/*随机数发生器*/food.yes=1;/*1表示需要出现新食物,0表示已经存在食物*/snake.life=0;/*活着*/snake.direction=1;/*方向往右*/snake.x[0]=100;snake.y[0]=100;/*蛇头*/snake.x[1]=110;snake.y[1]=100;snake.node=2;/*节数*/PrScore();/*输出得分*/while(1)/*可以重复玩游戏,压ESC键结束*/{while(!kbhit())/*在没有按键的情况下,蛇自己移动身体*/{if(food.yes==1)/*需要出现新食物*/{food.x=rand()%400+60;food.y=rand()%350+60;while(food.x%10!=0)/*食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到*/food.x++;while(food.y%10!=0)food.y++;food.yes=0;/*画面上有食物了*/}if(food.yes==0)/*画面上有食物了就要显示*/{setcolor(GREEN);rectangle(food.x,food.y,food.x+10,food.y-10);}for(i=snake.node-1;i>0;i--)/*蛇的每个环节往前移动,也就是贪吃蛇的关键算法*/{snake.x[i]=snake.x[i-1];snake.y[i]=snake.y[i-1];}/*1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头*/switch(snake.direction){case 1:snake.x[0]+=10;break;case 2: snake.x[0]-=10;break;case 3: snake.y[0]-=10;break;case 4: snake.y[0]+=10;break;}for(i=3;i<snake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/{if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]){GameOver();/*显示失败*/snake.life=1;break;}}if(snake.x[0]595||snake.y[0]<55||snake.y[0]>455)/*蛇是否撞到墙壁*/{GameOver();/*本次游戏结束*/snake.life=1;/*蛇死*/}if(snake.life==1)/*以上两种判断以后,如果蛇死就跳出内循环,重新开始*/break;if(snake.x[0]==food.x&&snake.y[0]==food.y)/*吃到食物以后*/{setcolor(0);/*把画面上的食物东西去掉*/rectangle(food.x,food.y,food.x+10,food.y-10);snake.x[snake.node]=-20;snake.y[snake.node]=-20;/*新的一节先放在看不见的位置,下次循环就取前一节的位置*/snake.node++;/*蛇的身体长一节*/food.yes=1;/*画面上需要出现新的食物*/score+=10;PrScore();/*输出新得分*/}setcolor(4);/*画出蛇*/for(i=0;i<snake.node;i++)rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);delay(gamespeed);setcolor(0);/*用黑色去除蛇的的最后一节*/rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10);}/*endwhile(!kbhit)*/if(snake.life==1)/*如果蛇死就跳出循环*/break;key=bioskey(0);/*接收按键*/if(key==ESC)/*按ESC键退出*/break;elseif(key==UP&&snake.direction!=4)/*判断是否往相反的方向移动*/snake.direction=3;elseif(key==RIGHT&&snake.direction!=2)snake.direction=1;elseif(key==LEFT&&snake.direction!=1)snake.direction=2;elseif(key==DOWN&&snake.direction!=3)snake.direction=4;}/*endwhile(1)*/}/*游戏结束*/void GameOver(void){cleardevice();PrScore();setcolor(RED);settextstyle(0,0,4);outtextxy(200,200,"GAME OVER");getch();}/*输出成绩*/void PrScore(void){char str[10];setfillstyle(SOLID_FILL,YELLOW);bar(50,15,220,35);setcolor(6);settextstyle(0,0,2);sprintf(str,"score:%d",score);outtextxy(55,20,str);}/*图形结束*/void Close(void){getch();closegraph();}
求VC++6.0能运行的贪吃蛇代码
#include #include #include #define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define ESC 0x011b int i,key; int score=0; int gamespeed=32000; struct Food /*食物的结构体*/ { int x; /*食物的横坐标*/ int y; /*食物的纵坐标*/ int yes; /*食物是否出现的变量*/ }food; struct Snack /*蛇的结构体*/ { int x[N]; int y[N]; int node; /*蛇的节数*/ int direction; /*蛇的方向*/ int life; /*蛇的生命,0活着,1死亡*/ }snake; void Init(void); /*图形驱动*/ void Close(void); /*关闭游戏函数*/ void DrawK(void); /*画图函数*/ void GameOver(void);/*输出失败函数*/ void GamePlay(); /*游戏控制函数 主要程序*/ void PrScore(void); /*分数输出函数*/ DELAY(char ch)/*调节游戏速度*/ { if(ch=='3') { delay(gamespeed); /*delay是延迟函数*/ delay(gamespeed); } else if(ch=='2') { delay(gamespeed); } } Menu()/*游戏开始菜单*/ { char ch; printf("Please choose the gamespeed:\n"); printf("1-Fast 2-Normal 3-Slow\n"); printf("\nPlease Press The numbers..\n"); do {ch=getch();} while(ch!='1'&&ch!='2'&&ch!='3'); clrscr(); return(ch); } /*主函数*/ void main(void) { int ch; ch=Menu(); Init(); DrawK(); GamePlay(ch); Close(); } void Init(void) { int gd=DETECT,gm; initgraph(&gd,&gm,"c:\\tc"); cleardevice(); } void DrawK(void) { setcolor(11); setlinestyle(SOLID_LINE,0,THICK_WIDTH); for(i=50;i<=600;i+=10) { rectangle(i,40,i+10,49); /*画出上边框*/ rectangle(i,451,i+10,460); /*画出下边框*/ } for(i=40;i<=450;i+=10) { rectangle(50,i,59,i+10); /*画出左边框*/ rectangle(601,i,610,i+10); /*画出右边框*/ } } void GamePlay(char ch) { randomize(); /*随机数发生器*/ food.yes=1; /*1代表要出现食物,0表示以存在食物*/ snake.life=0; snake.direction=1; snake.x[0]=100;snake.y[0]=100; snake.x[1]=110;snake.y[1]=100; snake.node=2; PrScore(); while(1) /*可以重复游戏*/ { while(!kbhit()) /*在没有按键的情况下蛇自己移动*/ { if(food.yes==1) /*需要食物*/ { food.x=rand()%400+60; food.y=rand()%350+60; /*使用rand函数随机产生食物坐标*/ while(food.x%10!=0) food.x++; while(food.y%10!=0) food.y++; /*判断食物是否出现在整格里*/ food.yes=0; /*现在有食物了*/ } if(food.yes==0) /*有食物了就要显示出来*/ { setcolor(GREEN); rectangle(food.x,food.y,food.x+10,food.y-10); } for(i=snake.node-1;i>0;i--) /*贪吃蛇的移动算法*/ { snake.x[i]=snake.x[i-1]; snake.y[i]=snake.y[i-1]; /*贪吃蛇的身体移动算法*/ } switch(snake.direction) /*贪吃蛇的头部移动算法,以此来控制移动*/ { case 1:snake.x[0]+=10;break; case 2:snake.x[0]-=10;break; case 3:snake.y[0]-=10;break; case 4:snake.y[0]+=10;break; } for(i=3;i<snake.node;i++) /*判断是否头部与身体相撞*/ { if(snake.x[i]==snake.x[0]&&snake.y[i]==snake.y[0]) { GameOver(); snake.life=1; break; } } /*下面是判断是否撞到墙壁*/ if(snake.x[0]595||snake.y[0]455) { GameOver(); snake.life=1; } if(snake.life==1) /*如果死亡就退出循环*/ break; if(snake.x[0]==food.x&&snake.y[0]==food.y) /*判断蛇是否吃到食物*/ { setcolor(0); rectangle(food.x,food.y,food.x+10,food.y-10); /*吃的食物后用黑色将食物擦去*/ snake.x[snake.node]=-20;snake.y[snake.node]=-20; /*现把增加的一节放到看不到的地方去*/ snake.node++; food.yes=1; score+=10; PrScore(); } setcolor(4); /*每次移动后将后面的身体擦去*/ for(i=0;i<snake.node;i++) rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10); delay(gamespeed); DELAY(ch); setcolor(0); rectangle(snake.x[snake.node-1],snake.y[snake.node-1],snake.x[snake.node-1]+10,snake.y[snake.node-1]-10); } if(snake.life==1) break; key=bioskey(0); /*接受按键*/ if(key==ESC) break; else if(key==UP&&snake.direction!=4)/*判断是否改变方向*/ snake.direction=3; else if(key==RIGHT&&snake.direction!=2) snake.direction=1; else if(key==LEFT&&snake.direction!=1) snake.direction=2; else if(key==DOWN&&snake.direction!=3) snake.direction=4; } } void GameOver(void) { cleardevice(); setcolor(RED); settextstyle(0,0,4); outtextxy(200,200,"GAME OVER"); getch(); } void PrScore(void) { char str[10]; setfillstyle(SOLID_FILL,YELLOW); bar(50,15,220,35); setcolor(6); settextstyle(0,0,2); sprintf(str,"scord:%d",score); outtextxy(55,20,str); } void Close(void) { getch(); closegraph(); }
求在VC++6.0中运行的贪吃蛇代码
#包括#包括#包括#包括#包括#包括#defineWIDTH40#defineHEIGH12枚举方向{//方向离开了,对的,向上下};StructFood{//食品Intx;Inty;};{//绘制蛇体intx;inty;structNode*next;};structSnake{//蛇属性duintlenth;//长度enumdirectiondir;//方向};structFood*food;//食物structSnake*snake;//蛇属性structNode*snode,*tail;//蛇身intSPEECH=200;intscore=0;//分数intsmark=0;//吃食物标zhi记inttimes=0;intSTOP=0;voidInitfood();//产生食物voidInitsnake();//构造snakevoidEatfood();//头部前进voidAddnode(intx,inty);//增加蛇身voiddisplay(structNode*shead);//显示蛇身坐dao标voidmove();//蛇移动voiddraw();//画蛇voidHomepage();//主页voidkeybordhit();//监控键盘按键voidAddtail();//吃到食物voidgotoxy(intx,inty)//定位光标{COORDpos;pos.X=x-1;pos.Y=y-1;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}voidInitsnake()//构造snake{inti;snake=(structSnake*)malloc(sizeof(structSnake));tail=(structNode*)malloc(sizeof(structNode));food=(structFood*)malloc(sizeof(structFood));snake->lenth=5;//初始长度 5snake->dir=RIGHT;//初始蛇头方向 右for(i=2;ilenth+2;i++)//增加 5 个结点{Addnode(i,2);}}voidInitfood()//产生食物{structNode*p=snode;intmark=1;srand((unsigned)time(NULL));//以时间为种子产生随机数while(1){food->x=rand()%(WIDTH-2)+2;//食物X坐标food->y=rand()%(HEIGH-2)+2;//食物Y坐标while(p!=NULL){if((food->x==p->x)&&(food->y==p->y))//如果食物产生在蛇身上{//则重新生成食物mark=0;//食物生成无效break;}p=p->next;}if(mark==1)//如果食物不在蛇身上,生成食物,否则重新生成食物{gotoxy(food->x,food->y);printf("%c",3);break;}mark=1;p=snode;}}voidmove()//移动{structNode*q,*p=snode;if(snake->dir==RIGHT){Addnode(p->x+1,p->y);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}}if(snake->dir==LEFT){Addnode(p->x-1,p->y);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}}if(snake->dir==UP){Addnode(p->x,p->y-1);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}}if(snake->dir==DOWN){Addnode(p->x,p->y+1);if(smark==0){while(p->next!=NULL){q=p;p=p->next;}q->next=NULL;free(p);}}}voidAddnode(intx,inty)//增加蛇身{structNode*newnode=(structNode*)malloc(sizeof(structNode));structNode*p=snode;newnode->next=snode;newnode->x=x;newnode->y=y;snode=newnode;//结点加到蛇头if(x=WIDTH||y=HEIGH)//碰到边界{STOP=1;gotoxy(10,19);printf("撞墙,游戏结束,任意键退出!\n");//失败_getch();free(snode);//释放内存free(snake);exit(0);}while(p!=NULL)//碰到自身{if(p->next!=NULL)if((p->x==x)&&(p->y==y)){STOP=1;gotoxy(10,19);printf("撞到自身,游戏结束,任意键退出!\n");//失败_getch();free(snode);//释放内存free(snake);exit(0);}p=p->next;}}voidEatfood()//吃到食物{Addtail();score++;}voidAddtail()//增加蛇尾{structNode*newnode=(structNode*)malloc(sizeof(structNode));structNode*p=snode;tail->next=newnode;newnode->x=50;newnode->y=20;newnode->next=NULL;//结点加到蛇头tail=newnode;//新的蛇尾}voiddraw()//画蛇{structNode*p=snode;inti,j;while(p!=NULL){gotoxy(p->x,p->y);printf("%c",2);tail=p;p=p->next;}if(snode->x==food->x&&snode->y==food->y)//蛇头坐标等于食物坐标{smark=1;Eatfood();//增加结点Initfood();//产生食物}if(smark==0){gotoxy(tail->x,tail->y);//没吃到食物清除之前的尾结点printf("%c",'');//如果吃到食物,不清楚尾结点}else{times=1;}if((smark==1)&&(times==1)){gotoxy(tail->x,tail->y);//没吃到食物清除之前的尾结点printf("%c",'');//如果吃到食物,不清楚尾结点smark=0;}gotoxy(50,12);printf("食物: %d,%d",food->x,food->y);gotoxy(50,5);printf("分数:%d",score);gotoxy(50,7);printf("速度:%d",SPEECH);gotoxy(15,14);printf("按o键加速");gotoxy(15,15);printf("按p键减速");gotoxy(15,16);printf("按空格键暂停");}voidHideCursor()//隐藏光标{CONSOLE_CURSOR_INFOcursor_info={1,0};SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);}voidHomepage()//绘主页{intx,y;HideCursor();//隐藏光标printf("----------------------------------------\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("|\t\t\t\t|\n");printf("----------------------------------------\n");gotoxy(5,13);printf("任意键开始游戏!按W.A.S.D控制方向");_getch();Initsnake();Initfood();gotoxy(5,13);printf("");}voidkeybordhit()//监控键盘{charch;if(_kbhit()){ch=getch();switch(ch){case'W':case 'w':if(snake->dir==DOWN)//如果本来方向是下,而按相反方向无效{break;}elsesnake->dir=UP;break;case'A':case 'a':if(snake->dir==RIGHT)//如果本来方向是右,而按相反方向无效{break;}elsesnake->dir=LEFT;break;case'S':case 's':if(snake->dir==UP)//如果本来方向是上,而按相反方向无效{break;}elsesnake->dir=DOWN;break;case'D':case 'd':if(snake->dir==LEFT)//如果本来方向是左,而按相反方向无效{break;}elsesnake->dir=RIGHT;break;case'O':case'o':if(SPEECH>=150)//速度加快{SPEECH=SPEECH-50;}break;case'P':case'p':if(SPEECH<=400)//速度减慢{SPEECH=SPEECH+50;}break;case''://暂停gotoxy(15,18);printf("游戏已暂停,按任意键恢复游戏");system("pause>nul");gotoxy(15,18);printf("");break;default:break;}}}intmain(void)//程序入口{Homepage();while(!STOP){keybordhit();//监控键盘按键move();//蛇的坐标变化draw();//蛇的重绘Sleep(SPEECH);//暂时挂起线程}return0;}扩展资料:注意事项:1.代码将源信息转换为易于通信或存储的符号。译码(译码)是还原和译码的过程,它将代码符号转换为接受者能够理解的形式。2.编码的原因之一是为了在普通语言(口头或书面)难以实现的情况下进行交流。例如,一面旗帜可以用一个特定的标记来表达一个特定的信息,而站在远处的另一个人可以解释标记来重现该信息。
求贪吃蛇代码,c++6.0
#include #include #include #include #include #include using namespace std;enum Direction{Up=72,Down=80,Left=75,Right=77};enum MapValue{Lattice,Food,Wall};struct Vector2{ int X,Y; Vector2 operator+( const Vector2 &value) { X+=value.X; Y+=value.Y; return *this; } bool operator ==(const Vector2 &value)const{ return X == value.X && Y == value.Y; }};void SetPos(SHORT x,SHORT y, char *str){ SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), COORD{x,y}); puts(str);}class Snake{public: Snake(const int &x, const int &y){Add({x,y});} void Add(const Vector2 &pos){ m_Body.push_front(pos); SetPos(pos.X,pos.Y,"O"); } void Move(const int &x, const int &y){ auto pos = m_Body.front(); Add(pos+Vector2{x,y}); pos = m_Body.back(); m_Body.pop_back(); SetPos(pos.X,pos.Y," "); } bool IsBody(const Vector2 &pos)const{ auto it=m_Body.begin(); for(it++;it!=m_Body.end();it++) if((*it)==pos) return true; return false; } bool IsFront(const Vector2 &pos)const{return m_Body.front() == pos;} Vector2 GetFront()const{return m_Body.front();}private: listm_Body;};class Map{public: Map(const int &width, const int &height):m_Width(width),m_Height(height){ m_Map = new char*[height]; for(int i=0;i<height;i++){ m_Map[i] = new char[width]; memset(m_Map[i],Lattice,width); } int end = height-1; for(int i=0;i<width;i++){ m_Map[0][i] = m_Map[end][i] = Wall; SetPos(i,0,"+"); SetPos(i,end,"+"); } end = width-1; for(int i=1;i<height;i++){ m_Map[i][0] = m_Map[i][end] = Wall; SetPos(0,i,"+"); SetPos(end,i,"+"); } } bool IsFood(const Vector2 &pos)const{return m_Map[pos.Y][pos.X] == Food;} bool IsWall(const Vector2 &pos)const{return m_Map[pos.Y][pos.X] == Wall;} void AddFood(const Snake &snake){ Vector2 pos; do{ pos={rand()%m_Width,rand()%m_Height}; }while(snake.IsBody(pos)||snake.IsFront(pos)||IsWall(pos)); m_Map[pos.Y][pos.X] = Food; SetPos(pos.X,pos.Y,"-"); } void DeleteFood(const Vector2 &pos){m_Map[pos.Y][pos.X] = Lattice;}private: int m_Width,m_Height; char **m_Map;};Vector2 &Input(char &ch, Vector2 &direction){ if(_kbhit()){ ch = getch(); if(ch == -32){ ch = getch(); switch(ch){ case Up:if(direction.Y!=1)direction = {0,-1};break; case Down:if(direction.Y!=-1)direction = {0,1};break; case Left:if(direction.X!=1)direction={-1,0};break; case Right:if(direction.X!=-1)direction={1,0};break; } } } return direction;}int main(){ srand(time(0)); CONSOLE_CURSOR_INFO cursor_info = {1, 0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info); Map map(50,20); Snake snake(1,1); Vector2 direction={1,0},movePos; char ch; map.AddFood(snake); while(true){ direction = Input(ch,direction); movePos = snake.GetFront()+direction; if(map.IsFood(movePos)){ snake.Add(movePos); map.DeleteFood(movePos); map.AddFood(snake); } snake.Move(direction.X,direction.Y); if(map.IsWall(snake.GetFront())|| snake.IsBody(snake.GetFront())){ break; } Sleep(200); } system("color fc"); SetPos(21,9,"GAMEOVER"); return 0;}
求c++贪吃蛇的代码
/*贪吃蛇游戏,由于是C++源码 且 用到Windows API ,是控制台界面不是图形界面,需要用VC++6.0 或 VC++2010 在windows环境编译运行。如果符合上述条件一定可以编译运行zjlj,2015.3.16*/#define DEBUG 0 //当程序在调试阶段时 DEBUG为 1#include#include#include#includeusing namespace std;void readini(FILE **fphead, int *score, char *argv[]) //创建或打开一个和运行文件对应的ini文件,读取最高纪录{ char filename[200],*pfilename; int flag=-1,i; strcpy(filename,argv[0]); for(i=0;filename[i]!='\0';i++) { if ('.'==filename[i])flag=1; } if(1==flag) { filename[i-1]='i'; filename[i-2]='n'; filename[i-3]='i'; } else { filename[i]='.'; filename[i+1]='i'; filename[i+2]='n'; filename[i+3]='i'; filename[i+4]='\0'; } for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i]; if ( (*fphead=fopen(pfilename, "rb+"))==NULL) { if ( (*fphead=fopen(pfilename, "wb+"))==NULL) { printf("无法创建或打开\"%s\"文件\n",pfilename); system("pause"); exit(0); } } else { fread(score,sizeof(int),1,*fphead); }}void writeini(FILE **fphead, int *score, char *argv[]) //打开一个和运行文件对应的ini文件,写入最高纪录{ char filename[200],*pfilename; int flag=-1,i; strcpy(filename,argv[0]); for(i=0;filename[i]!='\0';i++) { if ('.'==filename[i])flag=1; } if(1==flag) { filename[i-1]='i'; filename[i-2]='n'; filename[i-3]='i'; } else { filename[i]='.'; filename[i+1]='i'; filename[i+2]='n'; filename[i+3]='i'; filename[i+4]='\0'; } for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i]; if ( (*fphead=fopen(pfilename, "wb+"))==NULL) { printf("无法写入\"%s\"文件,磁盘写保护!\n",pfilename); system("pause"); exit(0); } else { rewind(*fphead); fwrite(score,sizeof(int),1,*fphead); fclose(*fphead); }}void gotoxy(int x,int y)//光标定位,光标定位函数SetConsoleCursorPosition是左上角位置是0,0然后向左向下延伸{COORD pos;pos.X=2*y;pos.Y=x;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}void color(int a)//颜色函数{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);} void Refresh(int q[][22], int grade, int gamespeed, int length,int score) // 输出贪吃蛇棋盘{ int i,j; for(i=0;i=0;i--) { start=clock(); while(clock()-start0) cout =(length+(grade-1)*8)*10)//判断是否破记录 { gotoxy(10,7); color(12); cout =(length+(grade-1)*8)*10)//判断是否破记录 { gotoxy(10,7); color(12); cout =8)//长度大于等于8重新计算长度,等级加1 { length -= 8;//重新计算长度 grade ++;//等级加1 if(gamespeed>50)//控制最快速度为50 gamespeed = 550 - grade * 50; // 改变自动前进时间间隔 } tcsQipan[x][y]= 3;//贪吃蛇棋盘相应坐标现在蛇头标志改为蛇头标志3 tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = 4;//贪吃蛇棋盘相应坐标原来蛇头标志改为蛇身标志4 head = (head+1)%400;//防止数组越界 tcsZuobiao[0][head] = x;//蛇头的x坐标 tcsZuobiao[1][head] = y;//蛇头的y坐标 do//随机出果子 { x1=rand()%20+1; y1=rand()%20+1; } while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子 tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5 gotoxy(22,12); cout << "\t游戏进行中!" << endl; Refresh(tcsQipan,grade,gamespeed,length,score); } else // 不吃果子 { if(otherkey) { tcsQipan [tcsZuobiao[0][tail]][tcsZuobiao[1][tail]]=0; tail=(tail+1)%400;//防止数组越界 tcsQipan [tcsZuobiao[0][head]][tcsZuobiao[1][head]]=4; head=(head+1)%400;//防止数组越界 tcsZuobiao[0][head]=x;//蛇头的x坐标 tcsZuobiao[1][head]=y;//蛇头的y坐标 tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]]=3; gotoxy(22,12); cout << "\t游戏进行中!" << endl; Refresh(tcsQipan,grade,gamespeed,length,score); } else { gotoxy(22,12); cout << "\t游戏暂停中!" << endl; } } /* 游戏运行时的核心算法结束 */ } while(1) { while(!kbhit()); if((direction =getch())==13)//按回车键开始下一局 break; if(direction ==27)//按ESC退出游戏 exit(0); } system("cls");//清除屏幕重新开始 } return 0;}
c语言贪吃蛇源代码怎么用?
C语言贪吃蛇源代码必须经过相应的C/C++编译器编译成EXE文件后才能运行。 由于我们通常使用的操作系统是Windows系统,而在该系统下最长用的C/C++编译器是VC++编译器,目前在大专院校常用的版本还是VC++6.0 下面就以VC++6.0来说明编译过程:1.在VC++6.0中通过“File”菜单下的 “Open”子菜单打开贪吃蛇代码 2.在VC++6.0中通过“Build”菜单下的 “Compile xxxx.xx”子菜单编译贪吃蛇代码 3.在VC++6.0中通过“Build”菜单下的 “Execute xxxx.exe”子菜单运行贪吃蛇程序 附:在VC++6环境下可运行的C/C++贪吃蛇源代码(无版权,自己编写,欢迎任意修改拷贝)/*C/C++贪吃蛇游戏,zjlj,2015.3.16*/#define DEBUG 0 //当程序在调试阶段时 DEBUG为 1#include#include#include#includeusing namespace std;void readini(FILE **fphead, int *score, char *argv[]) //创建或打开一个和运行文件对应的ini文件,读取最高纪录{ char filename[200],*pfilename; int flag=-1,i; strcpy(filename,argv[0]); for(i=0;filename[i]!='\0';i++) { if ('.'==filename[i])flag=1; } if(1==flag) { filename[i-1]='i'; filename[i-2]='n'; filename[i-3]='i'; } else { filename[i]='.'; filename[i+1]='i'; filename[i+2]='n'; filename[i+3]='i'; filename[i+4]='\0'; } for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i]; if ( (*fphead=fopen(pfilename, "rb+"))==NULL) { if ( (*fphead=fopen(pfilename, "wb+"))==NULL) { printf("无法创建或打开\"%s\"文件\n",pfilename); system("pause"); exit(0); } } else { fread(score,sizeof(int),1,*fphead); }}void writeini(FILE **fphead, int *score, char *argv[]) //打开一个和运行文件对应的ini文件,写入最高纪录{ char filename[200],*pfilename; int flag=-1,i; strcpy(filename,argv[0]); for(i=0;filename[i]!='\0';i++) { if ('.'==filename[i])flag=1; } if(1==flag) { filename[i-1]='i'; filename[i-2]='n'; filename[i-3]='i'; } else { filename[i]='.'; filename[i+1]='i'; filename[i+2]='n'; filename[i+3]='i'; filename[i+4]='\0'; } for(;filename[i]!='\\'&&i>=0;i--)pfilename=&filename[i]; if ( (*fphead=fopen(pfilename, "wb+"))==NULL) { printf("无法写入\"%s\"文件,磁盘写保护!\n",pfilename); system("pause"); exit(0); } else { rewind(*fphead); fwrite(score,sizeof(int),1,*fphead); fclose(*fphead); }}void gotoxy(int x,int y)//光标定位,光标定位函数SetConsoleCursorPosition是左上角位置是0,0然后向左向下延伸{COORD pos;pos.X=2*y;pos.Y=x;SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);}void color(int a)//颜色函数{SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);}void Refresh(int q[][22], int grade, int gamespeed, int length,int score) // 输出贪吃蛇棋盘{ int i,j; for(i=0;i=0;i--) { start=clock(); while(clock()-start0) cout =(length+(grade-1)*8)*10)//判断是否破记录 { gotoxy(10,7); color(12); cout =(length+(grade-1)*8)*10)//判断是否破记录 { gotoxy(10,7); color(12); cout =8)//长度大于等于8重新计算长度,等级加1 { length -= 8;//重新计算长度 grade ++;//等级加1 if(gamespeed>50)//控制最快速度为50 gamespeed = 550 - grade * 50; // 改变自动前进时间间隔 } tcsQipan[x][y]= 3;//贪吃蛇棋盘相应坐标现在蛇头标志改为蛇头标志3 tcsQipan[tcsZuobiao[0][head]][tcsZuobiao[1][head]] = 4;//贪吃蛇棋盘相应坐标原来蛇头标志改为蛇身标志4 head = (head+1)%400;//防止数组越界 tcsZuobiao[0][head] = x;//蛇头的x坐标 tcsZuobiao[1][head