用C/C++实现简单游戏开发:easyx实现幻彩贪吃蛇

效果:

标题

实现代码(粗略实现,有待优化):

#include
#include
#include
#include
#include
#include
using namespace std;
void PaintFrame();
void PaintStart();
void PaintLogin();
void PaintKeyboard();
void PaintEnd();
void PaintResume();
void CleanScreem();
void Snake();
void RandomBody(int x, int y);
void RandomFood(int x, int y);
bool KeyEnable(char c1,char c);
void Ruler();
HWND hwnd;

int leftin = 55, topin = 55, rightin = 795, bottomin = 595;
int main() {
	PaintFrame();
	PaintStart();
	PaintLogin();
	PaintResume();
	Snake();
	//getchar();
	PaintResume();
	PaintEnd();
}

void PaintFrame()
{
	hwnd = initgraph(1080, 640);
	setbkcolor(WHITE);
	cleardevice();
	//外边框
	setfillcolor(BLACK);
	fillroundrect(50, 50, 800, 600, 10, 10);
	//内边框
	CleanScreem();
}

void PaintStart()
{
	int locate1[4] = {1,0,0,0};
	vector locate(locate1, locate1+4);
	int left = (leftin + rightin) / 2 - 50,
		right = (leftin + rightin) / 2 - 50,
		top = (topin + bottomin) / 2 - 20,
		bottom = (topin + bottomin) / 2 + 20;
	setbkcolor(WHITE);
	setbkmode(OPAQUE);
	settextcolor(MAGENTA);
	settextstyle(50,40, _T("宋体"));
	TCHAR s[] = _T("WindowX");
	outtextxy(300, 240, s);
	for (int i = 0; i < 20; ++i) {
		Sleep(100);
		setfillcolor(WHITE);
		fillrectangle(left+100, top+60, right-100, bottom-60);
		for (int i = 0; i < 4; ++i) {
			if (locate[i]) {
				setfillcolor(YELLOW);
				fillcircle((left + right) / 2 + (i * 2 - 5) * 8 + 80,
					(top + bottom) / 2,
					8);
			}
			else {
				setfillcolor(GREEN);
				fillcircle((left + right) / 2 + (i * 2 - 5) * 8 + 80,
					(top + bottom) / 2,
					8);
			}
		}
		locate.push_back(locate[0]);
		locate.erase(locate.begin());
	}
	settextcolor(RED);
	moveto(20, 20);
}

void PaintLogin()
{
	CleanScreem();

	IMAGE img;
	loadimage(&img, _T("C:\\Users\\Administrator\\Desktop\\石原里美\\bk.png"));
	putimage(56,55,&img,SRCCOPY);

	MOUSEMSG ms;
}

void PaintKeyboard()
{

}

void PaintEnd()
{
	//EndBatchDraw();
	closegraph();
}

void PaintResume()
{
	IMAGE img;
	loadimage(&img, _T("C:\\Users\\Administrator\\Desktop\\石原里美\\bk.png"));
	putimage(56, 55, &img, SRCCOPY);
}

void CleanScreem()
{
	setfillcolor(WHITE);
	//fillroundrect(55, 55, 795, 575, 10, 10);
	fillrectangle(55, 55, 795, 575);
	setfillcolor(RGB(234,64,13));
	fillcircle(int(55 + 795) / 2, 586, 8);
}

void Snake()
{
	int dp[24][24] = {0};
	int len=4;
	int c='d',c1;
	int x = 0, y = 0;
	int startx = 75, starty = 75;
	dp[0][0] = len;
	srand(clock());
	while (x>=0&&x<24&&y>=0&&y<24) {
		for (int i = 0; i < 24; ++i) {
			for (int j = 0; j < 24; ++j) {
				if (dp[i][j]>0)dp[i][j]--;
			}
		}
		if (_kbhit() && (c1 = _getch(), 1));
		for (int k = 0; k < 200; ++k) {
			Sleep(1);
			if (_kbhit() && (c1 = _getch(), 1)) {
				if (KeyEnable(c1, c))
					c = c1;
			}
		}
		switch (c)
		{
		case 'w':
		case 'W': {
			if (dp[x - 1][y] == -1)len += 1;
			dp[x - 1][y] = dp[x - 1][y] <= 0 ? len : dp[x - 1][y];
			x--;
			break;
		}
		case 's':
		case 'S': {
			if (dp[x + 1][y] == -1)len += 1;
			dp[x + 1][y] = dp[x + 1][y] <= 0 ? len : dp[x + 1][y];
			x++;
			break;
		}
		case 'a':
		case 'A': {
			if (dp[x][y - 1] == -1)len += 1;
			dp[x][y - 1] = dp[x][y - 1] <= 0 ? len : dp[x][y - 1];
			y--;
			break;
		}
		case 'd':
		case 'D': {
			if (dp[x][y + 1] == -1)len += 1;
			dp[x][y + 1] = dp[x][y + 1] <= 0 ? len : dp[x][y + 1];
			y++;
			break;
		}
		default:
			break;
		}
		BeginBatchDraw();
		PaintResume();
		Ruler();
		for (int i = 0; i < 24; ++i) {
			for (int j = 0; j < 24; ++j) {
				if (dp[i][j] > 0)RandomBody(starty + j * 20, startx + i * 20);
				if (dp[i][j] < 0) { 
					RandomFood(starty + j * 20, startx + i * 20); 
				}
			}
		}
		EndBatchDraw();
		if (rand() % 100 > 90)
			dp[rand() % 24][rand() % 24] = dp[rand() % 24][rand() % 24] > 0 ? dp[rand() % 24][rand() % 24] : -1;
	}

	MessageBox(hwnd, TEXT("游戏结束!"), TEXT("贪吃蛇"), MB_OK);
}

void RandomBody(int x,int y)
{
	for (int i = 0; i < 4; i++) {
		setcolor(RGB(rand() % 150, rand() % 150, rand() % 150));
		fillrectangle(x+i*3,y+i*3,x+20-i*3,y+20-i*3);
		FlushBatchDraw();
	}
}

void RandomFood(int x, int y)
{
	/**也可以用图片作为食物,但需要适合尺寸
	IMAGE img;
	loadimage(&img, _T("C:\\Users\\Administrator\\Desktop\\石原里美\\水果.png"));
	putimage(y, x, &img, SRCCOPY);
	*/
	setfillcolor(RGB(0, rand() % 200 + 55, rand() % 200 + 55));
	fillrectangle(x , y , x + 20, y + 20);
	FlushBatchDraw();
}

bool KeyEnable(char c1, char c)
{
	if (c1 == 'A' || c1 == 'a' ||
		c1 == 'D' || c1 == 'd' ||
		c1 == 'W' || c1 == 'w' ||
		c1 == 'S' || c1 == 's') {
		switch (c)
		{
		case 'w':
		case 'W': {
			if (c1 != 's' && c1 != 'S')
				return true;
			break;
		}
		case 's':
		case 'S': {
			if (c1 != 'w' && c1 != 'W')
				return true;
			break;
		}
		case 'a':
		case 'A': {
			if (c1 != 'd' && c1 != 'D')
				return true;
			break;
		}
		case 'd':
		case 'D': {
			if (c1 != 'a' && c1 != 'A')
				return true;
			break;
		}
		default:
			break;
		}
	}
	return false;
}

void Ruler()#网格度量
{
	int startx = 75, starty = 75;
	int endx = 552, endy = 552;
	setcolor(RGB(230, 231, 232));
	for (int i = 0; i < 25; ++i) {
		for (int j = 0; j < 25; ++j) {
			line(startx + i * 20, starty, startx + i * 20, endy);
			line(startx, starty + i * 20, endx, starty + i * 20);
		}
	}
}

 

你可能感兴趣的:(c++)