CHDK_DE Vorschauversion  Trunk Rev. 5209
 Alle Datenstrukturen Dateien Funktionen Variablen Typdefinitionen Aufzählungen Aufzählungswerte Makrodefinitionen
gui_snake.c-Dateireferenz
#include "camera_info.h"
#include "stdlib.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 31 der Datei gui_snake.c.

#define RING_WIDTH   40

Definiert in Zeile 30 der Datei gui_snake.c.

#define SNAKE_ELEMENT_SIZE   8

Definiert in Zeile 29 der Datei gui_snake.c.

#define SNAKE_MAX_LENGTH   0xF0

Definiert in Zeile 28 der Datei gui_snake.c.

Dokumentation der Aufzählungstypen

anonymous enum
Aufzählungswerte
DIR_LEFT 
DIR_DOWN 
DIR_UP 
DIR_RIGHT 

Definiert in Zeile 32 der Datei gui_snake.c.

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

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 471 der Datei gui_snake.c.

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

Definiert in Zeile 211 der Datei gui_snake.c.

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

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

Definiert in Zeile 309 der Datei gui_snake.c.

310 {
311  switch (kbd_get_autoclicked_key()) {
312  case KEY_UP:
314  break;
315  case KEY_DOWN:
317  break;
318  case KEY_LEFT:
320  break;
321  case KEY_RIGHT:
323  break;
324  case KEY_SET:
325 
326 
327  break;
328  case KEY_ERASE:
329  case KEY_DISPLAY:
330  break;
331  }
332  return 0;
333 }
static void load_laby ( int  num)
static

Definiert in Zeile 242 der Datei gui_snake.c.

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

Definiert in Zeile 258 der Datei gui_snake.c.

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

Definiert in Zeile 279 der Datei gui_snake.c.

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

Variablen-Dokumentation

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

Definiert in Zeile 189 der Datei gui_snake.c.

unsigned char direction

Definiert in Zeile 44 der Datei gui_snake.c.

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

Definiert in Zeile 156 der Datei gui_snake.c.

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

Definiert in Zeile 47 der Datei gui_snake.c.

unsigned char level

Definiert in Zeile 43 der Datei gui_snake.c.

unsigned int points

Definiert in Zeile 42 der Datei gui_snake.c.

unsigned char ring[RING_WIDTH][RING_HEIGHT]

Definiert in Zeile 38 der Datei gui_snake.c.

int screen_left

Definiert in Zeile 25 der Datei gui_snake.c.

int screen_top

Definiert in Zeile 26 der Datei gui_snake.c.

unsigned char snake[SNAKE_MAX_LENGTH][2]

Definiert in Zeile 41 der Datei gui_snake.c.

unsigned short snake_head

Definiert in Zeile 39 der Datei gui_snake.c.

int snake_scale

Definiert in Zeile 23 der Datei gui_snake.c.

int snake_screen_size

Definiert in Zeile 24 der Datei gui_snake.c.

unsigned short snake_tail

Definiert in Zeile 40 der Datei gui_snake.c.

char str_buf[40]

Definiert in Zeile 45 der Datei gui_snake.c.

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

Definiert in Zeile 167 der Datei gui_snake.c.

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

Definiert in Zeile 178 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 200 der Datei gui_snake.c.