栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++跳高高小游戏初级版,带详细注释,简单易懂

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++跳高高小游戏初级版,带详细注释,简单易懂

#include 
#include 
#include 

using namespace std;
bool gameOver;//判断游戏开始结束
int  score,m;
//声明函数
void Setup();
void Draw();
void Input();
void Logic();
地图范围///
const int width = 40;
const int height = 10;


//主角坐标///
int x = 8;
int y = 6;
int n = 38;//地图横向坐标
int up = 0;

/主函数
int main1(void)
{
	Setup();
	while (!gameOver)
	{
		Draw();
		Input();
		Logic();
		Sleep(20);

	}
	return 0;
}

///初始化程序///
void Setup()
{
	
	gameOver = false;
	//m = rand() % height;//暂时没用到;
	score = 0;
}
//游戏逻辑/
void Logic()
{
	
	n--;
	if (n == 0)
		n = 38;
	if (n == 6)
		score = score + 1;
	if (x == 8 && y == n)
		gameOver = true;
	if (x == 6 && y == n)
		gameOver = true;
	if (x == 7 && y == n)
		gameOver = true;
	
///主角跳跃实现代码	
	if (up == 1&&x>4)
	{
		x--;
		if (x == 4)
			up = 2;
	}
	if (up == 2)
	{
		x++;
		if (x == 8)
			up = 0;
	}
}

绘制背景板
void Draw()
{
	system("cls");
	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 == x && j == y)
				cout << "O";
		    else if (i == 8 && j == n)
				cout << "#";
			else if (i == 7 && j == n)
				cout << "#";
			else if (i == 6 && j == n)
				cout << "#";
			else if (i == 8 && j < width)
				cout << "_";
			else if (j == width)
				cout << "*";
			else
				cout << " ";
		}
		cout << endl;
	}
	for (int i = 0; i < width + 2; i++)//画底部边界
		cout << "*";
	cout << endl;
	//cout << up << endl;
	cout << "score: " << score << endl;
}

/输入///
void Input()
{
	
	if (_kbhit())
	{
		switch (_getch())
		{
		case 72://小键盘↑键,控制跳跃
			up = 1;
			break;
		case 'x'://x键退出游戏
			gameOver = true;
			break;

		}
	}
}

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/433644.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号