游戏

raoyiou 2021-02-25 19:59:49 2021-03-08 21:29:35 5
#include <cstdio> 

#include <windows.h> 

#include <cstdlib>

#include <ctime>

#include <conio.h>

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

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

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

void t5() {
	
	Sleep(500);
	
}

void t15() {
	
	Sleep(1500);
	
}

void CS() {
	
	system("cls");
	
}

int main() {
	
	again:
		
	CS();
	
	char map[26][51]; 
	
	int LN, NOC;  
	
	int key;
	
	int rule, n, m, k = 1;
	
	int RN;
	
	for (int i = 1; i <= 25; i++) 
		for (int j = 1; j <= 50; j++)
			if (i != 1 || i != 25 || j != 1 || j != 50) 
			    map[i][j] = ' ';
				
	srand(time(0));			
    
    printf("请选择僵尸倍数:");
    scanf("%d", &m);
    while (k <= m) {
    	for (int i = 1; i <= 25; i++) {
		    for (int j = 1; j <= 50; j++) {
			   LN = rand()%25 + 1;
			   NOC = rand()%50 + 1;
		    }
	 	    map[LN][NOC] = '&';
	    }
	    k++;
	}
	CS();
	
	LN = rand()%24 + 2;
	NOC = rand()%49 + 2;
	map[LN][NOC] = '@';

	LN = rand()%24 + 2;
	NOC = rand()%49 + 2;
	map[LN][NOC] = '#';

	for (int i = 1; i <= 25; i++) 
		for (int j = 1; j <= 50; j++) 
			if (i == 1 || i == 25|| j == 1 || j == 50) 
			    map[i][j] = '*';

	printf("开始游戏");
	t15();
	CS();
	printf("本游戏不稳定!!!");
	t15();
	CS(); 
	blue("是否观看规则?\n");
	t5();
	blue("1、是     2、否\n");
	scanf("%d", &rule);
	
	if (rule == 1) {
		CS();
		t5();
		puts("“w”前进,“s”后退,“a”向左,“d”向右");
		t15();
		puts("你 = “#”,僵尸 = “&”,终点 = “@”,墙壁 = “*”");
		t15();
		puts("到达终点则胜利,碰到僵尸和墙壁则凉凉"); 
		t5();
	}	
			
	while (map[1][1] == '*') {
		CS();
		
		for (int i = 1; i <= 25; i++) { 
		    for (int j = 1; j <= 50; j++) 
		    	if (map[i][j] == '&') green("&");
		    	else if (map[i][j] == ' ') printf(" ");
		    	else if (map[i][j] == '*') blue("*");
		        else if (map[i][j] == '#') red("#");
		        else red("@");
		    printf("\n");
	    }
	    
	    getch();
	    key = getch();
	    
	    for (int i = 1; i <= 30; i++) {
	    	for (int j = 1; j <= 50; j++) {
	    		if (map[i][j] == '#') {
					map[i][j] = ' ';
					LN = i;
					NOC = j;
				} 
			    if (map[i][j] == '&') {
			    	map[i][j] = ' ';
			    	RN = rand()%4 + 1;
			    	if (RN == 1 && map[i - 1][j] != '*' && map[i - 1][j] != '@' && map[i - 1][j] != '&') 
					    map[i - 1][j] = '&';
			    	if (RN == 2 && map[i + 1][j] != '*' && map[i + 1][j] != '@' && map[i + 1][j] != '&')
					    map[i + 1][j] = '&';
			    	if (RN == 3 && map[i][j - 1] != '*' && map[i][j - 1] != '@' && map[i][j - 1] != '&')
					    map[i][j - 1] = '&';
			    	if (RN == 4 && map[i][j + 1] != '*' && map[i][j + 1] != '@' && map[i][j + 1] != '&')
					    map[i][j + 1] = '&';
				}
			}	
		}	
		
	    if (key == 'w')
		    LN--;
		if (key == 's')
		    LN++;
		if (key == 'a')
		    NOC--;
		if (key == 'd')
		    NOC++;
	
		if (map[LN][NOC] == '@' || map[LN][NOC] == '&' || map[LN][NOC] == '*') {
	    	CS();
	    	t5();
			   if(map[LN][NOC] == '@') red("你赢了");
			   else red("你凉了");
	   		t5();
	   		break;
	    }
			
	   	map[LN][NOC] = '#';		
	} 
	
	CS();
	printf("是否重新开始\n");
	t5();
	printf("1、是     2、否\n"); 
	scanf("%d", &n);
	if (n == 1) 
	    goto again;
	
	return 0;
	
} 
{{ vote && vote.total.up }}

共 5 条回复

raoyiou

一直在优化中。。。

root 站长

一步一步来,别着急

raoyiou

自从加了goto,有时候还会被卡死

root 站长

在代码第一行的前面加上 ```cpp, 最后一行的后面加上 ```,你的代码就会变得正常且好看。

raoyiou

map[i][j] = ' ';这条代码似乎有时被忽略了