版本号: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;
}