我用C++做了个沙盒游戏
给大家送游戏了~操作方法:
使用cin输入,每步操作后需按回车
WASD移动,P放置
移动到字符'w'的位置一获得木头
当你放置足够的木板后你就赢了
有环境的人可以启用\033和system("clr")
上代码(直接复制到Dev-c++):
切记!bccn的运行系统会编译超时!
程序代码:
#include <bits/stdc++.h> #include <cstdlib> #include <time.h> #include <cmath> #include <iostream> #include <fstream> #include <unistd.h> bool isLinux033=0; using namespace std; long long h=19,l=1,w=0,xp=0; //' '=水,'P'=玩家,'W'=木板 char setting_a[1024][1024]; int player_x=511; int player_y=511; int player_d='W'; int main(){ setting_a[511][511]='P'; setting_a[511][512]='W'; setting_a[512][511]='W'; setting_a[512][512]='W'; srand(time(NULL)); FIRST: cout<<"欢迎来到木筏求生"<<endl; cout<<"全部代码均由ghost_no_eq编写"<<endl; cout<<"按[S]键开始游戏..."<<endl; while(1){ char tmp; cin>>tmp; if(tmp=='s' || tmp=='S') goto GAME; } GAME: int setting_difficulty; int setting_maxx; int setting_maxy; cout<<"请输入显示页面的宽度(想通关请输入1~2,试玩可输入3~8)<<)"; cin>>setting_maxx; cout<<"请输入显示页面的高度(想通关请输入1~2,试玩可输入3~8)<<)"; cin>>setting_maxy; cout<<"请输入游戏难度(简单1~20,普通21~80,困难81~100))"; cin>>setting_difficulty; setting_difficulty+=20; int iswin=0; int ans; while(h>0){ ans++; if(l>63){ iswin++; break; } h++; if(xp/l>0){ l+=xp/l; xp=(xp+1)%l; } cout<<endl; for(int i=player_x-setting_maxx;i<player_x+setting_maxx;i++){ for(int j=player_y-setting_maxy;j<player_y+setting_maxy;j++){ if((i==player_x-setting_maxx || i==player_x+setting_maxx-1)|| (j==player_y-setting_maxy || j==player_y+setting_maxy-1)){ if(rand()%setting_difficulty==0){ if(setting_a[i][j]=='\0'){ setting_a[i][j]='w'; } } } switch(setting_a[i][j]){ case 'W': if(isLinux033) cout<<"\033[31;43m"; cout<<"W"; if(isLinux033) cout<<"\033[0m"; break; case '\0': if(isLinux033) cout<<"\033[44m"; cout<<" "; if(isLinux033) cout<<"\033[0m"; break; case 'P': if(isLinux033) cout<<"\033[7m"; cout<<"P"; if(isLinux033) cout<<"\033[0m"; break; case 'w': if(isLinux033) cout<<"\033[33;44m"; cout<<"w"; if(isLinux033) cout<<"\033[0m"; break; default: if(isLinux033) cout<<"\033[45m"; cout<<"?"; if(isLinux033) cout<<"\033[0m"; break; } } cout<<endl; } for(int i=0;i<2*setting_maxx;i++){ cout<<"="; } if(player_d=='\0'){ cout<<endl<<"您正在消耗体力,请尽快回到木筏上回血!"; h-=3; } if(player_d=='w'){ cout<<endl<<"您获得了一块木头!"; player_d='\0'; w++; } cout<<endl<<"HP:"<<h<<" WOOD:"<<w<<" LEVEL:"<<l<<" XP:"<<xp<<" TIME:"<<ans; char tmp='\0'; while(tmp=='\0'){ cin>>tmp; switch(tmp){ case 'w': setting_a[player_x][player_y]=player_d; player_x--; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 'W': setting_a[player_x][player_y]=player_d; player_x--; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 'a': setting_a[player_x][player_y]=player_d; player_y--; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 'A': setting_a[player_x][player_y]=player_d; player_y--; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 's': setting_a[player_x][player_y]=player_d; player_x++; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 'S': setting_a[player_x][player_y]=player_d; player_x++; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 'd': setting_a[player_x][player_y]=player_d; player_y++; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 'D': setting_a[player_x][player_y]=player_d; player_y++; player_d=setting_a[player_x][player_y]; setting_a[player_x][player_y]='P'; break; case 'p': if(w>0 && player_d!='W'){ player_d='W'; w--; xp++; } break; case 'P': if(w>0 && player_d!='W'){ player_d='W'; w--; xp++; } break; } } } if(iswin==0){ cout<<"您因为没有体力而被水淹没了!"; return 0; } else{ cout<<"防假速通验证码."<<setting_difficulty*ans+pow(ans%3+2,setting_difficulty%10)<<"."; cout<<"\n您通关了!您的难度为:"<<setting_difficulty-20<<" 您的用时为:"<<ans<<"帧"; } return 0; }
[此贴子已经被作者于2021-12-9 19:30编辑过]