HDU-游戏程序设计-蛇身数组更正版

上一篇笔记里面,我的蛇身出现了很多问题。花了很长时间修改,最后还有来自cwh的改正。现在蛇身基本没有问题。非常欣慰。cwhyyds
我们先来回顾一下上次写的蛇身,为什么写的不好。

void Logic()
{	
	int prevX = tailX[0];
	int prevY = tailY[0];
	int prev2X;
	int prev2Y;
	tailX[0] = x;
	tailY[0] = y;
	/*蛇头的标记是,tailX[0],第0节蛇身是蛇头。
*/
	for (i = 1; i < nTail; i++)
/*
i从1开始,1到ntail,
*/
	{
		prev2X = tailX[i];
		prev2Y = tailY[i];
		tailX[i] = prevX;
		tailY[i] = prevX;
		prevX = prev2X;
		prevY = prev2Y;

//这里是想要把前一个的坐标赋给后一个。这样后面的蛇身就会出现在限一个蛇身的位置。比如第2个在(10,90),第三个在(12,91),那么把第二个的数据传给三个。
//但是这么写其实没有用上prev2x,2y这个temp,

```cpp
在这里插入代码片

改良版:

	for (i = nTail; i >= 1; i--)
	{
		tailX[i] = tailX[i - 1];
		tailY[i] = tailY[i - 1];

	}
	tailX[0] = x;
	tailY[0] = y;
	
	cout << tailY[1];
	cout << tailX[1];

在这之后再次出现问题:第一个蛇身没有被打印出来。一直到吃到第二个果子,才出现第一节蛇身tailx[1],taily[1].
cwh做出到位的改正。在吃到第一个水果时,ntail=1,在draw函数中,k=1, k<ntail函数不成立。无法画出尾巴。
所以把ntail初始化为ntail=1,吃到水果后ntail=2,复合draw的判断。画出第一节蛇身。
更正后的代码如下,现在面临的问题是:
1.蛇身跟随头没有拐角处理
2.只有按键时才会触发蛇身打印。不按键就不显示蛇身
3.墙体突出一节。应该把空格,o,O,F设置为四选一的互斥事件,只打印一个内容。
4.屏幕闪烁,下一章引入双缓冲的概念
附上代码

#include<iostream>;
#include<windows.h>;
#include<conio.h>;
using namespace std;

bool gameOver;
const int width = 20;
const int height = 20;
int x, y, fruitX, fruitY, score;
int tailX[100], tailY[100];
int nTail=1;
int i, j;
enum eDirection { STOP = 0,LEFT,RIGHT,UP,DOWN };
eDirection dir;


void Initial()
{
	gameOver = false;
	dir = STOP;
	x = width / 2;
	y = height / 2;
	fruitX = rand() % width;
	fruitY = rand() % height;
}
void Draw()
{
	system("cls");
	HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
	int textColor = 0x06;
	SetConsoleTextAttribute(h, textColor);
	for (int i = 0; i < width+2; i++)
		cout << "1";
	cout << endl;
	for (int j = 0; j < height; j++)
	{
		for (int i = 0; i < width; i++)
		{
			if (i == 0)
				cout << "2";
			if (i == x && j == y) {
				textColor = 0X0a;
				SetConsoleTextAttribute(h, textColor);
				cout << "O";
			}

			else if (i == fruitX && j == fruitY)
			{
				textColor = 0x084;
				SetConsoleTextAttribute(h, textColor);
				cout << "F";
			}
			else 
			{
				for (int k = 1; k < nTail; k++)
				{
					//todo:画尾巴
					if (tailX[k] == i && tailY[k] == j) 
					{
						cout << "o";
					}
				}
				textColor = 0x06;
				SetConsoleTextAttribute(h, textColor);
				cout << " ";

			}
			
			if (i == width - 1)
				cout << "3";

		}
		cout << endl;

	}
	for (i = 0; i < width+2; i++)
		cout << "4";
	/*cout << endl;
	cout << "游戏得分" << score << endl;
	cout << tailX[1] << "tailX[1]" << endl;
	cout << tailY[1] << "taily[1]" << endl;
	cout << x << "xzuobiao" << endl;
	cout << y << "zuobiaoy" << endl;
	cout << fruitX << "fruitx" << endl;
	cout << fruitY << "fruity" << endl;
	*/
	cout << endl;
}

void Input()
{
	if (_kbhit())
	{
		switch (_getch())
		{
		case'a':
			dir = LEFT;
			x--;
		
			break;
		case'd':
			dir = RIGHT;
			x++;
		
			break;
		case'w':
			dir = UP;
			y--;
		
			break;
		case's':
			dir = DOWN;
			y++;
		
			break;
		case'x':
			gameOver = true;
			break;
		default:
			break;
		}
	}
}
void Logic()
{	
	for (i = nTail; i >= 1; i--)
	{
		tailX[i] = tailX[i - 1];
		tailY[i] = tailY[i - 1];

	}
	tailX[0] = x;
	tailY[0] = y;
	
	cout << tailY[1];
	cout << tailX[1];
	
	for (int i  = 0; i < nTail; i++)
		if (tailX[i] == x && tailY[i] == y)
			gameOver == true;

	if (x > width || x<0 || y>height || y < 0)
		gameOver = true;
	/*if (x >= width) x = 0;
	else if (x < 0) 
		x = width - 1;   
	if (y >= height)y = 0; 
	else if (y < 0) 
		y = height - 1;*/
	if (x == fruitX && y == fruitY)
	{
		score += 10;
		fruitX = rand() % width;	
		fruitY = rand() % height;
		nTail++;

	}

}

int main()
{	
	

	Initial();
	while (!gameOver)
	{
		Draw();
		Input();
		
		Logic();
	}
	
	system("pause");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值