File: snake.cpp

package info (click to toggle)
vdr-plugin-games 0.6.3-39
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 576 kB
  • sloc: cpp: 9,884; sh: 180; makefile: 146
file content (238 lines) | stat: -rw-r--r-- 4,655 bytes parent folder | download | duplicates (8)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*****************************************************************************\
*                                                                             *
* Programm:    Snake                                                          *
* License:     GPL (General Puplic License)                                   *
* Last Change: 2003-04-23                                                     *
* Authors:     Justin Todd <gameprogrammer@excite.com>                        *
*              Clemens Kirchgatterer <clemens@thf.ath.cx>                     *
*                                                                             *
\*****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "../display.h"
#include "../defines.h"

#define WIDTH  36
#define HEIGHT 27

#define EMPTY  0
#define FRUIT  1
#define SNAKE  2

static unsigned char field[HEIGHT][WIDTH];

static bool running, stopped;
static int key;

static int posx, posy, dirx, diry, length;
static int score, level, fruits, halt, over, size;

static void
move_left() {
	dirx = -1;
	diry = 0;
}

static void
move_right() {
	dirx = 1;
	diry = 0;
}

static void
move_up() {
	dirx = 0;
	diry = -1;
}

static void
move_down() {
	dirx = 0;
	diry = 1;
}

static void
reset_game() {
	int x, y;

	posx   = WIDTH/2;
	posy   = HEIGHT/2;
	length = 3;
	fruits = 0;
	halt   = 0;
	over   = 0;
	score  = 0;
	level  = 1;
	switch (RAND(3)) {
		case 0: move_up();    break;
		case 1: move_down();  break;
		case 2: move_left();  break;
		case 3: move_right(); break;
	}
	for (y=0; y<HEIGHT; y++) {
		for (x=0; x<WIDTH; x++) field[y][x] = EMPTY;
	}
	field[RAND(HEIGHT-1)][RAND(WIDTH-1)] = FRUIT;
}

static void
draw_block(int x, int y, int c) {
	int x1, y1, x2, y2;

	x1 = x*size;
	y1 = y*size;
	x2 = x*size+size;
	y2 = y*size+size;
	Dpy::rect_fill(x1, y1, x2, y2, c);
}

static void
draw_field() {
	int x, y;

	for (y=0; y<HEIGHT; y++) {
		for (x=0; x<WIDTH; x++) {
			switch (field[y][x]) {
				case EMPTY: draw_block(x, y, COL_BGR); break;
				case FRUIT: draw_block(x, y, COL_GRN); break;
				default:    draw_block(x, y, COL_WHT); break;
			}
		}
	}
}

static void
halt_game(void) {
	if (over) {
		reset_game();
		draw_field();
		return;
	}
	if (halt) {
		draw_field();
		halt = 0;
	} else {
		Dpy::message_show("Pause", NULL, COL_WHT);
		halt = 1;
	}
	Dpy::update();
}

static void
draw_score(void) {
	Dpy::status_left(score, COL_WHT);
	Dpy::status_right(level, COL_WHT);
}

static void
game_over(void) {
	over = 1;
	Dpy::message_show("Game Over", NULL, COL_WHT);
	Dpy::update();
}

static void
move_snake() {
	int x, y;

	posx += dirx;
	posy += diry;
	if (field[posy][posx] >= SNAKE) {
		game_over(); return;
	}
	if ((posx < 0) || (posx > WIDTH-1) || (posy < 0) || (posy > HEIGHT-1)) {
		game_over(); return;
	}
	if (field[posy][posx] == FRUIT) {
		x = RAND(WIDTH-1);
		y = RAND(HEIGHT-1);
		field[y][x] = FRUIT;
		draw_block(x, y, COL_GRN);
		length += level;
		score  += level;
		fruits += 1;
		if (fruits == 7) {
			if (level<9) level++;
			length = 3;
			fruits = 0;
		}
	}
	field[posy][posx] = SNAKE;
	for (y=0; y<HEIGHT; y++) {
		for (x=0; x<WIDTH; x++) {
			if (field[y][x] >= SNAKE) {
				if (field[y][x] >= length + SNAKE) {
					field[y][x] = EMPTY;
					draw_block(x, y, COL_BGR);
				} else {
					++field[y][x];
					draw_block(x, y, COL_WHT);
				}
			}
		}
	}
}

int
snake_start(int x, int y, int s, int c) {
	int X, Y, W, H;

	SRND();
	size = 4*s;
	W = WIDTH*size;
	H = HEIGHT*size;
	X = DISPLAY_MINX+(int)((DISPLAY_MAXX-W-DISPLAY_MINX)*((x+9.0)/18.0));
	Y = DISPLAY_MINY+(int)((DISPLAY_MAXY-H-DISPLAY_MINY)*((y+9.0)/18.0));
	Dpy::open(X, Y, W, H);
	Dpy::status_center("Snake", COL_WHT);
	reset_game();
	draw_field();
	draw_score();
	running = true;
	stopped = false;
	key = KEY_NONE;
	while (running) {
		switch (key) {
			case KEY_LEFT:  move_left();     break;
			case KEY_RIGHT: move_right();    break;
			case KEY_UP:    move_up();       break;
			case KEY_DOWN:  move_down();     break;
			case KEY_BACK:  running = false; break;
			case KEY_OK:    halt_game();     break;
			case KEY_NONE:                   break;
		}
		key = KEY_NONE;
		usleep(10000+10000*(10-level));
		if (halt || over) continue;
		if (running) {
			move_snake();
			if (!over) {
				draw_score();
				Dpy::update();
			}
		}
	}
	Dpy::close();
	stopped = true;
	return (0);
}

int
snake_stop(void) {
	running = false;
	while (!stopped) usleep(10000);
	return (0);
}

int
snake_key(int k) {
	key = k; // XXX maybe mutex lock or cond signal?
	if (key == KEY_BACK) {
		snake_stop();
		return (1);
	}
	return (0);
}