以下**片段就是整個源**
**主要是github上大佬寫的,附上位址(
我又在這基礎上優化了點,釋放漏釋放的記憶體,禁止蛇反向運動
#include #include #include #include #include #include #define src_width 70
#define src_height 20
window *g_mainwin;
int g_oldcur, g_score = 0, g_width, g_height;
typedef struct
pos;
pos fruit;
bool *spaces;
// queue stuff
struct s_node
*front = null,*back =null;
typedef struct s_node node;
pos * peek()
node * dequeue()
//queue a position at the back
void enqueue(pos position)
}//end queue stuff
//start snake stuff
void snake_write_text(int y,int x,char *str)
//draw the borders
void snake_draw_board()
for (i = 0; i < g_width;i++)
snake_write_text(g_height + 1, 2, "score:");
}//resets the terminal window and clears up the mem
void snake_game_over()
endwin();
exit(0);
}//當前位置是否在界限裡面
bool snake_in_bounds(pos position)
//2d座標對映1d
int snake_cooridinate_to_index(pos position)
//1d對映對應的座標
pos snake_index_to_coordinate(int index);}
//隨機出現水果
void snake_draw_fruit()
while (spaces[idx] || !snake_in_bounds(fruit));
attron(a_reverse);
snake_write_text(fruit.y, fruit.x, " ");
attroff(a_reverse);
}//handles moving the snake for each iteration
bool snake_move_player(pos head)
; snake_game_over();
}spaces[idx] = true;
enqueue(head);
//check if we're eating the fruit
if(head.x == fruit.x && head.y == fruit.y)
else
attron(a_reverse);
snake_write_text(head.y, head.x, " ");
attroff(a_reverse);
char buffer[25];
sprintf(buffer, "%d", g_score);
attrset(color_pair(2));
snake_write_text(g_height+1,9,buffer);
}void check_move_pos(pos *t_head,pos *head)
}else if(t_head->y == head->y)
} }}}
}int main(int argc,char *ar**)
srand(time(null));
noecho();
curs_set(2);
halfdelay(1);
keypad(g_mainwin, true);
g_oldcur = curs_set(0);
start_color( );
init_pair( 1, color_red, color_black );
init_pair( 2, color_green, color_black );
init_pair( 3, color_yellow, color_black );
init_pair( 4, color_blue, color_black );
init_pair( 5, color_cyan, color_black );
init_pair( 6, color_magenta, color_black );
init_pair( 7, color_white, color_black );
getmaxyx( g_mainwin, g_height, g_width );
g_width = g_width < src_width ? g_width : src_width;
g_height = g_height < src_height ? g_height : src_height;
// set up the 2d array of all spaces
spaces = (bool*) malloc( sizeof( bool ) * g_height * g_width );
snake_draw_board( );
snake_draw_fruit( );
pos head = ;
enqueue( head );
// event loop
while( 1 )
//禁止反向
check_move_pos(&t_head, &head);
if (!snake_in_bounds(head))
snake_game_over();
else
snake_move_player(head);
}snake_game_over();
}
C語言遊戲之貪吃蛇 鍊錶實現
早自習時突然想到怎麼寫貪吃蛇,話不多說,如下 開發環境 vs2015 雖然開始還是出了很多指標問題。很煩c語言指標 除錯了很久也大概是可以暢快的玩了。c語言新手,有很多寫得不好的地方望大神提出 當然,我也不介意把我 拿去學習 這樣就說明我的 有學習價值 標頭檔案 snake.h by liu yuc...
C語言構建的鍊錶貪吃蛇
借助遊戲內容分析貪吃蛇所需的功能主要包括這幾塊 移動游標模組 列印地圖模組和基本規則資訊 讀取最高分檔案列印初始蛇模組 列印時給予蛇的初始移動方向產生食物模組 1 保證食物在地圖內產生 2 保證食物不能出現在蛇體 蛇的生命狀態判斷模組 1 撞牆導致死亡 2 頭撞身體部位死亡 6.執行模組 1 讓蛇移...
C語言貪吃蛇
include include include include include include define screen width 40 遊戲螢幕寬度 define screen length 15 遊戲螢幕長度 define start x 16 螢幕起始x座標 define start y ...