CHDK_DE Vorschauversion  Trunk Rev. 5163
 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   42
 
#define RING_HEIGHT   24
 

Aufzählungen

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

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 game_over ()
 
static void snake_start ()
 
int gui_snake_init ()
 
int basic_module_init ()
 

Variablen

gui_handler GUI_MODE_SNAKE
 
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 26 der Datei gui_snake.c.

#define RING_WIDTH   42

Definiert in Zeile 25 der Datei gui_snake.c.

#define SNAKE_ELEMENT_SIZE   8

Definiert in Zeile 24 der Datei gui_snake.c.

#define SNAKE_MAX_LENGTH   0xF0

Definiert in Zeile 23 der Datei gui_snake.c.

Dokumentation der Aufzählungstypen

anonymous enum
Aufzählungswerte
DIR_LEFT 
DIR_RIGHT 
DIR_UP 
DIR_DOWN 

Definiert in Zeile 27 der Datei gui_snake.c.

27  {
28  DIR_LEFT = 0,
29  DIR_RIGHT,
30  DIR_UP,
31  DIR_DOWN
32 };

Dokumentation der Funktionen

int basic_module_init ( )

Definiert in Zeile 431 der Datei gui_snake.c.

431  {
432  return gui_snake_init();
433 }
static void draw_element ( int  x,
int  y,
const char  element[8][8],
char  angle 
)
static

Definiert in Zeile 206 der Datei gui_snake.c.

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

328  {
329  unsigned short new_head;
330  unsigned short new_tail;
331  unsigned short new_cord;
332  char game_over_flag = 0,draw_head,clear_tail,draw_points=1;
333  unsigned char prevdir = direction;
334  unsigned int i;
335  new_head = (snake_head+1)%SNAKE_MAX_LENGTH;
336  new_tail = (snake_tail+1)%SNAKE_MAX_LENGTH;
337  draw_head = 1;
338  clear_tail = 1;
339  if(direction == DIR_RIGHT){
340  new_cord = (snake[snake_head][0]+1)%RING_WIDTH;
341  snake[new_head][0] = new_cord;
342  snake[new_head][1] = snake[snake_head][1];
343  }
344  if(direction == DIR_LEFT){
345  new_cord = snake[snake_head][0]-1;
346  if(new_cord == 0xFFFF) new_cord = RING_WIDTH-1;
347  snake[new_head][0] = new_cord;
348  snake[new_head][1] = snake[snake_head][1];
349  }
350  if(direction == DIR_UP){
351  new_cord = snake[snake_head][1]-1;
352  if(new_cord == 0xFFFF) new_cord = RING_HEIGHT-1;
353  snake[new_head][0] = snake[snake_head][0];
354  snake[new_head][1] = new_cord;
355  }
356  if(direction == DIR_DOWN){
357  new_cord = (snake[snake_head][1]+1)%RING_HEIGHT;
358  snake[new_head][0] = snake[snake_head][0];
359  snake[new_head][1] = new_cord;
360  }
361 
362  i = snake_tail;
363  while(1){
364  if(snake[i][0] == snake[new_head][0] && snake[i][1] == snake[new_head][1]){
365  game_over_flag = 1;
366  break;
367  }
368  if(i == snake_head) break;
369  i = (i+1)%SNAKE_MAX_LENGTH;
370  }
371 
372  if(ring[snake[new_head][0]][snake[new_head][1]] == 1){
373  ring[snake[new_head][0]][snake[new_head][1]] = ' ';
375  points+=level;
376  draw_points = 1;
377  if((new_head+1)%SNAKE_MAX_LENGTH != snake_tail){
378  new_tail = snake_tail;
379  clear_tail = 0;
380  }
381  }else if(ring[snake[new_head][0]][snake[new_head][1]] != ' '){
382  game_over_flag = 1;
383  }
384  if(draw_head){
385  if(direction == DIR_UP) draw_element(snake[new_head][0],snake[new_head][1],head,2); //up
386  if(direction == DIR_LEFT) draw_element(snake[new_head][0],snake[new_head][1],head,0); //left
387  if(direction == DIR_DOWN) draw_element(snake[new_head][0],snake[new_head][1],head,1); //down
388  if(direction == DIR_RIGHT) draw_element(snake[new_head][0],snake[new_head][1],head,3); //right
389 
390  new_cord = snake_head-1;
391  if(new_cord == 0xFFFF) new_cord = SNAKE_MAX_LENGTH-1;
392  if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_UP) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,2);
393  else if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_LEFT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,0);
394  else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_UP) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,4);
395  else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] > snake[new_head][1] && direction == DIR_RIGHT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,3);
396  else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_RIGHT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,2);
397  else if(snake[new_cord][0] < snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_DOWN) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,0);
398  else if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_LEFT) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,4);
399  else if(snake[new_cord][0] > snake[new_head][0] && snake[new_cord][1] < snake[new_head][1] && direction == DIR_DOWN) draw_element(snake[snake_head][0],snake[snake_head][1],tail2,3);
400  else if(prevdir == direction && (direction == DIR_RIGHT || direction == DIR_LEFT)) draw_element(snake[snake_head][0],snake[snake_head][1],tail,0);
401  else if(prevdir == direction && (direction == DIR_UP || direction == DIR_DOWN)) draw_element(snake[snake_head][0],snake[snake_head][1],tail,1);
402  }
403  prevdir = direction;
404  if(clear_tail){
406  snake[snake_tail][1] * SNAKE_ELEMENT_SIZE,
407  camera_screen.disp_left + snake[snake_tail][0] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1,
408  snake[snake_tail][1] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
409  }
410  snake_head = new_head;
411  snake_tail = new_tail;
412  msleep(100);
413  if(game_over_flag){
414  game_over();
415  game_over_flag = 0;
416  }
417  if(draw_points){
418  draw_points = 0;
421  }
422 }
int gui_snake_init ( )
int gui_snake_kbd_process ( )

Definiert in Zeile 303 der Datei gui_snake.c.

303  {
304  switch (kbd_get_autoclicked_key()) {
305  case KEY_UP:
307  break;
308  case KEY_DOWN:
310  break;
311  case KEY_LEFT:
313  break;
314  case KEY_RIGHT:
316  break;
317  case KEY_SET:
318 
319 
320  break;
321  case KEY_ERASE:
322  case KEY_DISPLAY:
323  break;
324  }
325  return 0;
326 }
static void load_laby ( int  num)
static

Definiert in Zeile 235 der Datei gui_snake.c.

235  {
236  int x,y;
237  for(x=0;x<RING_WIDTH;x++)
238  for(y=0;y<RING_HEIGHT;y++){
239  ring[x][y] = labyrinth[num][y][x];
240  if(ring[x][y] != ' '){
241  draw_element(x,y,wall,0);
242  }else{
244  y * SNAKE_ELEMENT_SIZE,
245  camera_screen.disp_left + x * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1,
246  y * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1, MAKE_COLOR(COLOR_WHITE,COLOR_WHITE), RECT_BORDER0|DRAW_FILLED);
247  }
248  }
249 }
static void snake_generate_apple ( )
static

Definiert in Zeile 251 der Datei gui_snake.c.

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

Definiert in Zeile 274 der Datei gui_snake.c.

274  {
275  int i;
276  load_laby(rand()%4);
277  snake_head = 3;
278  snake_tail = 0;
279  level = 5;
280  points = 0;
281  for(i=snake_tail;i<snake_head;i++){
282  snake[i][0] = 2+i;
283  snake[i][1] = 2;
285  snake[i][1] * SNAKE_ELEMENT_SIZE,
286  camera_screen.disp_left + snake[i][0] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1,
287  snake[i][1] * SNAKE_ELEMENT_SIZE+SNAKE_ELEMENT_SIZE-1, MAKE_COLOR(COLOR_WHITE,COLOR_RED), RECT_BORDER1|DRAW_FILLED);
288  }
290  for(i=0;i<10;i++)
292 }

Variablen-Dokumentation

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

Definiert in Zeile 184 der Datei gui_snake.c.

unsigned char direction

Definiert in Zeile 39 der Datei gui_snake.c.

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

Definiert in Zeile 151 der Datei gui_snake.c.

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

Definiert in Zeile 42 der Datei gui_snake.c.

unsigned char level

Definiert in Zeile 38 der Datei gui_snake.c.

unsigned int points

Definiert in Zeile 37 der Datei gui_snake.c.

unsigned char ring[RING_WIDTH][RING_HEIGHT]

Definiert in Zeile 33 der Datei gui_snake.c.

unsigned char snake[SNAKE_MAX_LENGTH][2]

Definiert in Zeile 36 der Datei gui_snake.c.

unsigned short snake_head

Definiert in Zeile 34 der Datei gui_snake.c.

unsigned short snake_tail

Definiert in Zeile 35 der Datei gui_snake.c.

char str_buf[40]

Definiert in Zeile 40 der Datei gui_snake.c.

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

Definiert in Zeile 162 der Datei gui_snake.c.

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

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