本文主要是介绍基于easyx库的C/C++游戏编程实例-贪吃蛇|,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
贪吃蛇|双人贪吃蛇游戏设计
1.设计物体结构
蛇结构:
struct SnakePlayer
{int size;int r;int dir;int speed;POINT pos[SNAKE_SIZE];bool live;int score;
}snake;
食物结构:
struct Food
{int x;int y;int r;bool flag;DWORD color;
}food,foodtest[FOOD_SIZE];
2.游戏初始化
void GameInit()
{//播放背景音乐mciSendString(_T("open ./res/Snake_BGM.mp3 alias BGM"), 0, 0, 0);mciSendString(_T("play BGM repeat"), 0, 0, 0);//创建一个窗口,控制窗口台是自动创建的,图形窗口是需要自己手动创建的(后加 SHOWCONSOLE)initgraph(WIN_WIDTH, WIN_HEIGHT);//加载开始界面loadimage(&bg, _T("./res/Snake_Background.jpg"));putimage(0, 0, &bg);//让随机数每次进入时都不同,只调用一次!!!srand(GetTickCount());//开始界面setbkmode(TRANSPARENT);//设置透明背景settextcolor(GREEN);settextstyle(60, 0, _T("黑体"));outtextxy(WIDTH / 2, HEIGHT / 2 - 70,_T("贪吃蛇" ));settextstyle(20, 0, _T("黑体"));outtextxy(WIDTH / 2 + 30, HEIGHT / 2 + 20, _T("按'C'开始游戏" ));while (!GetAsyncKeyState('C'));//蛇初始状态snake.size = SIZE_BEGIN;snake.r = R_BEGIN;snake.dir = RIGHT;snake.speed=SPEED_MIN;snake.live = true;snake.score = 0;for (int i = 0; i < snake.size; i++){snake.pos[i].x = X_BEGIN - 10 * i;snake.pos[i].y = Y_BEGIN;}//产生食物ProduceFood();
}
3.游戏界面加载
void GameDraw()
{BeginBatchDraw();setbkcolor(RGB(28, 115, 119));cleardevice();//是用当前背景色清空屏幕,并将当前点移至 (0, 0),EASYX自带//游戏外界面settextcolor(GREEN);settextstyle(50, 0, _T("黑体"));outtextxy(WIDTH + 10, 20, _T("贪吃蛇" ));settextstyle(20, 0, _T("黑体"));outtextxy(WIDTH + 10, 120, _T("开始/继续游戏——C"));outtextxy(WIDTH + 10, 150, _T("重新开始——M"));outtextxy(WIDTH + 10, 180, _T("暂停游戏——空格"));outtextxy(WIDTH + 10, 210,_T("上——W或↑"));outtextxy(WIDTH + 10, 240,_T("下——S或↓"));outtextxy(WIDTH + 10, 270, _T("左——A或←"));outtextxy(WIDTH + 10, 300,_T("右——D或→"));outtextxy(WIDTH + 10, 330, _T("加速——B"));//line(0, HEIGHT, WIDTH, HEIGHT);//画直线rectangle(0, 0, WIDTH, HEIGHT);//画矩形边框settextcolor(BLACK);settextstyle(20, 0, _T("黑体"));outtextxy(10, HEIGHT + 5,_T("当前分数:"));char t[SNAKE_SIZE];sprintf_s(t, "%d", snake.score);outtextxy(140, HEIGHT + 5, *t);for (int i = 0; i < snake.size; i++){setfillcolor(RED);solidcircle(snake.pos[i].x, snake.pos[i].y, snake.r);}if (food.flag){setfillcolor(food.color);solidcircle(food.x, food.y, food.r);}/*//测试用for (int j = 0; j < FOOD_SIZE; j++){if (foodtest[j].flag){setfillcolor(foodtest[j].color);solidcircle(foodtest[j].x, foodtest[j].y, foodtest[j].r);}}*/EndBatchDraw();
}
4.随机生成一个食物
void ProduceFood()
{food.r = rand() % 3 + 7;//随机生成食物位置,不能在蛇的身体上food.x = rand() % (WIDTH - 30) + food.r;food.y = rand() % (HEIGHT - 30) + food.r;while(1){int i = 0;for(; i < snake.size; i++){if (snake.pos[i].x + snake.r >= food.x - food.r && snake.pos[i].x - snake.r <= food.x + food.r&& snake.pos[i].y + snake.r >= food.y - food.r && snake.pos[i].y - snake.r <= food.y + food.r){food.x = rand() % (WIDTH - 30) + food.r;food.y = rand() % (HEIGHT - 30) + food.r;break;}}if (i == snake.size)break;}//随机生成食物颜色,不能与背景相同food.color = RGB(rand() % 256, rand() % 256, rand() % 256);while (food.color == RGB(28, 115, 119)){food.color = RGB(rand() % 256, rand() % 256, rand() % 256);}food.flag = true;
}
5.蛇吃食物机制
void EatFood()
{if (food.flag && snake.pos[0].x+snake.r > food.x - food.r && snake.pos[0].x - snake.r < food.x + food.r&& snake.pos[0].y + snake.r > food.y - food.r && snake.pos[0].y - snake.r < food.y + food.r){food.flag = false;snake.size++;snake.score++;}if (!food.flag){ProduceFood();}
}
6.控制蛇移动
蛇移动机制:
void SnakeMove()
{//蛇的身体移动for (int i = snake.size - 1; i > 0; i--){//Sleep(1);snake.pos[i] = snake.pos[i - 1];}//碰墙死亡机制switch (snake.dir){case UP:snake.pos[0].y -= snake.speed;break;case DOWN:snake.pos[0].y += snake.speed;break;case LEFT:snake.pos[0].x -= snake.speed;break;case RIGHT:snake.pos[0].x += snake.speed;break;}//穿墙所需机制/*switch (snake.dir){case UP:snake.pos[0].y -= speed;if (snake.pos[0].y < 0){snake.pos[0].y = HEIGHT;}break;case DOWN:snake.pos[0].y += speed;if (snake.pos[0].y > HEIGHT){snake.pos[0].y = 0;}break;case LEFT:snake.pos[0].x -= speed;if (snake.pos[0].x < 0){snake.pos[0].x = WIDTH;}break;case RIGHT:snake.pos[0].x += speed;if (snake.pos[0].x > WIDTH){snake.pos[0].x = 0;}break;}*/
}
按键控制:
void KeyCtrl()
{//Windows函数, 非阻塞//按键大写可检测到大小写,小写都检测不到//按键需要顺序+else if,这样目前检测连续按不能往回走if (GetAsyncKeyState(VK_UP) || GetAsyncKeyState('W')){if (snake.dir != DOWN){snake.dir = UP;}}else if (GetAsyncKeyState(VK_LEFT) || GetAsyncKeyState('A')){if (snake.dir != RIGHT){snake.dir = LEFT;}}else if (GetAsyncKeyState(VK_DOWN) || GetAsyncKeyState('S')){if (snake.dir != UP){snake.dir = DOWN;}}else if (GetAsyncKeyState(VK_RIGHT) || GetAsyncKeyState('D')){if (snake.dir != LEFT){snake.dir = RIGHT;}}if (GetAsyncKeyState('B')){snake.speed += SPEED_GAP;if (snake.speed>= SPEED_MAX){snake.speed = SPEED_MIN;}}if (GetAsyncKeyState(VK_SPACE)){while (!GetAsyncKeyState('C'));}
}
7.蛇碰撞死亡机制
bool Bump()
{//碰身体死亡for (int i = 4; i < snake.size; i++) //最小情况为4{if (snake.pos[0].x == snake.pos[i].x && snake.pos[0].y == snake.pos[i].y){//snake.live = false;return false;}}//碰墙死亡if (snake.dir == LEFT && snake.pos[0].x - snake.r<0 || snake.dir == RIGHT && snake.pos[0].x + snake.r>WIDTH|| snake.dir == UP && snake.pos[0].y - snake.r<0 || snake.dir == DOWN && snake.pos[0].y + snake.r>HEIGHT){//snake.live = false;return false;}return true; // 默认true,可不写
}
8.执行
int main()
{while (1){GameInit();while (1){//Producefoodtest();GameDraw();EatFood();KeyCtrl();SnakeMove();FlushBatchDraw();Sleep(100); //可以让速度更慢if (!Bump() || GetAsyncKeyState('M'))break;}//播放死亡时的背景音乐mciSendString(_T("close BGM"), 0, 0, 0);mciSendString(_T("open ./res/Snake_Dead.mp3"), 0, 0, 0);mciSendString(_T("play ./res/Snake_Dead.mp3"), 0, 0, 0);settextcolor(RED);settextstyle(40, 0, _T("黑体"));outtextxy(WIDTH + 7, 400, _T("GAME OVER!"));settextstyle(20, 0, _T("黑体"));outtextxy(WIDTH + 10, 450, _T("获得总分:"));settextstyle(30, 0, _T("黑体"));char t[SNAKE_SIZE];sprintf_s(t, "%d", snake.score);outtextxy(WIDTH + 140, 445, *t);while (!GetAsyncKeyState('M'));//停住播放。不能和play相邻,它会立即关闭,因为程序执行太快!mciSendString(_T("close ./res/Snake_Dead.mp3"), 0, 0, 0);}return 0;
}
结果演示视频:
可见,我还对游戏画面进行了优化,添加了文字描述,画面较为美观。
基于此设计,我们可以衍生出双人贪吃蛇的游戏设计,这只不过是在贪吃蛇设计上添加一个蛇,再修改一下碰撞机制,比如说双方碰撞检测:
for (int k = 1; k < snake[i].size; k++){if (i + 1 < SNAKE_NUM&&snake[i].live && snake[i+1].live){if (snake[i].pos[0].x - snake[i].r < snake[i + 1].pos[k].x + snake[i].r && snake[i].pos[0].x + snake[i].r > snake[i + 1].pos[k].x - snake[i].r&& snake[i].pos[0].y - snake[i].r < snake[i + 1].pos[k].y + snake[i].r && snake[i].pos[0].y + snake[i].r > snake[i + 1].pos[k].y - snake[i].r){snake[i].live = false;DeadFood(i);}}if ((i - 1) >= 0&&snake[i].live && snake[i - 1].live){if (snake[i].pos[0].x - snake[i].r < snake[i - 1].pos[k].x + snake[i].r && snake[i].pos[0].x + snake[i].r > snake[i - 1].pos[k].x - snake[i].r&& snake[i].pos[0].y - snake[i].r < snake[i - 1].pos[k].y + snake[i].r && snake[i].pos[0].y + snake[i].r > snake[i - 1].pos[k].y - snake[i].r){snake[i].live = false;DeadFood(i);}}}
还可以添加一些玩法,增加游戏的可玩性。此外,可以设计游戏初始画面和结束画面,添加鼠标控制等。以下是我的双人贪吃蛇设计:
这篇关于基于easyx库的C/C++游戏编程实例-贪吃蛇|的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!