一个2D文字游戏
代码:
不好勿喷哦!
#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
#include<fstream>
using namespace std;
char en[50][1000];//地图
long long mo=1000000,people=3,cash=0;//钱,人
int yu=0,q=0,w=0,sd=0,jml=0,yh=0;//数量
int xf=70; //幸福值
bool flag_ll=0;
bool flag_bz=0,flag_kv=0;//设置里的开关
bool flag_cfzy=0,flag_scdh=0;
// ' i 1 0 9 j w h l s y q';
int Rand(int MIN, int MAX){//随机数
return rand() % (MAX - MIN + 1) + MIN;
}
void color(int a){//颜色
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
void shuomin(){//说明
system("cls");
cout << "这个人很懒,'神魔'都没写\n";
cout << "这是为了让玩家自己去探索!!!\n\n\n";
cout << "???(⊙o⊙)…eee\n\n\n";
cout << "备注:试试按住F件,WASD移动";
cout << " \n\n\n";
cout << " !!!完结撒花!!!" ;
system("pause");
}
void shch(){//商城
system("cls");
while(1){
cout << " 1. 2. 3. 4. \n";
cout << "金钱+100000 金钱+1000000 村民+100 村民+1000 \n";
cout << " 1现金 9现金 1现金 9现金 \n";
int g=_getch();
system("cls");
if(g=='1'&&cash<=0||g=='2'&&cash<=9||g=='3'&&cash<=0||g=='4'&&cash<=9){
cout << " 不够哦!!! \n";
Sleep(700);
return;
}
if(g=='1'){
mo+=100000;
cash--;
}
else if(g=='2'){
mo+=1000000;
cash-=9;
}
else if(g=='3'){
people+=100;
cash--;
}
else if(g=='4'){
people+=1000;
cash-=9;
}
else if(g==' ') return;
}
system("cls");
}
void haochushi(){//好的初始地图
mo=10000000;
people=100;
xf=100;
for(int i=1;i<=10;i++){
for(int j=1;j<=10;j++){
if(i==1||i==10||j==1||j==10){
en[i][j]='q';
}
}
}
for(int i=3;i<=9;i++){
en[1][i]='f';
}
en[9][1]='y';
en[9][2]='9';
en[9][4]='s';
yu++;
sd++;
w+=7;
}
void chushi(){//初始地图
mo=100000;
people=3;
yu=0;
sd=0;
w=0;
xf=70;
for(int i=0;i<30;i++){
for(int j=0;j<70;j++){
if(i==0||i==29||j==0||j==69){
en[i][j]='1';
}
else{
en[i][j]='0';
}
}
}
en[0][1]='j';
en[0][2]='z';
}
void jianzao(){//建造
system("cls");
int flag=0,_flag=0;
cout << "1. 房屋 (15000)\n";
cout << "2. 墙 (10)\n";
cout << "3. 医院 (300000)\n";
cout << "4. 商店 (50000)\n";
cout << "5.居民楼(100000)\n";
cout << "6. 银行 (10000)\n";
switch (_getch()){
case '1':flag=1;break;
case '2':flag=2;break;
case '3':flag=3;break;
case '4':flag=4;break;
case '5':flag=5;break;
case '6':flag=6;break;
}
system("cls");
if(flag==1&&mo<=15000||flag==2&&mo<=10||flag==3&&mo<=300000||flag==4&&mo<=50000||flag==5&&mo<=100000||flag==6&&mo<=10000){
cout << " 不够哦???";
Sleep(1000);
system("cls");
return;
}
else{
switch(flag){
case 1:mo=mo-15000;w++;break;
case 2:mo=mo-10;q++;break;
case 3:mo=mo-300000;yu++;break;
case 4:mo=mo-50000;sd++;break;
case 5:mo-=100000;jml++;break;
case 6:mo-=10000;yh++;break;
}
}
int x=15,y=35;
while(1){
for(int i=0;i<30;i++){
for(int j=0;j<70;j++){
if(i==x&&j==y){
cout << "▓ ";
if(flag==3||flag==6){
cout << "▓ ";
}
}
else if(en[i][j]=='i'){
cout << "★";
}
else if(en[i][j]=='1'){
cout << "█ ";
}
else if(en[i][j]=='0'){
if(flag==3&&i==x&&j==y+1||flag==3&&i==x&&j==y+1){
cout << "";
}
else{
cout << " ";
}
}
else if(en[i][j]=='j'){
cout << "█";
}
else if(en[i][j]=='f'){
cout << "屋";
}
else if(en[i][j]=='q'){
cout << "墙";
}
else if(en[i][j]=='y'){
cout << "医院";
}
else if(en[i][j]=='9'){
cout << "";
}
else if(en[i][j]=='s'){
cout << "店";
}
else if(en[i][j]=='h'){
cout << "银行";
}
else if(en[i][j]=='l'){
cout << "楼";
}
}
cout << "\n";
}
switch (_getch()){
case 'w':x--;break;
case 's':x++;break;
case 'a':y--;break;
case 'd':y++;break;
case ' ':_flag=1;break;
}
if(_flag){
if(flag==1){
en[x][y]='f';
}
else if(flag==2){
en[x][y]='q';
}
else if(flag==3){
en[x][y]='y';
en[x][y+1]='9';
}
else if(flag==4){
en[x][y]='s';
}
else if(flag==5){
en[x][y]='l';
}
else if(flag==6){
en[x][y]='h';
en[x][y+1]='9';
}
system("cls");
return;
}
system("cls");
}
}
void game(){//主游戏
system("cls");
int x=3,y=3,xl=5,flag_ren=0,lx,ly,flag_l0=0;
for(int i1=0;1;i1++){
for(int i=0;i<30;i++){
for(int j=0;j<70;j++){
if(en[i][j]=='2'){
cout << "▓ ";
}
else if(en[i][j]=='i'){
cout << "★";
}
else if(i==x&&y==j){
cout << "my";
}
else if(en[i][j]=='0'){
cout << " ";
}
else if(en[i][j]=='1'){
cout << "█ ";
}
else if(en[i][j]=='j'){
cout << "建造";
if((x==i+1&&y==j)||(x==i+1&&y==j+1)){
jianzao();
}
}
else if(en[i][j]=='f'){
cout << "屋";
}
else if(en[i][j]=='q'){
cout << "墙";
}
else if(en[i][j]=='y'){
cout << "医院";
}
else if(en[i][j]=='9'){
cout << "";
}
else if(en[i][j]=='s'){
cout << "店";
}
else if(en[i][j]=='l'){
cout << "楼";
}
else if(en[i][j]=='h'){
cout << "银行";
}
}
if(i==1) cout << "1.收税";
else if(i==2) cout << "2.???";
else if(i==3) cout << "3.???";
cout << "\n";
}
cout << "金钱" << mo;
cout << "\n村民" << people << "\n";
cout << "幸福值" << xf << "%\n";
switch (_getch()){
case 'w':x--;break;
case 's':x++;break;
case 'a':y--;break;
case 'd':y++;break;
case '1':
xf-=20;
if(people>0){
mo+people*10000;
}
break;
case 'f':flag_ll=1;break;
case 'q':shch();break;
}
if(!flag_l0&&flag_ll){
lx=x;
ly=y;
en[x][y]='i';
flag_l0=1;
}
else if(flag_ll){
x=lx;
y=ly;
flag_ll=0;
}
system("cls");
if(Rand(1,3)==2) mo=mo+yu*people*100;
if(flag_ren==30){
people=people+w;
flag_ren=0;
}
if(flag_ren%2==0){
people+=jml;
}
if(flag_ren%2!=0){
people+=yh/7;
}
if(xf<=10){
people--;
xf--;
}
else if(xf<=0){
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << " 愤怒的村民杀死了你";
cash--;
system("pause");
return;
}
else if(Rand(1,3)==2&&xf+1<=100) xf++;
else if(Rand(1,9)==9) xf--;
flag_ren++;
mo=mo+people*50;
mo=mo+people*sd*50;
if(flag_bz) mo=mo-mo/10000;
if(mo>=1000000000000000){
system("cls");
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << " WIN\n\n\n\n";
cash+=10;
system("pause");
return;
}
if(people<0){
people=0;
}
if(!flag_kv&&mo>=10000000000){
system("cls");
flag_cfzy=1;
cash+=3;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << " 获得成就\n\n\n\n";
system("pause");
system("cls");
}
if(!flag_kv&&sd>=30){
system("cls");
flag_scdh=1;
cash+=5;
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
cout << " 获得成就\n\n\n\n";
system("pause");
system("cls");
}
}
return;
}
void shezhi(){//设置
system("cls");
while(1){
cout << "1.货币贬值 ";
if(flag_bz) cout << "开\n";
else cout << "关\n";
cout << "2.好开局 ";
if(flag_kv) cout << "开\n";
else cout << "关";
int g=_getch();
if(g=='1') flag_bz=!flag_bz;
if(g=='2') flag_kv=!flag_kv;
else if(g==' ') break;
system("cls");
}
return;
}
void chjou(){//成就
system("cls");
cout << "财富自由 ";
if(flag_cfzy) cout << "已拥有\n";
else cout << "未拥有\n";
cout << "市场大亨 ";
if(flag_scdh) cout << "已拥有\n";
else cout << "未拥有\n";
system("pause");
system("cls");
}
int main(){//主函数
SetConsoleTitle("村落1.1.1");
color(8);
while(1){
if(flag_kv){
haochushi();
}
else{
chushi();
}
cout << " ---------------村落-1.1.1--------------- \n\n\n";
cout << " 1.新游戏 \n";
cout << " 2.介绍 \n";
cout << " 3.设置 \n";
cout << " 4.更多 \n";
int g1=_getch();
if(g1=='1'){
game();
}
else if(g1=='2'){
shuomin();
}
else if(g1=='3'){
shezhi();
if(flag_kv){
haochushi();
}
else{
chushi();
}
}
else if(g1=='4'){
cout << " 5.存档 \n";
cout << " 6.读档 \n";
cout << " 7.成就 \n";
int g2=_getch();
if(g2=='5'){
ofstream AAA;
AAA.open("AAA.txt");
AAA<<cash<<" ";
AAA<<flag_cfzy<<" ";
AAA<<flag_scdh<<" ";
}
else if(g2=='6'){
ifstream AAAi;
AAAi.open("AAA.txt");
AAAi>>cash;
AAAi>>flag_cfzy;
AAAi>>flag_scdh;
}
else if(g2=='7'){
chjou();
}
}
else{
break;
}
system("cls");
}
return 0;
}
Dev-C++可运行