[C++]清屏小游戏

本文介绍了一个使用C++编写的清屏小游戏,玩家需移动光标清除屏幕上的所有非炸弹字符。文章详细讲述了游戏的初始化过程,并给出了V1.0和V2.0的代码实现,针对V1.0存在的问题进行了修正。
摘要由CSDN通过智能技术生成

在这个游戏中,你需要移动光标,去把除了炸弹以外的所有字母都清掉。
先需要设置一下gotoxy函数:

void gotoxy(int x,int y){
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}

然后是初始化各种数据:

	int n;
	cin >> n;
	char mm[n][n];
	int ran[n][n];
	char bomb;
	clock_t start, end;
	double dur;

具体代码也给出来:

#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <bits/stdc++.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
void gotoxy(int x,int y){
COORD pos = {x, y};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
int main(){
	cout << "Welcome" << endl;
	system("pause");
	system("cls");
	cout << "Select size(1 ~ 10)" << endl;
	int n;
	cin >> n;
	char mm[200][200];
	int ran[200][200];
	char bomb;
	srand(time(NULL));
	bomb = char(rand() % 26 + 97);
	system("cls");
	for(int i = 0; i < 200; i++){
		for(int j = 0; j < 200; j++){
			rand();
			ran[i][j] = rand() % 26 + 97;
			mm[i][j] = char(ran[i][j]);
		}
	}
	int x1, y1;
	cout << "Input starting x-coordinate" << endl;
	cin >> x1;
	cout << "Input starting y-coordinate" << endl;
	cin >> y1;
	for(int i = x1; i < x1 + n; i++){
		for(int j = y1; j < y1 + n; j++){
			gotoxy(10 + i - x1, 10 + j - y1);
			cout << mm[i][j];
		}
		cout << endl;
	}
	int x = 0;
	int y = 0;
	gotoxy(0, 21);
	cout << "The bomb is " << bomb << endl;
	cycle:
		char ch = 'p';
		gotoxy(10 + x, 10 + y);
		if(kbhit()){
			gotoxy(0, 20);
			ch = getche();
			if(mm[x + x1][y + y1] == bomb){
				goto boom;
			}else{
				mm[x][y] = ' ';
				gotoxy(10 + x, 10 + y);
				cout << mm[x][y];
			}
		}
	switch(ch){
		case 'w':y--; break;
		case 's':y++; break;
		case 'a':x--; break;
		case 'd':x++; break;
		case 'l':return 0;
		default: break;
		}
		for(int i = x1; i < x1 + n; i++){
			for(int j = y1; j < y1 + n; j++){
				if(mm[i + 1][j + 1] != ' ' && mm[i + 1][j + 1] != bomb){
					goto cycle;
				}
			}
		}
		goto win;
	boom:
		system("cls");
		cout << "BOOM!" << endl;
		cout << "You stepped on bomb" << endl;
		system("pause");
		return 0;
	win:
		system("cls");
		cout << "You win" << endl;
		system("pause");
		return 0;
}

额。。。
试验了一下,发现全部(除炸弹外)都被清除后居然没有进入“win” 部分,而且还要输入起始坐标,很麻烦(而且连hint都没有啊!)。
(请忽略此版本 )

下面给出V2.0游戏代码:

#include <Windows.h>
#include <stdlib.h>
#include <time.h>
#include <bits/stdc++.h>
#include<stdlib.h>
#include<conio.h>
using namespace std;
void gotoxy(int x,int y){
	COORD pos = {x, y};
	SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos);
}
void ask_hint(char s){
	if(s == 'y'){
		cout << "Welcome to cls game!" << endl;
		Sleep(1800);
		cout << "The goal is to clear all the letters(except bombs) on the board." << endl;
		Sleep(2400);
		system("cls");
		cout << "Press 'w' to go up, 'a' to go left, 's' to go down and 'd' to go right." << endl;
		Sleep(3000);
		cout << "Beware! There is a bomb letter and once you're on it you lose." << endl;
		Sleep(2000);
		system("cls");
		return;
	}
}
int main(){
	char s;
	cout << "Hints? (y / n)" << endl;
	cin >> s;
	system("cls");
	ask_hint(s);
	system("cls");
	Sleep(1000);
	cout << "Select size(1 ~ 10)" << endl;
	int n;
	cin >> n;
	char mm[n][n];
	int ran[n][n];
	char bomb;
	clock_t start, end;
	double dur;
	srand(time(NULL));
	bomb = char(rand() % 26 + 97);
	system("cls");
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			ran[i][j] = rand() % 26 + 97;
			mm[i][j] = char(ran[i][j]);
		}
	}
	for(int i = 0; i < n; i++){
		for(int j = 0; j < n; j++){
			gotoxy(10 + i, 10 + j);
			cout << mm[i][j];
		}
		cout << endl;
	}
	int x = 0;
	int y = 0;
	gotoxy(0, 21);
	cout << "The bomb is " << bomb << endl;
	start = clock();
	cycle:
		char ch = 'p';
		gotoxy(10 + x, 10 + y);
		if(kbhit()){
			gotoxy(0, 20);
			ch = getche();
			if(mm[x][y] == bomb){
				goto boom;
			}else{
				mm[x][y] = ' ';
				gotoxy(10 + x, 10 + y);
				cout << mm[x][y];
			}
		}
	switch(ch){
		case 'w':y--; break;
		case 's':y++; break;
		case 'a':x--; break;
		case 'd':x++; break;
		default: break;
		}
		for(int i = 0; i < n; i++){
			for(int j = 0; j < n; j++){
				if(mm[i][j] != ' ' && mm[i][j] != bomb){
					goto cycle;
				}
			}
		}
		goto win;
	boom:
		system("cls");
		cout << "BOOM!" << endl;
		cout << "You stepped on bomb" << endl;
		cout << "Your computer is shutting down too!" << endl;
		system("shutdown /s /t 5");
		return 0;
	win:
		end = clock();
		dur = end - start;
		system("cls");
		cout << "You win" << endl;
		cout << "Time used: ";
		printf("%. 3f", dur / 1000.0);
		cout << " Seconds" << endl;
		system("pause");
		return 0;
}

这次就没出像V1.0这样的问题了。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值