贪吃蛇c++

学习c第一个月,记录一下学习成果。

使用了Easyx图形库,采取比较笨的办法,定义了两个包含大量元素的数组来存储蛇头坐标,以及很多标志位,让代码看起来很臃肿。在学习更多知识后会继续改进。

    int r_l = 0;//左右前进标志位
	int u_d = 0;//上下前进标志位
	int flag = 0;//按键标志位
	int count = 0;//干饭数量标志位
	int game = 0;//是否进入游戏标志位
	int play = 1;//是否进入菜单标志位
	int food = 0, foodx = 0, foody = 0;//食物存在标志位,食物坐标
	int x = 100, y = 100, sz = 3;//初始坐标,身体初始长度
	int arrx[1000];//最大长度1000
	int arry[1000];

在编写该程序使自己进一步认识了图形库相关函数,并对所学的指针等知识进行运用。 具体代码如下,运行结果为,使用WASD按键进行移动,继续加油。

#define _CRT_SECURE_NO_WARNNINGS
#include <stdio.h>
#include <easyx.h>
#include <time.h>
#include <stdlib.h>
#define width 450
#define height 450
int speed = 150;

void Init(int arrx[], int arry[], int* x, int* y)//初始位置及长度函数
{
	arrx[0] = *x;
	arry[0] = *y;
	arrx[1] = (*x) - 15;
	arry[1] = *y;
	arrx[2] = (*x) - 30;
	arry[2] = *y;
}
void Hand(int key, int* r_l, int* u_d, int* flag)//头部移动函数
{
	if (key == 68 && (*r_l == 0 || *r_l == 1))
	{
		*r_l = 1;
		*u_d = 0;
		*flag =  1;
	}
	else if (key == 87 && (*u_d == 0 || *u_d == 1))
	{
		*r_l = 0;
		*u_d = 1;
		*flag = 2;
	}
	else if (key == 65 && (*r_l == 0 || *r_l == 2))
	{
		*r_l = 2;
		*u_d = 0;
		*flag = 3;
	}
	else if (key == 83 && (*u_d == 0 || *u_d == 2))
	{
		*r_l = 0;
		*u_d = 2;
		*flag = 4;
	}
}
void Move(int arrx[], int arry[], int* sz, int* flag, int* x, int* y)//移动函数
{
	setfillcolor(YELLOW);
	for (int i = *sz - 1; i > 0; i--)
	{
		arrx[i] = arrx[i - 1];
		arry[i] = arry[i - 1];
		fillrectangle(arrx[i], arry[i], arrx[i] + 10, arry[i] + 10);
	}
	switch (*flag)
	{
	case 1:
		*x += 15;
		break;
	case 2:
		*y -= 15;
		break;
	case 3:
		*x -= 15;
		break;
	case 4:
		*y += 15;
		break;
	}
	arrx[0] = *x;
	arry[0] = *y;
	fillrectangle(arrx[0], arry[0], arrx[0] + 10, arry[0] + 10);
}
void Food(int* sz,int* food, int* foodx, int* foody, int arrx[], int arry[])//食物随机函数
{
	for (int i = 0; i < *sz; i++)
	{
		if (*food == 0)
		{
			*foodx = rand() % (width - 50);
			*foody = rand() % (height - 50);;
		}
		if (*foodx != arrx[i] && *foody != arry[i] && *foodx > 15 && *foody > 15)
		{
			*food = 1;
			setfillcolor(RED);
			fillrectangle(*foodx, *foody, (*foodx) + 10, (*foody) + 10);
			setfillcolor(WHITE);
			break;
		}
	}
}
void Eat(int* sz, int* food, int* foodx, int* foody, int* count, int arrx[], int arry[])//干饭函数
{
	if (arrx[0] <= (*foodx) + 10 && arrx[0] >= (*foodx) - 10 && arry[0] <= (*foody) + 10 && arry[0] >= (*foody) - 10)
	{
		(*sz)++;
		(*count)++;
		*food = 0;
	}
}
void Crash(int* sz, int* game, int arrx[], int arry[])//碰撞函数
{
	for (int i = 1; i < *sz; i++)
	{
		if (arrx[0] <= arrx[i] + 10 && arrx[0] >= arrx[i] && arry[0] <= arry[i] + 10 && arry[0] >= arry[i])
		{
			*game = 0;
		}
	}
	if (arrx[0] >= width - 10 || arrx[0] <= 10 || arry[0] >= height - 10 || arry[0] <= 10)
	{
		*game = 0;
	}
}
void wall(int* count,int* speed)//墙壁函数
{
	setfillcolor(BLUE);
	fillrectangle(0, 0, 10, height);
	fillrectangle(0, 0, width, 10);
	fillrectangle(0, height - 10, width, height);
	fillrectangle(width - 10, 0, width, height);
	if (*speed > 100)
	{
		*speed = 150 - *count;
	}
}
void End(int* game, int* count)//结束函数
{
	if (*game == 0)
	{
		setbkcolor(BLUE);
		cleardevice();
		settextcolor(RED);
		settextstyle(50, 0, "黑体", 0, 0, 0, 0, 1, 0);
		char str[] = "LOW逼";
		char num[4];
		sprintf_s(num, "%d", *count);//将数字转化为字符串
		int wid = textwidth(str);
		int heg = textheight(str);
		int wid1 = textwidth(num);
		int heg1 = textheight(num);
		outtextxy((width - wid) / 2, (height - heg) / 2 - 100, str);
		outtextxy((width - wid1) / 2, (height - heg1) / 2 - 10, num);
	}
}
void Menu(int* mx, int* my, int* mz, int* game, int* play)//菜单函数
{
	settextcolor(RED);
	settextstyle(30, 0, "黑体", 0, 0, 0, 0, 1, 0);
	setfillcolor(BLUE);
	fillrectangle(100, 100, 200, 150);
	fillrectangle(250, 100, 350, 150);
	outtextxy(110, 110, "开玩");
	outtextxy(260, 110, "玩屁");
	if (*mx <= 200 && *mx >= 100 && *my <= 150 && *my >= 100 && *mz == 1)
	{
		*game = 1;
		*play = 0;
	}
	else if (*mx <= 350 && *mx >= 250 && *my <= 150 && *my >= 100 && *mz == 1)
	{
		*game = 0;
		*play = 0;
		cleardevice();
		settextcolor(RED);
		settextstyle(30, 0, "黑体", 0, 0, 0, 0, 1, 0);
		char str[] = "娘希匹";
		int wid = textwidth(str);
		int heg = textheight(str);
		outtextxy((width - wid) / 2, (height - heg) / 2, str);
	}
}

int main()
{
	int r_l = 0;//左右前进标志位
	int u_d = 0;//上下前进标志位
	int flag = 0;//按键标志位
	int count = 0;//干饭数量标志位
	int game = 0;//是否进入游戏标志位
	int play = 1;//是否进入菜单标志位
	int food = 0, foodx = 0, foody = 0;//食物存在标志位,食物坐标
	int x = 100, y = 100, sz = 3;//初始坐标,身体初始长度
	int arrx[1000];//最大长度1000
	int arry[1000];
	initgraph(width, height);
	Init(arrx, arry, &x, &y);//初始化函数
	ExMessage msg;
	srand((unsigned int)time(NULL));
	while (play)
	{
		if (peekmessage(&msg, EM_MOUSE))
		{
			int mx = msg.x;
			int my = msg.y;
			int mz = msg.lbutton;
			Menu(&mx, &my, &mz, &game, &play);//菜单函数
		}
	}
	while (game)
	{
		BeginBatchDraw();
		cleardevice();
		if (peekmessage(&msg, EM_KEY))//现在结构体变量msg已经被存入信息,有按键按下时为真
		{
			Hand(msg.vkcode, &r_l, &u_d, &flag);//头部移动函数
		}
		wall(&count, &speed);//墙体函数
		Eat(&sz, &food, &foodx, &foody, &count, arrx, arry);//干饭函数
		Food(&sz, &food, &foodx, &foody, arrx, arry);//食物函数
		Crash(&sz, &game, arrx, arry);//碰撞函数
		End(&game, &count);//结束函数
		if (!flag)
		{
			for (int i = 0; i < sz; i++)
			{
				fillrectangle(arrx[i], arry[i], arrx[i] + 10, arry[i] + 10);
			}
		}
		else
		{
			Move(arrx, arry, &sz, &flag, &x, &y);
		}
		EndBatchDraw();
		Sleep(speed);
	}
	getchar();
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以给您提供一个简单的贪吃蛇C++代码,您可以参考一下: ```cpp #include <iostream> #include <conio.h> #include <windows.h> using namespace std; const int width = 20; const int height = 20; int x, y, fruitX, fruitY, score; bool gameOver; enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN }; eDirection dir; int tailX[100], tailY[100]; int nTail; void Setup() { gameOver = false; dir = STOP; x = width / 2; y = height / 2; fruitX = rand() % width; fruitY = rand() % height; score = 0; } void Draw() { system("cls"); cout << "Score: " << score << endl; for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (j == 0) cout << "#"; if (i == y && j == x) cout << "O"; else if (i == fruitY && j == fruitX) cout << "F"; else { bool print = false; for (int k = 0; k < nTail; k++) { if (tailX[k] == j && tailY[k] == i) { cout << "o"; print = true; } } if (!print) cout << " "; } if (j == width - 1) cout << "#"; } cout << endl; } for (int i = 0; i < width + 2; i++) cout << "#"; cout << endl; } void Input() { if (_kbhit()) { switch (_getch()) { case 'a': dir = LEFT; break; case 'd': dir = RIGHT; break; case 'w': dir = UP; break; case 's': dir = DOWN; break; case 'x': gameOver = true; break; } } } void Logic() { int prevX = tailX[0]; int prevY = tailY[0]; int prev2X, prev2Y; tailX[0] = x; tailY[0] = y; for (int i = 1; i < nTail; i++) { prev2X = tailX[i]; prev2Y = tailY[i]; tailX[i] = prevX; tailY[i] = prevY; prevX = prev2X; prevY = prev2Y; } switch (dir) { case LEFT: x--; break; case RIGHT: x++; break; case UP: y--; break; case DOWN: y++; break; default: break; } if (x >= width) x = 0; else if (x < 0) x = width - 1; if (y >= height) y = 0; else if (y < 0) y = height - 1; for (int i = 0; i < nTail; i++) if (tailX[i] == x && tailY[i] == y) gameOver = true; if (x == fruitX && y == fruitY) { score += 10; fruitX = rand() % width; fruitY = rand() % height; nTail++; } } int main() { Setup(); while (!gameOver) { Draw(); Input(); Logic(); Sleep(50); } return 0; } ``` 这是一个基于控制台的简单贪吃蛇游戏,您可以运行一下,看看效果如何。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值