贪吃蛇c语言程序代码500行(贪吃蛇编程c语言代码)

http://www.itjxue.com  2023-03-07 20:55  来源:未知  点击次数: 

贪吃蛇c语言代码

#define N 200

#include graphics.h

#include stdlib.h

#include dos.h

#define LEFT 0x4b00

#define RIGHT 0x4d00

#define DOWN 0x5000

#define UP 0x4800

#define ESC 0x011b

int 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;

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=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;i0;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;isnake.node;i++)/*从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来*/

{

if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])

{

GameOver();/*显示失败*/

snake.life=1;

break;

}

}

if(snake.x[0]55||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.xsnake.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;isnake.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==UPsnake.direction!=4)

/*判断是否往相反的方向移动*/

snake.direction=3;

else

if(key==RIGHTsnake.direction!=2)

snake.direction=1;

else

if(key==LEFTsnake.direction!=1)

snake.direction=2;

else

if(key==DOWNsnake.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语言贪吃蛇小游戏

我说下我的思路..

把屏幕分成多个格子,蛇所在的格子高亮显示,.高亮蛇前面的一个格子,去掉蛇尾的格子高亮显示,大概就是这样了。

求贪吃蛇C语言代码

// 2.cpp : Defines the entry point for the application.

//

#include "stdafx.h"

#includestdio.h

#includeconio.h

#includetime.h

#includewindows.h

#includestdlib.h

int length=1;//蛇的当前长度,初始值为1

int line[100][2];//蛇的走的路线

int head[2]={40,12};//蛇头

int food[2];//食物的位置

char direction;//蛇运动方向

int x_min=1,x_max=77, y_min=2, y_max=23;//设置蛇的运动区域

int tail_before[2]={40,12};//上一个状态的蛇尾

char direction_before='s';//上一个状态蛇的运动方向

int live_death=1;//死活状态,0死,1活

int eat_flag=0;//吃食物与否的状态。0没吃 1吃了

int max=0;

int delay;//移动延迟时间

void gotoxy(int x, int y)//x为列坐标,y为行坐标

{

COORD pos = {x,y};

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

SetConsoleCursorPosition(hOut, pos);

}

void hidden()//隐藏光标

{

HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);

CONSOLE_CURSOR_INFO cci;

GetConsoleCursorInfo(hOut,cci);

cci.bVisible=0;//赋1为显示,赋0为隐藏

SetConsoleCursorInfo(hOut,cci);

}

void update_score()

{

gotoxy(2,1);

printf("我的分数:%d",length);

gotoxy(42,1);

printf("最高记录:%d",max);

}

void create_window()

{

gotoxy(0,0);

printf("╔══════════════════╦═══════════════════╗");

printf("║ ║ ║");

printf("╠══════════════════╩═══════════════════╣");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("║ ║");

printf("╚══════════════════════════════════════╝");

}

void update_line()

{

int i;

if(eat_flag==0)//吃了食物就不用记住上一个状态的蛇尾,否则会被消掉

{

tail_before[0]=line[0][0];//记住上一个状态的蛇尾

tail_before[1]=line[0][1];

for(i=0;ilength-1;i++)//更新蛇头以后部分

{

line[i][0]=line[i+1][0];

line[i][1]=line[i+1][1];

}

line[length-1][0]=head[0];//更新蛇头

line[length-1][1]=head[1];

}

}

void initial()

{

FILE *fp;

gotoxy(head[0],head[1]);

printf("蛇");

line[0][0]=head[0];//把蛇头装入路线

line[0][1]=head[1];

if((fp=fopen("highest","r"))==NULL)

{

fp=fopen("highest","w");

fprintf(fp,"%d",0);

max=0;

fclose(fp);

}//第一次使用时,初始化奖最高分为0

else

{

fp=fopen("highest","r");

fscanf(fp,"%d",max);

}

update_score();

}

void createfood()

{

int flag,i;

srand((unsigned)time(NULL));

for(;;)

{

for(;;)

{

food[0]=rand()%(x_max+1);

if(food[0]%2==0 food[0]x_min)

break;

}//产生一个偶数横坐标

for(;;)

{

food[1]=rand()%(y_max);

if(food[1]y_min)

break;

}

for(i=0,flag=0;ilength;i++)//判断产生的食物是否在蛇身上,在flag=1,否则为0

if(food[0]==line[i][0] food[1]==line[i][1])

{ flag=1; break; }

if(flag==0)// 食物不在蛇身上 结束循环

break;

}

gotoxy(food[0],food[1]);

printf("蛇");

}

void show_snake()

{

gotoxy(head[0],head[1]);

printf("蛇");

if(eat_flag==0)//没吃食物时消去蛇尾

{

gotoxy(tail_before[0],tail_before[1]);

printf(" ");//消除蛇尾

}

else

eat_flag=0;//吃了食物就回到没吃状态

}

char different_direction(char dir)

{

switch(dir)

{

case 'a': return 'd';

case 'd': return 'a';

case 'w': return 's';

case 's': return 'w';

}

return 0;

}

void get_direction()

{

direction_before=direction;//记住蛇上一个状态的运动方向

while(kbhit()!=0) //调试

direction=getch();

if( direction_before == different_direction(direction) || (direction!='a' direction!='s' direction!='d' direction!='w') ) //新方向和原方向相反,或获得的方向不是wasd时,保持原方向

direction=direction_before;

switch(direction)

{

case 'a': head[0]-=2; break;

case 'd': head[0]+=2; break;

case 'w': head[1]--; break;

case 's': head[1]++; break;

}

}

void live_state()//判断蛇的生存状态

{

FILE *fp;

int i,flag;

for(i=0,flag=0;ilength-1;i++)//判断是否自己咬到自己

if( head[0]==line[i][0] head[1]==line[i][1])

{

flag=1;

break;

}

if(head[0]=x_min || head[0]=x_max || head[1]=y_min || head[1]=y_max || flag==1)

{

system("cls");

create_window();

update_score();

gotoxy(35,12);

printf("游戏结束!\n");

Sleep(500);

live_death=0;

fp=fopen("highest","w");

fprintf(fp,"%d",max);//保存最高分

}

}

void eat()

{

if(head[0]==food[0]head[1]==food[1])

{

length++;

line[length-1][0]=head[0];//更新蛇头

line[length-1][1]=head[1];

eat_flag=1;

createfood();

if(lengthmax)

max=length;

update_score();

if(delay100)

delay-=30;//加速

}

}

void main()

{

int x=0,y=0;

// int i;

hidden();//隐藏光标

create_window();

initial();

createfood();

for(direction='s',delay=600;;)

{

get_direction();

eat();

update_line();

live_state();//判断生死状态

if(live_death==1)

{

show_snake();

}

else

break;

Sleep(delay);

}

}

C语言的贪吃蛇源代码

?

//******友情提示:如想速度快点,请改小_sleep(500)函数中参数*****??

#include?stdio.h??

#include?stdlib.h??

#include?conio.h??

#include?string.h??

#include?time.h??

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;?i++)???//地图显示??

?{???

??for(j?=?0;?j??L;?j++)??

???printf("%c",?GameMap[i][j]);??

??printf("\n");??

?}??

?????

?printf("\n小小C语言贪吃蛇\n");??

?printf("按任意方向键开始游戏\n");??

????

?getch();???//先接受一个按键,使蛇开始往该方向走??

?Button();??//取出按键,并判断方向??

}??

void?Create_Food()??//在地图上随机产生食物??

{??

?int?fx,?fy;??

?while(1)??

?{??

??fx?=?rand()%H;??

?????fy?=?rand()%L;??

?????

??if(GameMap[fx][fy]?==?'.')??//不能出现在蛇所占有的位置??

??{???

???GameMap[fx][fy]?=?Sfood;??

??????break;??

??}??

?}??

}??

void?Show()??//刷新显示地图??

{??

?int?i,?j;??

?while(1)??

?{????

??_sleep(500);?//延迟半秒(1000为1s),即每半秒刷新一次地图??

??Button();???//先判断按键在移动??

??Move();??

??if(over)??//自吃或碰墙即游戏结束??

??{???

???printf("\n**游戏结束**\n");??

???printf("?????_\n");??

???getchar();??

??????break;??

??}??

??system("cls");???//清空地图再显示刷新吼的地图??

??for(i?=?0;?i??H;?i++)???

??{???

???for(j?=?0;?j??L;?j++)??

????printf("%c",?GameMap[i][j]);??

???printf("\n");??

??}??

?????

??printf("\n小小C语言贪吃蛇\n");??

??printf("按任意方向键开始游戏\n");??

?}??

}??

void?Button()??//取出按键,并判断方向??

{??

?if(kbhit()?!=?0)?//检查当前是否有键盘输入,若有则返回一个非0值,否则返回0??

?{???

??while(kbhit()?!=?0)??//可能存在多个按键,要全部取完,以最后一个为主??

??????key?=?getch();?//将按键从控制台中取出并保存到key中??

??switch(key)??

??{???//左??

???case?75:??Snake[0].now?=?0;??

??????????break;??

????????????//右??

????????????case?77:??Snake[0].now?=?1;???????

??????????break;??

????????????//上??

???case?72:??Snake[0].now?=?2;??

??????????break;??

????????????//下??

???case?80:??Snake[0].now?=?3;??

??????????break;??

??}??

?}??

}??

void?Move()???//蛇的移动??

{??

?int?i,?x,?y;??

????int?t?=?sum;??//保存当前蛇的长度??

?//记录当前蛇头的位置,并设置为空,蛇头先移动??

?x?=?Snake[0].x;??y?=?Snake[0].y;??GameMap[x][y]?=?'.';??

?Snake[0].x?=?Snake[0].x?+?dx[?Snake[0].now?];??

?Snake[0].y?=?Snake[0].y?+?dy[?Snake[0].now?];??

?Check_Border();???//蛇头是否越界??

?Check_Head(x,?y);??//蛇头移动后的位置情况,参数为:?蛇头的开始位置??

?if(sum?==?t)??//未吃到食物即蛇身移动哦??

????for(i?=?1;?i??sum;?i++)??//要从蛇尾节点向前移动哦,前一个节点作为参照??

?{??

??if(i?==?1)???//尾节点设置为空再移动??

???GameMap[?Snake[i].x?][?Snake[i].y?]?=?'.';??

?????

??if(i?==?sum-1)??//为蛇头后面的蛇身节点,特殊处理??

??{??

???Snake[i].x?=?x;??

?????????Snake[i].y?=?y;??

??????Snake[i].now?=?Snake[0].now;??

??}??

??else???//其他蛇身即走到前一个蛇身位置??

??{??

???Snake[i].x?=?Snake[i+1].x;??

?????????Snake[i].y?=?Snake[i+1].y;??

??????Snake[i].now?=?Snake[i+1].now;??

??}??

??????

??GameMap[?Snake[i].x?][?Snake[i].y?]?=?'#';?//移动后要置为'#'蛇身???

?}??

}??

void?Check_Border()??//检查蛇头是否越界??

{??

?if(Snake[0].x??0?||?Snake[0].x?=?H??

?||?Snake[0].y??0?||?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语言制作贪吃蛇的代码,及使用方法,用VC打

/*

C/C++贪吃蛇游戏,zjlj,2015.3.16

*/

#define DEBUG 0 //当程序在调试阶段时 DEBUG为 1

#includeiostream

#includewindows.h

#includetime.h

#includeconio.h

using 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;i22;i++)

{

for(j=0;j22;j++)

{

if(q[i][j]==0)//输出棋盘空白

{

gotoxy(i,j);

color(11);

cout"■";

}

if(q[i][j]==1||q[i][j]==2)//输出棋盘墙壁

{

gotoxy(i,j);

color(11);

cout"□";

}

if(q[i][j]==3)//输出蛇头

{

gotoxy(i,j);

color(14);

cout"★";

}

if(q[i][j]==4)//输出蛇身

{

gotoxy(i,j);

color(12);

cout"◆";

}

if(q[i][j]==5)//输出果子

{

gotoxy(i,j);

color(12);

cout"●";

}

}

if(i==0) cout "\t***********************";

if(i==1) cout "\t等级为:" grade;//显示等级

if(i==3) cout "\t自动前进时间";

if(i==4) cout "\t间隔为:" gamespeed "ms";//显示时间

if(i==6) cout "\t历史最高分为:" score "分";

if(i==7) cout "\t你现在得分为:" (length+(grade-1)*8)*10 "分";

if(i==8) cout "\t**********************";

if(i==9) cout "\t游戏说明:";

if(i==10) cout "\t(1)用小键盘方向键控制";

if(i==11) cout "\t蛇头运动方向;";

if(i==12) cout "\t(2)蛇每吃一个果子蛇身";

if(i==13) cout "\t增加一节;";

if(i==14) cout "\t(3)蛇咬到自己或碰到墙";

if(i==15) cout "\t壁游戏结束。";

if(i==18) cout "\t**********************";

if(i==19) cout "\tC/C++语言作业:";

if(i==20) cout "\tzjlj,2015.03.16 ";

}

}

int main(int argc, char *argv[]){

int tcsQipan[22][22]; // 贪吃蛇棋盘是一个二维数组(如22*22,包括墙壁)

int i,j,score,directiontemp;

FILE *fpini;//*fpini 信息文件

readini(fpini, score, argv);//读取ini文件的最高纪录

if (score0)//最高成绩小于零设置为零,初建文件会是负数

score=0;

while(1)

{

for(i=1;i=20;i++)

for(j=1;j=20;j++)

tcsQipan[i][j]=0; //贪吃蛇棋盘相应坐标标上中间空白部分的标志0

for(i=0;i=21;i++)

tcsQipan[0][i] = tcsQipan[21][i] = 1; //贪吃蛇棋盘相应坐标标上上下墙壁的标志1

for(i=1;i=20;i++)

tcsQipan[i][0] = tcsQipan[i][21] = 2; //贪吃蛇棋盘相应坐标标上左右墙壁的标志2

int tcsZuobiao[2][500]; //蛇的坐标数组

for(i=0; i4; i++)

{

tcsZuobiao[0][i] = 1;//蛇身和蛇头的x坐标

tcsZuobiao[1][i] = i + 1;//蛇身和蛇头的y坐标

}

int head = 3,tail = 0;//标示蛇头和蛇尾的数组偏移量

for(i=1;i=3;i++)

tcsQipan[1][i]=4; //蛇身

tcsQipan[1][4]=3; //蛇头

int x1, y1; // 随机出果子

srand(time(0));//设置随机种子

do

{

x1=rand()%20+1;

y1=rand()%20+1;

}

while(tcsQipan[x1][y1]!=0);//如果不是在空白处重新出果子

tcsQipan[x1][y1]=5;//贪吃蛇棋盘相应坐标标上果子的标志5

color(12);

cout"\n\n\t\t\t\t贪吃蛇游戏即将开始 !"endl;//准备开始

long start,starttemp;

int grade = 1, length = 4; //设置初始等级和蛇的初始长度

int gamespeed = 500; //设置初始前进时间间隔

for(i=3;i=0;i--)

{

start=clock();

while(clock()-start=1000);

system("cls");

if(i0)

cout "\n\n\t\t\t\t进入倒计时:" i endl; //倒计时显示

else

Refresh(tcsQipan,grade,gamespeed,length,score); //初始棋盘显示

}

int timeover=1,otherkey=1;//初始化超时时间和按键判断参数

char direction = 77; // 设置初始情况下,向右运动

int x=tcsZuobiao[0][head],y=tcsZuobiao[1][head];//保存蛇头坐标到x,y变量

while(1)//运行一局游戏

{

start = clock();

while((timeover=((starttemp=clock())-start=gamespeed))!kbhit());//如果有键按下或时间超过自动前进时间间隔则终止循环

if(direction==72||direction==80||direction==75 ||direction==77)

directiontemp=direction;//保留上一次方向按键

//starttemp=gamespeed+start-starttemp;//保留停留时间

if(timeover)

{

#if (DEBUG==1)

direction = getch();//调试代码

#else

if((direction =getch())==-32)

direction = getch();

#endif

}

#if (DEBUG==1)//调试代码

start=clock();

while(clock()-start=2000);

gotoxy(24,4);

cout "\t按键ASCII代码"(int)direction" "endl;

#endif

if(!(direction==72||direction==80||direction==75 ||direction==77))

{

otherkey=0;// 按键非方向键,otherkey设置为0

}

else

{

otherkey=1;// 按键为方向键,otherkey设置为1

}

if(direction==72 directiontemp==80)//忽略反方向按键

{

direction=32;

otherkey=0;

//start = clock();

//while(clock()-start=starttemp);

}

else if(direction==80 directiontemp==72)

{

direction=32;//设置按键为非方向键

otherkey=0;// 按键为非方向键,otherkey设置为0

// start = clock();

//while(clock()-start=starttemp);//补偿等待时间

}

else if(direction==75 directiontemp==77)

{

direction=32;

otherkey=0;

//start = clock();

//while(clock()-start=starttemp);

}

else if(direction==77 directiontemp==75)

{

direction=32;

otherkey=0;

//start = clock();

//while(clock()-start=starttemp);

}

switch(direction)//判断方向键

{

case 72: x= tcsZuobiao[0][head]-1; y= tcsZuobiao[1][head];break; // 向上

case 80: x= tcsZuobiao[0][head]+1; y= tcsZuobiao[1][head];break; // 向下

case 75: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]-1;break; // 向左

case 77: x= tcsZuobiao[0][head]; y= tcsZuobiao[1][head]+1;break; // 向右

default: break;

}

if(x==0 || x==21 ||y==0 || y==21) // 蛇头碰到墙壁,结束本局游戏

{

gotoxy(22,12);

cout "\t游戏已结束!" endl;

if(score=(length+(grade-1)*8)*10)//判断是否破记录

{

gotoxy(10,7);

color(12);

cout "闯关失败 加油耶!" endl;

fclose(fpini);//关闭ini文件

}

else

{

gotoxy(10,7);

color(12);

cout "恭喜您打破记录" endl;

score=(length+(grade-1)*8)*10;

writeini(fpini, score, argv);//写入ini文件的最高纪录

}

gotoxy(23,12);

cout "按回车键重新开始,按ESC退出游戏" endl;//显示的提示

break;//退出该局游戏

}

if(tcsQipan[x][y]!=0!(x==x1y==y1)tcsQipan[x][y]!=3) // 蛇头碰到蛇身,结束本局游戏

{

gotoxy(22,12);

cout "\t游戏已结束!" endl;

if(score=(length+(grade-1)*8)*10)//判断是否破记录

{

gotoxy(10,7);

color(12);

cout "闯关失败 加油耶!" endl;

fclose(fpini);//关闭ini文件

}

else

{

gotoxy(10,7);

color(12);

cout "恭喜您打破记录" endl;

score=(length+(grade-1)*8)*10;

writeini(fpini, score, argv);//写入ini文件的最高纪录

}

gotoxy(23,12);

cout "按回车键重新开始,按ESC退出游戏" endl;//显示的提示

break;//退出该局游戏

}

/*

游戏运行时的核心算法开始

*/

if(x==x1 y==y1) // 吃果子,长度加1

{

length ++;

if(length=8)//长度大于等于8重新计算长度,等级加1

{

length -= 8;//重新计算长度

grade ++;//等级加1

if(gamespeed50)//控制最快速度为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的贪吃蛇源代码,希望对你有帮助,不过运行这个时需要你的软件包含惊蛰EasyX图形函数,比如vc++6.0,如遇到问题问题可以联系我,希望对你有帮助。

#include graphics.h

#include stdlib.h

#include conio.h

#include time.h

#include stdio.h

#define LEFT 'a'

#define RIGHT 'd'

#define DOWN 's'

#define UP 'w'

#define ESC 27

#define N 200 /*蛇的最大长度*/

int i;

char key;

int score=0; /*得分*/

int gamespeed=100; /*游戏速度自己调整*/

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=9,gm=2;

initgraph(gd,gm," ");

cleardevice();

}

/*开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙*/

void DrawK(void)

{

/*setbkcolor(LIGHTGREEN);*/

setcolor(LIGHTCYAN);

setlinestyle(PS_SOLID,0,1); /*设置线型*/

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)

{

srand(time(NULL)); /*随机数发生器*/

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;i0;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;isnake.node;i++)

{

if(snake.x[i]==snake.x[0]snake.y[i]==snake.y[0])

{

GameOver(); /*显示失败*/

snake.life=1;

break;

}

}

if(snake.x[0]55||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.xsnake.y[0]==food.y)/*吃到食物以后*/

{

setcolor(BLACK); /*把画面上的食物东西去*/

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(RED); /*画出蛇*/

for(i=0;isnake.node;i++)

rectangle(snake.x[i],snake.y[i],snake.x[i]+10,snake.y[i]-10);

Sleep(gamespeed);

setcolor(BLACK); /*用黑色去除蛇的的最后*/

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=getch(); /*接收按键*/

if (key == ESC) break; /*按ESC键退出*/

switch(key)

{

case UP:

if(snake.direction!=4) /*判断是否往相反的方向移动*/

snake.direction=3;

break;

case RIGHT:

if(snake.direction!=2)

snake.direction=1;

break;

case LEFT:

if(snake.direction!=1)

snake.direction=2;

break;

case DOWN:

if(snake.direction!=3)

snake.direction=4;

break;

}

}/*endwhile(1)*/

}

/*游戏结束*/

void GameOver(void)

{

cleardevice();

PrScore();

setcolor(RED);

setfont(56,0,"黑体");

outtextxy(200,200,"GAME OVER");

getch();

}

/*输出成绩*/

void PrScore(void)

{

char str[10];

setfillstyle(YELLOW);

bar(50,15,220,35);

setcolor(BROWN);

setfont(16,0,"宋体");

sprintf(str,"score:%d",score);

outtextxy(55,16,str);

}

/*图形结束*/

void Close(void)

{

closegraph();

}

(责任编辑:IT教学网)

更多