c++沙盒小游戏1.0

         YUTU做了3个文件:沙盒.cpp、game.h、sh.h。

Switch、case、getch控制键盘热键,相信各位大神也知道。

system(“cls”);清屏。

最底端的

sh.h(一定要用这个文件名,大神除外)

#include<bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <conio.h>
#include <cmath>
#include <windows.h>
using namespace std;
char cz[15][30];
void printf_red(const char *s)
{
    printf("\033[0m\033[1;31m%s\033[0m", s);
}

void printf_green(const char *s)
{
    printf("\033[0m\033[1;32m%s\033[0m", s);
}

void printf_yellow(const char *s)
{
    printf("\033[0m\033[1;33m%s\033[0m", s);
}

void printf_blue(const char *s)
{
    printf("\033[0m\033[1;34m%s\033[0m", s);
}

void printf_pink(const char *s)
{
    printf("\033[0m\033[1;35m%s\033[0m", s);
}

void printf_cyan(const char *s)
{
    printf("\033[0m\033[1;36m%s\033[0m", s);
}
void czms(){
	char kejiajunchupin,fx,fk=' ';
	int sdf1=8,sdf2=8;
	while(kejiajunchupin!=27){
		kejiajunchupin=getch();
		switch(kejiajunchupin){
			case 'w':
			 		if(1){
			 			sdf1--;
	  }

					 fx='w';
					 system("cls");
					 break;
				case 's':
					if(1){
					sdf1++;
					 }

					 fx='s';
					 system("cls");
					 break;
			 	case 'a':
			 		if(1){
					 sdf2--;
					 }

					 fx='a';
					 system("cls");
					 break;
			 	case 'd':
			 		if(1){
					sdf2++;
					 }

					 fx='d';
					 system("cls");
					 break;
				case 't':
					fk=getch();
					system("cls");
					 break;
				case ' ':


					 if(fx=='w'){
					cz[sdf1-1][sdf2]=fk;
					}else if(fx=='s'){
					cz[sdf1+1][sdf2]=fk;
					}else if(fx=='a'){
					cz[sdf1][sdf2-1]=fk;
					}else if(fx=='d'){
					cz[sdf1][sdf2+1]=fk;
					}
					system("cls");
					break;
				default :
					system("cls");
					break;
		}
		for(int i=0;i<15;i++){
for(int j=0;j<30;j++){
if(i==sdf1&&j==sdf2){
	if(fx=='w'){
		cout<<"上";
	}else if(fx=='s'){
		cout<<"下";
	}else if(fx=='a'){
		cout<<"左";
	}else if(fx=='d'){
		cout<<"右";
	}
}else if(cz[i][j]>31&&cz[i][j]<127){
	cout<<cz[i][j]<<cz[i][j];
}else{
	cout<<cz[i][j];
}
}
cout<<endl;
}
	cout<<"按t切换方块,按空格放方块(方块初始为空格),按ESC退出";

	}
}

接着是game.h(除了大神,萌新一定一定要用这个文件名称)

#include"sh.h"
using namespace std;
void m(){
	printf_red(" ");
	system("mode con cols=120 lines=35");
	system("title YUTU制作——沙盒游戏");
	system("cls");
	char dt[15][30],kejiajunchupin,fx,sj=60;
	int sdf1=8,sdf2=8,g=1,die,gw1=9,gw2=9,hp=10,gwhp=1;
	
	for(int i=0;i<15;i++){
		for(int j=0;j<30;j++){
				dt[i][j]='#';
				cz[i][j]=' ';
		}
	}
	dt[9][9]='~';
	dt[30][30]='~';
	dt[30][29]='~';
	dt[30][28]='~';
	dt[30][27]='~';
	dt[29][27]='~';
	dt[3][30]='~';
	dt[3][29]='~';
	dt[3][28]='~';
	dt[3][27]='~';
	dt[4][27]='~';
	dt[7][7]='w';
	dt[5][5]='w';
	dt[6][6]='w';
	dt[10][10]='w';
	dt[21][21]='w';
	dt[22][30]='w';
	dt[3][29]='w';
	dt[3][28]='w';
	dt[3][27]='w';
	dt[4][27]='w';
	dt[8][8]=' ';
	dt[10][9]=' ';dt[10][10]=' ';dt[10][11]=' ';dt[9][9]=' ';dt[9][10]='~';dt[9][11]='~';
	string name;
	cout<<"你的名字叫什么?\n";
	cin>>name;
	system("cls");
		while(1){
		
for(int i=0;i<15;i++){
for(int j=0;j<30;j++){
if(i==sdf1&&j==sdf2){
	if(fx=='w'){
		cout<<"上玩";
	}else if(fx=='s'){
		cout<<"下玩";
	}else if(fx=='a'){
		cout<<"左玩";
	}else if(fx=='d'){
		cout<<"右玩";
	}
	if(dt[i][j]=='~'){
		hp--;
		die=1;
	}
	if(hp==0){
		sdf1=8;
		sdf2=8;//
	}
}else if(i==gw1&&j==gw2){
	cout<<"(@@)";
if(dt[gw1][gw2]=='~'){
	gwhp=0;
	gw1=100;
}
}else{
	if(sj>50&&sj<170){
		if(dt[i][j]=='#'){
			cout<<"■■";
		}else if(dt[i][j]=='@'){
			printf_yellow("■■");
		}else if(dt[i][j]=='~'){
			printf_red("■■");
		}else if(dt[i][j]=='w'){
			printf_blue("");
		}else{
			cout<<dt[i][j]<<dt[i][j]<<dt[i][j]<<dt[i][j];
		}
	}else{
			if(dt[i][j]=='~'){
			printf_red("■■");
		}else if(dt[i][j]=='w'){
			printf_blue("");
		}else{
			cout<<dt[i][j]<<dt[i][j]<<dt[i][j]<<dt[i][j];
		}
	}
}
}
cout<<endl;
for(int j=0;j<30;j++){
if(i==sdf1&&j==sdf2){
	if(fx=='w'){
		cout<<"↑家";
	}else if(fx=='s'){
		cout<<"↓家";
	}else if(fx=='a'){
		cout<<"←家";
	}else if(fx=='d'){
		cout<<"→家";
	}
	}else if(i==gw1&&j==gw2){
	cout<<"u怪u";

}else{
	if(sj>50&&sj<170){
		if(dt[i][j]=='#'){
			cout<<"■■";
		}else if(dt[i][j]=='@'){
			printf_yellow("■■");
		}else if(dt[i][j]=='~'){
			printf_red("■■");
		}else if(dt[i][j]=='w'){
			printf_blue("");
		}else{
			cout<<dt[i][j]<<dt[i][j]<<dt[i][j]<<dt[i][j];
		}
	}else{
			if(dt[i][j]=='~'){
			printf_red("■■");
		}else if(dt[i][j]=='w'){
			printf_blue("");
		}else{
			cout<<dt[i][j]<<dt[i][j]<<dt[i][j]<<dt[i][j];
		}
	}
}
}
cout<<endl;
}
if(sj>50&&sj<170){
	cout<<"早上\n";
	cout<<"按空格挖掘,按wasd上下左右行走,按x、f放方块,\n按k清空地图,按c创造,按j时间+12,按TAB设置\n有时候野怪懒得游泳,它只有1滴血\n玩家名称:"<<name<<"\n";
	cout<<"X:"<<sdf1<<"Y:"<<sdf2<<"\n";
}else{
	cout<<"晚上\n";
	cout<<name<<":“黑,真他妈黑啊”\n";
}
if(gwhp==0){
	cout<<"野怪意外死亡,已经跑走了\n";
}
if(sdf1<0||sdf1>=15||sdf2<0||sdf2>=30){
	hp=0;
	die=2;
}
for(int i=1;i<=hp;i++){
	if(i%2==1){
		cout<<"(";
	}else{
		cout<<")";
	}
}
if(hp==0){
	if(die==1){
		cout<<"你发现地板是熔岩做的\n";
		
	}else if(die==2){
		cout<<name<<"掉出了这个世界\n";
	}else if(die==3){
		cout<<"野怪把"<<name<<"吃掉了\n";
	}
	sdf1=8;
	sdf2=8;
	hp=10;
}
kejiajunchupin=getch();
			 switch(kejiajunchupin){
			 	case 'w':
			 		if(dt[sdf1-1][sdf2]==' '||dt[sdf1-1][sdf2]=='~'||dt[sdf1-1][sdf2]=='w'){
			 			sdf1--;
					 }

					 fx='w';
					 system("cls");
					 break;
				case 's':
					if(dt[sdf1+1][sdf2]==' '||dt[sdf1+1][sdf2]=='~'||dt[sdf1+1][sdf2]=='w'){
					sdf1++;
					 }

					 fx='s';
					 system("cls");
					 break;
			 	case 'a':
			 		if(dt[sdf1][sdf2-1]==' '||dt[sdf1][sdf2-1]=='~'||dt[sdf1][sdf2-1]=='w'){
					 sdf2--;
					 }

					 fx='a';
					 system("cls");
					 break;
			 	case 'd':
			 		if(dt[sdf1][sdf2+1]==' '||dt[sdf1][sdf2+1]=='~'||dt[sdf1][sdf2+1]=='w'){
					sdf2++;
					 }

					 fx='d';
					 system("cls");
					 break;
				case ' ':


					if(fx=='w'&&dt[sdf1-1][sdf2]!='~'&&dt[sdf1-1][sdf2]!='w'){
					dt[sdf1-1][sdf2]=' ';
					}else if(fx=='s'&&dt[sdf1+1][sdf2]!='~'&&dt[sdf1+1][sdf2]!='w'){
					dt[sdf1+1][sdf2]=' ';
					}else if(fx=='a'&&dt[sdf1][sdf2-1]!='~'&&dt[sdf1][sdf2-1]!='w'){
					dt[sdf1][sdf2-1]=' ';
					}else if(fx=='d'&&dt[sdf1][sdf2+1]!='~'&&dt[sdf1][sdf2+1]!='w'){
					dt[sdf1][sdf2+1]=' ';
					}
					sdf1%=15;
					sdf2%=30;
					system("cls");
					break;
                case 'x':


	  			if(fx=='w'){
					dt[sdf1-1][sdf2]='@';
					}else if(fx=='s'){
					dt[sdf1+1][sdf2]='@';
					}else if(fx=='a'){
					dt[sdf1][sdf2-1]='@';
					}else if(fx=='d'){
					dt[sdf1][sdf2+1]='@';
					}
					system("cls");
					break;
				case 'f':


					 if(fx=='w'){
					dt[sdf1-1][sdf2]='@';
					}else if(fx=='s'){
					dt[sdf1+1][sdf2]='@';
					}else if(fx=='a'){
					dt[sdf1][sdf2-1]='@';
					}else if(fx=='d'){
					dt[sdf1][sdf2+1]='@';
					}
					system("cls");
					break;
                case 'k':
                	for(int i=0;i<=15;i++){
							for(int j=0;j<=30;j++){
							dt[i][j]=' ';
								}


					}
						system("cls");
						break;
	 			case 'c':
	 				czms();
					system("cls");
						break;
				case 'j':
					sj+=120;
					system("cls");
						break;

			
                case 9:
                	system("cls");
					while(1){
						cout<<"<功能——设置>\n";
						if(g==1){
							cout<<"血量=20\n  ";
	  						}   else if(g==2){
							  cout<<"修改名字\n  ";
						}else if(g==0){
							cout<<"退出\n";
						}
						cout<<"ws控制列表,空格确定\n";
						kejiajunchupin=getch();
						if(kejiajunchupin=='w'){
							g--;
							g%=3;
						}else if(kejiajunchupin=='s'){
							g++;
							g%=3;
						}else if(kejiajunchupin==' '){
							if(g==1){
							hp=20;
							}else if(g==2){
							cout<<"输入你的新名字:";
							cin>>name;
							}else if(g==0){
							break;
							}

						}
						system("cls");
                	}
                

				default:
					system("cls");
					break;
			 }
			 if(gwhp==1){
			 	if(gw1<sdf1&&dt[gw1+1][gw2]==' '||gw1<sdf1&&dt[gw1+1][gw2]=='~'||gw1<sdf1&&dt[gw1+1][gw2]=='w'){
			 	gw1++;
			 }else if(gw1>sdf1&&dt[gw1-1][gw2]==' '||gw1<sdf1&&dt[gw1-1][gw2]=='~'||gw1<sdf1&&dt[gw1-1][gw2]=='w'){
			 	gw1--;
			 }else if(gw2<sdf2&&dt[gw1][gw2+1]==' '||gw1<sdf1&&dt[gw1][gw2+1]=='~'||gw1<sdf1&&dt[gw1][gw2+1]=='w'){
			 	gw2++;
			 }else if(gw2>sdf2&&dt[gw1][gw2-1]==' '||gw1<sdf1&&dt[gw1][gw2-1]=='~'||gw1<sdf1&&dt[gw1][gw2-1]=='w'){
			 	gw2--;
			 }else if(sdf1==gw1&&sdf2==gw2){
			 	hp--;
			 	die=3;
			 }

			 }else{
			 	gw1=4;
			 	gw2=2;
				gwhp=1;
			 }
			 sj++;
			 sj%=240;
}
}

最后,只要插入main就行了,运行要用这个,三个文件要放在同一个文件夹里

这个可以随便取名字

#include"game.h"
int main(){m();}

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值