小遊戲 貪吃蛇 基礎版

2021-08-26 20:38:00 字數 4177 閱讀 4709

#ifndef  __snake_h__

#define __snake_h__

#include

#include

#include

#include

enum direction

;enum gamestatus

;#define red "/033[0;32;31m"

#define blue "/033[0;32;34m"

#define wall "■"

#define food "●"

#define flower "*"

#define init_x 10

#define init_y 10

typedef struct snakenode

snakenode, *psnakenode;

typedef struct snake

snake, *psnake;

void gamestart(psnake ps);//開始遊戲

void welcometogame();//歡迎介面

void createmap();//設定地圖

void initsnake(psnake ps);//初始化蛇

void createfood(psnake ps);//放置食物

void pause();//暫停遊戲

void snakemove(psnake ps);//控制蛇的移動

int nexthasfood(psnakenode next,psnake ps);//判斷下一步是否有食物

void eatfood(psnakenode next,psnake ps);//吃掉食物

void nofood(psnakenode next,psnake ps);//沒有食物時蛇的移動

void gamerun(psnake ps);//遊戲繼續

void gameend(psnake ps);//結束遊戲

#endif //__snake_h__

#include

"snake.h"

#include

#include

#include

#include

#include

void setpos(int x,int y)

; handle houtput=

null;

console_cursor_info cursorinfo;

pos.x=x;

pos.y=y;

houtput=getstdhandle(std_output_handle);

setconsolecursorposition(houtput,pos);

getconsolecursorinfo(houtput, &cursorinfo);//獲取控制台游標資訊

cursorinfo.bvisible =

0; //隱藏控制台游標

setconsolecursorinfo(houtput, &cursorinfo);//設定控制台游標狀態

}void line()//開始前的問候語邊框

//下for(i=

20;i<=

80;i+=

2)

//左for(i=

9;i<=

22;i++)

//右for(i=

9;i<=

22;i++)

}void welcometogame()

void creatmap()

//下for(i=

0;i<=

58;i+=

2)

//左for(i=

1;i<=

27;i++)

//右for(i=

1;i<=

27;i++)

}psnakenode buynode()

pret->x=

0; pret->y=

0; pret->next=

null;

return pret;

}void initsnake(psnake ps)

while(cur)//列印蛇身

ps->_sleeptime=

500;//確定蛇的速度

ps->_totalscore=

0; ps->_status=ok;

ps->_dir=right;

ps->_addscore=

10; ps->_psnake=first;

}void creatfood(psnake ps)//建立食物

while(pfood->x%2!=

0);//座標在合法範圍內

while(cur)//避免在蛇身上

cur=cur->next;

}setpos(pfood->x,pfood->y);

printf(food);

ps->_pfood=pfood;

}void gamestart(psnake ps)

int nexthasfood(psnakenode next,psnake ps)//判斷next是否有食物

void eatfood(psnakenode next,psnake ps)

ps->_totalscore+=ps->_addscore;//吃掉食物加分

creatfood(ps);//再次放置食物

} void nofood(psnakenode next,psnake ps)

setpos(cur->next->x,cur->next->y);

printf(" ");

free(cur->next);

cur->next=

null;

}void snakemove(psnake ps)

case down:

case left:

case right:

}}void pause()//暫停遊戲

}int killbywall(psnake ps)

return0;}

int killbyitself(psnake ps)

pnext=pnext->next;

}return0;}

void printfhelp()

void gamerun(psnake ps)

else

if( getasynckeystate (vk_down) && ps->_dir!=up )

else

if( getasynckeystate (vk_left) && ps->_dir!=right )

else

if( getasynckeystate (vk_right) && ps->_dir!=left )

else

if( getasynckeystate (vk_space) )

else

if( getasynckeystate (vk_escape) )

else

if( getasynckeystate (vk_f2))

}else

if( getasynckeystate (vk_f1))

}sleep(ps->_sleeptime);

snakemove(ps); //讓蛇移動

if(killbywall(ps))

break;

if(killbyitself(ps))

break;

}while(ps->_status == ok);

}void gameend(psnake ps)

free(ps->_pfood);

ps->_pfood=

null;

break;

}case kill_by_self:

free(ps->_pfood);

ps->_pfood=

null;

break;}}

}

#include"snake.h"

#include

.h>

void test()

int main()

貪吃蛇小遊戲

1 doctype html 2 html 3 head 4 title snake title 5head 6 body style text align center margih 100px background color aaaaaa 7 canvas id canv width 400 ...

簡易版貪吃蛇小遊戲。

以上就是我測試的結果,能完成基本的功能。ifndef game h define gamae h include include include include define rows 20 define cols 20 define snake length 20 20 define sleep ...

c 貪吃蛇小遊戲(初始版)

用c 類 建立標頭檔案 include coordinate.h struct coord 座標 1 蛇的處理 建立標頭檔案 include snake.h pragma once 防止標頭檔案包含 include include 圖形庫函式 include include coordinate.h...