CHDK_DE Vorschauversion  Trunk Rev. 6014
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gui_snake.c-Dateireferenz
#include "camera_info.h"
#include "keyboard.h"
#include "lang.h"
#include "conf.h"
#include "gui.h"
#include "gui_draw.h"
#include "gui_lang.h"
#include "gui_batt.h"
#include "gui_mbox.h"
#include "modes.h"
#include "module_def.h"
#include "simple_game.c"
+ Include-Abhängigkeitsdiagramm für gui_snake.c:

gehe zum Quellcode dieser Datei

Makrodefinitionen

#define SNAKE_MAX_LENGTH   0xF0
 
#define SNAKE_ELEMENT_SIZE   8
 
#define RING_WIDTH   40
 
#define RING_HEIGHT   24
 

Aufzählungen

enum  { DIR_LEFT = 0, DIR_DOWN, DIR_UP, DIR_RIGHT }
 

Funktionen

void gui_game_menu_kbd_process ()
 
int gui_snake_kbd_process ()
 
void gui_snake_draw ()
 
static void draw_element (int x, int y, const char element[8][8], char angle)
 
static void load_laby (int num)
 
static void snake_generate_apple ()
 
static void snake_start ()
 
static void game_over ()
 
int gui_snake_init ()
 
int basic_module_init ()
 

Variablen

gui_handler GUI_MODE_SNAKE
 
int snake_scale
 
int snake_screen_size
 
int screen_left
 
int screen_top
 
unsigned char ring [RING_WIDTH][RING_HEIGHT]
 
unsigned short snake_head
 
unsigned short snake_tail
 
unsigned char snake [SNAKE_MAX_LENGTH][2]
 
unsigned int points
 
unsigned char level
 
unsigned char direction
 
char str_buf [40]
 
const unsigned char labyrinth [4][RING_HEIGHT][RING_WIDTH]
 
const char head [8][8]
 
const char tail [8][8]
 
const char tail2 [8][8]
 
const char apple [8][8]
 
const char wall [8][8]
 
ModuleInfo _module_info
 

Makro-Dokumentation

#define RING_HEIGHT   24

Definiert in Zeile 30 der Datei gui_snake.c.

#define RING_WIDTH   40

Definiert in Zeile 29 der Datei gui_snake.c.

#define SNAKE_ELEMENT_SIZE   8

Definiert in Zeile 28 der Datei gui_snake.c.

#define SNAKE_MAX_LENGTH   0xF0

Definiert in Zeile 27 der Datei gui_snake.c.

Dokumentation der Aufzählungstypen

anonymous enum
Aufzählungswerte
DIR_LEFT 
DIR_DOWN 
DIR_UP 
DIR_RIGHT 

Definiert in Zeile 31 der Datei gui_snake.c.

31  {
32  DIR_LEFT = 0,
33  DIR_DOWN,
34  DIR_UP,
35  DIR_RIGHT,
36 };

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 470 der Datei gui_snake.c.

470  {
471  return gui_snake_init();
472 }
static void draw_element ( int  x,
int  y,
const char  element[8][8],
char  angle 
)
static

Definiert in Zeile 210 der Datei gui_snake.c.

210  {
213  int xx,yy,px,py;
214  for(xx=0;xx<SNAKE_ELEMENT_SIZE;xx++)
215  for(yy=0;yy<SNAKE_ELEMENT_SIZE;yy++){
216  char c = COLOR_RED;
217  char col = 0;
218 
219  if(angle == 0) col = element[yy][xx];
220  if(angle == 1) col = element[xx][7-yy];
221  if(angle == 2) col = element[xx][yy];
222  if(angle == 3) col = element[yy][7-xx];
223  if(angle == 4) col = element[7-xx][yy];
224  switch(col){
225  case 'R': c = COLOR_RED; break;
226  case 'G': c = COLOR_GREEN; break;
227  case 'B': c = COLOR_BLACK; break;
228  case 'b': c = COLOR_BLUE; break;
229  case 'Y': c = COLOR_YELLOW; break;
230  case 'O': c = 0x28; break;
231  default: c = COLOR_WHITE; break;
232  };
233  for (px=xx*snake_scale; px<(xx+1)*snake_scale; px+=1)
234  for (py=yy*snake_scale; py<(yy+1)*snake_scale; py+=1)
235  draw_pixel( x+px, y+py, c );
236  }
237 }
void gui_game_menu_kbd_process ( )

Definiert in Zeile 7 der Datei simple_game.c.

8 {
9  running = 0;
11 }
void gui_snake_draw ( )

Definiert in Zeile 334 der Datei gui_snake.c.

335 {
336  unsigned short new_head;
337  unsigned short new_tail;
338  unsigned short new_cord;
339  char game_over_flag = 0, draw_head, clear_tail, draw_points = 1;
340  unsigned char prevdir = direction;
341  unsigned int i;
342  new_head = (snake_head + 1) % SNAKE_MAX_LENGTH;
343  new_tail = (snake_tail + 1) % SNAKE_MAX_LENGTH;
344  draw_head = 1;
345  clear_tail = 1;
346  if (direction == DIR_RIGHT)
347  {
348  new_cord = (snake[snake_head][0] + 1) % RING_WIDTH;
349  snake[new_head][0] = new_cord;
350  snake[new_head][1] = snake[snake_head][1];
351  }
352  else if (direction == DIR_LEFT)
353  {
354  new_cord = snake[snake_head][0] - 1;
355  if (new_cord == 0xFFFF)
356  new_cord = RING_WIDTH - 1;
357  snake[new_head][0] = new_cord;
358  snake[new_head][1] = snake[snake_head][1];
359  }
360  else if (direction == DIR_UP)
361  {
362  new_cord = snake[snake_head][1] - 1;
363  if (new_cord == 0xFFFF)
364  new_cord = RING_HEIGHT - 1;
365  snake[new_head][0] = snake[snake_head][0];
366  snake[new_head][1] = new_cord;
367  }
368  else if (direction == DIR_DOWN)
369  {
370  new_cord = (snake[snake_head][1] + 1) % RING_HEIGHT;
371  snake[new_head][0] = snake[snake_head][0];
372  snake[new_head][1] = new_cord;
373  }
374 
375  i = snake_tail;
376  while (1)
377  {
378  if (snake[i][0] == snake[new_head][0] && snake[i][1] == snake[new_head][1])
379  {
380  game_over_flag = 1;
381  break;
382  }
383  if (i == snake_head)
384  break;
385  i = (i + 1) % SNAKE_MAX_LENGTH;
386  }
387 
388  if (ring[snake[new_head][0]][snake[new_head][1]] == 1)
389  {
390  ring[snake[new_head][0]][snake[new_head][1]] = ' ';
392  points += level;
393  draw_points = 1;
394  if ((new_head + 1) % SNAKE_MAX_LENGTH != snake_tail)
395  {
396  new_tail = snake_tail;
397  clear_tail = 0;
398  }
399  }
400  else if (ring[snake[new_head][0]][snake[new_head][1]] != ' ')
401  {
402  game_over_flag = 1;
403  }
404  if (draw_head)
405  {
406  draw_element(snake[new_head][0], snake[new_head][1], head, direction);
407 
408  new_cord = snake_head - 1;
409  if (new_cord == 0xFFFF)
410  new_cord = SNAKE_MAX_LENGTH - 1;
411  if (snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_UP)
412  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 2);
413  else if (snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_LEFT)
414  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 0);
415  else if (snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_UP)
416  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 4);
417  else if (snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_RIGHT)
418  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 3);
419  else if (snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_RIGHT)
420  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 2);
421  else if (snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_DOWN)
422  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 0);
423  else if (snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_LEFT)
424  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 4);
425  else if (snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_DOWN)
426  draw_element(snake[snake_head][0], snake[snake_head][1], tail2, 3);
427  else if (prevdir == direction && (direction == DIR_RIGHT || direction == DIR_LEFT))
428  draw_element(snake[snake_head][0], snake[snake_head][1], tail, 0);
429  else if (prevdir == direction && (direction == DIR_UP || direction == DIR_DOWN))
430  draw_element(snake[snake_head][0], snake[snake_head][1], tail, 1);
431  }
432  prevdir = direction;
433  if (clear_tail)
434  {
436  screen_top + snake[snake_tail][1] * snake_screen_size,
437  screen_left + snake[snake_tail][0] * snake_screen_size + snake_screen_size - 1,
438  screen_top + snake[snake_tail][1] * snake_screen_size + snake_screen_size - 1, MAKE_COLOR(COLOR_WHITE, COLOR_WHITE), RECT_BORDER0 | DRAW_FILLED);
439  }
440  snake_head = new_head;
441  snake_tail = new_tail;
442  msleep(100);
443  if (game_over_flag)
444  {
445  game_over();
446  game_over_flag = 0;
447  }
448  if (draw_points)
449  {
450  draw_points = 0;
453  }
454 }
int gui_snake_kbd_process ( )

Definiert in Zeile 308 der Datei gui_snake.c.

309 {
310  switch (kbd_get_autoclicked_key()) {
311  case KEY_UP:
313  break;
314  case KEY_DOWN:
316  break;
317  case KEY_LEFT:
319  break;
320  case KEY_RIGHT:
322  break;
323  case KEY_SET:
324 
325 
326  break;
327  case KEY_ERASE:
328  case KEY_DISPLAY:
329  break;
330  }
331  return 0;
332 }
static void load_laby ( int  num)
static

Definiert in Zeile 241 der Datei gui_snake.c.

241  {
242  int x,y;
243  for(x=0;x<RING_WIDTH;x++)
244  for(y=0;y<RING_HEIGHT;y++){
245  ring[x][y] = labyrinth[num][y][x];
246  if(ring[x][y] != ' '){
247  draw_element(x,y,wall,0);
248  }else{
250  screen_top + y * snake_screen_size,
251  screen_left + x * snake_screen_size+snake_screen_size-1,
252  screen_top + y * snake_screen_size+snake_screen_size-1, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
253  }
254  }
255 }
static void snake_generate_apple ( )
static

Definiert in Zeile 257 der Datei gui_snake.c.

258 {
259  int x,y,i;
260  while(1)
261  {
262  randomagain:
263  x = rand() % RING_WIDTH;
264  y = rand() % RING_HEIGHT;
265  if(ring[x][y] != ' ') continue;
266  i = snake_tail;
267  while(1){
268  if(snake[i][0] == x && snake[i][1] == y) goto randomagain;
269  if(i == snake_head) break;
270  i = (i+1)%SNAKE_MAX_LENGTH;
271  }
272  draw_element(x,y,apple,0);
273  ring[x][y] = 1;
274  break;
275  }
276 }
static void snake_start ( )
static

Definiert in Zeile 278 der Datei gui_snake.c.

279 {
280  int i;
281  load_laby(rand()%4);
282  snake_head = 2;
283  snake_tail = 0;
284  level = 5;
285  points = 0;
286  for(i=snake_tail;i<=snake_head;i++){
287  snake[i][0] = 2+i;
288  snake[i][1] = 2;
290  screen_top + snake[i][1] * snake_screen_size,
291  screen_left + snake[i][0] * snake_screen_size+snake_screen_size-1,
292  screen_top + snake[i][1] * snake_screen_size+snake_screen_size-1, MAKE_COLOR(COLOR_WHITE,COLOR_RED), RECT_BORDER1|DRAW_FILLED);
293  }
295  for(i=0;i<10;i++)
297 }

Variablen-Dokumentation

const char apple[8][8]
Initialisierung:
= {
" ",
" GGGB ",
"GGG B ",
" G RRR ",
" RRRRR ",
" RRRRR ",
" RRRRR ",
" RRR ",
}

Definiert in Zeile 188 der Datei gui_snake.c.

unsigned char direction

Definiert in Zeile 43 der Datei gui_snake.c.

const char head[8][8]
Initialisierung:
= {
" GG ",
" GBBG",
" G BG",
" GGGGGGG",
"RGGGGGGG",
" GGGGGGG",
" ",
" ",
}

Definiert in Zeile 155 der Datei gui_snake.c.

const unsigned char labyrinth[4][RING_HEIGHT][RING_WIDTH]

Definiert in Zeile 46 der Datei gui_snake.c.

unsigned char level

Definiert in Zeile 42 der Datei gui_snake.c.

unsigned int points

Definiert in Zeile 41 der Datei gui_snake.c.

unsigned char ring[RING_WIDTH][RING_HEIGHT]

Definiert in Zeile 37 der Datei gui_snake.c.

int screen_left

Definiert in Zeile 24 der Datei gui_snake.c.

int screen_top

Definiert in Zeile 25 der Datei gui_snake.c.

unsigned char snake[SNAKE_MAX_LENGTH][2]

Definiert in Zeile 40 der Datei gui_snake.c.

unsigned short snake_head

Definiert in Zeile 38 der Datei gui_snake.c.

int snake_scale

Definiert in Zeile 22 der Datei gui_snake.c.

int snake_screen_size

Definiert in Zeile 23 der Datei gui_snake.c.

unsigned short snake_tail

Definiert in Zeile 39 der Datei gui_snake.c.

char str_buf[40]

Definiert in Zeile 44 der Datei gui_snake.c.

const char tail[8][8]
Initialisierung:
= {
" ",
" ",
" ",
"G GGG GG",
"GGGGGGGG",
"GGG GGG ",
" ",
" ",
}

Definiert in Zeile 166 der Datei gui_snake.c.

const char tail2[8][8]
Initialisierung:
= {
" ",
" ",
" ",
" GG ",
"GGGG ",
"GGGGG ",
" GGG ",
" GG ",
}

Definiert in Zeile 177 der Datei gui_snake.c.

const char wall[8][8]
Initialisierung:
= {
"OOOOO OO",
"OOOOO OO",
" ",
"OO OOOOO",
"OO OOOOO",
" ",
"OOOOO OO",
"OOOOO OO",
}

Definiert in Zeile 199 der Datei gui_snake.c.