C语言简易游戏贪吃蛇

C语言简易游戏贪吃蛇,第1张

文章目录
  • 前言

  • 一、功能介绍


  • 二、编写步骤

  • 三.效果展示
  • 总结

前言

主要本人初学C语言做的简易小游戏,许多地方还需改进并且有不足之处,在以后将更加完善。



一、功能介绍

(1)蛇可以上下左右移动(速度可以更改),且颜色可以更改;
(2)食物随机出现;
(3)有加分数功能的实现;
(4)将食物吃掉之后蛇的长度也随之改变;


二、编写步骤

主要的相关步骤不在此复述,代码如下。


#define _CRT_SECURE_NO_WARNINGS 1


#include 
#include 
//包含easyx图形库
#include 

#define WIN_WIDTH 640
#define WIN_HEIGHT 480
#define MAX_SNAKE 300 //蛇的最大节数


//枚举
enum DIR//枚举蛇的方向
{
	UP,
	DOWN,
	RIGHT,
	LEFT,
};

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;





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;
}

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);
	}
	//绘制食物
	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);
}

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

void GameControl()
{
	//获取键盘输入,_getch();
	char key = _getch();
	switch (key)
	{
	case 72:
		if (snake.dir != DOWN)
		{
			snake.dir = UP;
		}
		break;
	case 80:
		if (snake.dir != UP)
		{
			snake.dir = DOWN;
		}
		break;
	case 75:
		if (snake.dir != RIGHT)
		{
			snake.dir = LEFT;
		}
		break;
	case 77:
		if (snake.dir != LEFT)
		{
			snake.dir = RIGHT;
		}
		break;
	}
	printf("%d\n", key);
}

void Creatfood()
{
	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;
	}
}

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);//随机生成三个数
		Creatfood();
	}
}
int main()
{
	//创建窗口,Init初始化 graphics图形窗口
	initgraph(WIN_WIDTH, WIN_HEIGHT, SHOWCONSOLE);
	setbkcolor(RGB(207, 214, 299));//设置颜色
	cleardevice();//刷新窗口
	GameInit();

	while (1)
	{
		GameDraw();
		GameMove();
		if (_kbhit())
		{
			GameControl();
		}
		EatFood();
		Sleep(90);
	}
	getchar();

	return 0;
}
三.效果展示

视频将不在此展示,只上传图片样式。



总结

本次制作的简易游戏有许多功能尚未完善,之后将弥补空缺功能,而且游戏还可以扩大开发效果。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/562574.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-01
下一篇 2022-04-01

发表评论

登录后才能评论

评论列表(0条)

保存