結構體鍊錶,動態分配記憶體,鍵盤輸入檢測,設定游標。
1,涉及多個結構體,分別定義蛇,方向,結點,位置座標,遊戲
2,結點串聯形成鍊錶,遍歷獲取成員座標,列印符號得到蛇身。
3,不斷的加頭,去尾,重新遍歷座標,再列印形成蛇的移動。
4,食物產生的位置判定,不能越界,也不能與蛇身體重合。
5,蛇的轉向判定,一條規則,不允許倒退。
6,轉向的實現,跟行進方向決定新的關節座標(當前頭的上下左右)
7,死亡檢測,是否頭節點座標是否與牆壁重合,是否與身體其他關節重合。
8,加速減速,設定重新整理休眠時間實現。
9,可以設定游標,就能實現制定位置列印制定符號。
結構體標頭檔案
#pragma once
typedef enum direction
direction;
typedef struct position
position;
typedef struct node
node;
typedef struct snake
snake;
typedef struct game
game;
#pragma once
#include "model.h"
void setpos(int x, int y);
void displaywall(int width, int height);
void displaysnake(const snake *psnake);
void displayfood(const position *pfood);
void cleansnakenode(const position *ppos);
void displaysnakenode(const position *ppos);
void gamedescription1();
void gamedescription2();
#include "view.h"
#include "model.h"
#include #include void viewinit()
//設定游標位置
void setpos(int x, int y)
; setconsolecursorposition(houtput, coord);
}static void translatesetpos(int x, int y)
void displaywall(int width, int height)
// 下
setpos(0, height + 1);
for (int i = 0; i < width + 2; i++)
// 左
for (int i = 0; i < height + 2; i++)
// 右
for (int i = 0; i < height + 2; i++) }
void displaysnake(const snake *psnake)
}void displayfood(const position *pfood)
void cleansnakenode(const position *ppos)
void displaysnakenode(const position *ppos)
//遊戲說明介面
void gamedescription1()
void gamedescription2()
#include "model.h"
#include "view.h"
#include #include #include #include #include void snakeinit(snake *psnake)
node->next = psnake->tail;
psnake->tail = node;
} psnake->direction = left;
}bool isoverlap(position pos,snake *psnake)
} return false;
}//生成食物
position generatefood(int width, int height, snake *psnake)
while (isoverlap(pos, psnake));
displayfood(&pos);
return pos;
}//初始化game
void gameinit(game *pgame)
//得到蛇即將要走的座標
position getnextposition(const snake *psnake)
return next;
}//蛇頭上加一格
void addhead(snake *psnake, position nextpos)
//蛇尾減一格
void removetail(snake *psnake)
//判斷蛇能不能吃到食物
bool iseat(position food, position next)
//判斷蛇是否撞牆
bool killedbywall(const snake *psnake,int width,int height)
return true;
}//判斷蛇是否撞到自己
bool killedbyself(const snake *psnake,position nextpos)
} return false;
}//判斷死沒死
bool gameover(const snake *psnake,position nextpos,int width,int height)
if (killedbyself(psnake,nextpos))
else
}//暫停遊戲
void pausegame() }}
void gamerun()
else if (game.snake.direction != up&&getasynckeystate(vk_down))
else if (game.snake.direction != right&&getasynckeystate(vk_left))
else if (game.snake.direction != left&&getasynckeystate(vk_right))
else if (getasynckeystate(vk_f1)) //加速
}} else if (getasynckeystate(vk_f2)) //減速
}} else if (getasynckeystate(vk_space))
else if (getasynckeystate(vk_escape))
position nextpos = getnextposition(&game.snake);
if (iseat(game.food,nextpos))
else
if (gameover(&game.snake,nextpos,game.width,game.height))
sleep(sleeptime); }
}//測試控制游標的位置
void demosetpos()
}int main()
C語言貪吃蛇
include include include include include include define screen width 40 遊戲螢幕寬度 define screen length 15 遊戲螢幕長度 define start x 16 螢幕起始x座標 define start y ...
C語言 貪吃蛇
include include include define framewidght 22 define frameheight 22 short generaterandnumber short a,short b void posconsolecursor short x,short y voi...
C語言貪吃蛇
親測devc 編譯器完美執行 define crt secure no warnings include include include include void readini file fphead,int score,char argv 建立或開啟乙個和執行檔案對應的ini檔案,讀取最高紀錄 ...