导入easyx包
下载地址:https://easyx.cn/
导入教程:将下载好的easyx.exe解压
解压后会有两个文件
1.将include文件中的两个头文件复制到你的开发工具的include里面(vriual studio为例子)
再将lib文件中的x64和x86也复制到开发工具的对应的目录中
然后重启开发工具就行(vriual studio)
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#include<mmsystem.h> //音乐头文件
#pragma comment(lib,"winmm.lib") //库文件
using namespace std;
/*
1.做界面 创建窗口,图形窗口 easyx
2.蛇的结构
*/
#define SNAKE_num 500
enum DIR { //蛇的方向
UP,
DOWN,
LEFT,
RIGHT,
};
struct Snake {
int size; //蛇的长度
int dir; //方向
int speed; //移动速度
POINT coor[SNAKE_num]; //坐标
}snake;
//食物的结构
struct Food {
int x;
int y;
int r;
bool flag; //是否被吃
DWORD color;//事物的颜色
}food;
//数据初始化
void GameInit() {
//播放背景音乐
//mciSendString("open ./res/BGM.mp3 alias BGM", 0, 0, 0);
//mciSendString("play BGM repeat",0,0,0);
//初始化图形窗口
initgraph(640, 480);
//初始化蛇
snake.size = 3;
snake.speed = 10;
snake.dir = RIGHT;
for (int i =0; i <=snake.size - 1; i++) {
snake.coor[i].x = 40-10*i ;
snake.coor[i].y = 10;
}
//初始化食物
food.x = rand()%630;
food.y = rand()%470;
food.color = RGB(255, 0, 0);
food.r = 5;
food.flag = true;
}
void GameDraw() {
//双缓冲绘图,防止闪屏
BeginBatchDraw();
//设置背景颜色
setbkcolor(RGB(28, 115, 119));
cleardevice(); //清空绘图设备
//绘制蛇
setfillcolor(GREEN);
for (int i = 0; i < snake.size; i++) {
solidcircle(snake.coor[i].x, snake.coor[i].y,5);
}
//绘制食物
setfillcolor(food.color);
if (food.flag) {
solidcircle(food.x, food.y, food.r);
}
EndBatchDraw();
}
//移动蛇
void snakeMove() {
//让身体跟着3头移动
for (int i = snake.size-1; i > 0; i--) {
snake.coor[i] = snake.coor[i - 1];
}
switch (snake.dir) {
case RIGHT:
snake.coor[0].x+=snake.speed;
if (snake.coor[0].x >= 640) { //beyound hight
snake.coor[0].x = 0;
}
break;
case LEFT:
snake.coor[0].x-= snake.speed;
if (snake.coor[0].x <= 0) { //beyound hight
snake.coor[0].x = 640;
}
break;
case UP:
snake.coor[0].y-= snake.speed;
if (snake.coor[0].y <= 0) { //beyound hight
snake.coor[0].y = 480;
}
break;
case DOWN:
snake.coor[0].y+= snake.speed;
if (snake.coor[0].y >= 480) { //beyound hight
snake.coor[0].y = 0;
}
break;
case ' ': //游戏暂停
if (_kbhit()) {
if (_getch() == ' ') {
while (_getch() != ' ');
}
}
break;
}
}
//通过按键改变蛇的移动方向
void keyControl() {
//判断有没有按键
if (_kbhit()) {
//72 80 75 77 上下左右键值
switch (_getch()) {
case 'w':
case'W':
case 72:
if (snake.dir != DOWN) {
snake.dir = UP;
}
break;
case 's':
case'S':
case 80:
if (snake.dir != UP) {
snake.dir = DOWN;
}
break;
case 'a':
case'A':
case 75:
if (snake.dir != RIGHT) {
snake.dir = LEFT;
}
break;
case 'd':
case'D':
case 77:
if (snake.dir != LEFT) {
snake.dir = RIGHT;
}
break;
}
}
}
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) {
food.flag = false;
snake.size++;
}
if (!food.flag) {
food.x = rand() % 640;
food.y = rand() % 480;
food.color = RGB(rand() % 256+30, rand() % 256, rand() % 256);
food.r = 5;
food.flag = true;
}
}
/*
可以加一个分数,吃一个食物加n分
*/
int main() {
srand(GetTickCount());//获取系统开机到现在,经过的毫秒数
GameInit();
while (true) {
snakeMove();
EatFood();
GameDraw();
keyControl();
Sleep(100);
}
return 0;
}