C语言贪吃蛇vs2019(别人的代码)(非原创)

#define _CRT_SECURE_NO_WARNINGS 1

#undef UNICODE
#undef _UNICODE


#include <stdio.h>
#include <Windows.h>
#include <graphics.h>//游戏图像界面头文件
#include <time.h>//随机时间生成食物
#include <stdlib.h>



#include <mmsyscom.h>//游戏内播放音乐
#pragma comment(lib,"winmm.lib")


#include <conio.h>

#define WIN_WIDTH 640//游戏界面长和宽
#define WIN_HEIGHT 480
#define MAX_SNAKE 100//蛇最大节数
//enum一种数据类型:枚举
enum DIR
{
	UP,
	DOWN,
	LEFT,
	RIGHT,
};
struct Snake_tlg//蛇的结构体
{
	int num;//蛇身长度
	int dir;//蛇的方向
	int score;//得分
	int size;//蛇的尺寸
	POINT coor[MAX_SNAKE];//
}snake;
struct Food_tlg
{
	POINT fd;//食物的坐标
	int flag;//食物是否被吃掉
	DWORD color;//定义实物颜色

}food;

void Gamelnit();
void GameDraw();
void EatFood();
void KeyControl();
void Pause();
void DontEatSelf();
void SnakeMove();
void main()
{
	initgraph(WIN_WIDTH, WIN_HEIGHT, SHOWCONSOLE);//初始化一个图形窗口
	printf("\t欢迎进入贪吃蛇!\n");
	printf("\tPlay Game Right Now!\n");

	
		Gamelnit();
		while(1)
		{
			SnakeMove();
			GameDraw();
			EatFood();
			KeyControl();
			DontEatSelf();
			Sleep(50);
			Pause();
		}
		getchar();
		closegraph();
	
	//system("pause");
	//return 0;
}
void Gamelnit()
{
	srand((unsigned int)time(NULL));//time的头文件是time.h
	snake.num = 3;//定义蛇初始有三节身体
	snake.dir = RIGHT;//蛇头向右
	snake.score = 0;//开始时分数为0
	snake.size = 10;//初始化蛇的身体大小
	snake.coor[2].x = 0;//第三节尾巴的坐标
	snake.coor[2].y = 0;//蛇的身体是左上角一个长条
	snake.coor[1].x = 0 + snake.size;//蛇的第二节身体
	snake.coor[1].y = 0;
	snake.coor[0].x = 0 + 2 * snake.size;//蛇头
	snake.coor[0].y = 0;
	//初始化食物
	food.fd.x = rand() % (WIN_WIDTH / 10) * 10;
	food.fd.y = rand() % (WIN_HEIGHT / 10) * 10;
	food.flag = 1;//初始化食物存在
	food.color = RGB(rand() % 256, rand() % 256, rand() % 256);//随机蝉声食物颜色

	mciSendString("open Carnival_De_Brazil.mp3 alias a", 0, 0,0);
	mciSendString("play a repeat", 0, 0, 0);

}
void GameDraw()
{
	setbkcolor(RGB(105, 160, 141));//设置背景颜色
	cleardevice();//清楚其他元素
	//化蛇
	setfillcolor(YELLOW);
	int i;
	for (i = 0; i < snake.num; i++)
	{
		
setlinecolor(BLACK);
		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);
	}
	//显示分数
	char temp[20] = "";
	sprintf(temp, "分数:%d", snake.score);
	outtextxy(20, 20, temp);
}
void KeyControl()
{
	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 SnakeMove()
{
	/*移动是时蛇尾一截移动到蛇头*/
	int i;
	for (i = snake.num - 1; i > 0; i--)//因为对数组进行操作,所以是-1
	{
		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 = WIN_HEIGHT;
		}
		break;
	case DOWN:snake.coor[0].y += 10;
		if (snake.coor[0].y >= WIN_HEIGHT)
		{
			snake.coor[0].y = 0;
		}
		break;
	case LEFT:snake.coor[0].x -= 10;
		if (snake.coor[0].x +10<=0)
		{
			snake.coor[0].x = WIN_WIDTH;
		}
		break;
	case RIGHT:snake.coor[0].x += 10;
		if (snake.coor[0].x >= WIN_WIDTH)
		{
			snake.coor[0].x = 0;
 		}
		break;
	default:printf("操作错误,重新操作!"); break;
	}
}
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;//食物消失
	}
	if (food.flag == 0)//食物吃掉之后据徐产生食物
	{
		food.fd.x = rand() % (WIN_WIDTH / 10) * 10;
		food.fd.y = rand() % (WIN_HEIGHT / 10) * 10;
		food.flag = 1;
		food.color = RGB(rand() % 256, rand() % 256, rand() % 256);
	}
}
void DontEatSelf()
{
	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!");
			_getch();
			exit(666);
		}
	}
}

void Pause()
{
	if (_getch() == 32)//32表示空格
	{
		while (1)
		{
			_getch();
		}
	}
}

复制别人的代码,只是为了以后可以看看,vs里可以运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值