点缀星空

偶尔会发一些自己做的小软件

贪吃蛇的增强版——像素蛇

游戏规则


1.游戏中有两种东西:食物(黄)和敌人(红),食物吃了会变长,敌人吃(?)了会变短
2.食物和敌人会动,而且移动有一定智能与随机性
3.最短3格,最长布满全场,该游戏没有死亡的可能
4.撞到墙或者身体会变短
5.可用wasd ijkl ↑←↓→ 键来更改移动方向,空格暂停或播放,Esc退出
6.左键图标拖拽,右键图标折叠或打开,中键图标退出
7.目的是在更短的时间内变得更长

运行截图****


像素蛇-运行截图


下载

下载ZIP压缩包(包含CPP代码,ICO图标,SLN解决方案,EXE程序等内容,43KB)

源码


/*
* 游戏名称:像素蛇
* 作者:点缀星空
* 环境:Visual Studio 2019, EazyX20211109
* 联系:QQ 3516473554
* 最终修改:2021-12-10
* 游戏规则:
* 1.游戏中有两种东西:食物和敌人,食物吃了会变长,敌人吃(?)了会变短
* 2.食物和敌人会动,而且移动有一定智能与随机性
* 3.最短3格,最长布满全场,该游戏没有死亡可能
* 4.撞到墙或者身体会变短
* 5.可用wasd ijkl ↑←↓→ 键来更改移动方向,空格暂停或播放,Esc退出
* 6.左键图标拖拽,右键图标折叠或打开,中键图标退出
*/

#undef UNICODE
#undef _UNICODE
#define _CRT_SECURE_NO_DEPRECATE
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NONSTDC_NO_DEPRECATE
//#define OUTPUT_DATA  	//取消注释这个可以达到显示数据的目的
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <easyx.h>
#pragma comment (lib ,"imm32.lib") 

#define loop(a, b) for (int a = 0; a < b; a++)
#define aloop(a, b) for (int a = b - 1; a >= 0; a--)
#define loop2(a, b, c) for (int a = b; a < c; a++)
#define aloop2(a, b, c) for (int a = c - 1; a >= b; a--)
#define initrand srand((unsigned)time(NULL)) 	//初始化随机数

#define width 32 	//游戏区域宽(格子)
#define height 24 	//游戏区域高(格子)

#define UP 0 		//方向为上
#define DOWN 1 		//方向为下
#define LEFT 2 		//方向为左
#define RIGHT 3 	//方向为右
//用于记录方向

#define EMP 0 		//空
#define BODY 1 		//蛇身
#define HEAD 2 		//蛇头
#define FOOD 3 		//食物
#define ENEMY 4 	//敌人
//用于jilu记录每一格的内容

struct item
{
	POINT begin = { -1,-1 }; 	//开始点
	POINT now = { -1,-1 }; 		//现在的位置
	double speed = 150; 		//几毫秒动一格
	long btime = 0; 			//开始时间
	short direct = RIGHT; 		//方向
};
//食物和敌人使用的结构体

bool pause = false; 				//是否暂停
long pausetime = 0, pausestart; 	//暂停总时长与暂停开始时间
item food[51], enemy[51]; 			//食物与敌人
int size = 15; 						//格子大小
int direct = RIGHT; 				//现在的方向
int wsize = 50; 					//左上角小窗口的大小
const int jixian = width * height; 	//蛇的极限长度
double speed = 200; 				//蛇的速度(毫秒/格)
bool die = false; 					//是否撞墙
long ltime = 0; 					//蛇上次移动的时间(距离程序开始运行)
#define wW (size*width) 			//游戏界面宽
#define wH (size*height) 			//游戏界面高
int bj = 7; 						//边距
int nvalue = 20; 					//食物/敌人数量
short jilu[width][height]; 			//记录全局
short sx[width * height + 10], sy[width * height + 10], sl = 0;
//蛇在的x位置,y位置,蛇的长度;(sx[0]为蛇头的x位置,sx[1]为第一节蛇身的x位置,sy[2]为第二节蛇身的y位置,以此类推)
int x, y; 							//窗口在屏幕的位置
int rW; 							//工作区宽度
int rH; 							//工作区高度
COLORREF uc = 0x080808, nc = 0xAAAAAA, headc = 0x555555, foodc = 0x00EEEE, enemyc = 0x0000AA, bodyc = 0x080808;
//焦点窗口时的颜色,非焦点窗口时的颜色,头部颜色,食物颜色,敌人颜色,身体颜色
long ldraw = 0; 					//上次绘画界面时间(距离程序开始运行)

void setfont(int size, LPCSTR string)
{
	LOGFONT font;
	font.lfHeight = size;
	font.lfWidth = 0;
	font.lfEscapement = 0;
	font.lfOrientation = 0;
	font.lfWeight = 0;
	font.lfItalic = false;
	font.lfUnderline = false;
	font.lfStrikeOut = false;
	font.lfCharSet = ANSI_CHARSET;
	font.lfOutPrecision = OUT_DEVICE_PRECIS;
	font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	font.lfQuality = PROOF_QUALITY;
	font.lfPitchAndFamily = DEFAULT_PITCH;
	strcpy(font.lfFaceName, string);
	settextstyle(&font);
}
void setfont(int size, LPCSTR string, int Weight)
{
	LOGFONT font;
	font.lfHeight = size;
	font.lfWidth = 0;
	font.lfEscapement = 0;
	font.lfOrientation = 0;
	font.lfWeight = Weight;
	font.lfItalic = false;
	font.lfUnderline = false;
	font.lfStrikeOut = false;
	font.lfCharSet = ANSI_CHARSET;
	font.lfOutPrecision = OUT_DEVICE_PRECIS;
	font.lfClipPrecision = CLIP_DEFAULT_PRECIS;
	font.lfQuality = PROOF_QUALITY;
	font.lfPitchAndFamily = DEFAULT_PITCH;
	strcpy(font.lfFaceName, string);
	settextstyle(&font);
}
//设置字体(最高输出质量)
void set(int pos, int x, int y)
{
	sx[pos] = x;
	sy[pos] = y;
}
//设置蛇的某一个节点,参数为:第几节,x位置,y位置
void inititem(bool isfood)
{
	int x, y, nx = -1, ny = -1;
	if (direct == LEFT) nx = sx[0] - 1;
	else if (direct == RIGHT) nx = sx[0] + 1;
	else if (direct == UP) ny = sy[0] - 1;
	else if (direct == DOWN) ny = sy[0] + 1;
	if (isfood)
	{
		int i = 0;
		for (i = 0; i < nvalue; i++)
		{
			if (food[i].begin.x == -1)goto out;
		}
	out:
		x = rand() % width;
		y = rand() % height;
		if (jilu[x][y] != EMP)
		{
			goto out;
		}
		if (x == nx || y == ny) goto out;
		food[i].begin.x = x;
		food[i].begin.y = y;
		food[i].now.x = x;
		food[i].now.y = y;
		food[i].speed = (rand() % (int)round(speed / 2)) + speed * 7 / 8;
		food[i].direct = rand() % 4;
	}
	else
	{
		int i = 0;
		for (i = 0; i < nvalue; i++)
		{
			if (enemy[i].begin.x == -1)goto out2;
		}
	out2:
		x = rand() % width;
		y = rand() % height;
		if (jilu[x][y] != EMP)
		{
			goto out2;
		}
		if (x == nx || y == ny) goto out2;
		enemy[i].begin.x = x;
		enemy[i].begin.y = y;
		enemy[i].now.x = x;
		enemy[i].now.y = y;
		enemy[i].speed = (rand() % (int)round(speed / 2)) + speed * 7 / 8;
		enemy[i].direct = rand() % 4;
	}
}
//初始化食物与敌人结构体,参数为要初始化的是否为食物
void forward(bool baoliu = false)
{
	aloop(i, sl)
	{
		sx[i + 1] = sx[i];
		sy[i + 1] = sy[i];
	}
	if (direct == LEFT) { sx[0] = sx[1] - 1; sy[0] = sy[1]; }
	else if (direct == RIGHT) { sx[0] = sx[1] + 1; sy[0] = sy[1]; }
	else if (direct == UP) { sx[0] = sx[1]; sy[0] = sy[1] - 1; }
	else if (direct == DOWN) { sx[0] = sx[1]; sy[0] = sy[1] + 1; }
	if (sx[0] < 0 || sy[0] < 0 || sx[0] >= width || sy[0] >= height)
	{
		loop(i, sl)
		{
			sx[i] = sx[i + 1];
			sy[i] = sy[i + 1];
		}
		die = true;
	}
	else if (!baoliu)
	{
		sx[sl] = -1;
		sy[sl] = -1;
	}
	else sl++;
}
//让蛇前进一格,参数为前进的时候是否保留最后一格,如果保留了的话可以产生变长一格的效果
void shorter()
{
	sx[sl] = -1;
	sy[sl] = -1;
	sl--;
	sx[sl] = -1;
	sy[sl] = -1;
}
//让蛇缩短一格
void topause()
{
	if (pause)
	{
		pausestart = clock();
	}
	else
	{
		pausetime += clock() - pausestart;
	}
}
//暂停
bool SetWindowTransparent(HWND hwnd, COLORREF crkcolor, BYTE bAlpha, DWORD dwFlags)
{
	HINSTANCE hm = GetModuleHandle(_T("USER32.DLL"));
	if (hm)
	{
		FARPROC fun = GetProcAddress(hm, "SetLayeredWindowAttributes");
		bool (WINAPI * SetLayeredWindowAttributes)(HWND, COLORREF, BYTE, DWORD) = (bool (WINAPI*) (HWND, COLORREF, BYTE, DWORD))fun;
		if (SetLayeredWindowAttributes)
		{
			LONG ret = GetWindowLong(hwnd, GWL_EXSTYLE);
			ret |= WS_EX_LAYERED;
			SetWindowLong(hwnd, GWL_EXSTYLE, ret);
			SetLayeredWindowAttributes(hwnd, crkcolor, bAlpha, dwFlags);
		}
		FreeLibrary(hm);
		return true;
	}
	else
	{
		return false;
	}
}
void SetWindowsForm(HWND hWnd, int tmd = 200)
{
	setbkcolor(BLACK);
	cleardevice();
	SetWindowTransparent(hWnd, BLACK, tmd, LWA_COLORKEY);
	SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
}
//设置窗口背景透明(这两个函数是在网上找到的方案,非原创,有部分改动)
void drawwindow(int x, int y, bool type = false)
{
	setfillcolor(uc);
	setlinecolor(nc);
	x -= 57;
	y -= 5;
	setlinestyle(0, 1);
	fillrectangle(x, y, x + 50, y + 50);
	rectangle(x + 30, y + 10, x + 40, y + 20);
	rectangle(x + 20, y + 10, x + 30, y + 20);
	rectangle(x + 20, y + 20, x + 30, y + 30);
	rectangle(x + 20, y + 30, x + 30, y + 40);
	rectangle(x + 10, y + 30, x + 20, y + 40);
	x += 57;
	y += 5;
	if (!type)
	{
		settextcolor(0x080808);
		setfont(20, "微软雅黑", 50);
		char a[200];
		int now = clock() - pausetime;
		int sec = (int)round(now / 1000);
		int min = (int)round(sec / 60);
		sec = sec % 60;
		int hour = (int)round(min / 60);
		min = min % 60;
		if (!pause)
		{
			if (hour == 0)
			{
				if (min == 0)
				{
					if (direct == LEFT) sprintf(a, "%d个食物 %d个敌人 方向左 速度%.0f毫秒/格 长度%d格 耗时%02d秒", nvalue, nvalue, speed, sl, sec);
					else if (direct == RIGHT) sprintf(a, "%d个食物 %d个敌人 方向右 速度%.0f毫秒/格 长度%d格 耗时%02d秒", nvalue, nvalue, speed, sl, sec);
					else if (direct == UP) sprintf(a, "%d个食物 %d个敌人 方向上 速度%.0f毫秒/格 长度%d格 耗时%02d秒", nvalue, nvalue, speed, sl, sec);
					else if (direct == DOWN) sprintf(a, "%d个食物 %d个敌人 方向下 速度%.0f毫秒/格 长度%d格 耗时%02d秒", nvalue, nvalue, speed, sl, sec);
				}
				else
				{
					if (direct == LEFT) sprintf(a, "%d个食物 %d个敌人 方向左 速度%.0f毫秒/格 长度%d格 耗时%02d分%02d秒", nvalue, nvalue, speed, sl, min, sec);
					else if (direct == RIGHT) sprintf(a, "%d个食物 %d个敌人 方向右 速度%.0f毫秒/格 长度%d格 耗时%02d分%02d秒", nvalue, nvalue, speed, sl, min, sec);
					else if (direct == UP) sprintf(a, "%d个食物 %d个敌人 方向上 速度%.0f毫秒/格 长度%d格 耗时%02d分%02d秒", nvalue, nvalue, speed, sl, min, sec);
					else if (direct == DOWN) sprintf(a, "%d个食物 %d个敌人 方向下 速度%.0f毫秒/格 长度%d格 耗时%02d分%02d秒", nvalue, nvalue, speed, sl, min, sec);
				}
			}
			else
			{
				if (direct == LEFT) sprintf(a, "%d个食物 %d个敌人 方向左 速度%.0f毫秒/格 长度%d格 耗时%d时%02d分%02d秒", nvalue, nvalue, speed, sl, hour, min, sec);
				else if (direct == RIGHT) sprintf(a, "%d个食物 %d个敌人 方向右 速度%.0f毫秒/格 长度%d格 耗时%d时%02d分%02d秒", nvalue, nvalue, speed, sl, hour, min, sec);
				else if (direct == UP) sprintf(a, "%d个食物 %d个敌人 方向上 速度%.0f毫秒/格 长度%d格 耗时%d时%02d分%02d秒", nvalue, nvalue, speed, sl, hour, min, sec);
				else if (direct == DOWN) sprintf(a, "%d个食物 %d个敌人 方向下 速度%.0f毫秒/格 长度%d格 耗时%d时%02d分%02d秒", nvalue, nvalue, speed, sl, hour, min, sec);
			}
		}
		else
		{
			if (hour == 0)
			{
				if (min == 0)
				{
					if (direct == LEFT) sprintf(a, "%d个食物 %d个敌人 方向左 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
					else if (direct == RIGHT) sprintf(a, "%d个食物 %d个敌人 方向右 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
					else if (direct == UP) sprintf(a, "%d个食物 %d个敌人 方向上 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
					else if (direct == DOWN) sprintf(a, "%d个食物 %d个敌人 方向下 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
				}
				else
				{
					if (direct == LEFT) sprintf(a, "%d个食物 %d个敌人 方向左 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
					else if (direct == RIGHT) sprintf(a, "%d个食物 %d个敌人 方向右 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
					else if (direct == UP) sprintf(a, "%d个食物 %d个敌人 方向上 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
					else if (direct == DOWN) sprintf(a, "%d个食物 %d个敌人 方向下 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
				}
			}
			else
			{
				if (direct == LEFT) sprintf(a, "%d个食物 %d个敌人 方向左 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
				else if (direct == RIGHT) sprintf(a, "%d个食物 %d个敌人 方向右 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
				else if (direct == UP) sprintf(a, "%d个食物 %d个敌人 方向上 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
				else if (direct == DOWN) sprintf(a, "%d个食物 %d个敌人 方向下 速度%.0f毫秒/格 长度%d格 暂停中", nvalue, nvalue, speed, sl);
			}
		}
		setfillcolor(0xF8F8F8);
		solidrectangle(x - 5 + 2, y - 25 + 2, x + textwidth(a) + 5 - 2, y - 25 + textheight(a) - 2);
		outtextxy(x, y - 25, a);

#ifdef OUTPUT_DATA
		setfont(15,"等线");
		sprintf(a, "当前时间%d 暂停时长%d", ltime, pausetime);
		if (direct == LEFT)strcat(a, " 左");
		else if (direct == RIGHT)strcat(a, " 右");
		else if (direct == UP)strcat(a, " 上");
		else if (direct == DOWN)strcat(a, " 下");
		solidrectangle(2 - 5 + 2, 0 + 2, 5 + textwidth(a) + 5 - 2, 0 + textheight(a) - 2);
		outtextxy(5, 0, a);
		sprintf(a, "食物数据:");
		solidrectangle(2 - 5 + 2, 25 + 2, 5 + textwidth(a) + 5 - 2, 25 + textheight(a) - 2);
		outtextxy(5, 25, a);
		nvalue += 5;
		loop(i, nvalue)
		{
			if (food[i].direct == LEFT) sprintf(a, "第%02d个 方向左 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, food[i].speed, food[i].now.x, food[i].now.y, food[i].btime);
			else if (food[i].direct == RIGHT) sprintf(a, "第%02d个 方向右 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, food[i].speed, food[i].now.x, food[i].now.y, food[i].btime);
			else if (food[i].direct == UP) sprintf(a, "第%02d个 方向上 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, food[i].speed, food[i].now.x, food[i].now.y, food[i].btime);
			else if (food[i].direct == DOWN) sprintf(a, "第%02d个 方向下 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, food[i].speed, food[i].now.x, food[i].now.y, food[i].btime);
			solidrectangle(2, i * 20 + 45 + 2, 5 + textwidth(a) + 5 - 2, i * 20 + 45 + textheight(a) - 2);
			outtextxy(5, i * 20 + 45, a);
		}
		sprintf(a, "敌人数据:");
		solidrectangle(2 - 5 + 2, nvalue * 20 + 50 + 2, 5 + textwidth(a) + 5 - 2, nvalue * 20 + 50 + textheight(a) - 2);
		outtextxy(5, nvalue * 20 + 50, a);
		loop(i, nvalue)
		{
			if (enemy[i].direct == LEFT) sprintf(a, "第%02d个 方向左 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, enemy[i].speed, enemy[i].now.x, enemy[i].now.y, enemy[i].btime);
			else if (enemy[i].direct == RIGHT) sprintf(a, "第%02d个 方向右 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, enemy[i].speed, enemy[i].now.x, enemy[i].now.y, enemy[i].btime);
			else if (enemy[i].direct == UP) sprintf(a, "第%02d个 方向上 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, enemy[i].speed, enemy[i].now.x, enemy[i].now.y, enemy[i].btime);
			else if (enemy[i].direct == DOWN) sprintf(a, "第%02d个 方向下 速度%.f毫秒每格 位置(%02d,%02d) 上次移动时间%d", i + 1, enemy[i].speed, enemy[i].now.x, enemy[i].now.y, enemy[i].btime);
			solidrectangle(2, i * 20 + nvalue * 20 + 70 + 2, 5 + textwidth(a) + 5 - 2, i * 20 + nvalue * 20 + 70 + textheight(a) - 2);
			outtextxy(5, i * 20 + nvalue * 20 + 70 , a);
		}
		nvalue -= 5;
		//查看数据用
#endif

		setlinestyle(0, 4);
		rectangle(x - 3, y - 3, x + wW + 3, y + wH + 3);
		setlinecolor(uc);
		setfillcolor(0xF8F8F8);
		setlinestyle(0, 2);
		fillrectangle(x - 3, y - 3, x + wW + 3, y + wH + 3);
		setlinecolor(nc);
		setlinestyle(0, 1);
		loop(i, width)loop(j, height)
		{
			if (jilu[i][j] == BODY)
			{
				setfillcolor(bodyc);
				fillrectangle(x + i * size, y + j * size, x + i * size + size - 1, y + j * size + size - 1);
			}
			else if (jilu[i][j] == HEAD)
			{
				setfillcolor(headc);
				fillrectangle(x + i * size, y + j * size, x + i * size + size - 1, y + j * size + size - 1);
			}
			else if (jilu[i][j] == FOOD)
			{
				setfillcolor(foodc);
				fillrectangle(x + i * size, y + j * size, x + i * size + size - 1, y + j * size + size - 1);
			}
			else if (jilu[i][j] == ENEMY)
			{
				setfillcolor(enemyc);
				fillrectangle(x + i * size, y + j * size, x + i * size + size - 1, y + j * size + size - 1);
			}
		}
	}
}
//绘制窗口
void initsnake(int length = 3)
{
	loop(i, width * height)
	{
		sx[i] = -1;
		sy[i] = -1;
	}
	int nowpos = length - 1;
	int outof = (length % width), hangs = (length - outof + width) / width;
	loop(i, hangs)
	{
		if (i == hangs - 1) loop(j, outof)
		{
			set(nowpos, j, i);
			nowpos--;
		}
		else if (i % 2 == 0)loop(j, width)
		{
			set(nowpos, j, i);
			nowpos--;
		}
		else aloop(j, width)
		{
			set(nowpos, j, i);
			nowpos--;
		}
		if (nowpos == -1) goto out;
	}
out:
	sl = length;
	if (sl > 503)speed = 50;
	else if (sl != 3)speed = (double)200 - ((sl - 3) * 150 / 500);
	else speed = 200;
	if (sl > 503) nvalue = 40;
	if (sl != 3)nvalue = ((int)round((sl - 3) * 20 / 500)) + 20;
	else nvalue = 20;
	ltime = (long)(clock() - speed);
	direct = RIGHT;
}
//初始化蛇
void tojilu()
{
	loop(i, width)loop(j, height)jilu[i][j] = EMP;
	loop(i, nvalue)
	{
		if (food[i].begin.x != -1)
		{
			jilu[food[i].now.x][food[i].now.y] = FOOD;
		}
		else break;
	}
	loop(i, nvalue)
	{
		if (enemy[i].begin.x != -1)
		{
			jilu[enemy[i].now.x][enemy[i].now.y] = ENEMY;
		}
		else break;
	}
	loop2(i, 1, width * height)
	{
		if (sx[i] != -1)
		{
			jilu[sx[i]][sy[i]] = BODY;
		}
		else break;
	}
	jilu[sx[0]][sy[0]] = HEAD;
}
//将蛇,食物,敌人等消息载入jilu
void moveitem(bool isfood, int pos)
{
	if (isfood)
	{
		if (rand() % 3 == 0)
		{
			food[pos].direct = rand() % 4;
		}
		if (food[pos].direct == LEFT)
		{
			if (food[pos].now.x == 0) food[pos].direct = RIGHT;
			else if (jilu[food[pos].now.x - 1][food[pos].now.y] != EMP) food[pos].direct = RIGHT;
			else food[pos].now.x--;
		}
		if (food[pos].direct == RIGHT)
		{
			if (food[pos].now.x == width - 1) food[pos].direct = LEFT;
			else if (jilu[food[pos].now.x + 1][food[pos].now.y] != EMP) food[pos].direct = LEFT;
			else food[pos].now.x++;
		}
		if (food[pos].direct == UP)
		{
			if (food[pos].now.y == 0) food[pos].direct = DOWN;
			else if (jilu[food[pos].now.x][food[pos].now.y - 1] != EMP) food[pos].direct = DOWN;
			else food[pos].now.y--;
		}
		if (food[pos].direct == DOWN)
		{
			if (food[pos].now.y == height - 1) food[pos].direct = UP;
			else if (jilu[food[pos].now.x][food[pos].now.y + 1] != EMP) food[pos].direct = UP;
			else food[pos].now.y++;
		}
	}
	else
	{
		if (rand() % 3 == 0)
		{
			enemy[pos].direct = rand() % 4;
		}
		if (enemy[pos].direct == LEFT)
		{
			if (enemy[pos].now.x == 0) enemy[pos].direct = RIGHT;
			else if (jilu[enemy[pos].now.x - 1][enemy[pos].now.y] != EMP) enemy[pos].direct = RIGHT;
			else enemy[pos].now.x--;
		}
		if (enemy[pos].direct == RIGHT)
		{
			if (enemy[pos].now.x == width - 1) enemy[pos].direct = LEFT;
			else if (jilu[enemy[pos].now.x + 1][enemy[pos].now.y] != EMP) enemy[pos].direct = LEFT;
			else enemy[pos].now.x++;
		}
		if (enemy[pos].direct == UP)
		{
			if (enemy[pos].now.y == 0) enemy[pos].direct = DOWN;
			else if (jilu[enemy[pos].now.x][enemy[pos].now.y - 1] != EMP) enemy[pos].direct = DOWN;
			else enemy[pos].now.y--;
		}
		if (enemy[pos].direct == DOWN)
		{
			if (enemy[pos].now.y == height - 1) enemy[pos].direct = UP;
			else if (jilu[enemy[pos].now.x][enemy[pos].now.y + 1] != EMP) enemy[pos].direct = UP;
			else enemy[pos].now.y++;
		}
	}
}
//计算食物与敌人的移动,参数为 是否是食物 与 第几个食物/敌人
void move()
{
	if (pause)return;
	long now = clock() - pausetime;
	int i = 0;
	for (i = 0; i < nvalue; i++)
	{
		if (food[i].begin.x == -1) break;
	}
	while (i < nvalue)
	{
		inititem(true);
		food[i].btime = now;
		i++;
	}
	i = 0;
	for (i = 0; i < nvalue; i++)
	{
		if (enemy[i].begin.x == -1) break;
	}
	while (i < nvalue)
	{
		inititem(false);
		enemy[i].btime = now;
		i++;
	}
	loop(i, nvalue)
	{
		if (food[i].begin.x != -1)
		{
			if (now - food[i].btime > (int)round(food[i].speed))
			{
				moveitem(true, i);
				food[i].btime += (int)round(food[i].speed);
			}
		}
	}
	loop(i, nvalue)
	{
		if (enemy[i].begin.x != -1)
		{
			if (now - enemy[i].btime > (int)round(enemy[i].speed))
			{
				moveitem(false, i);
				enemy[i].btime += (int)round(enemy[i].speed);
			}
		}
	}
	long la = now - ltime;
	if (la > speed)
	{
		initrand;
		if (direct == LEFT)
		{
			if (jilu[sx[0] - 1][sy[0]] == FOOD)
			{
				if (sl <= jixian)forward(true);
				loop(i, nvalue)
				{
					if (food[i].now.x == sx[0] && food[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							food[j] = food[j + 1];
						}
						food[nvalue] = food[50];
						break;
					}
				}
			}
			else if (jilu[sx[0] - 1][sy[0]] == ENEMY)
			{
				if (sl > 3) shorter();
				forward();
				loop(i, nvalue)
				{
					if (enemy[i].now.x == sx[0] && enemy[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							enemy[j] = enemy[j + 1];
						}
						enemy[nvalue] = food[50];
						break;
					}
				}
			}
			else if (jilu[sx[0] - 1][sy[0]] == BODY)
			{
				if (sl > 3) shorter();
			}
			else forward();
			if (die)
			{
				if (sl > 3) shorter();
				die = false;
			}
		}
		else if (direct == RIGHT)
		{
			if (jilu[sx[0] + 1][sy[0]] == FOOD)
			{
				if (sl != jixian)forward(true);
				loop(i, nvalue)
				{
					if (food[i].now.x == sx[0] && food[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							food[j] = food[j + 1];
						}
						break;
					}
				}
			}
			else if (jilu[sx[0] + 1][sy[0]] == ENEMY)
			{
				if (sl > 3) shorter();
				forward();
				loop(i, nvalue)
				{
					if (enemy[i].now.x == sx[0] && enemy[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							enemy[j] = enemy[j + 1];
						}
						break;
					}
				}
			}
			else if (jilu[sx[0] + 1][sy[0]] == BODY)
			{
				if (sl > 3) shorter();
			}
			else forward();
			if (die)
			{
				if (sl > 3) shorter();
				die = false;
			}
		}
		else if (direct == UP)
		{
			if (jilu[sx[0]][sy[0] - 1] == FOOD)
			{
				if (sl != jixian)forward(true);
				loop(i, nvalue)
				{
					if (food[i].now.x == sx[0] && food[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							food[j] = food[j + 1];
						}
						break;
					}
				}
			}
			else if (jilu[sx[0]][sy[0] - 1] == ENEMY)
			{
				if (sl > 3) shorter();
				forward();
				loop(i, nvalue)
				{
					if (enemy[i].now.x == sx[0] && enemy[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							enemy[j] = enemy[j + 1];
						}
						break;
					}
				}
			}
			else if (jilu[sx[0]][sy[0] - 1] == BODY)
			{
				if (sl > 3) shorter();
			}
			else forward();
			if (die)
			{
				if (sl > 3) shorter();
				die = false;
			}
		}
		else if (direct == DOWN)
		{
			if (jilu[sx[0]][sy[0] + 1] == FOOD)
			{
				if (sl != jixian)forward(true);
				loop(i, nvalue)
				{
					if (food[i].now.x == sx[0] && food[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							food[j] = food[j + 1];
						}
						break;
					}
				}
			}
			else if (jilu[sx[0]][sy[0] + 1] == ENEMY)
			{
				if (sl > 3) shorter();
				forward();
				loop(i, nvalue)
				{
					if (enemy[i].now.x == sx[0] && enemy[i].now.y == sy[0])
					{
						loop2(j, i, nvalue)
						{
							enemy[j] = enemy[j + 1];
						}
						break;
					}
				}
			}
			else if (jilu[sx[0]][sy[0] + 1] == BODY)
			{
				if (sl > 3) shorter();
			}
			else forward();
			if (die)
			{
				if (sl > 3) shorter();
				die = false;
			}
		}
		ltime += (int)round(speed);
	}
	if (sl > 503)speed = 50;
	else if (sl != 3)speed = (double)200 - ((sl - 3) * 150 / 500);
	else speed = 200;
	if (sl > 503) nvalue = 40;
	if (sl != 3)nvalue = ((int)round((sl - 3) * 20 / 500)) + 20;
	else nvalue = 20;
	loop2(i, nvalue, nvalue + 5)
	{
		food[nvalue] = food[50];
		enemy[nvalue] = enemy[50];
	}
}
//移动蛇并控制其他参数
int main()
{
	initrand;
	bool zzd = false;
	RECT rt;
	POINT lastp;
	bool zd = false;
	lastp.x = -1;
	SystemParametersInfo(SPI_GETWORKAREA, 0, (PVOID)&rt, 0);
	rW = rt.right - rt.left;
	rH = rt.bottom - rt.top;
	x = (rW - wW) / 2;
	y = (rH - wH) / 2;
	int lx = -1, ly = -1;
	HWND hwnd = initgraph(rW, rH);
	LONG nowWinStyle = GetWindowLong(hwnd, GWL_STYLE);
	SetWindowLong(hwnd, GWL_STYLE, (nowWinStyle | WS_OVERLAPPED) & ~WS_CAPTION & ~WS_THICKFRAME & ~WS_BORDER);
	cleardevice();
	SetWindowPos(hwnd, HWND_TOP, rt.left, rt.top, rW, rH, 0);
	SetWindowsForm(hwnd);
	setbkmode(TRANSPARENT);
	//设置窗口属性

	HIMC himc = NULL;
	himc = ImmAssociateContext(GetHWnd(), NULL);
	//禁用输入法(由于只需要输入字符)

	char t = 0; 			//储存字符
	ExMessage m; 			//消息存放
	bool windowm = false; 	//窗口有无变动
	initsnake();
	tojilu();
	loop(i, nvalue) { inititem(true); inititem(false); }
	while (1)
	{
		windowm = false;
		if (hwnd == GetForegroundWindow())
		{
			uc = 0x080808;
			if (!zzd)
			{
				zd = false;
			}
		}
		else
		{
			uc = 0x555555;
			zd = true;
			if (!pause)
			{
				pause = true;
				topause();
			}
		}
		BeginBatchDraw();
		cleardevice();
		tojilu();
		drawwindow(x, y, zd);
		FlushBatchDraw();
		EndBatchDraw();
		if ((!pause && peekmessage(NULL, -1, false)) || pause)
		{
			m = getmessage();
			if (m.message == WM_MOUSEMOVE)
			{
				if (m.lbutton == true)
				{
					if (lastp.x == -1)
					{
						lastp.x = m.x;
						lastp.y = m.y;
					}
					else
					{
						lx = x;
						ly = y;
						x += m.x - lastp.x;
						y += m.y - lastp.y;
						lastp.x = m.x;
						lastp.y = m.y;
					}
				}
				else lastp.x = -1;
				windowm = true;
			}
			else if (m.message == WM_LBUTTONDOWN)
			{
				lastp.x = m.x;
				lastp.y = m.y;
				windowm = true;
			}
			else if (m.message == WM_RBUTTONUP)
			{
				zd = !zd;
				if (zd)
				{
					if (!pause)
					{
						pause = true;
						topause();
					}
					zzd = true;
				}
				else
				{
					zzd = false;
				}
				windowm = true;
			}
			else if (m.message == WM_MBUTTONUP)
			{
				exit(0);
			}
			else if (m.message == WM_CHAR)
			{
				t = m.ch;
				switch (t)
				{
				case 'a':case 'j':case 'A':case 'J':if (direct != RIGHT)direct = LEFT; break;
				case 'd':case 'l':case 'D':case 'L':if (direct != LEFT)direct = RIGHT; break;
				case 'w':case 'i':case 'W':case 'I':if (direct != DOWN)direct = UP; break;
				case 's':case 'k':case 'S':case 'K':if (direct != UP)direct = DOWN; break;
				case 27:exit(0); windowm = true; break;
				case ' ':
					if (!zd)
					{
						pause = !pause;
						topause();
						windowm = true;
					}
				}
			}
			else if (m.message == WM_KEYDOWN)
			{
				t = m.vkcode;
				switch (t)
				{
				case VK_LEFT:if (direct != RIGHT)direct = LEFT; break;
				case VK_RIGHT:if (direct != LEFT)direct = RIGHT; break;
				case VK_UP:if (direct != DOWN)direct = UP; break;
				case VK_DOWN:if (direct != UP)direct = DOWN; break;
				}
			}
			else
			{
				Sleep(10);
			}
			if (x < bj + wsize + 2)
			{
				x = bj + wsize + 2;
			}
			if (!zd)
			{
				if (y - 20 < bj)
				{
					y = bj + 20;
				}
				if (x + wW + bj > rW)
				{
					x = rW - wW - bj;
				}
				if (y + wH + bj > rH)
				{
					y = rH - wH - bj;
				}
			}
			else
			{
				if (y < bj)
				{
					y = bj;
				}
				if (x - 4 > rW)
				{
					x = rW + 4;
				}
				if (y + wsize + 2 - 4 > rH)
				{
					y = rH - wsize - 2 + 4;
				}
			}
		}
		move();
		if (!windowm)
		{
			if (clock() - ldraw < 20)
			{
				Sleep(20 - (clock() - ldraw));
			}
			ldraw = clock();
		}
	}
	closegraph();
	return 0;
}
//主函数

添加评论