1 贪吃蛇效果展示
前言:C语言初学者都是在控制台上开发一些小游戏什么,由于没有界面(比如图片、音乐,不能做出拥有自己风格的程序)学起来比较乏味,很难坚持下去。所以我在这里就教大家使用简单的图形化工具——easyx图形界面库,做一些好玩的,超丝滑的贪吃蛇
2 背景知识
开发贪吃蛇小游戏,让初学者也能感受界面的魅力。做的不是很完善,希望大家可以自己动手完善它表白程序设计原理:
第一步,我们需要一张地图,图形库初始化的窗口是自带坐标的(左上角为0,0)。
第二步,我们需要一条蛇,这条蛇由蛇头和蛇身组成(这里我们初始化三节蛇)。
第三步,我们需要食物,并且在蛇吃掉食物之后将蛇的身体变长,而且重新生成一个食物。
第四步,蛇需要移动,这应该是最难实现的(实现蛇身坐标的传值,除蛇头以外,每一节蛇的坐标都是上节蛇的上一次运动之前的坐标,有点绕~)。
第五步,我们需要能用键盘控制蛇的运动方向,这个会和蛇的移动有一些联系。
第六步,蛇撞到墙或者撞到自己的身体就会死亡(判断坐标是否重合就可以)。
3 错误及决绝方案
此程序使用Vs209+easyx开发。刚开始学习C语言或者没接触过easyx的伙伴,拿到代码之后肯定有很多的错误,这就是所谓的给了源码也不能运行,所以配置环境和查看报错也是作为编程学习者,必须要掌握的技能。相关知识的基础讲解可观看专栏教程:
4. 话不多说上代码
4.1 头文件snake.h
我们把一些函数的声明,和结构体的定义放在头文件中:
#ifndef _SNAKE_H_#define _SNAKE_H_#include<graphics.h>#include<conio.h>#include<stdio.h>#include<time.h>#define WIN_WIDTH 640 //窗口宽高#define WIN_HEIGHT 480#define MAX_SNAKE 10000 //蛇的最大节数enum DIR //蛇的方向{ UP, DOWN, LEFT, RIGHT,};//每一节蛇的位置信息typedef struct _Pos{ int x; int y; DWORD color;}Pos;typedef struct Snake_tlg //蛇的结构体{ int num; int dir;//蛇的方向 int score;//分数 int size;//蛇的宽度和高度 int speed;//移动速度 Pos coor[MAX_SNAKE];}Snake;typedef struct Food_tlg{ int x; int y; int r; int flag; DWORD color;}Food;//画眼睛函数void DrawEye(int x, int y);//初始化数据void GameInit();//游戏的绘制void GameDraw();//蛇的移动void SnakeMove();//控制方向void KeyControl();//吃食物void EatFood();//暂停游戏 按空格暂停和继续void stop();//定时器bool Timer(clock_t ms);#endif
4.2 实现文件snake.cpp
OK,头文件部分完毕,接下来进入snake.cpp代码编写环节。
#include"snake.h"Snake snake;Food food;//画眼睛函数void DrawEye(int x, int y){ setfillcolor(WHITE); solidcircle(x, y, 3); setfillcolor(BLACK); solidcircle(x, y, 2);}void GameInit(){ srand(GetTickCount()); //初始化蛇 snake.num = 10; snake.dir = RIGHT; snake.score = 0; snake.size = 10; snake.speed = 1; snake.coor[0].x = 20; snake.coor[0].y = 10; for (int i = 0; i < snake.num; i++) { snake.coor[i].color = GREEN; } //初始化食物 food.x = rand() % (WIN_WIDTH - 10); food.y = rand() % (WIN_HEIGHT - 10); food.flag = 1; food.r = rand() % 3 + 2; food.color = RGB(rand() % 256, rand() % 256, rand() % 256);}void GameDraw(){ //设置背景颜色 setbkcolor(RGB(191, 215, 215)); cleardevice(); //画蛇 for (int i = 0; i < snake.num; i++) { setfillcolor(snake.coor[i].color); solidcircle(snake.coor[i].x, snake.coor[i].y, snake.size / 2); } //画眼睛 int eyewidth = 5; switch (snake.dir) { case UP: case DOWN: DrawEye(snake.coor[0].x - eyewidth, snake.coor[0].y); DrawEye(snake.coor[0].x + eyewidth, snake.coor[0].y); break; case LEFT: case RIGHT: DrawEye(snake.coor[0].x, snake.coor[0].y - eyewidth); DrawEye(snake.coor[0].x, snake.coor[0].y + eyewidth); break; } //画食物 if (food.flag == 1) { setfillcolor(food.color); solidcircle(food.x, food.y, food.r); } //显示分数 char temp[20] = "; sprintf(temp, "分数:%d", snake.score); setbkmode(TRANSPARENT); outtextxy(20, 20, temp);}void SnakeMove(){ 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; if (snake.coor[0].y + 10 <= 0) { snake.coor[0].y = WIN_HEIGHT; } break; case DOWN: snake.coor[0].y += snake.speed; if (snake.coor[0].y - 10 >= WIN_HEIGHT) { snake.coor[0].y = 0; } break; case LEFT: snake.coor[0].x -= snake.speed; if (snake.coor[0].x + 10 <= 0) { snake.coor[0].x = WIN_WIDTH; } break; case RIGHT: snake.coor[0].x += snake.speed; if (snake.coor[0].x - 10 >= WIN_WIDTH) { snake.coor[0].x = 0; } break; }}void KeyControl(){ //使用win32API获取键盘消息 if (GetAsyncKeyState(VK_UP) && snake.dir != DOWN) { snake.dir = UP; } if (GetAsyncKeyState(VK_DOWN) && snake.dir != UP) { snake.dir = DOWN; } if (GetAsyncKeyState(VK_LEFT) && snake.dir != RIGHT) { snake.dir = LEFT; } if (GetAsyncKeyState(VK_RIGHT) && snake.dir != LEFT) { snake.dir = RIGHT; } //按A键加速,不按回复初始状态 if (GetAsyncKeyState('A') && snake.speed == 1) { snake.speed = 5; } else if (snake.speed == 5) { snake.speed = 1; }}void EatFood(){ if (snake.coor[0].x >= food.x - food.r && snake.coor[0].x <= food.x + food.r && snake.coor[0].y >= food.y - food.r && snake.coor[0].y <= food.y + food.r && food.flag == 1) { snake.num += 5; snake.score += 10; food.flag = 0; //每一节的颜色都要初始化 for (int i = snake.num - 5; i < snake.num; i++) { snake.coor[i].color = food.color; } } if (food.flag == 0) { food.x = rand() % (WIN_WIDTH - 10); food.y = rand() % (WIN_HEIGHT - 10); food.flag = 1; food.r = rand() % 10 + 3; food.color = RGB(rand() % 256, rand() % 256, rand() % 256); }}void stop(){ if (_kbhit() && _getch() == 32) { while (getch() != 32); }}bool Timer(clock_t ms){ static clock_t t = clock(); if (clock() - t > ms) { t = clock(); return true; } return false;}
4.3 测试文件main.cpp
最后我们一起来测试一下吧
#include"snake.h"int main(){ //创建一个图形窗口 initgraph(WIN_WIDTH, WIN_HEIGHT); GameInit(); BeginBatchDraw();//① while (1) { if(Timer(10)) { SnakeMove(); } GameDraw(); FlushBatchDraw();//② EatFood(); KeyControl(); stop(); } EndBatchDraw();//③ ①②③为双缓冲绘图防止闪屏代码 //关闭图形窗口 closegraph(); return 0;}