精简版的贪吃蛇教程c++图形库实现最简单不看后悔(一个简单框架可改)

实现需要修改两个玩意第一个下载easyx https://blog.csdn.net/Daisyes/article/details/81333529?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162642722916780274136998%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=162642722916780274136998&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2alltop_positive~default-1-81333529.first_rank_v2_pc_rank_v29&utm_term=easyx%E4%B8%8B%E8%BD%BD&spm=1018.2226.3001.4187
第二个需要修改编译器内容
https://blog.csdn.net/VioletHan7/article/details/79517321?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522162644037216780357271409%2522%252C%2522scm%2522%253A%252220140713.130102334…%2522%257D&request_id=162644037216780357271409&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduend~default-1-79517321.first_rank_v2_pc_rank_v29&utm_term=outtextxy%E5%87%BD%E6%95%B0%E6%8A%A5%E9%94%99&spm=1018.2226.3001.4187
此外附上教程视频 最简单的实现方式不看后悔哦
https://www.bilibili.com/video/BV1b54y1z7yH
#include
#include<graphics.h>

using namespace std;
class Food

{
public:
POINT fd;
int flag;
DWORD color;

}food;
enum DIR//蛇移动的方向
{
UP,
DOWN,
LEFT,
RIGHT,

};
#define W 640
#define H 480
#define MAX 100
class Snake
{
public:
int num;//身体的节数
int dir;//蛇的方向
int score;//分数
int size;
POINT coor[MAX];
}snake;
void GameInit()
{
food.fd.x = rand() % (W/10)*10;
food.fd.y = rand() % (H/10)*10;
food.flag = 1;
food.color = BLACK;

//初始化蛇
snake.num = 3;
snake.dir = RIGHT;
snake.size = 10;
snake.score=0;
snake.coor[2].x = 0;
snake.coor[2].y = 0;
snake.coor[1].x = snake.size;
snake.coor[1].y = 0;
snake.coor[0].x = 2 * snake.size;
snake.coor[0].y = 0;
food.color = BLACK;

}
void GameDraw()
{
setbkcolor(RGB(105, 160, 141));//设置背景颜色
cleardevice();
//画蛇

for (int i = 0; i < snake.num; i++)
{
	setlinecolor(BLACK);
	setfillcolor(RED);
	fillrectangle(snake.coor[i].x, snake.coor[i].y, snake.coor[i].x + snake.size, snake.coor[i].y + snake.size);
}
//画食物
if(food.flag==1)
{
	setfillcolor(food.color);
fillellipse(food.fd.x, food.fd.y, food.fd.x + 10, food.fd.y + 10);

}
if (food.flag == 0)
{
food.fd.x = rand() % (W / 10) * 10;
food.fd.y = rand() % (H / 10) * 10;
food.flag = 1;

}

}
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 -= 10;
if (snake.coor[0].y +10<= 0)
{
snake.coor[0].y = H;
}
break;
case DOWN:
snake.coor[0].y += 10;
if (snake.coor[0].y + 10 >= H)
{
snake.coor[0].y = 0;
}
break;
case LEFT:

	snake.coor[0].x -= 10;
	if (snake.coor[0].x + 10 <=0)
	{
		snake.coor[0].x = W;
	}
	break;
case RIGHT:
	snake.coor[0].x += 10;
	if (snake.coor[0].x + 10 >= W)
	{
		snake.coor[0].x = 0;
	}
	break;
default:
	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;
}

}
void EatFood()
{
if (snake.coor[0].x == food.fd.x&&snake.coor[0].y == food.fd.y&&food.flag==1)
{
snake.num++;
snake.score += 10;
food.flag = 0;
}

}
void eat()
{
for (int i = 4; i < snake.num; i++)
{
if (snake.coor[0].x == snake.coor[i].x&&snake.coor[0].y == snake.coor[i].y)
{
outtextxy(200, 200, “Game Over”);
getchar();
exit(666);
}
}
}
void Pause()
{

char ch = _getch();
if (ch == 65)
{
	while (1)
	{
		if (_getch() != 65)
			break;
	}
}

}
int main()
{
initgraph(W, H, SHOWCONSOLE);
GameInit();
DWORD t1, t2;

t1 = t2 = GetTickCount();//控制时间作用
BeginBatchDraw;//双缓冲作用




		while (1)
		{
			if (t2 - t1 > 50)
			{
				SnakeMove();
				t1 = t2;
			}
			t2 = GetTickCount();
			EatFood();
			FlushBatchDraw();//双缓冲作用
			KeyControl();
			eat();

			GameDraw();
			





		}
		



getchar();
closegraph();
return 0;

}
希望新人可以互关 大家一起学习

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不说再见qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值