c++简易贪吃蛇程序(主打一个入手快,易懂,有成就感嘿嘿)

#include <iostream>
#include <conio.h>
#include <windows.h>
#include <cstdio>//C

using namespace std;
bool gameOver;//布尔判断游戏是否结束
const int width = 40;//画板40*20
const int height = 20;
int x, y, fruitx, fruity, score;
int tailX[100], tailY[100];
int nTail;

enum eDirection { STOP = 0, LEFT, RIGHT, UP, DOWN };
eDirection dir;//枚举上下左右

void Setup()//游戏初始化
{
	gameOver = false;
	dir = STOP;
	x = width / 2;//小蛇出生位置坐标
	y = height / 2;
	fruitx = rand() % width;//随机生成水果坐标
	fruity = rand() % height;
	score = 0;
}
void Draw()//画布
{
	system("cls");//Linux用"clear"cpu刷新
	cout << "          贪吃蛇小游戏   游戏规则" << endl;
	cout << "  使用小键盘↑↓←→控制小蛇吃果子(F),"<<endl<<"不可以碰到墙壁和自己的身体哦!-----swu_ly"<<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;
	cout << "Score:" << score << endl;//得分板
}
void Input()//输入小键盘上下左右控制
{
	if (_kbhit())
	{
		switch (_getch())
		{
		case 75:
			dir = LEFT;
			break;
		case 77:
			dir = RIGHT;
			break;
		case 72:
			dir = UP;
			break;
		case 80:
			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 || y>height || y < 0)//不允许穿墙
	{
		int x;
		gameOver = true;
		cout << "gameover!\a";
		x = MessageBox(GetForegroundWindow(), "您撞墙啦,游戏结束!", "gameover!", 1);
	}

	/*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)
		{
			int x;
			gameOver = true;
			cout << "gameover!\a";
			x = MessageBox(GetForegroundWindow(), "您吃到自己啦,游戏结束!", "gameover!", 1);
			
		}
	if (x == fruitx && y == fruity)//水果被吃后重新生成,并且加十分
	{

		score += 10;
		fruitx = rand() % width;
		fruity = rand() % height;
		nTail++;
	}
}

int main(int argc, char **argv)
{
	Setup();
	while (!gameOver)
	{
		Draw();
		Input();
		Logic();
		Sleep(20); Sleep(20);//调节小蛇速度

	}
	return 0;
}

 

 

  • 13
    点赞
  • 55
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

皮皮牛牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值