大二下的游戏程序设计第一个小作业,以此篇记录一下,感谢李仕老师的循循善诱和同学们的热情讨论。
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
#define LeftBorder 3;
#define TopBorder 3;
bool gameOver,Died; //游戏结束否
bool gamePause,fruitFlash; //游戏暂停否
const int width = 50; //游戏窗口宽度
const int height = 20; //游戏窗口高度
int x, y, fruitX, fruitY; //蛇头坐标;果子坐标
int score; //得分
int level;
int tailX[100], tailY[100], nTail;//
enum eDirection {
STOP = 0, LEFT, RIGHT, UP, DOWN }; //方向枚举
eDirection dir; //方向变量
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
//游戏初始化
void Initial()
{
gameOver = false; //游戏未结束
dir = STOP; //初始方向为停止
x = width / 2;
y = height / 2; //当前蛇头初始位置:区域中心
tailX[0] = x;
tailY[0] = y;
fruitX = (rand() % width) + 1;
fruitY = (rand() % height) + 1; //花的位置:随机产生
score = 0; //初始得分为0
nTail = 1; //
level = 1;
gamePause = false;
fruitFlash = false;
Died = false;
CONSOLE_CURSOR_INFO cci;
cci.bVisible = 0;
cci.dwSize = 1;
SetConsoleCursorInfo(h, &cci);
}
void setPos(int cursorX, int cursorY)
{
COORD pos;
pos.X = cursorX + LeftBorder;
pos.Y = cursorY + TopBorder;
SetConsoleCursorPosition(h, pos);
}
void DrawMap()
{
system("CLS")<