推箱子游戏

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <Windows.h>
using namespace std;
int main(int argc, char** argv) {
    int h=20;
    int l=20;
    int x=3;
    int y=3;
    char a;
    int q_x[3];
    int q_y[3];
    srand(time(0));
    for(int i=0;i<3;i++){
    	q_x[i]=rand()%20;
    	q_y[i]=rand()%20;
	}
    while(1){
        for(int i=0;i<h;i++){
            for(int ii=0;ii<l;ii++){
            	bool a=true;
            		if(i==y&&ii==x){
                    	cout<<setw(2)<<"O";
                    }else{
                    	for(int j=0;j<3;j++){
							if(i==q_y[j]&&ii==q_x[j]){
		                    	cout<<setw(2)<<"■";
		                    	a=false;
		                    } 
		                }
		                if(a){
	                    	cout<<setw(2)<<".";
	                	}
                	}
            }
            cout<<endl;
        }
        cout<<"输入wasd控制方向,# 退出"<<endl;
        cin>>a;
	        if(a=='w'){
	            y--;
	            for(int i=0;i<3;i++){
	            	if(q_y[i]==y&&q_x[i]==x){
	                	q_y[i]--;
	            	}
            	}
	            if(y<0){
	                y=0;
	            }
	        }else if(a=='s'){
	            y++;
	            for(int i=0;i<3;i++){
	            	if(q_y[i]==y&&q_x[i]==x){
	                	q_y[i]++;
	            	}
	            }
	            if(y>=h){
	                y=h-1;
	            }
	        }else if(a=='a'){
	            x--;
	            for(int i=0;i<3;i++){
	            	if(q_y[i]==y&&q_x[i]==x){
	                	q_x[i]--;
	            	}
	            }
	            if(x<0){
	                x=0;
	            }
	        }else if(a=='d'){
	            x++;
	            for(int i=0;i<3;i++){
	            	if(q_y[i]==y&&q_x[i]==x){
	                	q_x[i]++;
	            	}
	            }
	            if(x>=l){
	                y=l-1;
	            }
	        }else if(a=='#'){
	            return 0;
	        }
        system("cls");
    }
    return 0;
}

  

posted @ 2023-08-08 20:25  王ys  阅读(23)  评论(0编辑  收藏  举报