如何在C++上独立运行函数
How to run the functions independently on C++
我正在尝试使用C++在控制台上制作游戏。目前,我主要有三个功能。
int main() {
...
while (!level1 && !player.dead) {
drowing(l1_map);
PlayerMovements(empty, l1_map);
Enemy1Movements(enemy1, l1_map, lastloc);
cls();
}
}
第一个函数绘制地图:
void drowing(char l1_map[26][50]) {
for (unsigned int i = 0; i < 26; i++) {
cout << endl;
for (unsigned int x = 0; x < 50; x++) {
if (x == 0) {
cout << " ";
}
//Draws the map and change colours;
//player
if (l1_map[i][x] == player.character) {
if (l1_map[player.locX][player.locY] == l1_map[enemy1.locX][enemy1.locY]) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 252);
cout << l1_map[i][x];
}
if (l1_map[player.locX][player.locY] != l1_map[enemy1.locX][enemy1.locY]) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 250);
cout << l1_map[i][x];
}
}//wall
else if (l1_map[i][x] == wall) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 128);
cout << l1_map[i][x];
}//enemy
else if (l1_map[i][x] == enemy1.character) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 252);
cout << l1_map[i][x];
}//empty space
else if (l1_map[i][x] == 32) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 509);
cout << l1_map[i][x];
}//key1
else if (l1_map[i][x] == key1.character && !key1.picked) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 509);
cout << l1_map[i][x];
}//key2
else if (l1_map[i][x] == key2.character && !key1.picked) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
cout << l1_map[i][x];
}//key3
else if (l1_map[i][x] == key3.character && !key1.picked) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
cout << l1_map[i][x];
}//doors1
else if (l1_map[i][x] == doors1.character) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 253);
cout << l1_map[i][x];
}//doors2
else if (l1_map[i][x] == doors2.character) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
cout << l1_map[i][x];
}//doors3
else if (l1_map[i][x] == doors3.character) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
cout << l1_map[i][x];
}//doors4
else if (l1_map[i][x] == doors3.character) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 240);
cout << l1_map[i][x];
}//exit
else if (l1_map[i][x] == get_out1.character && !get_out1.open) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 139);
cout << l1_map[i][x];
}
else
{
cout << l1_map[i][x];
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 15);
}
}
}
}
第二个功能允许玩家使用键盘箭头在地图上移动:
void PlayerMovements(char& empty, char l1_map[26][50]) {
char awall = 219;
if (_kbhit()) {
switch ((_getch())) { //what button is pressed;
// move up
case 72:
if (l1_map[player.locX - 1][player.locY] != wall && l1_map[player.locX - 1][player.locY] != '[') {
player.locX--;
if (l1_map[player.locX + 1][player.locY] == player.character) {
l1_map[player.locX + 1][player.locY] = empty;
}
if (key3.locX == player.locX && key3.locY == player.locY) {
l1_map[key3.locX][key3.locY] = ' ';
}
}
break;
//move down
case 80:
if (l1_map[player.locX + 1][player.locY] != wall && l1_map[player.locX + 1][player.locY] != '[') {
player.locX++;
if (l1_map[player.locX - 1][player.locY] == player.character) {
l1_map[player.locX - 1][player.locY] = empty;
}
}
break;
case 75:
//left
if (l1_map[player.locX][player.locY - 1] != wall && l1_map[player.locX][player.locY - 1] != '[') {
player.locY--;
if (l1_map[player.locX][player.locY + 1] == player.character) {
l1_map[player.locX][player.locY + 1] = empty;
}
}
break;
case 77: // player moves right
if (l1_map[player.locX][player.locY + 1] != wall && l1_map[player.locX][player.locY + 1] != '[') {
player.locY++;
if (l1_map[player.locX][player.locY - 1] = player.character) {
l1_map[player.locX][player.locY - 1] = empty;
}
}
break;
}
}
}
第三个功能是上下移动的敌人:
int Enemy1Movements(enemy& enemy1, char l1_map[26][50], char& lastloc) {
//this_thread::sleep_for(chrono::milliseconds(500));
char empty = ' ';
bool ValidUp = false;
bool ValidDown = false;
bool ValidLeft = false;
bool ValidRight = false;
if (l1_map[enemy1.locX + 1][enemy1.locY] != wall && l1_map[enemy1.locX + 1][enemy1.locY] != '-') {
ValidDown = true;
}
if (l1_map[enemy1.locX - 1][enemy1.locY] != wall && l1_map[enemy1.locX - 1][enemy1.locY] != '-') {
ValidUp = true;
}
if (l1_map[enemy1.locX][enemy1.locY - 1] != wall && l1_map[enemy1.locX - 1][enemy1.locY] != '-') {
ValidLeft = true;
}
if (l1_map[enemy1.locX][enemy1.locY + 1] != wall && l1_map[enemy1.locX + 1][enemy1.locY] != '-') {
ValidRight = true;
}
////enemy move up and down
//////////////////////////
if (lastloc != 'u' && ValidDown) {
enemy1.locX++;
lastloc = 'd';
if (l1_map[enemy1.locX - 1][enemy1.locY] == enemy1.character) {
l1_map[enemy1.locX - 1][enemy1.locY] = 32;
}
}
else if (lastloc == 'd' && ValidUp) {
enemy1.locX--;
lastloc = 'u';
if (l1_map[enemy1.locX + 1][enemy1.locY] == enemy1.character) {
l1_map[enemy1.locX + 1][enemy1.locY] = 32;
}
}
else {
if (ValidUp) {
enemy1.locX--;
lastloc = 'u';
if (l1_map[enemy1.locX + 1][enemy1.locY] == enemy1.character) {
l1_map[enemy1.locX + 1][enemy1.locY] = 32;
}
}
else {
enemy1.locX++;
lastloc = 'd';
if (l1_map[enemy1.locX - 1][enemy1.locY] == enemy1.character) {
l1_map[enemy1.locX - 1][enemy1.locY] = 32;
}
}
}
return NULL;
}
我正在努力实现的目标:
我需要减慢EnemyMovemens()
功能,因为敌人会快速移动。或者使PlayerMovements()
功能独立于其他功能。
问题:
我尝试使用线程。像this_thread::sleep_for()
和Sleep()
这样的功能。将它们放入EnemyMovements()
功能中。在这种情况下,它会减慢所有应用程序的速度,并且我的播放器移动速度非常慢。
我不需要代码,因为我喜欢自己学习。我只需要一个指导,现在该往哪个方向移动。
操作系统:赢10
基本问题是游戏循环中没有任何计时,因此一切都以相同的速度发生。 网上有一些很好的文章讨论了游戏计时循环,例如:http://www.koonsolo.com/news/dewitters-gameloop/引导您完成从您现在使用的方法到将游戏更新计时与显示计时分开的更好方法的步骤。
这篇文章没有告诉你如何减慢敌人相对于你的玩家的速度 - 一种方法是有一个 enemySpeed 变量并使用类似的东西:
// outside main loop
int enemySlowness = 20; // adjust this as needed
int enemyMoveTimer = 0;
...
if (++enemyMoveTimer > enemySlowness)
{
Enemy1Movements(enemy1, l1_map, lastloc);
enemyMoveTime = 0;
}
这种方法存在问题,但再加上该文章中的一些想法应该可以让您走上正确的轨道。
相关文章:
- 运行同一解决方案的另一个项目的项目
- CMake-按正确顺序将项目与C运行时对象文件链接
- 如何运行位于boost/libs/python/example/tutorial目录中的hello.cpp和Jamfil
- 代码在main()中运行,但在函数中出现错误
- 有什么理由更喜欢从 IDE 中运行应用程序而不是运行独立的可执行文件?
- 如何连接两个文件夹/项目的生成文件,这两个文件夹/项目必须一起运行且独立运行
- 在独立的 Xcode 操场中运行 c 或 c++ 代码
- OPENCV:是否应该在独立线程中运行VideoWriter的写入
- DBUS 代码在放置在守护进程内时崩溃,但在没有守护进程代码的独立独立 main() 函数中运行良好
- 如何在C++上独立运行函数
- 从 Visual C++ 运行时有效,但有时不是独立的
- 派生运行独立代码的子级和父级
- 是否可以像Java一样在VM中运行,从而使C++平台独立
- C++将项目作为独立文件运行,而不是作为Code::Blocks项目运行
- c++使运行时间独立于平台
- Visual Studio c++ -运行独立的.cpp文件
- 函数应该在两个类中的一个/两个中运行,还是独立运行?
- 在程序运行时隐藏控制台的独立于平台的方法
- 如何使此代码中的线程独立运行
- 共享库和可执行文件与 Linux 上的静态 C 运行时链接.它们中的每一个都有像Windows这样的独立堆吗?