C++小游戏(改进)

前言

我画了一个大大的大饼.........

😓😓😓😓😓😓😓😓😓😓😓

说重点

1.之前那个游戏,啊我发现有点幼稚,于是我改了,把很多不必要的去掉了。

2.把撞墙的机制改了,不会像之前那个那么恶心

这里放上之前的文章,你们自行比对

C++写小游戏_用c++写游戏_Sans的头的博客-CSDN博客

改进后代码 

#include "resource.h"
#include <iostream>
#include <cstdlib>
#include <windows.h>
#pragma comment(lib, "winmm.lib")
#include <mmsystem.h>
#include <time.h>
#include <conio.h>
#include <fstream> 
#define WNDLONG 81
using namespace std;
COORD crd;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
void map_draw();
void gotoxy();
void SHCursor(bool IS_HIDE);
void welcome();
void Wall();
void reward();
void mines();
void coin();
void Play_Again();
void Game();
void draw();
void die_show();
char map[17][17] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,
				  1,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,1,
				  1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, };
struct Player {
	int x;       //玩家x坐标 
	int y;       //玩家y坐标 
	double num;  //玩家得分
	int f;       //玩家生命
	Player() {
		x = 15;
		y = 8;
		num = 0.0;
		f = 3;
	}
}p;
int main() {
	system("mode con cols=81 lines=39");
	SetConsoleTitle(L"The one who gets a hundred points is the dad");
	SetWindowLong(FindWindow(L"ConsoleWindowClass", NULL), GWL_STYLE, GetWindowLong(FindWindow(L"ConsoleWindowClass", NULL), GWL_STYLE) & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX & ~WS_MINIMIZEBOX);
	welcome();
	return 0;
}
void map_draw() {
	int i, j;
	cout << "\t\t\b\b\b\b\b\b\b\b\b";
	for (i = 0; i < 17; i++)
	{
		cout << "\t\t\t";
		for (j = 0; j < 17; j++)
		{
			switch (map[i][j])
			{
			case 0:cout << "  "; break;
			case 1:cout << "■"; break;
			case 2:cout << "人"; break;
			case 3:cout << "◎"; break;
			case 4:cout << "★"; break;
			case 6:cout << "⊕"; break;
			case 7:cout << "墙"; break;
			case 8:cout << "■"; break;
			}
		}
		cout << endl;
	}
}
void gotoxy() {
	COORD pos;
	pos.X = 0;
	pos.Y = 0;
	SetConsoleCursorPosition(hOut, pos);
}
void SHCursor(bool IS_HIDE) {
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	CONSOLE_CURSOR_INFO CursorInfo;
	GetConsoleCursorInfo(handle, &CursorInfo);
	CursorInfo.bVisible = IS_HIDE;
	SetConsoleCursorInfo(handle, &CursorInfo);
}
void welcome() {
	crd.Y = 15;
	SHCursor(false);
	SetConsoleCursorPosition(hOut, crd);
	mciSendString(TEXT("open music\\bgm2.mp3 alias s1"), NULL, 0, NULL);
	mciSendString(TEXT("play s1 repeat"), NULL, 0, NULL);
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
	cout << "\t\tThe one who gets a hundred points is the dad\n\t\t【Press any key to begin】---------------->>\n\n";
	char sh = _getch();
	mciSendString(TEXT("pause s1"), NULL, 0, NULL);
	mciSendString(TEXT("close s1"), NULL, 0, NULL);
	system("cls");
	Game();
}
void Wall() {
	int diyz, diys;
	srand(time(0));
	for (int diyynum = 0; diyynum <= 79; diyynum++) {
		diyz = rand() % 13 + 1;
		diys = rand() % 14 + 1;
		map[diyz][diys] = 0;
		map[diyz][diys] = 7;
	}
}
void reward() {
	int rz, rs, renum;
	srand(time(0));
	renum = rand() % 4 + 1;
	for (int er = 0; er <= renum; er++) {
		rz = rand() % 13 + 1;
		rs = rand() % 14 + 1;
		map[rz][rs] = 0;
		map[rz][rs] = 4;
	}
}
void mines() {
	int mz, ms, minum;
	srand(time(0));
	minum = rand() % 2 + 1;
	for (int mi = 1; mi <= minum; mi++) {
		mz = rand() % 13 + 1;
		ms = rand() % 14 + 1;
		map[mz][ms] = 0;
		map[mz][ms] = 6;
	}
}
void coin() {
	int wx, wy, zs;
	srand(time(0));
	wx = rand() % 15 + 1;
	wy = rand() % 14 + 1;
	zs = rand() % 5 + 1;
	for (int i = 0; i <= zs; i++) {
		if (map[wx][wy] == 0 || map[wx][wy] == 8) {
			map[wx][wy] = 0;
			map[wx][wy] = 3;
		}
	}
}
void Play_Again() {
	crd.X = 14; crd.Y = 18; SetConsoleCursorPosition(hOut, crd);
	SetConsoleTextAttribute(hOut, FOREGROUND_RED| BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
	SHCursor(true);
	for (int i = 0; i <= 17; i++) {
		for (int j = 0; j <= 16; j++) {
			if (map[i][j] == 7 || map[i][j] == 3 || map[i][j] == 4 || map[i][j] == 6 || map[i][j] == 8 || map[i][j] == 2) {
				map[i][j] = 0;
			}
		}
	}
	p.num = 0;
	p.f = 3;
	p.x = 15;
	p.y = 8;
	map[p.x][p.y] = 2;
	SetConsoleTextAttribute(hOut, FOREGROUND_BLUE | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
	cout << "Whether to start over ( Y / N ):";
	char ch;
	cin >> ch;
	if (ch == 'Y' || ch == 'y') {
		system("cls");
		Game();
	}
	else if (ch == 'N' || ch == 'n') {
		system("cls");
		welcome();
	}
	else {
		MessageBox(NULL, L"Don't pick randomly!", L" ", MB_OK | MB_SETFOREGROUND);
		return Play_Again();
	}
}
void Game() {
	reward();
	Wall();
	mines();
	draw();
	mciSendString(TEXT("open music\\bgm.mp3 alias s3"), NULL, 0, NULL);
	mciSendString(TEXT("play s3 repeat"), NULL, 0, NULL);
	SHCursor(false);
	while (1) {
		coin();
		int ch = _getch();
        //这里改成了只有前进时撞墙才会扣血,之前那个地雷(啊,算不上)
        //一开始只会清空得分,现在会扣血(-_--)
		if (ch == 72 || ch == 119 || ch == 87) {
			if (map[p.x - 1][p.y] == 0) {
				map[p.x][p.y] = 0;
				p.x--;
				map[p.x][p.y] = 2;
			}
			if (map[p.x - 1][p.y] == 3) {
				map[p.x][p.y] = 8;
				p.x--;
				map[p.x][p.y] = 2;
				p.num++;
			}
			if (map[p.x - 1][p.y] == 4) {
				map[p.x][p.y] = 0;
				p.x--;
				map[p.x][p.y] = 2;
				p.num += 4;
			}
			if (map[p.x - 1][p.y] == 6) {
				map[p.x][p.y] = 0;
				p.x--;
				map[p.x][p.y] = 2;
				p.num = 0;
				p.f--;
				MessageBox(NULL, L"Score cleared", L" ", MB_OK | MB_SETFOREGROUND);
			}
			if (map[p.x - 1][p.y] == 7) {
				MessageBox(NULL, L"You walk into the wall", L" ", MB_OK | MB_SETFOREGROUND);
				p.f--;
			}
			if (map[p.x - 1][p.y] == 8 && map[p.x - 2][p.y] == 0) {
				map[p.x][p.y] = 0;
				p.x -= 1;
				map[p.x][p.y] = 2;
				map[p.x - 1][p.y] = 8;
			}
		}
		if (ch == 80 || ch == 115 || ch == 83) {
			if (map[p.x + 1][p.y] == 0) {
				map[p.x][p.y] = 0;
				p.x++;
				map[p.x][p.y] = 2;
			}
			if (map[p.x + 1][p.y] == 3) {
				map[p.x][p.y] = 8;
				p.x++;
				map[p.x][p.y] = 2;
				p.num++;
			}
			if (map[p.x + 1][p.y] == 4) {
				map[p.x][p.y] = 0;
				p.x++;
				map[p.x][p.y] = 2;
				p.num += 4;
			}
			if (map[p.x + 1][p.y] == 6) {
				map[p.x][p.y] = 0;
				p.x++;
				map[p.x][p.y] = 2;
				p.num = 0;
				p.f--;
				MessageBox(NULL, L"Score cleard", L" ", MB_OK | MB_SETFOREGROUND);
			}
			if (map[p.x + 1][p.y] == 8 && map[p.x + 2][p.y] == 0) {
				map[p.x][p.y] = 0;
				p.x += 1;
				map[p.x][p.y] = 2;
				map[p.x + 1][p.y] = 8;
			}
		}
		if (ch == 75 || ch == 97 || ch == 65) {
			if (map[p.x][p.y - 1] == 0) {
				map[p.x][p.y] = 0;
				p.y--;
				map[p.x][p.y] = 2;
			}
			if (map[p.x][p.y - 1] == 3) {
				map[p.x][p.y] = 8;
				p.y--;
				map[p.x][p.y] = 2;
				p.num++;
			}
			if (map[p.x][p.y - 1] == 4) {
				map[p.x][p.y] = 0;
				p.y--;
				map[p.x][p.y] = 2;
				p.num += 4;
			}
			if (map[p.x][p.y - 1] == 6) {
				map[p.x][p.y] = 0;
				p.y--;
				map[p.x][p.y] = 2;
				p.num = 0;
				p.f--;
				MessageBox(NULL, L"Score cleard", L" ", MB_OK | MB_SETFOREGROUND);
			}
			if (map[p.x][p.y - 1] == 8 && map[p.x][p.y - 2] == 0) {
				map[p.x][p.y] = 0;
				p.y -= 1;
				map[p.x][p.y] = 2;
				map[p.x][p.y - 1] = 8;
			}
		}
		if (ch == 77 || ch == 100 || ch == 68) {
			if (map[p.x][p.y + 1] == 0) {
				map[p.x][p.y] = 0;
				p.y++;
				map[p.x][p.y] = 2;
			}
			if (map[p.x][p.y + 1] == 3) {
				map[p.x][p.y] = 8;
				p.y++;
				map[p.x][p.y] = 2;
				p.num++;
			}
			if (map[p.x][p.y + 1] == 4) {
				map[p.x][p.y] = 0;
				p.y++;
				map[p.x][p.y] = 2;
				p.num += 4;
			}
			if (map[p.x][p.y + 1] == 6) {
				map[p.x][p.y] = 0;
				p.y++;
				map[p.x][p.y] = 2;
				p.num = 0;
				p.f--;
				MessageBox(NULL, L"Score cleard", L" ", MB_OK | MB_SETFOREGROUND);
			}
			if (map[p.x][p.y + 1] == 8 && map[p.x][p.y + 2] == 0) {
				map[p.x][p.y] = 0;
				p.y += 1;
				map[p.x][p.y] = 2;
				map[p.x][p.y + 1] = 8;
			}
		}
		if (ch == 112) {
			mciSendString(TEXT("pause s3"), NULL, 0, NULL);
			mciSendString(TEXT("close s3"), NULL, 0, NULL);
			MessageBox(NULL, L"You throw in the towel", L"!!!", MB_OK | MB_SETFOREGROUND);
			break;
		}
		if (p.f <= 0) {
			mciSendString(TEXT("pause s3"), NULL, 0, NULL);
			mciSendString(TEXT("close s3"), NULL, 0, NULL);
			MessageBox(NULL, L"You are dead!!", L"", MB_OK | MB_SETFOREGROUND);
			break;
		}
		gotoxy();
		draw();
	}
	die_show();
	Play_Again();
}
void draw() {
	for (int of = 1; of <= WNDLONG; of++) { cout << "▁"; }
	cout << "<GAME:TOWGAHPITD>\n\n";
	cout << "\twall:墙 \tmines:⊕\n"
		<< "\tcoins:◎\treward:★\n";
	for (int of = 1; of <= WNDLONG; of++) { cout << "▁"; }
	cout << endl;
	map_draw();
	for (int of = 1; of <= WNDLONG; of++) { cout << "▁"; }
	cout << endl;
	cout << "\tHP:";
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | FOREGROUND_INTENSITY | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
	for (int i = 1; i <= p.f; i++) { cout << "〓"; }
	cout << "  \n\n";
	SetConsoleTextAttribute(hOut, FOREGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
	cout << "\tPress the key “P” to admit defeat...Move:“↑”,“↓”,“←”,“→”\n";
	cout << "\t【Target】Get the coins\n";
	for (int ef = 1; ef <= WNDLONG; ef++) cout << "▁";
	cout << endl;
}
void die_show() {
	system("cls");
	mciSendString(TEXT("open music\\over.mp3 alias s2"), NULL, 0, NULL);
	mciSendString(TEXT("play s2"), NULL, 0, NULL);
	crd.X = 17; crd.Y = 13; SetConsoleCursorPosition(hOut, crd);
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
	cout << "▁▁▁▁▁▁▁GAME(×_×)OVER▁▁▁▁▁▁▁";
	crd.X = 17; crd.Y = 15; SetConsoleCursorPosition(hOut, crd);
	SetConsoleTextAttribute(hOut, FOREGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED);
	cout << "\t\t\t\b\b\b\b\b\b\b\b\b\b\b【Score】" << p.num << "\n" << endl;
	Sleep(1000);
	mciSendString(TEXT("close s2"), NULL, 0, NULL);
}

运行效果

 

 

 

小问题

文字提示我改成了英文,你问为社么,我的VS出问题了打中文出乱码,我一点办法没有

感想

这里说一下我的想法

这个游戏还有很大的改进空间,我说的是玩法方面,有兴趣的可以自己去加点其他功能

然后我可能不会再改进这个游戏了

写出这个游戏的时候我还没加入csdn,最开始它只是简单的小地图,几面墙,古老的符号(@#…&*)这样的

后来有了选难度的模块

但后来我把这个模块删除了,我感觉它像一个累赘

后来我把它升级成了美观的字符(现在就像诺基亚手机上的怀旧游戏)

……

再见

这就是这个游戏的发展过程……

我现在开始入手untiy了,也许有一天我能把它图形化

不说了,ヾ( ̄▽ ̄)Bye~Bye~

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值