今天整理电脑, 发现自己去年就是大一上学期快结束时写的贪吃蛇, 那时
还没有写blog, 今天拿出来晒晒啊,嘿嘿
多多交流,多多交流......
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<dos.h>
#include<stdlib.h>
#include<graphics.h>
#include "welcome.c" /**//*这个是我自己写的一个汉字矩阵, 在下面的welcome函数中使用 */
/**//*direction*/
#define left 1
#define right 2
#define up 3
#define down 4
#define N 40
#define TIME 10
/**//*user control*/
#define moveup 18432
#define movedown 20480
#define moveleft 19200
#define moveright 19712
#define ESC 283
#define l 9836
#define L 9804
struct snake
...{
int x;
int y;
};
/**//*
//N is the max length of snake, x and y mean its position
//body[0] is the head
//
*/
/**//*其实是结构体链表最好了 , 这里 偷个懒, 有兴趣,自己改一下, 很容易的 */
struct snake body[N];
const struct snake *p=body;
int direct=down;
int foodx,foody;
int tail=3;
int foodflag=1;/**//*judge that if can display a food*/
int eatflag=0;/**//*judge if snake has eat the food*/
long mark=0;
int level=100000000;
int olddirect=down;
int again=1;
int endflag=0;
void drawmat(char *mat,int matsize,int x,int y,int color)
/**//*依次:字模指针、点阵大小、起始坐标(x,y)、颜色*/
...{
int i,j,k,n;
n=(matsize-1)/8+1;
for(j=0;j<matsize;j++)
for(i=0;i<n;i++)
for(k=0;k<8;k++)
if(mat[j*n+i]&(0x80>>k)) /**//*测试为1的位则显示*/
putpixel(x+i*8+k,y+j,color);
}
void welcome()
...{
/**//*outtextxy(200,200,"WELCOME TO THE SNAKE GAME!!!!");*/
drawmat(huan64K,64,200,200,10);
drawmat(ying64K,64,400,200,10);
getch();
cleardevice();
}
void backset()
...{
char site[20];
cleardevice();
setbkcolor(1);
rectangle(25,25,435,435);
outtextxy(450,35,"score: 0");
outtextxy(450,55,"level: 1");
outtextxy(450,75,"Press "L" or "l"To ");
outtextxy(450,95, "input 1-7 ");
outtextxy(450,115, "for speed control!");
outtextxy(450,300,"Start :Enter");
outtextxy(450,340,"Exit :Esc");
sprintf(site, "up : %c",24);
outtextxy(450,420,site);
sprintf(site, "down : %c",25);
outtextxy(540,420,site);
sprintf(site, "right: %c",26);
outtextxy(540,380,site);
sprintf(site, "left : %c",27);
outtextxy(450,380,site);
}
void initialize()
...{
int i,j;
for(i=0;i < N;i++ )
...{
body[i].x=0;
body[i].y=0;
}
/**//*snake has four at beginning.*/
body[0].x=35; body[0].y=65;
body[1].x=35; body[1].y=55;
body[2].x=35; body[2].y=45;
body[3].x=35; body[3].y=35;
direct=down;
tail=3;
foodflag=1;/**//*judge that if can display a food*/
eatflag=0;/**//*judge if snake has eat the food*/
mark=0;
level=100000000;
olddirect=down;
again=1;
endflag=0;
}
void draw()
...{
int i=1;
setfillstyle(1,4);
/**//**/
bar((p->x)-5,(p->y)-5,(p->x)+5,(p->y)+5);
setfillstyle(9,4);
do
...{
bar(((p+i)->x)-5,((p+i)->y)-5,((p+i)->x)+5,((p+i)->y)+5);
i++;
}while((p+i)->x != 0);
}
void rubber()
...{
int i=0;
setfillstyle(1,1);
/**//**/
do
...{
bar(((p+i)->x)-5,((p+i)->y)-5,((p+i)->x)+5,((p+i)->y)+5);
i++;
}while((p+i)->x != 0);
}
void gameover()
...{
char ch;
endflag=1;
outtextxy(200,200,"GAMEOVER!");
outtextxy(200,250,"Try again? Y/N:");
ch=getch();
while(1)
...{
if(ch=='y'|| ch=='Y')
...{
again=1;
break;
}
else if(ch == 'N' || ch == 'n')
...{
again=0;
break;
}
ch = getch();
}
}
void judge()
...{
int i;
if(p->y < 35 ) gameover();
if(p->y > 425) gameover();
if(p->x < 35) gameover();
if(p->x > 425) gameover();
for(i=1;i<=tail;i++)
...{
if((p->x == (p+i)->x) && (p->y==(p+i)->y))
gameover();
}
}
void move(int towards)
...{
int endp;
delay(TIME*level);
/**//* at first ,rubber the old snake*/
rubber();
/**//* let the after hold the before one'position*/
for(endp=tail;endp>=1;endp--)
...{
(p+endp)->x = (p+endp-1)->x;
(p+endp)->y = (p+endp-1)->y;
}
switch(towards) /**//*change the head body[0]*/
...{
case left :
body[0].x-=10;break;
case right:
body[0].x+=10;break;
case up:
body[0].y-=10;break;
case down:
body[0].y+=10;break;
default: break;
}
draw();
judge();/**//**//**//*this fun decide if gameover?*/
}
void food()
...{
int i;
int disflag=1;
if(foodflag)
...{
do
...{
disflag=1;
foodx=random(42)*10+5;
foody=random(42)*10+5;
for(i=0;i< N;i++)
...{
if(foodx==body[i].x)
...{
disflag=0;
}
}
}while(!(foodx>=35 && foody>=35 && disflag==1));
setfillstyle(1,15);
bar(foodx-5,foody-5,foodx+5,foody+5);
foodflag=0;
}
}
void score()
...{
char ventor[30];
mark+=10;
/**//*here add function for display score!*/
setfillstyle(1,1);
sprintf(ventor,"%ld",mark);
bar(500,35,550,42);
outtextxy(500,35,ventor);
if(mark==(N-4)*10)
...{
outtextxy(200,200,"YON WIN !");
getch();
getch();
exit(0);
}
}
void eatjudge()
...{
if(body[1].x==foodx && body[1].y==foody)
...{
eatflag=1;
foodflag = 1;
score();
}
/**//*else
eatflag=0;*/
}
void grade_up()
...{
/**//*here realize the level*/
int grade;
char r;
setfillstyle(1,1);
bar(500,55,550,62);
grade=(int)getch()-48;
sprintf(&r,"%d",grade);
outtextxy(500,55,&r);
level=pow(10,9-grade);
/**//*display*/
}
void addlen()
...{
if(eatflag)
...{
tail++;
if(tail >= N) grade_up();
(p+tail)->x =8888;
(p+tail)->y = 1;
eatflag = 0;
}
}
void run()
...{
static int ch = movedown;
/**//*int endp=tail;*/
while(1)
...{
if(endflag) break;
if( kbhit())
...{
ch=bioskey(0);
olddirect=direct;
switch(ch)
...{
case moveleft: if(olddirect!=right)
direct=left;break;
case moveright :if(olddirect!=left)
direct=right;break;
case moveup : if(olddirect!=down)
direct=up;break;
case movedown : if(olddirect!=up)
direct=down;break;
case L:
case l: grade_up();break;
case ESC :outtextxy(200,200,"BYE bye!");
getch();
getch();
exit(0);
default: break;
}
}
move(direct);
food();
eatjudge();
addlen();
}
}
int main()
...{
int gd,gm;
gd=DETECT;
registerbgidriver(EGAVGA_driver);
initgraph(&gd, &gm, "");
welcome();/**//*the start action*/
while(again==1)
...{
endflag=0;
backset();/**//*the set of background*/
initialize();
draw();
run();
}
return 0;
}
整个程序不是太长,因为写的功能相对简单,也没有什么特效,
下面是哪个上面文件需要包含的welcome.c
char ying64K[]=...{
/**//* 以下是 '迎' 的 64点阵楷体_GB2312 字模,512 byte */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x3C,0x00,0x00,0x00,
0x00,0x30,0x00,0x00,0x70,0x00,0x00,0x00,
0x00,0x3C,0x00,0x00,0xE0,0x00,0x00,0x00,
0x00,0x1E,0x00,0x01,0xC0,0x00,0x00,0x00,
0x00,0x1F,0x00,0x03,0x00,0x00,0x00,0x00,
0x00,0x0F,0x80,0x06,0x00,0x01,0xF0,0x00,
0x00,0x07,0x80,0x18,0x00,0x07,0xF8,0x00,
0x00,0x03,0x01,0xF0,0x07,0xFF,0xFC,0x00,
0x00,0x00,0x00,0xC0,0x03,0xE0,0x38,0x00,
0x00,0x00,0x00,0xC0,0x03,0x00,0x38,0x00,
0x00,0x00,0x00,0xC0,0x03,0x00,0x38,0x00,
0x00,0x00,0x00,0x40,0x03,0x00,0x30,0x00,
0x00,0x00,0x00,0x40,0x03,0x00,0x30,0x00,
0x00,0x00,0x00,0x40,0x03,0x00,0x30,0x00,
0x00,0x00,0x00,0x40,0x03,0x00,0x70,0x00,
0x00,0x0C,0x00,0x40,0x03,0x00,0x70,0x00,
0x00,0x3F,0x00,0x40,0x03,0x00,0x70,0x00,
0x00,0xFF,0x80,0x60,0x03,0x00,0x70,0x00,
0x1F,0xC7,0x80,0x60,0x43,0x00,0x70,0x00,
0x0E,0x07,0x00,0x60,0x83,0x0C,0xE0,0x00,
0x00,0x06,0x00,0x61,0x83,0x07,0xE0,0x00,
0x00,0x06,0x00,0x67,0x01,0x03,0xE0,0x00,
0x00,0x04,0x00,0x7E,0x01,0x01,0xC0,0x00,
0x00,0x0C,0x00,0x7C,0x01,0x01,0xC0,0x00,
0x00,0x0C,0x00,0x78,0x01,0x01,0x80,0x00,
0x00,0x08,0x00,0xF0,0x01,0x01,0x00,0x00,
0x00,0x04,0x00,0xE0,0x01,0x00,0x00,0x00,
0x00,0x04,0x00,0xE0,0x01,0x00,0x00,0x00,
0x00,0x04,0x00,0x40,0x01,0x00,0x00,0x00,
0x00,0x04,0x00,0x00,0x01,0x00,0x00,0x00,
0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x00,
0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x00,
0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x00,
0x00,0x02,0x00,0x00,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,
0x00,0x3F,0x80,0x00,0x00,0x00,0x00,0x00,
0x1F,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00,
0x0F,0xF0,0x7F,0xC0,0x00,0x00,0x00,0x00,
0x0F,0x80,0x03,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x7F,0xE0,0x00,0x00,0x00,
0x00,0x00,0x00,0x1F,0xFF,0x80,0x00,0x00,
0x00,0x00,0x00,0x03,0xFF,0xFF,0xFF,0xFE,
0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xF8,
0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0xE0,
0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0x00,
0x00,0x00,0x00,0x00,0x00,0xFF,0xF8,0x00,
0x00,0x00,0x00,0x00,0x00,0x1F,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
char huan64K[]=...{
/**//* 以下是 '欢' 的 64点阵楷体_GB2312 字模,512 byte */
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1E,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x1C,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x38,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x30,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x60,0x00,0x00,0x00,
0x00,0x00,0x08,0x00,0xC0,0x00,0x00,0x00,
0x00,0x00,0x3C,0x00,0xC0,0x03,0x80,0x00,
0x00,0x03,0xFE,0x01,0x80,0x1F,0xC0,0x00,
0x00,0x1F,0xFF,0x01,0x81,0xFF,0xF0,0x00,
0x03,0xFC,0x0F,0x03,0xFF,0xFF,0xF8,0x00,
0x0F,0xE0,0x0E,0x03,0xFC,0x03,0xF0,0x00,
0x07,0x00,0x0C,0x06,0x00,0x03,0x80,0x00,
0x00,0x00,0x1C,0x04,0x00,0x03,0x00,0x00,
0x00,0x00,0x1C,0x08,0x00,0x06,0x00,0x00,
0x00,0x00,0x18,0x00,0x00,0x04,0x00,0x00,
0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,
0x00,0x40,0x38,0x00,0x30,0x00,0x00,0x00,
0x00,0x60,0x38,0x00,0x38,0x00,0x00,0x00,
0x00,0x30,0x30,0x00,0x38,0x00,0x00,0x00,
0x00,0x18,0x70,0x00,0x38,0x00,0x00,0x00,
0x00,0x0E,0x70,0x00,0x38,0x00,0x00,0x00,
0x00,0x07,0xE0,0x00,0x38,0x00,0x00,0x00,
0x00,0x07,0xE0,0x00,0x38,0x00,0x00,0x00,
0x00,0x03,0xC0,0x00,0x38,0x00,0x00,0x00,
0x00,0x01,0xE0,0x00,0x3C,0x00,0x00,0x00,
0x00,0x01,0xF0,0x00,0x30,0x00,0x00,0x00,
0x00,0x01,0xF8,0x00,0x72,0x00,0x00,0x00,
0x00,0x03,0xF8,0x00,0x61,0x00,0x00,0x00,
0x00,0x03,0x3C,0x00,0x60,0x80,0x00,0x00,
0x00,0x06,0x1C,0x00,0xC0,0xC0,0x00,0x00,
0x00,0x0E,0x0E,0x00,0xC0,0x60,0x00,0x00,
0x00,0x0C,0x0E,0x00,0xC0,0x70,0x00,0x00,
0x00,0x18,0x06,0x01,0x80,0x38,0x00,0x00,
0x00,0x38,0x02,0x01,0x80,0x1C,0x00,0x00,
0x00,0x30,0x02,0x03,0x00,0x1E,0x00,0x00,
0x00,0x60,0x00,0x06,0x00,0x0F,0x00,0x00,
0x00,0xC0,0x00,0x06,0x00,0x07,0x80,0x00,
0x01,0x80,0x00,0x0C,0x00,0x07,0xC0,0x00,
0x02,0x00,0x00,0x18,0x00,0x03,0xF0,0x00,
0x04,0x00,0x00,0x30,0x00,0x01,0xFC,0x00,
0x08,0x00,0x00,0x60,0x00,0x01,0xFF,0x00,
0x00,0x00,0x01,0x80,0x00,0x00,0xFF,0xE0,
0x00,0x00,0x02,0x00,0x00,0x00,0x7F,0xF8,
0x00,0x00,0x04,0x00,0x00,0x00,0x3F,0xF0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
上面的是 欢迎 两个字的点阵, 你可以用一般的点阵生成器来生成你想
要的汉字.
呵呵,欢迎交流啊,写在评论中即可.