太阳是黑色的

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

贪吃蛇进度(1)

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

现在分为两个文件:snake.h和snake.c

如下:snake.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <conio.h>
#include <graphics.h>
#include <dos.h>
#include <snake.h>
/* 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 < MAXSNAKELEN; 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 < MAXSNAKELEN; 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;
        }
    }
}
|2|left

一条评论 »

No comments yet.

RSS feed for comments on this post.

Leave a comment

要发表评论,您必须先登录