/* ======================================= */
Update: 20170713
新版贪吃蛇:传送门
/* ======================================= */
最近闲来无事用C语言写了个贪吃蛇解解闷, 其实这玩意儿也不难写, 其一就是如何绘制蛇和背景, 其二就是怎么把用户的输入实现成蛇的转向. 我这里使用了curses.h和pthread.h库, 前者是在Cmdline下绘制图形, 后者是多线程库.
当蛇撞到边界或者自己身体里, 画面就会停住, 表示游戏结束, 这时按任意键就会回到主菜单.
注意,本游戏在窗口模式下正常运行,全屏可能会有BUG。
编译:
g++ main.c -lcurses -pthread
#include <iostream>
#include <cstdio>
#include <cstring>
#include <curses.h>//graph lib
#include <unistd.h>//usleep
#include <pthread.h>// mul process
#include <time.h>
#include <cstdlib>
const int DMENU=1;//the manu status
const int DGAME=2;//start game
const int MENU_START=1;
const int MENU_EXIT=2;
const int MENU_ABOUT=3;
const int HEAD_LEFT=1;
const int HEAD_DOWN=2;
const int HEAD_UP=3;
const int HEAD_RIGHT=4;
using namespace std;
struct curPoint
{
int x, y;
};
struct MainSnack
{
curPoint body[10000] ;
int len, arrowHead, score, dead;
};
char StrMenuOption[10][255];
int MenuTotalLine;
int MenuChosed;
pthread_t Pid;
MainSnack Ms;
void xcInit();//Initial for the program