C++ 字符跑酷#5 游戏制作实录

博主在闲暇之余决定制作一款游戏,通过_kbhit()函数实现按键检测,但遇到按键长按时有延迟的问题。通过GetKeyState()函数解决了这一问题。随后,博主快速开发了怪物行走的逻辑,但在实现过程中遇到了怪物卡墙的bug。经过一天的努力,成功修复并完善了怪物系统,期待读者的关注和后续更新。
摘要由CSDN通过智能技术生成

版本号:1.4

嗨,我是一块铌金属。

对的,我期末考试考完了,又闲了。

那就……做游戏吧!

首先,为了我们的游戏像德芙一样丝滑,我使用了_kbhit()函数。

这个函数的作用是:判断是否有按键按下。

可是改完之后,又有问题出现了。

就是如果按住一个键不放,按下之后会等待0.5秒才会继续。

为此,我花了两个小时的时间,终于找到了解决方案!!!

就是这个函数:GetKeyState(int)

这个函数的作用就是判断某个按键是否按下。

那好,改一改。

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>

using namespace std;

void Clear_Screen() {    
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = {0, 0};
    SetConsoleCursorPosition(hConsole, coordScreen);
}

void color(int m) {
	HANDLE consolehend;
	consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(consolehend, m);
}
 
string a[8] = {
	"################################# ################################# #################################", 
	"#                               ###                               ###                               #",
	"#                               *                                 *                                E#",
	"#                             #####                             W####                             W##",
	"#                            #### #                             W## #                             W##",
	"#          #   #   #        ##### #                             W## #                             W##",
	"#         ## # # # # #     ###### #                             W## #                             W##",
	"################################# ########M###MMM###MM############# ########M###MMM###MM#############",
};

int Bx = 6;
int By = 1;
int x = 6;
int y = 1;
bool flag = 0;
bool costume = 0;

void check() {
	if (a[x][y] == 'M') {
		x = Bx;
		y = By;
	}
	if (a[x][y] == '*') {
		Bx = x;
		By = y;
	}
	if (a[x][y] == 'W') {
		costume = 1;
	} else {
		costume = 0;
	}
	if (a[x][y] == 'E') {
		flag = 1;
	}
}

int main() {
	while (1) {
		Sleep(50);
		if (a[x + 1][y] != '#' && a[x + 1][y] != 'W' && x + 1 < 8) {
			x++;
			check();
		}
		Clear_Screen();
		printf("   嗨~欢迎来到字符酷跑,在这里,I是你,#是墙,M是岩浆,碰到了就会死了,W是水,可以游上去,*是存档点  \n");
		printf("                          E就是终点,还有,w往上跳,a向左走,d向右走,加油~                          \n");
		for (int i = 0; i < 8; i++) {
			int len = a[i].size();
			for (int j = 0; j < len; j++) {
				if (i == x && j == y) {
					if (costume == 0) {
						color(6);
						printf("I");
					} else {
						color(1);
						printf("U");
					}
					continue;
				} 
				switch (a[i][j]) {
					case '*':
						color(2);
						printf("%c", a[i][j]);
						break;
					case 'M':
						color(4);
						printf("%c", a[i][j]);
						break;
					case 'W':
						color(9);
						printf("%c", a[i][j]);
						break;
					case 'E':
						color(14);
						printf("%c", a[i][j]);
						break;
					default:
						color(0xF);
						printf("%c", a[i][j]);
						break;
				}
			}
			printf("\n");
		}
		if (flag) {
			break;
		}
		color(0xF);
		if (GetKeyState('A') < 0 && a[x][y - 1] != '#') {
			y--;
		}
		if (GetKeyState('D') < 0 && a[x][y + 1] != '#') {
			y++; 
			check();
		}
		if (GetKeyState('W') < 0 && (a[x - 1][y] == 'W' || a[x + 1][y] == 'W')) {
			costume = 1;
			x--;
		} else if (GetKeyState('W') < 0 && a[x + 1][y] == '#') {
			costume = 0;
			int t = x;
			for (int i = t; i >= t - 4; i--) {
				if (a[i][y] == '#') {
					break;
				}
				x--;
				check();
			}
		}
		if (GetKeyState('S') < 0 && (a[x - 1][y] == 'W' || a[x + 1][y] == 'W') && a[x + 1][y] != '#') {
			x++;
			costume = 1;
			check();
		}
	}
	system("cls");
	printf("You Win!");
	return 0;
}

那……就这样?

噢,当然了,就这样结束了。

吗……?

不,当然不是啦。

虽然发文助手已经提醒我可以发文了,但是,我还是感觉空虚了点。

 那……添加一下怪物系统吧……

首先,创建一下怪物的逻辑。

我也懒得想太多,就设定他为走来走去吧。

于是我匆匆忙忙地写了个类。

他喵的,怪物呢? 

噢,没显示啊~

现在就让你出现!

 (箭头所指的怪物)

欸不是,你别跑啊!回来!你这怎么还卡墙上了????

(呜呜呜问号啊,你没得好惨啊!)

这是个bug,得治!

尾声

花费一天时间,终于是把怪物系统做好了,还希望大家能点个关注,下次更新游戏就不用担心找不到我啦~

下一次更新嘛……要等我啃完依稀_yixy大佬的EGE教程再说吧!

相信你们已经知道我下期要干嘛了……同样的,Backrooms的研发同样得等一会。

(突然发现代码没放上来)

#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>

using namespace std;

void Clear_Screen() {    
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD coordScreen = {0, 0};
    SetConsoleCursorPosition(hConsole, coordScreen);
}

void color(int m) {
	HANDLE consolehend;
	consolehend = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(consolehend, m);
}
 
string a[9] = {
	"#####################################################################################################", 
	"#                                                                                                   #",
	"#                               *                                 *                                E#",
	"#                             #####                             W####                             W##",
	"#                            #### #                             W## #                             W##",
	"#          #   #   #        ##### #                             W## #                             W##",
	"#         ## # # # # #     ###### #                             W## #                             W##",
	"################################# ########M###MMM###MM############# ########M###MMM###MM#############",
	"#####################################################################################################"
};

class monster {
	public:
		bool direc = 0;
		int Mx = 6;
		int My = 5;
		void move() {
			if (a[Mx + 1][My] != '#' && Mx + 1 < 8) {
				Mx++;
			}
			if (direc == 0) {
				if (a[Mx][My + 1] == '#') {
					int t = Mx;
					for (int i = t; i >= t - 3; i--) {
						if (a[i][My] == '#') {
							break;
						}
						Mx--;
					}
				}
				My = My + 1;
			} else {
				if (a[Mx][My - 1] == '#') {
					int t = Mx;
					for (int i = t; i >= t - 3; i--) {
						if (a[i][My] == '#') {
							break;
						}
						Mx--;
					}
				}
				My = My - 1;
			}
			if ((My == 98 && direc == 0) || (My == 1 && direc == 1)) {
				direc = !direc;
			} 
		}
};

int Bx = 6;
int By = 1;
int x = 6;
int y = 1;
bool flag = 0;
bool costume = 0;
monster Mons;

void check() {
	if (a[x][y] == 'M') {
		x = Bx;
		y = By;
	}
	if (a[x][y] == '*') {
		Bx = x;
		By = y;
	}
	if (a[x][y] == 'W') {
		costume = 1;
	} else {
		costume = 0;
	}
	if (a[x][y] == 'E') {
		flag = 1;
	}
}

int main() {
	while (1) {
		Sleep(50);
		Mons.move();
		if (a[x + 1][y] != '#' && a[x + 1][y] != 'W' && x + 1 < 9) {
			x++;
			check();
		}
		if (x == Mons.Mx - 1 || x == Mons.Mx || x == Mons.Mx + 1) {
			if (y == Mons.My - 1 || y == Mons.My || y == Mons.My + 1) {
				x = Bx;
				y = By;
			}
		}
		Clear_Screen();
		printf("   嗨~欢迎来到字符酷跑,在这里,I是你,#是墙,M是岩浆,碰到了就会死了,W是水,可以游上去,*是存档点  \n");
		printf("                          E就是终点,还有,w往上跳,a向左走,d向右走,加油~                          \n");
		for (int i = 0; i < 9; i++) {
			int len = a[i].size();
			for (int j = 0; j < len; j++) {
				if (i == x && j == y) {
					if (costume == 0) {
						color(6);
						printf("I");
					} else {
						color(1);
						printf("U");
					}
					continue;
				} 
				if (i == Mons.Mx && j == Mons.My) {
					color(6);
					printf("?");
					continue;
				}
				switch (a[i][j]) {
					case '*':
						color(2);
						printf("%c", a[i][j]);
						break;
					case 'M':
						color(4);
						printf("%c", a[i][j]);
						break;
					case 'W':
						color(9);
						printf("%c", a[i][j]);
						break;
					case 'E':
						color(14);
						printf("%c", a[i][j]);
						break;
					default:
						color(0xF);
						printf("%c", a[i][j]);
						break;
				}
			}
			printf("\n");
		}
		if (flag) {
			break;
		}
		color(0xF);
		if (GetKeyState('A') < 0 && a[x][y - 1] != '#') {
			y--;
		}
		if (GetKeyState('D') < 0 && a[x][y + 1] != '#') {
			y++; 
			check();
		}
		if (GetKeyState('W') < 0 && (a[x - 1][y] == 'W' || a[x + 1][y] == 'W')) {
			costume = 1;
			x--;
		} else if (GetKeyState('W') < 0 && a[x + 1][y] == '#') {
			costume = 0;
			int t = x;
			for (int i = t; i >= t - 4; i--) {
				if (a[i][y] == '#') {
					break;
				}
				x--;
				check();
			}
		}
		if (GetKeyState('S') < 0 && (a[x - 1][y] == 'W' || a[x + 1][y] == 'W') && a[x + 1][y] != '#') {
			x++;
			costume = 1;
			check();
		}
	}
	system("cls");
	printf("You Win!");
	return 0;
}

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值