【vc++精品源码栏目提醒】:网学会员,鉴于大家对vc++精品源码十分关注,论文会员在此为大家搜集整理了“C++语言贪吃蛇游戏源代码 - 其它资料”一文,供大家参考学习!
/Author : 柳印奇email : liuyinqi2012sina.comDescription : 蛇年来临创作一个与蛇有关的经典游戏《贪吃蛇》,与同学们分享蛇年美好的时光。
欢迎交流与探讨,直接将代码粘贴到 VC6.0 的环境下即可运行。
← 左转→ 右转↓ 向下↑ 向上积分达到一定程度,会有换命的活动,命最多 6 条。
难度会随积分的上升逐渐上升,最多到 6 的难度。
/includeincludeincludeincludepragma commentlib winmm.libusing namespace stddefine GameW 20define GameH 11const int CtrlLeft GameW24 3struct Point Point Point operatorconst Point rhs _x rhs._x _y rhs._y return this Pointint x int y _x x _y y int _x _yHANDLE g_hOutput GetStdHandleSTD_OUTPUT_HANDLE nHANDLE g_hI put GetStdHandleSTD_INPUT_HANDLEPoint g_ptCursor00BOOL isChecking FALSEBOOL g_bGameOver FALSEBOOL bCreateEgg FALSEint g_nGameBackGameHGameWint nowKeyInfo -1int g_nDiff 1int g_nLife 3int g_nScore 0void SetCursorCOORD cd SetConsoleCursorPositiong_hOutput cdvoid SetCursorint x int y COORD cd x y SetCursorcdvoid SetBlockCursorint x int y COORD cd 2x 2 y 1 SetCursorcdvoid SetBackint x int y BOOL bk SetBlockCursorx y if bk printfs ■ else printf bool Outint x int y return x 0 y 0 x GameW y GameHstruct Egg int x int y Point fangxiang Egg Eggint px int py x px y py fangxiang._x 1 fangxiang._y 0 Eggconst Egg rhs x rhs.x y rhs.y fangxiang rhs.fangxiang Egg operatorconst Egg rhs x rhs.x y rhs.y fangxiang rhs.fangxiang return this void DrawMe if Outxy SetBackx y TRUE void eraseMe if Outxy SetBackx y FALSE Egg eggclass Snake public: vector List Snake SnakeEgg eg List.push_backeg void move int i eraseMe for i List.size - 1 i 0 i-- Listi Listi-1 List0.x List0.fangxiang._x List0.y List0.fangxiang._y ifEategg bCreateEgg FALSE g_nScore Check DrawMe void changFXint dx int dy Beep1200050 ifList0.fangxiang._x -dx List0.fangxiang._y -dy List0.fangxiang._x dx List0.fangxiang._y dy return List0.fangxiang._x dx List0.fangxiang._y dy move int EatEgg eg int m List.size-1 ifList0.x eg.x List0.y eg.y eg.x Listm.x - Listm.fangxiang._x eg.y Listm.y - Listm.fangxiang._y List.push_backeg return 1 return 0 void DrawMe int i for i 0 i List.size i Listi.DrawMe void eraseMe int i for i 0 i List.size i Listi.eraseMe void eraseAll int ij for i 0 i GameH i forj 0 j GameW j SetBackj i FALSE void resetSnakeint n 3 int x 3 int y 5 List.clear g_nDiff 1 g_nScore 0 int i for i 0i n i Egg ax-i y List.push_backa DrawMeint deal int i int eatMe 0 for i 1 i g_nDiff g_nDiff 10 ifg_nDiff 50 g_nLife1 ifg_nLife 0 i-- forj GameW-1 j 0 j-- SetBackj i FALSE Sleep10 g_nGameBackij 0 else g_bGameOver TRUE void DrawFrameint x int y int nWidth int nHeight int i fori 0 i nWidth i SetCursorx 2i 2 y printfs 一 SetCursorx 2i 2 y nHeight1 printfs ┄ fori 0 i nHeight i SetCursorx y i 1 printfs ┆ SetCursorx nWidth22 y i 1 printfs ┆ SetCursorx y printfs ┌ SetCursorx y nHeight1 printfs └ SetCursorx nWidth22 y printfs ┐ SetCursorx nWidth22 y nHeight1 printfs ┘void GameInit CONSOLE_CURSOR_INFO cursor_info cursor_info.bVisible FALSE cursor_info.dwSize 100 SetConsoleCursorI