贪吃蛇之文字版、简易版
这个贪吃蛇主要分3部分(可参照main函数中的case):节点移动,节点映射和打印。简单易懂,当然,还很不完善 因为比较懒,所以直接include "*.c"了....
main.c
程序代码:
#include "stdlib.h" #include "stdio.h" #include "time.h" #include "me_unstd.c" #include "me.c" #define S 115 #define A 97 #define G 103 #define E 101 #define Q 113 #define ARRAY_ONE 25 #define ARRAY_TWO 80 #define NODE 640 #define FOOD NODE-1 char map [ARRAY_ONE][ARRAY_TWO] = {0}; struct pos_tag { int column; int row; }; struct pos_tag node [NODE] = {0}; int node_count = 0; void init (void); void node_move (int key); void node_map (void); void flush (void); void check (int key); void over (void); void change_food (void); void food_map (void); int main (void) { int key = 0; init (); flush (); while (1) { key = get_ch (); switch (key) { case S: check (S); node_move (S); node_map (); food_map (); flush (); break; case A: check (A); node_move (A); node_map (); food_map (); flush (); break; case G: check (G); node_move (G); node_map (); food_map (); flush (); break; case E: check (E); node_move (E); node_map (); food_map (); flush (); break; case Q: return 0; default: break; } } return 0; } void init (void) { make_map (ARRAY_ONE, ARRAY_TWO, map, ' '); node [0] = (struct pos_tag){9, 10}; node [FOOD] = (struct pos_tag){12, 40}; map [node[0].column][node[0].row] = '*'; map [node[FOOD].column][node[FOOD].row] = '*'; node_count = 1; } void node_move (int key) { for (int i=(node_count-1); i>0; i--) { node[i].column = node[i-1].column; node[i].row = node[i-1].row; } switch (key) { case S: node[0].column--; break; case A: node[0].column++; break; case G: node[0].row -= 2; break; case E: node[0].row += 2; break; } } void node_map (void) { make_map (ARRAY_ONE, ARRAY_TWO, map, ' '); for (int i=(node_count-1); i>=0; i--) { map[node[i].column][node[i].row] = '*'; } } void food_map (void) { map[node[FOOD].column][node[FOOD].row] = '*'; } void change_food (void) { node[FOOD].column = time (NULL) % 25; if (0 != (node[FOOD].row = time (NULL) % 80) % 2) { if (node[FOOD].row > 40) { node[FOOD].row--; } else { node[FOOD].row++; } } } void flush (void) { goto_xy (1, 1); print_map (ARRAY_ONE, ARRAY_TWO, map, "%c", ""); goto_xy (1, 1); } void check (int key) { switch (key) { case S: if ('*' == map[node[0].column-1][node[0].row]) { if (node[FOOD].column == node[0].column-1 && node[FOOD].row == node[0].row) { node_count++; change_food (); } else { over (); } } if (node[0].column <= 0) { over (); } break; case A: if ('*' == map[node[0].column+1][node[0].row]) { if (node[FOOD].column == node[0].column+1 && node[FOOD].row == node[0].row) { node_count++; change_food (); } else { over (); } } if (node[0].column >= ARRAY_ONE-1) { over (); } break; case G: if ('*' == map[node[0].column][node[0].row-2]) { if (node[FOOD].column == node[0].column && node[FOOD].row == node[0].row-2) { node_count++; change_food (); } else { over (); } } if (node[0].row <= 0) { over (); } break; case E: if ('*' == map[node[0].column][node[0].row+2]) { if (node[FOOD].column == node[0].column && node[FOOD].row == node[0].row+2) { node_count++; change_food (); } else { over (); } } if (node[0].row >= ARRAY_TWO-2) { printf ("%d", node[0].row); over (); } break; } } void over (void) { exit (0); }
me_unstd.c
程序代码:
void goto_xy (int x, int y) { _gotoxy (x, y); } int get_ch (void) { return _getch (); } void sleep (int sec) { _sleep (sec); }
me.c
程序代码:
void print_map (int one, int two, char map[one][two], char* format_two, char* format_one) { for (int i=0; i<one; i++) { for (int i_t=0; i_t<two; i_t++) { printf (format_two, map[i][i_t]); } printf (format_one); } } void make_map (int one, int two, char map[one][two], char element) { for (int i=0; i<one; i++) { for (int i_t=0; i_t<two; i_t++) { map [i][i_t] = element; } } }
[ 本帖最后由 pycansi 于 2013-10-30 23:12 编辑 ]