#include#include #include #include //C using namespace std; bool gameOver;//布尔判断游戏是否结束 const int width = 40;//画板40*20 const int height = 20; int x, y, fruitx, fruity, score; int tailX[100], tailY[100]; int nTail; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir;//枚举上下左右 void Setup()//游戏初始化 { gameOver = false; dir = STOP; x = width / 2;//小蛇出生位置坐标 y = height / 2; fruitx = rand() % width;//随机生成水果坐标 fruity = rand() % height; score = 0; } void Draw()//画布 { system("cls");//Linux用"clear"cpu刷新 cout << " 贪吃蛇小游戏 游戏规则" << endl; cout << " 使用小键盘↑↓←→控制小蛇吃果子(F),"< width || x<0 || y>height || y < 0)//不允许穿墙 { int x; gameOver = true; cout << "gameover!a"; x = MessageBox(GetForegroundWindow(), "您撞墙啦,游戏结束!", "gameover!", 1); } for (int i = 0; i < nTail; i++)//不允许吃自己 if (tailX[i] == x && tailY[i] == y) { int x; gameOver = true; cout << "gameover!a"; x = MessageBox(GetForegroundWindow(), "您吃到自己啦,游戏结束!", "gameover!", 1); } if (x == fruitx && y == fruity)//水果被吃后重新生成,并且加十分 { score += 10; fruitx = rand() % width; fruity = rand() % height; nTail++; } } int main(int argc, char **argv) { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(20); Sleep(20);//调节小蛇速度 } return 0; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)