C语言贪吃蛇源程序代码双人对抗
创新C语言双人贪吃蛇小游戏

}
delimage(img);
}
void setfoodcrd()
{
food.crd.x = rand() % (600 / SNAKELEN) * SNAKELEN;
food.crd.y = rand() % (480 / SNAKELEN) * SNAKELEN;
void showfood();//在相应的坐标画出食物
void snakemove(struct SNAKE *p);//移动蛇,实际就是改变蛇各节的坐标
void judgeslod(struct SNAKE *p);//判断蛇是否撞墙或者撞到自己
void judgesef(struct SNAKE *p);//判断蛇是否吃到食物了
int _COLOR; //标记蛇的颜色
}snake[2];
int speed = SPEED;//方便以后在程序中改变速度
int mode_num;
int wall_1 = 100 , wall_2 = -165;
void init();//初始化游戏
void setfoodcrd();//设置食物要出现的坐标
putimage(0, 0, img);
if (!food.flag)
{
setfoodcrd();
}
for(p = snake, i = 0; i < mode_num; i++, p++)
snakemove(p);
PIMAGE img1 = newimage();
getimage(img1, "pic\\pool.jpg");
{
C语言小游戏源代码《贪吃蛇》

void init(void){/*构建图形驱动函数*/ int gd=DETECT,gm; initgraph(&gd,&gm,""); cleardevice(); }
欢迎您阅读该资料希望该资料能给您的学习和生活带来帮助如果您还了解更多的相关知识也欢迎您分享出来让我们大家能共同进步共同成长
C 语言小游戏源代码《贪吃பைடு நூலகம்》
#define N 200/*定义全局常量*/ #define m 25 #include <graphics.h> #include <math.h> #include <stdlib.h> #include <dos.h> #define LEFT 0x4b00 #define RIGHT 0x4d00 #define DOWN 0x5000 #define UP 0x4800 #define Esc 0x011b int i,j,key,k; struct Food/*构造食物结构体*/ { int x; int y; int yes; }food; struct Goods/*构造宝贝结构体*/ { int x; int y; int yes; }goods; struct Block/*构造障碍物结构体*/ { int x[m]; int y[m]; int yes; }block; struct Snake{/*构造蛇结构体*/ int x[N]; int y[N]; int node; int direction; int life; }snake; struct Game/*构建游戏级别参数体*/ { int score; int level; int speed;
贪吃蛇游戏c语言源代码

̰³ÔÉßÓÎÏ·cÓïÑÔÔ´´úÂë.txtÊÀÉÏ×îÕä¹óµÄ²»ÊÇÓÀÔ¶µÃ²»µ½»òÒѾµÃµ½µÄ£¬¶øÊÇÄãÒѾµÃµ½²¢ÇÒËæÊ±¶¼ÓпÉÄÜʧȥµÄ¶«Î÷£¡°®ÇéÊǵƣ¬ÓÑÇéÊÇÓ°×Ó¡£µÆÃðʱ£¬Äã»á·¢ÏÖÖÜΧ¶¼ÊÇÓ°×Ó¡£ÅóÓÑ£¬ÊÇÔÚ×îºó¿ÉÒÔ¸øÄãÁ¦Á¿µÄÈË¡£#inclu de <stdlib.h>#include <graphics.h>#include <bios.h>#include <dos.h>#include <conio.h>#define Enter 7181#define ESC 283#define UP 18432#define DOWN 20480#define LEFT 19200#define RIGHT 19712#ifdef __cplusplus#define __CPPARGS ...#else#define __CPPARGS#endifvoid interrupt (*oldhandler)(__CPPARGS);void interrupt newhandler(__CPPARGS);void SetTimer(void interrupt (*IntProc)(__CPPARGS));void KillTimer(void);void Initgra(void);void TheFirstBlock(void);void DrawMap(void);void Initsnake(void);void Initfood(void);void Snake_Headmv(void);void Flag(int,int,int,int);void GameOver(void);void Snake_Bodymv(void);void Snake_Bodyadd(void);void PrntScore(void);void Timer(void);void Win(void);void TheSecondBlock(void);void Food(void);void Dsnkorfd(int,int,int);void Delay(int);struct Snake{int x;int y;int color;}Snk[12];struct Food{int x;int y;int color;}Fd;int flag1=1,flag2=0,flag3=0,flag4=0,flag5=0,flag6=0,checkx,checky,num,key=0,Times,Score,Hscore,Snkspeed,TimerCounter,TureorFalse; char Sco[2],Time[6];void main(){ Initgra();SetTimer(newhandler);TheFirstBlock();while(1){DrawMap();Snake_Headmv();GameOver();Snake_Bodymv();Snake_Bodyadd();PrntScore();Timer();Win();if(key==ESC)break;if(key==Enter){cleardevice();TheFirstBlock();}TheSecondBlock();Food();Delay(Snkspeed);}closegraph();KillTimer();}void interrupt newhandler(__CPPARGS){TimerCounter++;oldhandler();}void SetTimer(void interrupt (*IntProc)(__CPPARGS)){oldhandler=getvect(0x1c);disable();setvect(0x1c,IntProc);enable();}void KillTimer(){disable();setvect(0x1c,oldhandler);enable();}void Initgra(){int gd=DETECT,gm;initgraph(&gd,&gm,"d:\\tc");}void TheFirstBlock(){setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The First Block");loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=10;num=2;Times=0;key=0;TureorFalse=1;TimerCounter=0;Time[0]='0';Time[1]='0';Time[2]=':';Time[3]='1';Time[4]='0';Time[5]='\0'; }else if(key==ESC) cleardevice();else goto loop;}void DrawMap(){line(10,10,470,10);line(470,10,470,470);line(470,470,10,470);line(10,470,10,10);line(480,20,620,20);line(620,20,620,460);line(620,460,480,460);line(480,460,480,20);}void Initsnake(){randomize();num=2;Snk[0].x=random(440);Snk[0].x=Snk[0].x-Snk[0].x%20+50;Snk[0].y=random(440);Snk[0].y=Snk[0].y-Snk[0].y%20+50;Snk[0].color=4;Snk[1].x=Snk[0].x;Snk[1].y=Snk[0].y+20;Snk[1].color=4;}void Initfood(){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;}void Snake_Headmv(){if(bioskey(1)){key=bioskey(0);switch(key){case UP:Flag(1,0,0,0);break;case DOWN:Flag(0,1,0,0);break;case LEFT:Flag(0,0,1,0);break;case RIGHT:Flag(0,0,0,1);break;default:break;}}if(flag1){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color); }if(flag2){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].y+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}if(flag3){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x-=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}if(flag4){checkx=Snk[0].x;checky=Snk[0].y;Dsnkorfd(Snk[0].x,Snk[0].y,0);Snk[0].x+=20;Dsnkorfd(Snk[0].x,Snk[0].y,Snk[0].color);}}void Flag(int a,int b,int c,int d){flag1=a;flag2=b;flag3=c;flag4=d;}void GameOver(){int i;if(Snk[0].x<20||Snk[0].x>460||Snk[0].y<20||Snk[0].y>460) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop1:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();elsegoto loop1;}for(i=3;i<num;i++){if(Snk[0].x==Snk[i].x&&Snk[0].y==Snk[i].y) {cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop2:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}elseif(key==ESC)cleardevice();else goto loop2;}}}void Snake_Bodymv(){int i,s,t;for(i=1;i<num;i++){Dsnkorfd(checkx,checky,Snk[i].color); Dsnkorfd(Snk[i].x,Snk[i].y,0);s=Snk[i].x;t=Snk[i].y;Snk[i].x=checkx;Snk[i].y=checky;checkx=s;checky=t;}}void Food(){if(flag5){randomize();Fd.x=random(440);Fd.x=Fd.x-Fd.x%20+30;Fd.y=random(440);Fd.y=Fd.y-Fd.y%20+30;Fd.color=random(14)+1;flag5=0;}Dsnkorfd(Fd.x,Fd.y,Fd.color);}void Snake_Bodyadd(){if(Snk[0].x==Fd.x&&Snk[0].y==Fd.y) {if(Snk[num-1].x>Snk[num-2].x){num++;Snk[num-1].x=Snk[num-2].x+20;Snk[num-1].y=Snk[num-2].y;Snk[num-1].color=Fd.color;}elseif(Snk[num-1].x<Snk[num-2].x) {num++;Snk[num-1].x=Snk[num-2].x-20; Snk[num-1].y=Snk[num-2].y; Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y>Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y+20; Snk[num-1].color=Fd.color;}elseif(Snk[num-1].y<Snk[num-2].y) {num++;Snk[num-1].x=Snk[num-2].x; Snk[num-1].y=Snk[num-2].y-20; Snk[num-1].color=Fd.color;}flag5=1;Score++;}}void PrntScore(){if(Hscore!=Score){setcolor(11);settextstyle(0,0,3); outtextxy(490,100,"SCORE"); setcolor(2);setfillstyle(1,0);rectangle(520,140,580,180); floodfill(530,145,2);Sco[0]=(char)(Score+48);Sco[1]='\0';Hscore=Score;setcolor(4);settextstyle(0,0,3); outtextxy(540,150,Sco);}}void Timer(){if(TimerCounter>18){Time[4]=(char)(Time[4]-1);if(Time[4]<'0'){Time[4]='9';Time[3]=(char)(Time[3]-1);}if(Time[3]<'0'){Time[3]='5';Time[1]=(char)(Time[1]-1);}if(TureorFalse){setcolor(11);settextstyle(0,0,3);outtextxy(490,240,"TIMER");setcolor(2);setfillstyle(1,0);rectangle(490,280,610,320);floodfill(530,300,2);setcolor(11);settextstyle(0,0,3);outtextxy(495,290,Time);TureorFalse=0;}if(Time[1]=='0'&&Time[3]=='0'&&Time[4]=='0') {setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"Game Over");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();}else if(key==ESC) cleardevice();else goto loop;}TimerCounter=0;TureorFalse=1;}}void Win(){if(Score==3)Times++;if(Times==2){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(160,220,"You Win");loop:key=bioskey(0);if(key==Enter){cleardevice();TheFirstBlock();key=0;}else if(key==ESC) cleardevice();else goto loop;}}void TheSecondBlock(){if(Score==3){cleardevice();setcolor(11);settextstyle(0,0,4);outtextxy(100,220,"The Second Block"); loop:key=bioskey(0);if(key==Enter){cleardevice();Initsnake();Initfood();Score=0;Hscore=1;Snkspeed=8;num=2;key=0;}else if(key==ESC) cleardevice();else goto loop;}}void Dsnkorfd(int x,int y,int color) {setcolor(color);setfillstyle(1,color);circle(x,y,10);floodfill(x,y,color);}void Delay(int times){int i;for(i=1;i<=times;i++)delay(15000);}。
vc贪吃蛇c语言代码

vc贪吃蛇c语言代码#include "stdio.h"#include "stdio.h"#include "windows.h"#include "time.h"#include "setjmp.h"#define MAXNOD 500#define UP 1#define DOWN -1#define LEFT -2#define RIGHT 2#define YES 1#define NO 0jmp_buf retry;typedef struct{int x;int y;int status;}Food;typedef struct {int *px;int *py;int direction;int nodlen;int score;}Snack;int gotoxy(int x, int y){COORD cd;cd.X = x;cd.Y = y;returnSetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE ),cd); }void initialization(Snack *pss){system("color 0e");pss->px=(int *)malloc(MAXNOD*sizeof(int));pss->py=(int *)malloc(MAXNOD*sizeof(int));memset(pss->px,0,MAXNOD);memset(pss->py,0,MAXNOD);pss->px[0]=0;pss->py[0]=0;pss->px[1]=1;pss->py[1]=0;pss->direction=RIGHT;pss->nodlen=2;pss->score=0;}void getscoresys(Snack scr){gotoxy(68,7);cprintf("score: %d",scr.score);}int ctrltoi(char ctr){switch(ctr){case 'w':return UP;case 's':return DOWN;case 'a':return LEFT;case 'd':return RIGHT ;}}void boundary(){int cnt,y;gotoxy(15,3);for (cnt=0;cnt<45;cnt++) {cprintf("%c",4);}for (y=4;y<20;y++){gotoxy(15,y);cprintf("%c",219); gotoxy(59,y);cprintf("%c",219);}gotoxy(15,20);for (cnt=0;cnt<45;cnt++) {cprintf("%c",4);}gotoxy(68,9);cprintf("UP: W"); gotoxy(68,10);cprintf("DOWN: S");gotoxy(68,11);cprintf("LEFT: A");gotoxy(68,12);cprintf("RIGHT: D");gotoxy(68,14);cprintf("PAUSE: BLANK");gotoxy(68,15);cprintf("EXIT :ESC");}int isdead(Snack ts){int i;char select;for(i=0;i<ts.nodlen-1;i++)< p="">if((ts.px[i]==ts.px[ts.nodlen-1]&&ts.py[i]==ts.py[ts.nodlen-1])||((ts.px[ts.nodlen-1]<0))||(ts.px[ts.nodl en-1]>42)||(ts.py[ts.nodlen-1]<0)||(ts.py[ts.nodlen-1]>15)) {system("cls");gotoxy(15,12);cprintf("Game Over! Press ESC to exit, any other key to retry\a\n");flushall();select=getch();if(select==27) exit(0);system("cls");longjmp(retry,1);}void getfood(Snack s,Food *pf) {int i;cnt: do{pf->x=rand()%43;pf->y=rand()%16;for (i=0;i<s.nodlen;i++)< p=""> {if (s.px[i]==pf->x&&s.py[i]==pf->y) {goto cnt;}}break;}while(1);pf->status=YES;}int main(){Snack ss;Food foo;int i,j,dire;char ctrl;srand((unsigned)time(NULL)); setjmp(retry);initialization(&ss);getfood(ss,&foo);dogotoxy(foo.x+16,foo.y+4);if (foo.status==NO)getfood(ss,&foo);cprintf("%c",3);boundary();if(_kbhit()){ctrl=getch();if (ctrl=='w'||ctrl=='s'||ctrl=='a'||ctrl=='d') {dire=ctrltoi(ctrl);if(ss.direction==(0-dire)) ;elsess.direction=dire;}else if (ctrl==' ')system("pause");else if (ctrl==27)exit(0);}for (i=0;i<ss.nodlen-1;i++)< p="">{ss.px[i]=ss.px[i+1];ss.py[i]=ss.py[i+1];}switch(ss.direction){case UP:ss.py[ss.nodlen-1]=ss.py[ss.nodlen-1]-1;break;case DOWN:ss.py[ss.nodlen-1]=ss.py[ss.nodlen-1]+1;break;case LEFT:ss.px[ss.nodlen-1]=ss.px[ss.nodlen-1]-1;break;case RIGHT:ss.px[ss.nodlen-1]=ss.px[ss.nodlen-1]+1;}for(i=0;i<ss.nodlen;i++)< p="">{gotoxy(ss.px[i]+16,ss.py[i]+4);cprintf("%c",4);}isdead(ss);if (ss.px[ss.nodlen-1]==foo.x&&ss.py[ss.nodlen-1]==foo.y) { for (j=0;j<ss.nodlen;j++)< p="">{ss.px[ss.nodlen-j]=ss.px[ss.nodlen-j-1];ss.py[ss.nodlen-j]=ss.py[ss.nodlen-j-1];}ss.score +=10;ss.nodlen++;foo.status=NO;}getscoresys(ss);_sleep(199);system("cls");flushall();}while(1);}</ss.nodlen;j++)<> </ss.nodlen;i++)<> </ss.nodlen-1;i++)<> </s.nodlen;i++)<></ts.nodlen-1;i++)<>。
C语言贪吃蛇源程序代码双人对抗

C语言贪吃蛇源程序代码双人对抗.txt第一次笑是因为遇见你,第一次哭是因为你不在,第一次笑着流泪是因为不能拥有你。
#include <stdio.h>#include <conio.h>#include <dos.h>#include <graphics.h>#include <stdlib.h>#include <bios.h>#include <time.h>#include <malloc.h>#define LEFT 100#define TOP 30#define RIGHT 538#define BOTTOM 468#define SIZE 20#define SL 100 /*SL表示产生食物的个数*/typedef struct Snake/*表示蛇的结点*/{int bx,by;struct Snake *next;}Snake,*SnakeList;SnakeList createsnake(int cx,int cy)/*创建一个蛇头。
*/{SnakeList l;l=(SnakeList)malloc(sizeof(Snake));l->next=NULL;l->bx=cx;l->by=cy;return l;}void paintsnake(SnakeList l,int scoler,int tcoler)/*输出蛇身的函数,也可作为消去蛇身的函数。
*/{SnakeList p;int x,y;p=l;setfillstyle(SOLID_FILL,scoler);for(;p->next;){p=p->next;x=p->bx;y=p->by;bar(x,y,x+SIZE,y+SIZE);}if(scoler==2)setfillstyle(SOLID_FILL,scoler);elsesetfillstyle(SOLID_FILL,tcoler);x=l->bx;y=l->by;bar(x,y,x+SIZE,y+SIZE);}void changsnake(SnakeList l,int fx,int fy,int s) /*fx,fy表示蛇头移动方向,分别取值为1,-1;s表示是否吃到食物,1表示吃到,0表示没有。
vc贪吃蛇c语言代码

#include "stdio.h"#include "stdio.h"#include "windows.h"#include "time.h"#include "setjmp.h"#define MAXNOD 500#define UP 1#define DOWN -1#define LEFT -2#define RIGHT 2#define YES 1#define NO 0jmp_buf retry;typedef struct{int x;int y;int status;}Food;typedef struct {int *px;int *py;int direction;int nodlen;int score;}Snack;int gotoxy(int x, int y){COORD cd;cd.X = x;cd.Y = y;return SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),cd); }void initialization(Snack *pss){system("color 0e");pss->px=(int *)malloc(MAXNOD*sizeof(int));pss->py=(int *)malloc(MAXNOD*sizeof(int));memset(pss->px,0,MAXNOD);memset(pss->py,0,MAXNOD);pss->px[0]=0;pss->py[0]=0;pss->px[1]=1;pss->py[1]=0;pss->direction=RIGHT;pss->nodlen=2;pss->score=0;}void getscoresys(Snack scr){gotoxy(68,7);cprintf("score: %d",scr.score);}int ctrltoi(char ctr){switch(ctr){case 'w':return UP;case 's':return DOWN;case 'a':return LEFT;case 'd':return RIGHT ;}}void boundary(){int cnt,y;gotoxy(15,3);for (cnt=0;cnt<45;cnt++){cprintf("%c",4);}for (y=4;y<20;y++){gotoxy(15,y);cprintf("%c",219);gotoxy(59,y);cprintf("%c",219);}gotoxy(15,20);for (cnt=0;cnt<45;cnt++){cprintf("%c",4);}gotoxy(68,9);cprintf("UP: W");gotoxy(68,10);cprintf("DOWN: S");gotoxy(68,11);cprintf("LEFT: A");gotoxy(68,12);cprintf("RIGHT: D");gotoxy(68,14);cprintf("PAUSE: BLANK");gotoxy(68,15);cprintf("EXIT :ESC");}int isdead(Snack ts){int i;char select;for(i=0;i<ts.nodlen-1;i++)if((ts.px[i]==ts.px[ts.nodlen-1]&&ts.py[i]==ts.py[ts.nodlen-1])||((ts.px[ts.nodlen-1]<0))||(ts.px[ts.nodl en-1]>42)||(ts.py[ts.nodlen-1]<0)||(ts.py[ts.nodlen-1]>15)){system("cls");gotoxy(15,12);cprintf("Game Over! Press ESC to exit, any other key to retry\a\n");flushall();select=getch();if(select==27) exit(0);system("cls");longjmp(retry,1);}}void getfood(Snack s,Food *pf){int i;cnt: do{pf->x=rand()%43;pf->y=rand()%16;for (i=0;i<s.nodlen;i++){if (s.px[i]==pf->x&&s.py[i]==pf->y){goto cnt;}}break;}while(1);pf->status=YES;}int main(){Snack ss;Food foo;int i,j,dire;char ctrl;srand((unsigned)time(NULL));setjmp(retry);initialization(&ss);getfood(ss,&foo);do{gotoxy(foo.x+16,foo.y+4);if (foo.status==NO)getfood(ss,&foo);cprintf("%c",3);boundary();if(_kbhit()){ctrl=getch();if (ctrl=='w'||ctrl=='s'||ctrl=='a'||ctrl=='d'){dire=ctrltoi(ctrl);if(ss.direction==(0-dire)) ;elsess.direction=dire;}else if (ctrl==' ')system("pause");else if (ctrl==27)exit(0);}for (i=0;i<ss.nodlen-1;i++){ss.px[i]=ss.px[i+1];ss.py[i]=ss.py[i+1];}switch(ss.direction){case UP:ss.py[ss.nodlen-1]=ss.py[ss.nodlen-1]-1;break;case DOWN:ss.py[ss.nodlen-1]=ss.py[ss.nodlen-1]+1;break;case LEFT:ss.px[ss.nodlen-1]=ss.px[ss.nodlen-1]-1;break;case RIGHT:ss.px[ss.nodlen-1]=ss.px[ss.nodlen-1]+1;}for(i=0;i<ss.nodlen;i++){gotoxy(ss.px[i]+16,ss.py[i]+4);cprintf("%c",4);}isdead(ss);if (ss.px[ss.nodlen-1]==foo.x&&ss.py[ss.nodlen-1]==foo.y) {for (j=0;j<ss.nodlen;j++){ss.px[ss.nodlen-j]=ss.px[ss.nodlen-j-1];ss.py[ss.nodlen-j]=ss.py[ss.nodlen-j-1];}ss.score +=10;ss.nodlen++;foo.status=NO;}getscoresys(ss);_sleep(199);system("cls");flushall();}while(1);}。
贪吃蛇的c语言源程序

#include<stdio.h>#include<graphics.h>#include<stdlib.h>#include<dos.h>#include<bios.h>#include<time.h>#define NULL 0#define UP 4471#define LEFT 7777#define DOWN 8051#define RIGHT 8292#define PAUSE 6512#define ESC 283#define SNAKE_COLOR 4#define SNAKE_BOND_COLOR 2#define SNAKE_STYLE LTBKSLASH_FILL #define FOOD_STYLE CLOSE_DOT_FILL #define MAP_COLOR 8#define MAP_BOND_COLOR 6#define MAP_STYLE SOLID_FILLtypedef struct Snake_Node{int x,y;struct Snake_Node *next;}Snake_Node,*P_Snake_Node;struct{int x,y,color;}Food;struct{int dx;int dy;}Direct={0,1};static int speed=1,len=4,px,py;static P_Snake_Node Head;static P_Snake_Node Map;void paint_node(int,int,int,int,int);void Put_Food(int);void Put_Snake_Node(P_Snake_Node,int); void Init();void Grow_up(int,int);void Make_Map();void Auto_Make_Map();void Load_Game();void Auto_Start();int Snake_Dead();void GO_GO_GO();void Play_Game();void Exit_Save();void Failed();void main();void paint_node(int x,int y,int color1,int color2,int style){setfillstyle(SOLID_FILL,color2);bar(x*10,479-y*10,x*10+9,479-y*10-9);setfillstyle(style,color1);bar(x*10+1,479-y*10-1,x*10+9-1,479-y*10-9+1);setfillstyle(SOLID_FILL,15);}void Put_Food(int color){randomize();Food.x=random(46)+1;Food.y=random(46)+1;Food.color=color;paint_node(Food.x,Food.y,Food.color,Food.color,FOOD_STYLE);}void Put_Snake_Node(P_Snake_Node p,int flag){if(flag)paint_node(p->x,p->y,SNAKE_COLOR,SNAKE_BOND_COLOR,SNAKE_STYLE);elsepaint_node(p->x,p->y,getbkcolor(),getbkcolor(),SOLID_FILL);}void Init(){int gdrive=VGA,gmode=VGAHI;initgraph(&gdrive,&gmode,"d:\\TC20\\turboc2");setfillstyle(XHATCH_FILL,2);bar(490,0,509,479);setfillstyle(SOLID_FILL,6);bar(480,0,489,479);setfillstyle(SOLID_FILL,15);setcolor(9);setlinestyle(CENTER_LINE,0,3);line(490,0,490,479);setcolor(3);setlinestyle(SOLID_LINE,0,3);line(509,0,509,479);line(509,479,639,479);line(639,479,639,0);line(639,0,509,0);setlinestyle(SOLID_LINE,0,0);setcolor(10);settextstyle(TRIPLEX_FONT,HORIZ_DIR,3); outtextxy(530,12,"GREEDY");outtextxy(537,40,"SNAKE");settextstyle(0,0,0);setcolor(13);outtextxy(510,80,"<<============>>"); setcolor(1);setlinestyle(CENTER_LINE,0,3);line(573,90,573,305);outtextxy(510,310,"+++++++++++++++++"); setcolor(5);settextstyle(GOTHIC_FONT,0,5);outtextxy(512,390,"~~~~~~~");outtextxy(512,392,"~~~~~~~");outtextxy(512,388,"~~~~~~~");setcolor(10);settextstyle(SANS_SERIF_FONT,1,1); outtextxy(510,115,"->");outtextxy(510,165,"<-");settextstyle(SANS_SERIF_FONT,0,1); outtextxy(512,215,"->");outtextxy(512,265,"<-");setcolor(14);settextstyle(DEFAULT_FONT,0,1); outtextxy(550,128,":W");outtextxy(550,175,":S");outtextxy(550,222,":A");outtextxy(550,272,":D");setcolor(10);settextstyle(SMALL_FONT,0,5);outtextxy(575,125,"PAUSE /");outtextxy(575,140,"CONTINUE:");outtextxy(580,250,"EXIT:");/*************/}void Grow_Up(int x,int y){P_Snake_Node p;p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=x;p->y=y;p->next=Head->next;Head->next=p;Head=p;++len;++speed;}void Auto_Make_Map(){P_Snake_Node p,q;p=q=Map=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=0;p->y=0;p->next=NULL;while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=(q->x)+1;p->y=q->y;q->next=p;p->next=NULL;q=p;if((p->x)==47) break;}while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=q->x;p->y=(q->y)+1;q->next=p;p->next=NULL;q=p;if((p->y)==47) break;}while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=(q->x)-1;p->y=q->y;q->next=p;p->next=NULL;q=p;if((p->x)==0) break;}while(1){p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=q->x;p->y=(q->y)-1;q->next=p;p->next=NULL;q=p;if((p->y)==1) break;}p=Map;while(p){paint_node(p->x,p->y,MAP_COLOR,MAP_BOND_COLOR,MAP_STYLE);p=p->next;}}void Auto_Start(){int i=1;P_Snake_Node p,q;Head=p=(P_Snake_Node)malloc(sizeof(Snake_Node));p->x=24;p->y=24-3;p->next=NULL;q=p;while(i<=3){q=(P_Snake_Node)malloc(sizeof(Snake_Node));p->next=q;q->x=p->x;q->y=(p->y)+1;q->next=NULL;p=q;++i;}p->next=Head;Head=p;while(i>=1){Put_Snake_Node(p,1);p=p->next;i--;}Put_Food(14);Auto_Make_Map();}int Snake_Dead(){P_Snake_Node p;int i=1;p=Head->next->next;while(i<=len-4){if(p->x==px && p->y==py)return(1);++i;p=p->next;}p=Map;while(p){if(p->x==px && p->y==py)return(1);p=p->next;}return(0);}void GO_GO_GO(){px=(Head->x)+(Direct.dx);py=(Head->y)+(Direct.dy);if(!Snake_Dead()){if(px==Food.x&&py==Food.y){Grow_Up(px,py);Put_Snake_Node(Head,1);Put_Food(14);}else{Head=Head->next;Put_Snake_Node(Head,0);Head->x=px;Head->y=py;Put_Snake_Node(Head,1);}}elseFailed();}void Play_Game(){int wait;while(1){while(!kbhit()){GO_GO_GO();wait=0;while(wait<=2){delay(30000);++wait;}}switch(bioskey(0)){case UP:{if(Direct.dx!=0&&Direct.dy!=-1){Direct.dx=0;Direct.dy=1;}break;}case LEFT:{if(Direct.dx!=1&&Direct.dy!=0){Direct.dx=-1;Direct.dy=0;}break;}case DOWN:{if(Direct.dx!=0&&Direct.dy!=1){Direct.dx=0;Direct.dy=-1;}break;}case RIGHT:{if(Direct.dx!=-1&&Direct.dy!=0){Direct.dx=1;Direct.dy=0;}break;}case ESC:{exit(1);break;}}}}void Failed(){setcolor(4);settextstyle(TRIPLEX_FONT, HORIZ_DIR, 6);outtextxy(90,200,"GAME OVER!");getch();exit(1);/**********/ }void main(){Init();getch();Auto_Start();Play_Game();getch(); closegraph();}。
(完整word版)C语言最简洁的贪吃蛇源代码

C语言最简洁的贪吃蛇源代码.txt每天早上起床都要看一遍“福布斯”富翁排行榜,如果上面没有我的名字,我就去上班。
谈钱不伤感情,谈感情最他妈伤钱。
我诅咒你一辈子买方便面没有调料包。
#include〈graphics.h>#include<conio。
h〉#include〈dos.h〉#include<bios。
h>#include<stdlib。
h〉#define STATIC 0#define TRUE 1#define FALSE 0#define UP 1#define RIGHT 2#define DOWN 3#define LEFT 4#define VK_LEFT 0x4b00 /*上下左右键的值*/#define VK_RIGHT 0x4d00#define VK_DOWN 0x5000#define VK_UP 0x4800#define VK_ESC 0x011bint board[22][22];int snakelength=0;struct snake{public:int x=0;int y=0;int direction;}body[20];snake food;void makefood();/*产生一个食物*/int eatfood(); /*蛇吃掉食物*/void right(); /*上下左右的函数了*/void down();void left();void up();void getdirection(); /*判断蛇的方向*/move(snake *body)/*让蛇动起来*/{int x=body[0].x,y=body[0].y;if(body—>direction==RIGHT&&board[y][x+1]!=1)right();else if(body—>direction==DOWN&&board[y+1][x]!=1)down(); else if(body->direction==LEFT&&board[y][x—1]!=1)left(); else if(body—>direction==UP&&board[y-1][x]!=1)up();return 0;}void print() /*在屏幕上显示蛇*/{int i,j,x=0,y=0;for(i=1;i〈21;i++)for(j=1;j<21;j++)board[i][j]=0;for(i=0;i〈20;i++){x=body[i]。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
C语言贪吃蛇源程序代码双人对抗#include <conio.h>#include <dos.h>#include <graphics.h>#include <stdlib.h>#include <bios.h>#include <time.h>#include <malloc.h>#define LEFT 100#define TOP 30#define RIGHT 538#define BOTTOM 468#define SIZE 20#define SL 100 /*SL表示产生食物的个数*/typedef struct Snake/*表示蛇的结点*/{int bx,by;struct Snake *next;}Snake,*SnakeList;SnakeList createsnake(int cx,int cy)/*创建一个蛇头。
*/{SnakeList l;l=(SnakeList)malloc(sizeof(Snake));l->next=NULL;l->bx=cx;l->by=cy;return l;}void paintsnake(SnakeList l,int scoler,int tcoler)/*输出蛇身的函数,也可作为消去蛇身的函数。
*/{SnakeList p;int x,y;p=l;setfillstyle(SOLID_FILL,scoler);for(;p->next;)p=p->next;x=p->bx;y=p->by;bar(x,y,x+SIZE,y+SIZE);}if(scoler==2)setfillstyle(SOLID_FILL,scoler);elsesetfillstyle(SOLID_FILL,tcoler);x=l->bx;y=l->by;bar(x,y,x+SIZE,y+SIZE);}void changsnake(SnakeList l,int fx,int fy,int s) /*fx,fy表示蛇头移动方向,分别取值为1,-1;s 表示是否吃到食物,1表示吃到,0表示没有。
*/{SnakeList p=l;int x,y,t;x=p->bx;y=p->by;p->bx=x+fx*(SIZE+2);p->by=y+fy*(SIZE+2);for(;p->next;){p=p->next;t=p->bx;p->bx=x;x=t;t=p->by;p->by=y;y=t;}if(s){p->next=(SnakeList)malloc(sizeof(Snake));p->next->bx=x;p->next->by=y;p->next->next=NULL;}}void setfood(int *fx,int *fy,SnakeList al,SnakeList bl)/*设置随机食物*/ {int a,b,s=1,s2;SnakeList slist1;SnakeList slist2;setfillstyle(SOLID_FILL,6);srand(time(NULL));for(;s;){slist1=al;slist2=bl;a=rand()%(SIZE);b=rand()%(SIZE);a=LEFT+(a)*22;b=TOP+(b)*22;s2=1;for(;slist1&&s2;){if(a==slist1->bx&&b==slist1->by)s2=0;elseslist1=slist1->next;}s2=1;for(;slist2&&s2;){if(a==slist2->bx&&b==slist2->by)s2=0;elseslist2=slist2->next;}if(slist1==NULL&&slist2==NULL)s=0;}*fx=a;*fy=b;bar(*fx,*fy,*fx+20,*fy+20);}void qufood(int fx,int fy)/*消去食物*/setfillstyle(SOLID_FILL,2);bar(fx,fy,fx+20,fy+20);}void freesnake(SnakeList psnake)/*释放蛇的空间。
*/{SnakeList p;p=psnake->next;psnake->next=NULL;for(;p;){p=psnake->next;free(psnake);psnake=p;}}int nothit(SnakeList snake1,SnakeList snake2,int x,int y)/*检查snake1是否撞击snake2。
*/ {int a1,a2,b1,b2,bj=1;a1=snake1->bx;b1=snake1->by;a1=a1+x*(SIZE+2);b1=b1+y*(SIZE+2);a2=snake2->bx;b2=snake2->by;for(;snake2&&bj;){if(a1==a2&&b1==b2){bj=0;}else{snake2=snake2->next;a2=snake2->bx;b2=snake2->by;}return bj;}void paintpig()/*画一个猪头。
*/{setwritemode(1);/*设置绘制线条的模式,1表示异或模式*/setfillstyle(1,2);/*设置图片填充模式和颜色,1表示实色填充,2表示绿色*/pieslice(220,140,0,360,80);/*画耳朵*/pieslice(420,140,0,360,80);setfillstyle(1,6);pieslice(320,240,0,360,150);/*画脸*/arc(260,180,30,160,30);/*画眉毛*/arc(380,180,30,160,30);setfillstyle(1,4);sector(320,260,0,360,50,40);/*画鼻子*/setfillstyle(1,8);pieslice(300,260,0,360,5);/*鼻孔*/pieslice(340,260,0,360,5);arc(320,260,230,310,60);/*画嘴*/}void numtostr(int n,char a[3])/*把一个数字n转换成字符串,并存储在a中。
*/{int w;w=n%10;a[1]=w+'0';w=n/10;a[0]=w+'0';a[2]='\0';}void main(){int gr=DETECT,gm;int s1=0,s2=0,fx1=0,fy1=1,fx2=0,fy2=1;/*s1表示snake1的身长,s2表示snake2的身长;fx*,fy*表示蛇头运动方向;*/int dx,dy,add1,add2,k,k1=1,k2=1;/*dx,dy表示食物的位置;add*=1表示是否吃到食物;k*=1表示蛇头越界*/int i=1,j=0,q=1;/*j=0表示只有一条蛇,j=1表示有两条蛇,j=2表示没有蛇退出游戏*/ unsigned long t;/*t表示延迟的时间*/char w[3];SnakeList lsnake1,lsnake2;/*registerbgidriver(EGAVGA_driver);*//*detectgraph(*gr, *gm);*/initgraph(&gr,&gm,"");/*设置屏幕为图形模式*/ cleardevice();/*清屏函数*/for(;q;){cleardevice();paintpig();settextstyle(0,0,3);outtextxy(100,10," I like pigs!");settextstyle(1,0,2);outtextxy(300,400,"One snake!");outtextxy(300,420,"Two snakes!");outtextxy(300,440,"Quit Game");outtextxy(450,420,"Producer:Chen Chen");outtextxy(450,440,"Time:May 23 2012");outtextxy(30,370,"Keys used:Arrow keys");outtextxy(30,380,"'W''A''S'D'");outtextxy(30,390," 'enter'"); setfillstyle(1,4);pieslice(280,400+j*20,0,360,5);i=1;for(;i;){k=bioskey(0);/*获取按键的扫描码*/sound(1300);/*响声函数*/delay(10000);/*延迟函数*/nosound();/*关闭响声函数*/switch (k){case 20480:/*向下键的扫描码*/setfillstyle(1,0);pieslice(280,400+j*20,0,360,5);j=(j+1)%3;setfillstyle(1,4);pieslice(280,400+j*20,0,360,5);break;case 7181:/*enter键的扫描码*/if(j==0||j==1){i=0;}else{closegraph();/*关闭图形模式的函数*/exit(0); /*退出程序函数*/}break;default : break;}}cleardevice();setfillstyle(SOLID_FILL,2);/*设置填充图案的模式和颜色*/ bar(LEFT,TOP,RIGHT,BOTTOM);/*绘画屏幕*/lsnake1=createsnake(496,140);/*生成lsnake1的蛇头*/ lsnake2=createsnake(144,140);/*生成lsnake2的蛇头*/ setfood(&dx,&dy,lsnake1,lsnake2);k1=1,k2=1;fx1=0,fy1=1,fx2=0,fy2=1;s1=0,s2=0;t=100000;for(;(k1||k2)&&(s1+s2<SL);){add1=0;add2=0;if(kbhit())/*kbhit()函数检测是否有按键*/{sound(1200);delay(10000);k=bioskey(0);/*获取按键的扫描码*/delay(1000);nosound();switch(k){case 19200: /*按向左键*/if(fx1==0){fx1=-1;fy1=0;}break;case 19712: /*按向右键*/ if(fx1==0){fx1=1;fy1=0;}break;case 18432: /*按向上键*/ if(fy1==0){fx1=0;fy1=-1;} break;case 20480: /*按向下键*/ if(fy1==0){fx1=0;fy1=1;}break;case 7777: /*按向左键*/ if(fx2==0){fx2=-1;fy2=0;}break;case 8292: /*按向右键*/ if(fx2==0){fx2=1;fy2=0;}break;case 4471: /*按向上键*/ if(fy2==0)fx2=0;fy2=-1;}break;case 8051: /*按向下键*/if(fy2==0){fx2=0;fy2=1;}break;default: break;}}if(j==1){if((lsnake2->bx)>=LEFT&&(lsnake2->bx+SIZE)<=RIGHT&&(lsn ake2->by)>=TOP&&(lsnake2->by+SIZE)<=BOTTOM)/*检测snake2的蛇头是否出界*/{if((lsnake2->bx==dx)&&(lsnake2->by==dy))/*检测snake2是否吃到食物*/ {t=t-800;setfillstyle(1,0);bar(48,60,88,100);qufood(dx,dy);add2=1;s2++;setfillstyle(1,1);bar(48,60,88,100);numtostr(s2,w);outtextxy(58,70,w);outtextxy(58,80,"Blue");}}else{k2=0;/*freesnake(lsnake2);*/}if(k2&&nothit(lsnake2,lsnake1,fx2,fy2))paintsnake(lsnake2,2,2);/*消去蛇*/changsnake(lsnake2,fx2,fy2,add2);/*蛇身坐标移动*/paintsnake(lsnake2,14,1);/*绘制蛇*/}}else{k2=0;paintsnake(lsnake2,14,9);}if((lsnake1->bx)>=LEFT&&(lsnake1->bx+SIZE)<=RIGHT&&(lsn ake1->by)>=TOP&&(lsnake1->by+SIZE)<=BOTTOM){if((lsnake1->bx==dx)&&(lsnake1->by==dy)){t=t-800;/*t减小表示延迟减小,即表示蛇移动速度加快*/setfillstyle(1,0);bar(540,60,600,100);qufood(dx,dy);add1=1;s1++;setfillstyle(1,4);bar(560,60,600,100);numtostr(s1,w);outtextxy(570,70,w);outtextxy(570,80,"Red");}}else{k1=0;/*freesnake(lsnake1);*/}if(k1&& nothit(lsnake1,lsnake2,fx1,fy1)){paintsnake(lsnake1,2,2);changsnake(lsnake1,fx1,fy1,add1);paintsnake(lsnake1,5,4);}if(add1==1||add2==1)/*当两条蛇有一条吃到食物是,重新设置食物*/ setfood(&dx,&dy,lsnake1,lsnake2);/*产生食物*/delay(t);delay(t);delay(t);delay(t);delay(t);}if(j==0){if(s1==SL){settextstyle(0,0,3);outtextxy(100,200,"You are Win!");getch();settextstyle(1,0,1);}else{settextstyle(0,0,5);outtextxy(100,200,"GAME OVER !");getch();settextstyle(1,0,1);}}else{if(s1>s2){settextstyle(0,0,5);outtextxy(100,200,"Red Win!");getch();settextstyle(1,0,1);}else if(s1<s2){settextstyle(0,0,5);outtextxy(100,200,"Blue Win!"); getch();settextstyle(1,0,1);}else{settextstyle(0,0,5);outtextxy(100,200,"A draw!"); getch();settextstyle(1,0,1);}}}}。