创建一个空项目
源文件中添加一个新建项,在创建的新建项里编写代码
主函数
int main()
{
MOUSEMSG m;
initgraph(WIN_WIDTH, WIN_HEIGHT,SHOWCONSOLE);
//设置面板颜色
setbkcolor(RGB(280, 240, 200));
//刷新窗口
cleardevice();
setfillcolor(RED);
fillcircle(290, 210, 50);
settextstyle(20, 0, "微软黑体");
setbkmode(TRANSPARENT);
outtextxy(250, 200, "开始游戏");
settextstyle(20, 0, "微软黑体");
setbkmode(TRANSPARENT);
outtextxy(220, 280, "空格键暂停/继续");
int x = 290;
int y = 210;
while (1) {
m = GetMouseMsg(); //监听鼠标消息
if ( sqrt((double)((m.x - x) * (m.x - x) + (m.y - y) * (m.y - y))) < 50) {
setlinecolor(BLUE);
circle(290, 210, 60);
if (m.uMsg == WM_LBUTTONDOWN) { //如果按下鼠标左键实现相应功能.
cleardevice();
GameInit();
int n = 0;
while (1) {
GameMove();
GameDraw();
//如果有按键才动
if (_kbhit()) {
GameControl();
GetFlagKey();
}
EatFood();
n = GameOver();
if (n) {
LastPage();
}
Sleep(80);
}
break;
}
}
else { //当鼠标不在目标位置就覆盖之前的边框.
setlinecolor(RGB(280, 240, 200));
circle(290, 210, 60);
}
}
system("pause");
return 0;
}
头文件
#include<stdio.h>
#include<graphics.h>
#include<conio.h> //_getch(); 的头文件
#include<math.h>
#include<cstdlib>
枚举
enum DIR {
UP=72,
DOWN=80,
LEFT=75,
RIGHT=77,
STOP=32,
ESC=27,
};
结构体
食物:
struct Food {
int x;
int y;
DWORD color;
bool flag; //表示是否被吃掉,是否要重新生成食物
}food;
位置信息:
struct Pos {
int x;
int y;
DWORD color;
};
蛇:
struct Snake {
int num; //当前节数
int dir; //方向
int score;
int size;
int speed; //移动速度
struct Pos coor[MAX_SNAKE];
}snake;
函数体
1.初始化食物和蛇
void GameInit() {
//设置随机数种子
srand(GetTickCount()); //用开机时间作为随机数种子
snake.num = 3; //初始蛇长
snake.dir = RIGHT;
snake.score = 0;
snake.size = 10;
snake.speed = 10;
snake.coor[2].x = 0;
snake.coor[2].y = 0;
snake.coor[2].color = RGB(rand() % 256, rand() % 256, rand() % 256); //随机生成三个数,用三原色表示
snake.coor[1].x = 10;
snake.coor[1].y = 0;
snake.coor[1].color = RGB(rand() % 256, rand() % 256, rand() % 256); //随机生成三个数,用三原色表示
snake.coor[0].x = 20;
snake.coor[0].y = 0;
snake.coor[0].color = RGB(rand() % 256, rand() % 256, rand() % 256); //随机生成三个数,用三原色表示
//初始化食物
food.x = rand() % (WIN_WIDTH/10)*10; //使食物摆放位置与蛇行走的路径在同一网格下
food.y = rand() % (WIN_HEIGHT/10)*10;
food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
food.flag = true;
}
2.绘制面板
void GameDraw() {
cleardevice();
for (int i = 0; i < snake.num; i++) {
//画矩形
setfillcolor(snake.coor[i].color); //设置填充颜色
fillrectangle(snake.coor[i].x , snake.coor[i].y, snake.coor[i].x + 10, snake.coor[i].y + 10);
}
//绘制食物
//solidcircle(food.x,food.y,5)
if (food.flag) {
setfillcolor(food.color);
solidellipse(food.x, food.y, food.x + 10, food.y + 10);
}
//绘制分数
char temp[20] = "";
sprintf(temp, "分数:%d",snake.score);
outtextxy(10, 10, temp);
}
3.移动蛇
void GameMove() {
//从蛇尾开始
for (int i =snake.num - 1; i > 0; i--) {
snake.coor[i].x = snake.coor[i - 1].x;
snake.coor[i].y = snake.coor[i - 1].y;
}
switch (snake.dir) {
case UP:
snake.coor[0].y -= snake.speed;
break;
case DOWN:
snake.coor[0].y += snake.speed;
break;
case LEFT:
snake.coor[0].x -= snake.speed;
break;
case RIGHT:
snake.coor[0].x += snake.speed;
break;
}
}
4.获取键盘输入
void GameControl() {
//获取键盘输入 _getch(); 不回显函数
char key = _getch();
switch (key) {
case UP:
if (snake.dir != DOWN) {
snake.dir = UP;
}
break;
case DOWN:
if (snake.dir != UP) {
snake.dir = DOWN;
}
break;
case LEFT:
if (snake.dir != RIGHT) {
snake.dir = LEFT;
}
break;
case RIGHT:
if (snake.dir != LEFT) {
snake.dir = RIGHT;
}
break;
case STOP:
flagKey = 1;
break;
case ESC:
flagKey = 2;
break;
}
printf("%d\n", key);
}
注:在函数体外面定义变量flagKey:
int flagKey = 0;
5.创造食物
void CreateFood() {
if (!food.flag) {
food.x = rand() % (WIN_WIDTH / 10) * 10; //使食物摆放位置与蛇行走的路径在同一网格下
food.y = rand() % (WIN_HEIGHT / 10) * 10;
food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
food.flag = true;
}
}
6.吃食物
void EatFood() {
if (food.flag && snake.coor[0].x == food.x && snake.coor[0].y == food.y) {
food.flag = false;
snake.num++;
snake.score += 10;
snake.coor[snake.num-1].color=RGB(rand() % 256, rand() % 256, rand() % 256);
CreateFood();
}
}
7.游戏结束
int GameOver() {
//碰墙
if (snake.coor[0].x<0 || snake.coor[0].x>WIN_WIDTH || snake.coor[0].y<0 || snake.coor[0].y>WIN_HEIGHT) {
return 1;
}
//吃到自己
for (int i = 1; i < snake.num - 1; i++) {
if (snake.coor[0].x == snake.coor[i].x && snake.coor[0].y == snake.coor[i].y) {
return 1;
}
}
return 0;
}
8.记录分数
void GetScore() {
settextcolor(BLACK);
settextstyle(30, 0, "宋体");
char temp[20] = "";
sprintf(temp, "分数:%d", snake.score);
outtextxy(250, 250, temp);
}
9.对于游戏结束页面
void LastPage() {
cleardevice();
settextcolor(BLACK);
settextstyle(50, 0, "宋体");
outtextxy(200, 200, "Game Over");
GetScore();
settextstyle(20, 0, "微软黑体");
setbkmode(TRANSPARENT);
outtextxy(245, 290, "按任意键退出");
system("pause");
if (_kbhit) {
exit(0);
closegraph();
}
}
10.暂停/继续、退出游戏
void GetFlagKey(){
if (flagKey == 1)
{
setfillcolor(RED);
fillcircle(290, 210, 50);
settextstyle(20, 0, "微软黑体");
setbkmode(TRANSPARENT);
outtextxy(270, 200, "暂停");
flagKey = 0;
system("pause");
}
else if (flagKey == 2)
{
flagKey = 0;
LastPage();
}
}
成果展示