航海王ol兑换码:用c语言编写一个简单的动画或休闲小游戏

来源:百度文库 编辑:杭州交通信息网 时间:2024/05/05 05:05:59
简单的小游戏或动画的c语言代码,如贪吃蛇,推箱子等。要有现成的编码啊,我要交个学期报告。拜托了,哪位高人。

http://post.baidu.com/f?kz=5417859
贴吧里大把

//贪吃蛇游戏,可以运行,我测试过
#include<stdio.h>
#include<dos.h>
#include<graphics.h>
#include<stdlib.h>
#define UP 0x48
#define DOWN 0x50
#define LEFT 0x4b
#define RIGHT 0x4d
#define SPACE 0x39
#define ENTER 0x1c
#define ESC 0x1
#define SW 60
#define SL 20
#define Gsize 20
#define S 0
#define X 1
#define Z 2
#define Y 3

typedef struct SN{
int x;
int y;
int direction;
struct SN *next;
}SNAKE;

SNAKE *tail;
int rank=0;
int grilled[21][16]={0};
unsigned size,size1;
void *buffer,*buffer1;

void interrupt (*oldtime)();
void interrupt newtime();
void install(void interrupt (*haddr)());

int timer=0;

int getkey()
{
union REGS rg;
rg.h.ah=0;
int86(0x16,&rg,&rg);
return rg.h.ah;
}

SNAKE *initmainwindow()
{
int i,j,k;
SNAKE *p,*w;
for(i=0;i<20;i++)
{
grilled[0]=1;
grilled[15]=1;
}
for(j=0;j<16;j++)
{
grilled[0][j]=1;
grilled[20][j]=1;
}
rectangle(SW+20,SL+20,SW+300,SL+400);
rectangle(SW+19,SL+19,SW+299,SL+399);
rectangle(SW+Gsize*6,SL+Gsize*6,SW+Gsize*6+20,SL+Gsize*6+20);
bar(SW+Gsize*6+2,SL+Gsize*6+2,SW+Gsize*6+18,SL+Gsize*6+18);
size=imagesize(SW+Gsize*6,SL+Gsize*6,SW+Gsize*6+20,SL+Gsize*6+20);
buffer=malloc(size);
getimage(SW+Gsize*6,SL+Gsize*6,SW+Gsize*6+20,SL+Gsize*6+20,buffer);

for(k=1;k<3;k++)
{
putimage(SW+Gsize*6,SL+Gsize*(6+k),buffer,XOR_PUT);
grilled[6+k][6]=1;
}

w=NULL;
for(k=2;k>=0;k--)
{
p=(SNAKE *)malloc(sizeof(SNAKE));
p->x=6;p->y=6+k;
p->direction=S;
p->next=w;
w=p;
}
rectangle(SW+Gsize*2,SL+Gsize*4,SW+Gsize*2+15,SL+Gsize*4+15);
rectangle(SW+Gsize*2+2,SL+Gsize*4+2,SW+Gsize*2+13,SL+Gsize*4+13);
grilled[4][2]=2;

size1=imagesize(SW+Gsize*2,SL+Gsize*4,SW+Gsize*2+15,SL+Gsize*4+15);
buffer1=malloc(size1);
getimage(SW+Gsize*2,SL+Gsize*4,SW+Gsize*2+15,SL+Gsize*4+15,buffer1);

return w;
}

int main()
{
int driver=VGA,mode=VGAHI;
char key;
tail=NULL;
initgraph(&driver,&mode,"");
randomize();
tail=initmainwindow();
oldtime=getvect(0x1c);
while(1)
{
key=getkey();
switch(key)
{
case UP :
tail->direction=S;break;
case DOWN:
tail->direction=X;break;
case LEFT:
tail->direction=Z;break;
case RIGHT:
tail->direction=Y;break;

case ESC: closegraph();exit(1);
case SPACE :install(oldtime);break;
case ENTER :install(newtime);break;
}
}
}

void interrupt newtime()
{
int xx=1,yy=1;
char s[4];
SNAKE *p,*pend,*pp;
p=(SNAKE *)malloc(sizeof(SNAKE));
p->next=NULL;
if(timer++==3)
{
timer=0;
switch(tail->direction)
{
case S: if(grilled[tail->y-1][tail->x]==1) exit(1);
p->x=tail->x;p->y=tail->y-1;p->direction=tail->direction;p->next=NULL;
break;
case X: if(grilled[tail->y+1][tail->x]==1) exit(1);
p->x=tail->x;p->y=tail->y+1;p->direction=tail->direction;p->next=NULL;
break;
case Z: if(grilled[tail->y][tail->x-1]==1) exit(1);
p->x=tail->x-1;p->y=tail->y;p->direction=tail->direction;p->next=NULL;
break;
case Y: if(grilled[tail->y][tail->x+1]==1) exit(1);
p->x=tail->x+1;p->y=tail->y;p->direction=tail->direction;p->next=NULL;
break;

}
if(grilled[p->y][p->x]==2)
{
putimage(SW+Gsize*(p->x),SL+Gsize*(p->y),buffer1,XOR_PUT);
putimage(SW+Gsize*(p->x),SL+Gsize*(p->y),buffer,XOR_PUT);
grilled[p->y][p->x]=1;
p->next=tail;
tail=p;
rank++;
bar(375,390,400,420);
sprintf(s,"%d",rank);
setcolor(GREEN);
outtextxy(380,400,s);
do{
xx=random(14)+1;yy=random(19)+1;
}while((grilled[yy][xx]==1)||grilled[yy][xx]==2);
grilled[yy][xx]=2;
putimage(SW+Gsize*xx,SL+Gsize*yy,buffer1,XOR_PUT);
}

else
{
putimage(SW+Gsize*(p->x),SL+Gsize*(p->y),buffer,XOR_PUT);
grilled[p->y][p->x]=1;
p->next=tail;
tail=p;
pend=tail;
while(pend->next) {pp=pend;pend=pend->next;}
putimage(SW+Gsize*(pend->x),SL+Gsize*(pend->y),buffer,XOR_PUT);
grilled[pend->y][pend->x]=0;
pp->next=NULL;
}
}

}

void install(void interrupt (*hadder)())
{
disable();
setvect(0x1c,hadder);
enable();
}