应用随机数种子对RGB色值进行随机 更替,实现贪吃蛇的彩色变化# define _CRT_SECURE_NO_WARNINGS
# include<stdio.h>
# include<stdlib.h>
# include<graphics.h>
# include<conio.h>
# include<time.h>
# include<mmsystem.h>
// 坐标属性
typedef struct point
{
int x, y;
}GAMEPOINT;
// 蛇的结构体
struct Snake
{
GAMEPOINT SKxy[800]; // 坐标
int position; // 方向
int num; // 节数
}Snake;
// 食物结构体
struct Food
{
GAMEPOINT FDxy; // 坐标
int flag; // 标记
int grade; // 分数
}Food;
// 枚举:方向
enum position {up,down,left,right};//***************************************************模块化设计功能***********************************************************//
// ***************************蛇的功能实现
// 1.初始化蛇的
void initSnake()
{
// 前三节 坐标初始化
Snake.SKxy[2].x = 0; Snake.SKxy[2].y = 0;
Snake.SKxy[1].x = 10; Snake.SKxy[1].y = 0;
Snake.SKxy[0].x = 20; Snake.SKxy[0].y = 0;
// 前三节 方向初始化
Snake.position = right;
Snake.num = 3;
}
// 2.画蛇(画填充矩形)
void DrawSnake()
{
for (int i = 0; i < Snake.num; i )
{
setlinecolor(BLACK); // 设置边框 // 边框采用黑色
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); // 设置填充颜色 // 三原色随机产生
fillrectangle(Snake.SKxy[i].x, Snake.SKxy[i].y, Snake.SKxy[i].x 10, Snake.SKxy[i].y 10); // 依据坐标填充
}
}
// 3.动起来的彩色菜花蛇
void MoveSnake()
{
// 蛇的身体就是坐标转换(一个一个的往前挪动)
// 1.蛇身的移动
for (int i = Snake.num - 1; i > 0; i--)
{
Snake.SKxy[i].x = Snake.SKxy[i - 1].x; // 把前一节的x坐标赋值给后一节的坐标(以头部为前)
Snake.SKxy[i].y = Snake.SKxy[i - 1].y;
}
// 2.蛇头的移动
switch (Snake.position)
{
case up:
Snake.SKxy[0].y -= 10;
break;
case down:
Snake.SKxy[0].y = 10;
break;
case left:
Snake.SKxy[0].x -= 10;
break;
case right:
Snake.SKxy[0].x = 10;
break;
}
}
// 4.用户按键操作
void KeyDown()
{
char UserKey = _getch(); // 获取用户键盘输入
switch (UserKey)
{
case 'W':
case 'w':
case 72:
if (Snake.position != down)
Snake.position = up;
break;
case 'S':
case 's':
case 80:
if (Snake.position != up)
Snake.position = down;
break;
case 'A':
case 'a':
case 75:
if (Snake.position != right)
Snake.position = left;
break;
case 'D':
case 'd':
case 77:
if (Snake.position != left)
Snake.position = right;
break;
}
}
//***************************屏幕初始化
void InitScream()
{
initgraph(800, 800, NULL);
setbkcolor(RGB(184, 200, 183));
cleardevice(); // 设置颜色后需要刷新窗口界面
}
//***************************食物的功能实现
// 1.初始化食物
void InitFood()
{
// 食物坐标随机出现
Food.FDxy.x = rand() % 80 * 10;
Food.FDxy.y = rand() % 60 * 10;
Food.flag = 1; // 1代表食物还存在
// 食物不能出现在蛇身上
for (int i = 0; i < Snake.num; i )
{
if ((Food.FDxy.x == Snake.SKxy[i].x) && (Food.FDxy.y == Snake.SKxy[i].y))
{
Food.FDxy.x = rand() % 80 * 10;
Food.FDxy.y = rand() % 60 * 10;
}
}
}
// 2.画食物
void DrawFood()
{
setlinecolor(BLACK); // 边框采用黑色
setfillcolor(RGB(rand() % 255, rand() % 255, rand() % 255)); // 三原色随机产生
fillrectangle(Food.FDxy.x, Food.FDxy.y, Food.FDxy.x 10, Food.FDxy.y 10); // 依据坐标填充
}//***************************食物的功能实现
// 人物原型已具备,开始规则设置
// 1.吃食物
void EatFood()
{
// 原理:蛇头食物坐标相等
if (Snake.SKxy[0].x == Food.FDxy.x && Snake.SKxy[0].y == Food.FDxy.y)
{
Snake.num ;
Food.flag = 0;
Food.grade = 10;
}
}
// 2.显示分数
void ShowGrade()
{
// 格式化打印
char grade[20] = "";
sprintf(grade, "grade:%d", Food.grade);
settextcolor(LIGHTGREEN);
settextstyle(25, 0, "楷体");
outtextxy(670, 50, grade);
}
// 蛇死亡了
int SnakeDie()
{
// 撞墙
HWND hwnd = NULL;
if (Snake.SKxy[0].x > 800 || Snake.SKxy[0].x < 0 || Snake.SKxy[0].y>800 || Snake.SKxy[0].y < 0)
{
mciSendString("close 3.mp3", 0, 0, 0);
MessageBox(hwnd, "撞墙,游戏结束", "GAME--OVER", MB_OK);
return 1;
}
// 撞自己
for (int i = 1; i < Snake.num; i )
{
if (Snake.SKxy[0].x == Snake.SKxy[i].x && Snake.SKxy[0].y == Snake.SKxy[i].y)
{
mciSendString("close 3.mp3", 0, 0, 0);
MessageBox(hwnd, "自杀,游戏结束", "GAME--OVER", MB_OK);
return 1;
}
}
return 0;
}
int main()
{
// 随机函数
srand((unsigned int)time(NULL));
// 屏幕初始化
InitScream();
// 加载音乐
mciSendString("open 3.mp3",0,0,0);
mciSendString("play 3.mp3",0,0,0);
initSnake();
DrawSnake();
while (1)
{
cleardevice();
ShowGrade();
MoveSnake();
DrawSnake();
if (Food.flag == 0) // 食物不存在(蛇已经吃掉了,flag = 0)才会初始化
{
InitFood();
}
if (_kbhit() ) // 按键按下才运动
{
KeyDown();
}
if (SnakeDie())
{
break;
}
DrawFood();
EatFood();
Sleep(50);
}
system("pause");
closegraph;
return 0;
}
评论