首页 > 编程语言 >C++:自治我的世界2D.V0.0.4.5

C++:自治我的世界2D.V0.0.4.5

时间:2024-10-09 18:23:09浏览次数:3  
标签:4.5 map MC int altitude C++ 2D 2000 void

更新内容:增加挖掘进度,挖掘需要时间了,但还有BUG

操作说明:A,D移动;W跳跃;上,下,右+上,右+下,左+上,左+下撸方块;M开关大地图

#include<bits/stdc++.h> 
#include<windows.h> 
#define KEY_DOWN(VK_NONAME) ((GetAsyncKeyState(VK_NONAME) & 0x8000) ? 1:0) 
using namespace std;


void move();//玩家移动 
void Generate_terrain();//生成地形 
int random(int l,int r);//生成l到r的随机数 
void Color(int a,int b);//切换字体颜色
string block_name(int a);//方块id转名称 
void mapcolor(int a);//切换当前字符颜色
void mapdisplay(int s);//输出当前字符对应的形态
string label(int a);//是否有碰撞,木制,石制,土制
int Block_hardness(int a);//方块硬度,-1表示不可破坏
void hidden(bool a);//0隐藏光标,1显示光标
void paint(int x,int y,string s);//在x,y地覆盖并输出s 
void gotoxy(int x,int y);//移动坐标 




int MC_map[2000][2000];//地图 
int Scene_color=11;//背景颜色 
double the_time=80,sx=0,sy=0;//时间,加速度 
int px=100,py=1000,Scene_size_x=7,Scene_size_y=4;//玩家位置x,玩家位置y,屏幕大小x,屏幕大小y
int Falling_objects[1000],Ep=-100,ex=-10,ey=-10;//掉落物,Excavation progress--->挖掘进度
struct theInventory
{
	int name;
	int number;
};
theInventory Inventory[4][9];


int main()
{
	hidden(0); 
	srand((unsigned)1);
	Generate_terrain();//生成地形
	
	
	for(long long start=0;;start++)
	{
		the_time+=0.1;//时间流逝 
		if(the_time>240) the_time=0;//0点时从头再来 
		if(70<the_time&&the_time<=180) Scene_color=11;//白天 
		if(180<the_time&&the_time<=200) Scene_color=3;//傍晚 
		if(200<the_time||the_time<=50) Scene_color=1;//深夜 
		if(50<the_time&&the_time<=70) Scene_color=3;//凌晨 
		
		
		for(int x=py-Scene_size_y;x<py+Scene_size_y+1;x++)//输出界面 
		{
			for(int y=px-Scene_size_x;y<px+Scene_size_x+1;y++)
			{
				int n=(y-px+Scene_size_x)*2,m=x-py+Scene_size_y;
			    if(x>=0&&x<2000&&y>=0&&y<2000)    gotoxy((y-px+Scene_size_x)*2,x-py+Scene_size_y)    ,mapcolor(MC_map[x][y]),mapdisplay(MC_map[x][y]);
			    int a=int(double(double(int(the_time+40)%240)/90*(Scene_size_x*2))),b=Scene_size_y/2-1;
				Color(14,14);
				if((y-px+Scene_size_x)*2==a*2&&x-py+Scene_size_y==b&&MC_map[x][y]==0&&Scene_color==1) gotoxy(a*2,b),cout<<"  ";
				if((y-px+Scene_size_x)*2==a*2&&x-py+Scene_size_y==b+1&&MC_map[x][y]==0&&Scene_color==1) gotoxy(a*2,b+1),cout<<"  ";
		        if((y-px+Scene_size_x)*2==a*2+2&&x-py+Scene_size_y==b+2&&MC_map[x][y]==0&&Scene_color==1) gotoxy(a*2+2,b+2),cout<<"  ";
				if((y-px+Scene_size_x)*2==a*2+2&&x-py+Scene_size_y==b-1&&MC_map[x][y]==0&&Scene_color==1) gotoxy(a*2+2,b-1),cout<<"  ";
				if(x==py-1&&y==px){gotoxy(n,m),Color(0,14);cout<<"''";}//输出头 
			    else if(x==py&&y==px){gotoxy(n,m),Color(9,9);cout<<"  ";}//输出身体*/ 
				
			}
			Color(0,0); 
			cout<<'/'<<endl;
		}
		
		Color(7,0);
		cout<<px<<" "<<py<<" "<<Ep<<"           "<<the_time<<"           ";
		
		
		
		Sleep(100);//每0.15秒执行一帧 
		move(); 
		
	}
	return 0;
}






void Game_begins()
{
	
}
void move()//玩家移动
{
	string sb=label(MC_map[py+1][px]);
	if(KEY_DOWN('W')&&MC_map[py+1][px]!=0&&sb[0]=='1') sy=-1;//跳跃 
	if(KEY_DOWN('D')) sx+=1;//右移动
	if(KEY_DOWN('A')) sx-=1;//左移动
	int x=-10,y=-10;//被挖掘的方块坐标 
	if(KEY_DOWN(39)&&KEY_DOWN(38)) x=px+1,y=py-1;//右,上 
	else if(KEY_DOWN(39)&&KEY_DOWN(40)) x=px+1,y=py;//右,下 
	else if(KEY_DOWN(37)&&KEY_DOWN(38)) x=px-1,y=py-1;//左,上 
	else if(KEY_DOWN(37)&&KEY_DOWN(40)) x=px-1,y=py;//左,下
	else if(KEY_DOWN(38)) x=px,y=py-2;//上 
	else if(KEY_DOWN(40)) x=px,y=py+1;//下 
	
	
	if(ex!=x&&ey!=y&&MC_map[y][x]!=0) Ep=Block_hardness(MC_map[y][x]),ex=x,ey=y;//当开始挖掘并且 
	if(ex==x&&ey==y&&x!=-1) Ep--;
	if(x==-10&&y==-10) Ep=-100,ex=-10,ey=-10;
	if(Ep<=0&&Ep!=-100) MC_map[y][x]=0,Ep=-100;
	if(MC_map[y][x]!=0&&Ep==-100) Ep=Block_hardness(MC_map[y][x]),ex=x,ey=y;
	
	
	if(KEY_DOWN('M'))//展示地形图 
	{
		system("cls");
		for(int x=py-100;x<py+100;x++)
		{
			for(int y=px-100;y<px+100;y++)
			{
			    if(x==py-1&&y==px){Color(0,14);cout<<"''";}
			    else if(x==py&&y==px){Color(9,9);cout<<"  ";}
			    else if(x>=0&&x<2000&&y>=0&&y<2000) mapcolor(MC_map[x][y]),mapdisplay(MC_map[x][y]);
	   		}
			Color(0,0); 
			cout<<'/'<<endl;
		}
		while(!KEY_DOWN('M'));//直到按下M关闭地图 
	}
	hidden(0);
	for(int s=0;s<abs(int(sx));s++)//x移动 	
		{
		if(px>=0&&px<=1999) px+=int(sx)/abs(int(sx));
		if(px<0) px=0;
		if(px>=2000) px=1999;
		string SB1=label(MC_map[py-1][px]),SB2=label(MC_map[py][px]);
		if(SB1[0]=='1'||SB2[0]=='1')  {px-=int(sx)/abs(int(sx)),sx=0;break;} 
	}
	for(int s=0;s<abs(int(sy));s++)//y移动 
	{
		if(py>=0&&py<2000) py+=int(sy)/abs(int(sy));
		if(py<0) py=0;
		if(py>=2000) py=1999;
		string SB1=label(MC_map[py-1][px]),SB2=label(MC_map[py][px]);
		if(SB1[0]=='1'||SB2[0]=='1')  {py-=int(sy)/abs(int(sy)),sy=0;break;} 
	}
	sx*=0.5;//摩擦力 
	sy+=1;//重力 
}
void Generate_terrain()//生成地形 
{
	int altitude=700;
	for(int x=0;x<2000;x++) for(int y=0;y<2000;y++) MC_map[y][x]=0;
	for(int x=0;x<2000;x++) 
	{
		int a=int(2000-double(altitude)/50*49+random(-1,1));
		MC_map[1999][x]=1;
		for(int y=1998;y>a;y--) MC_map[y][x]=4;
		for(int y=a;y>2000-altitude;y--) MC_map[y][x]=3;
		MC_map[2000-altitude][x]=2;
		int sb=rand();
		if(sb%50==5)//生成树 
		{
			MC_map[2000-altitude-1][x]=5;
			MC_map[2000-altitude-2][x]=5;
			MC_map[2000-altitude-3][x]=5;
			MC_map[2000-altitude-4][x]=5;
			MC_map[2000-altitude-4][x-1]=6;
			MC_map[2000-altitude-4][x-2]=6;
			MC_map[2000-altitude-4][x+1]=6;
			MC_map[2000-altitude-4][x+2]=6;
			MC_map[2000-altitude-5][x]=6;
			MC_map[2000-altitude-5][x-1]=6;
			MC_map[2000-altitude-5][x-2]=6;
			MC_map[2000-altitude-5][x+1]=6; 
			MC_map[2000-altitude-5][x+2]=6;
			MC_map[2000-altitude-6][x]=6;
			MC_map[2000-altitude-6][x+1]=6;      
			MC_map[2000-altitude-6][x-1]=6;
		}
		else if(sb%10==2) MC_map[2000-altitude-1][x]=7;
		if(altitude<800) altitude+=random(0,1);
		if(altitude>600) altitude+=random(-1,0);
	}
}
void Generate_tree(int x,int y)
{
	bool a=1;
	//for(int i=)
} 
void make_Falling_objects(int a)
{
	for(int i=0;i<4;i++) for(int j=0;j<9;j++) if(Inventory[i][j].name==a&&Inventory[i][j].number<64) 
	{
		Inventory[i][j].number++;
		return;
	}
	for(int i=0;i<4;i++) for(int j=0;j<9;j++) if(Inventory[i][j].name==0) 
	{
		Inventory[i][j].name=a;
		Inventory[i][j].number++;
		return;
	}
}
int random(int l,int r)//生成l到r的随机数 
{
	return (rand()%(r-l+1))+l;
}
void Color(int a,int b)//切换字体颜色 
{
	int c;
	if(b>=1)c=a+b*16;
	else c=a;
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
string block_name(int a)//方块id转名称 
{
	if(a==0) return "空气";
	if(a==1) return "基岩";
	if(a==2) return "草块";
	if(a==3) return "泥土";
	if(a==4) return "石头";
	if(a==5) return "橡木";
	if(a==6) return "树叶";
	if(a==7) return "草丛";
}
void mapcolor(int a)//颜色转化
{
	if(a==0) Color(Scene_color,Scene_color);
	if(a==1) Color(0,8);
	if(a==2) Color(6,10);
	if(a==3) Color(6,6);
	if(a==4) Color(8,8);
	if(a==5) Color(8,6);
	if(a==6) Color(10,2);
	if(a==7) Color(10,Scene_color);//"x-"指没有碰撞箱 
}
void mapdisplay(int s)//输出当前字符对应的形态 
{
	if(s==0) cout<<"  ";
	if(s==1) cout<<"▓▓";
	if(s==2) cout<<"▅▅"; 
	if(s==3) cout<<"■";
	if(s==4) cout<<"■";
	if(s==5) cout<<"||";
	if(s==6) cout<<"▓▓";
	if(s==7) cout<<"▍▍";
}
string label(int a)//方块标签:是否有碰撞,木制,石制,土制 
{
	string s="0000"; 
	if(a==0) ;
	if(a==1) s[0]='1';
	if(a==2) s[0]='1',s[3]='1';
	if(a==3) s[0]='1',s[3]='1';
	if(a==4) s[0]='1',s[2]='1';
	if(a==5) s[0]='1',s[1]='1';
	if(a==6) s[0]='1';
	if(a==7) ;
	return s;
}
int Block_hardness(int a)//方块硬度,-1表示不可破坏
{
	if(a==0) return -1;
	if(a==1) return -1;
	if(a==2) return 4;
	if(a==3) return 4;
	if(a==4) return 12;
	if(a==5) return 7;
	if(a==6) return 1;
	if(a==7) return 1;
}
void hidden(bool a)//隐藏光标
{ 
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO cci;
	GetConsoleCursorInfo(hOut,&cci);
	cci.bVisible=a;//赋1为显示,赋0为隐藏
	SetConsoleCursorInfo(hOut,&cci);
}
void paint(int x,int y,string s)// 在x,y地覆盖并输出s 
{
	gotoxy(y,x);
	cout<<s;
}
void gotoxy(int x, int y)//覆盖清屏 ,从指定行列覆盖
{
	COORD pos = {x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut, pos);
	return ;
}

标签:4.5,map,MC,int,altitude,C++,2D,2000,void
From: https://blog.csdn.net/jxh1145/article/details/142693758

相关文章

  • C++消灭星星游戏编程【目录】
    欢迎来到zhooyu的专栏。主页:【zhooyu】专栏:【C++消灭星星游戏编程】特色:【保姆级教程,含每一课程源码】致力于用最简洁的语言,最简单的方式,最易懂的知识,带大家享受编程的快乐。消灭星星游戏编程演示效果消灭星星游戏编程演示效果本专栏内容:消灭星星的小游戏保姆......
  • 实验一 C++
    实验任务1:task1.cpp:1#include<iostream>2#include<string>3#include<vector>4#include<algorithm>56usingnamespacestd;78//声明9//模板函数声明10template<typenameT>11voidoutput(constT&c);1213......
  • 实验1 现代C++编程初体验
    任务一#include<iostream>#include<string>#include<vector>#include<algorithm>usingnamespacestd;template<typenameT>voidoutput(constT&c);voidtest1();voidtest2();voidtest3();intmain(){cout<<&qu......
  • C++编译并运行后出现Process finished with exit code 139 (interrupted by signal 11
    问题描述:        代码运行意外终止,报错信息为Processfinishedwithexitcode139(interruptedbysignal11:SIGSEGV)CMakeList文件如下:cmake_minimum_required(VERSION3.26)project(SLAM)set(CMAKE_CXX_STANDARD17)set(CMAKE_CXX_STANDARD_REQUIRED......
  • 调用sdapi/v1/txt2img接口,报错“Couldn‘t load custom C++ ops”
    后端启动stable_diffusion的api接口nohuppythonlaunch.py --use-cpuall--skip-torch-cuda-test   --api--api-log  --listen--server-name192.168.1.204>/home/third_party_app/llm/stable-diffusion-webui/logs/all.log2>&1 &服务接口http://192.168......
  • (2024最新毕设合集)基于SpringBoot的乡村书屋小程序-31881|可做计算机毕业设计JAVA、PHP
    摘要随着信息技术的快速发展和互联网的广泛普及,数字化服务的需求不断增长,乡村书屋作为传统的文化服务机构也需要适应这一变革。本研究将使用Java开发技术,通过springboot作为框架,结合微信小程序,和MySQL作为数据存储的技术,开发一套功能齐备可移动的乡村书屋小程序,旨在提升乡......
  • 【C++】priority_queue的介绍和模拟实现
    【C++】priority_queue的介绍和模拟实现一.priority_queue的介绍1.priority_queue的基本介绍2.priority_queue的使用介绍二.priority_queue的模拟实现一.priority_queue的介绍1.priority_queue的基本介绍优先队列是一种容器适配器,根据严格的弱排序标准,它的......
  • C++ day04(友元 friend、运算符重载、String字符串)
    目录【1】友元friend1》概念2》友元函数 3》友元类 4》友元成员函数 【2】运算符重载1》概念2》友元函数运算符重载 ​编辑 3》成员函数运算符重载4》赋值运算符与类型转换运算符重载 5》注意事项【3】String字符串类【1】友元friend1》概念定义:......
  • 《 C++ 修炼全景指南:十四 》大数据杀手锏:揭秘 C++ 中 BitSet 与 BloomFilter 的神奇性
    本篇博客深入探讨了C++中的两种重要数据结构——BitSet和BloomFilter。我们首先介绍了它们的基本概念和使用场景,然后详细分析了它们的实现方法,包括高效接口设计和性能优化策略。接着,我们通过对比这两种数据结构的性能,探讨了在不同应用场景中的选择依据。最后,博客还涵盖......
  • 【C++】二叉搜索树
    文章目录1、二叉搜索树的说明性2、二叉搜索树2.1二叉搜索树的概念2.2二叉搜索树的操作2.2.1插入2.2.2查找2.2.3删除2.3二叉搜索树的实现2.4二叉搜索树的应用二叉搜索树的性能分析1、二叉搜索树的说明性map和set特性需要先铺垫二叉搜索树,而二叉搜索树也是一种树形......