少年听雨歌楼上,红烛昏罗帐.壮年听雨客舟中,江阔云低断雁叫西风. 而今听雨僧庐下,鬓已星星也! 悲欢离合总无情,一任阶前点滴到天明。

2004年7月27日星期二

贪吃蛇进度(1)

现在实现了计时器机制和事件机制。下一步是怎么去实现贪吃蛇的走动。

现在分为两个文件:snake.h和snake.c
如下:snake.h
#define NEWEVENT (EventLink)malloc(sizeof(event))
#define MAXSNAKELEN 30 /* Max length of the snake */
#define DEFAULTSNAKECOLOR WHITE /* The default snake color */

#define DEFAULTSPEED 100000 /* The start speed of the snake */
#define DEFAULTSTARTX 100 /* The start x of the snake */
#define DEFAULTSTARTY 100 /* The start y of the snake */
#define MAXEVENT 1 /* The startup event number */

/* Keyboard definition */
#define ENTER 13

/* The prototype of bool type */
typedef enum
{
true,
false
}bool;


/* The prototype of directions */
typedef enum
{
up,
right,
down,
left
}direction;

/* The prototype of a point of the snake */
typedef struct
{
int posx;
int posy;
direction dir; /* The current direction of this point */
}point;


/* The prototype of the snake */
typedef struct
{
point dots[MAXSNAKELEN];
}snake;

#ifndef __SNAKE

/* prevent multiple includes */
#define __SNAKE
/* The prototype of the event */
typedef struct
{
long interval;/* The interval of the event occurs */
long timer;
void (*invoke)(); /* The function pointer to the invoke action */
}event;


/* The prototype of the eventlist */
struct eventlist
{
event *eventnow;
struct eventlist *next;
};


typedef struct eventlist Node;
typedef Node *Link;
typedef event *EventLink;



int screenx=0;
int screeny=0;

void Init(snake *s);
void EventHandler(Link);
void EventInsert(Link,EventLink);
Link EventCreater(Link);
void EventFree(Link);

/* Registry Events */
void Controler(); /* The main keyboard controler */
void DrawPoint();
void DrawLine();
void DrawSnake(snake *s);

void EventInsert(Link eventhead,EventLink newevent)
{
Link pointer;
Link newlist;
pointer=eventhead;
while(1)
{
if (pointer->next==NULL)
{
newlist=(Link)malloc(sizeof(Node));
newlist->eventnow=newevent;
newlist->next=NULL;
pointer->next=newlist;
break;
}
pointer=pointer->next;
}
}

Link EventCreater(Link eventhead)
{
Link newevent;
EventLink tempevent;

int i;

eventhead=(Link)malloc(sizeof(Node));
/* Create event list */
if (eventhead==NULL)
{
printf("Memory allocate Failure!!\n");
}
else
{
tempevent=(EventLink)malloc(sizeof(event));
tempevent->interval=1;
tempevent->timer=0;
tempevent->invoke=Controler;
eventhead->eventnow=tempevent;
eventhead->next=NULL;
}
return eventhead;
}

void EventFree(Link eventhead)
{
Link pointer;
EventLink tempevent;
while(eventhead !=NULL)
{
pointer=eventhead;
eventhead=eventhead->next;
tempevent=eventhead->eventnow;
free(pointer);
free(tempevent);
}
}
#endif


Snake.c
#include
#include
#include
#include
#include
#include
#include

/* To return the head pointer of the eventlist */


int main(void)
{
snake me;
Link head;
int gdriver=DETECT,gmode;
EventLink tempevent;

/* Init graphic mode */
initgraph(&gdriver,&gmode,"");
Init(&me);
head=EventCreater(head);
tempevent=NEWEVENT;
tempevent->interval=10000;
tempevent->timer=0;
tempevent->invoke=DrawPoint;
EventInsert(head,tempevent);
EventHandler(head);
EventFree(head);
closegraph();
clrscr();
return 0;
}

void DrawLine()
{
int x;
int y;
x=random(500);
y=random(500);
circle(x,y,20);
}

void DrawPoint()
{
int x1,x2;
int y1,y2;
x1=random(200);x2=random(100);
y1=random(200);y2=random(100);
line(x1,y1,x2,y2);
}

void Controler()
{

}

void Init(snake *s)
{
int i,j;
int sx=DEFAULTSTARTX,sy=DEFAULTSTARTY;
randomize();
for (i=0;i<>
{
s->dots[i].posx=sx;
s->dots[i].posy=sy;
s->dots[i].dir=up;
sy++;
}
screenx=getmaxx();
screeny=getmaxy();
}

/* Draw snake */
void DrawSnake(snake *s)
{
int i;
for (i=0;i<>
{
putpixel(s->dots[i].posx,s->dots[i].posy,DEFAULTSNAKECOLOR);
}
}

void EventHandler(Link eventhead)
{
int i;
Link pointer;
while(1)
{
delay(1);
pointer=eventhead;
while (pointer!=NULL)
{

pointer->eventnow->timer++;
if ((pointer->eventnow->timer)>=(pointer->eventnow->interval))
{

/* Invoke current event */
pointer->eventnow->invoke();
pointer->eventnow->timer=0;
}
pointer=pointer->next;

}
if(kbhit())
{
if (getch()==ENTER)
break;
}
}
}

没有评论: