zoukankan      html  css  js  c++  java
  • 一个无聊的游戏——吃豆人

    退役后无聊自制了一个游戏...
    本想打个2048,限于能力,就照着半成品改成了这个。
    Cmd输出太慢有点晃眼。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<ctime>
    #include<cstdlib>
    #include<conio.h>
    #include<Windows.h>
    using namespace std;
    
    const int MAXC=100,MAXR=100;
    
    class Game{
    	int mat[MAXC][MAXR],dx[4],dy[4];
    	bool f;
    	int x,y,score,col,row,tar;
    	private:
    	void cls(){
    		system("cls");
    	}
    	int dir(){
    		char c1,c2;
    		c1=getch(),c2=getch();
    		switch(c2){
    			case 72:return 0;//up
    			case 75:return 1;//left
    			case 77:return 2;//right
    			case 80:return 3;//down
    		}
    	}
    	void topblock(){
    		printf("┌");
    		for(int i=0;i<col-1;++i)
    			printf("─┬");
    		puts("─┐");
    	}
    	void buttonblock(){
    		printf("└");
    		for(int i=0;i<col-1;++i)
    			printf("─┴");
    		puts("─┘");
    	}
    	void commonblock(){
    		printf("├");
    		for(int i=0;i<col-1;++i)
    			printf("─┼");
    		puts("─┤");
    	}
    	public:
    	Game(){
    		memset(mat,0,sizeof mat);
    		dx[0]=dy[1]=-1,dx[3]=dy[2]=1;
    		dx[1]=dx[2]=dy[0]=dy[3]=0;
    		mat[0][0]=1;
    		x=0,y=0;
    		tar='*';
    		score=0;//initation 
    		row=11,col=19;//settings
    	}
    	void start(){
    		print();
    		while(1){
    			int t=dir(),nx=x+dx[t],ny=y+dy[t];
    			if(nx>=0&&ny>=0&&nx<row&&ny<col){
    				if(mat[nx][ny]==tar)
    					f=0;
    				mat[nx][ny]=mat[x][y];
    				mat[x][y]=0;
    				x=nx,y=ny;
    				print();
    				if(!f){
    					nx=rand()%row,ny=rand()%col;
    					if(!mat[nx][ny])
    						mat[nx][ny]=tar,f=1;
    				}
    			}
    		}
    	}
    	void print(){
    		cls();
    		topblock();
    		for(int i=0;i<row;++i){
    			printf("│");
    			for(int j=0;j<col;++j){
    				switch(mat[i][j]){
    					case 0:printf("  │");break;
    					default:printf("%2c│",mat[i][j]);
    				}
    			}
    			puts("");
    			if(i==row-1)
    				buttonblock();
    			else commonblock();
    		}
    	}
    };
    
    int main(){
    	srand((unsigned)time(0));
    	Game().start();
    	return 0;
    }
    
  • 相关阅读:
    大型网站架构不得不考虑的10个问题
    js中settimeout方法加参数
    shell字符串操作详解
    linux shell 逻辑运算符、逻辑表达式
    c#判断网络连接状态示例代码
    asp.net读取excel文件多种方法
    asp.net导出excel示例代码
    php 数组排序代码
    php数组去重复代码
    php反射应用实例代码
  • 原文地址:https://www.cnblogs.com/chwhc/p/8258058.html
Copyright © 2011-2022 走看看