微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C++游戏中物体的移动

如何解决C++游戏中物体的移动

所以我正在为学校项目制作游戏。你可能对这个很熟悉。它的打砖块,或用球来摧毁砖块并被平台偏转的游戏。

目前我被困在了一个点上。我已经知道如何使用 _getch() 移动我的平台,但是当我放入一个球时,它也在按键时移动。我无法弄清楚如何在有或没有任何按键的情况下同时运行它。或者如果有办法每次都跳过一个按键直到它被注册

到目前为止,这是我的代码。球的运动并不完整,现在只是一个原型。任何帮助将不胜感激。

我使用的是学校提供的图形库,但您可以使用 C++ 图形库。

#include <iostream>
#include "myconsole.h"
#include "mygraphics.h"
#include <conio.h>
#include <string>

using namespace std;

void rect() {
    COLORREF White = RGB(255,255,255);
    COLORREF Blue = RGB(135,206,235);
    //     x1   y    x2   y 
    myRect(400,475,550,480,Blue,Blue);
    _getch();
}

void circle() {
    COLORREF Red = RGB(255,0);
    myEllipse(0,50,300,350,Red,Red);
    _getch();
}

void moverect() {
    COLORREF White = RGB(255,235);
    char _x;
    const char _r = 'd';
    const char _l = 'a';
    int x1 = 400; 
    int x2 = 550;
    int by1 = 455;
    int by2 = 475;
    int m = 48;
    
    while (1) {
        _x = _getch();
        system("cls");
        if (_x == _r) {
            if (x2 < 970) {
                x1 += 10;
                x2 += 10;
                for (int i = 0; i < 10; i++) {
                    myRect(x1++,x2++,Blue);
                }
            }
            else
                myRect(x1,x2,Blue);
        }
        else if (_x == _l) {
            if (x1 > 0) {
                x1 -= 10;
                x2 -= 10;
                for (int i = 0; i < 10; i++) {
                    myRect(x1--,x2--,Blue);

                }
            }
            else
                myRect(x1,Blue);
        }
        myEllipse(463,by1 -= 10,487,by2 -= 10,White,White);
    }
}

int main() {
    {
        moverect();
        return 0;
    }
}

解决方法

由于您似乎在使用 Windows 和 Microsoft Visual C++,您可以使用 _kbhit(),它会告诉您是否按下了某个键。请注意,这不是标准的 C++。

这可能看起来像:

if (_kbhit()) {
    _x = _getch();
}
else {
    _x = -1;
}

或者,其他人提到了线程,虽然 John 也指出 _getch() 也不是标准的 C++,但我认为介绍一些线程的概念可能会有所帮助。使用线程有多种方法,但我将展示我认为最简单的方法。

首先我们将创建一个类,这是为了隐藏包含最新字符的变量。我们希望它隐藏,以便在从主线程读取它时,使用最新的字符(设置为 -1),以便在我们下次调用该函数时,如果尚未输入另一个字符,该函数将返回 -1。

请注意,如果两个线程同时读取和写入变量,则会发生未定义的行为,因此我们将使用原子变量,因为它是在两个线程尝试同时访问它时定义的。或者存在互斥锁,它们速度较慢,但​​允许锁定和解锁更复杂的类型,以便一次只有一个线程可以访问它们。

class LatestChar {
public:
    /// initialize latest character to -1
    LatestChar() :
        latest_char{ -1 }
    {}
    /// returns latest character or -1 if there is no latest character
    int getLatestChar() {
        // get latest character
        int temp_latest_char{ latest_char };
        // consume latest character (so if this is called again,-1 is returned)
        latest_char = -1;
        // return latest character
        return temp_latest_char;
    }
private:
    /// equal to latest character or -1 when there is no character to get
    ///
    /// this is atomic because it might be read from the main thread at the same
    /// time it is written from the getch thread.
    std::atomic_int latest_char;
};

接下来我们需要创建调用 _getch() 的线程。这是一个成员变量,将在构造函数中声明,它将开始运行并获取最新的字符并重复。

class LatestChar {
public:
    /// initialize latest character to -1,/// and starts thread that gets the latest character
    LatestChar() :
        ...,getch_thread([this] {
            while (true) {
                latest_char = _getch();
            }
        })
    {}
    ...
private:
    ...
    /// this thread loops until told to stop,and updates the latest character
    std::thread getch_thread;
};

差不多完成了,我们现在需要在程序结束时重新加入线程,我们将使用另一个原子变量,这次是一个布尔值,代表一个标志说“嘿,我们已经完成了字符的获取,完成你的任务正在做并加入主线程。

请注意,getch 线程只会在读取一个字符后进行检查,因此当您关闭程序时,您可能需要再输入一个字符,另一种方法是调用 std::terminate() 之类的东西,这将停止强制整个程序,这在可以避免的情况下可能是不希望的。

我们将在构造函数中将标志初始化为false(确保它在getch线程之前初始化,否则getch线程可能会在将其设置为false之前检查该值。它们的初始化顺序是相同的按照它们在类定义中声明的顺序。),在线程中检查它,并在析构函数中将其设置为 true,然后调用 join 函数,该函数将等待线程完成并将 join 连接回 main。>

class LatestChar {
public:
    /// initialize latest character to -1,end thread flag to false,end_thread{ false },getch_thread([this] {
            while (!end_thread) {
                latest_char = _getch();
            }
        })
    {}
    /// sets end thread flag to true and joins getch thread with main thread
    ~LatestChar() {
        end_thread = true;
        getch_thread.join();
    }
    ...
private:
    ...
    /// this flag tells the getch thread to stop,like at the end of a program,/// _getch() will need to receive a character before this is checked
    /// 
    /// this is atomic because it might be read from the getch thread at the
    /// same time it is written from the main thread.
    ///
    /// make sure this is initialized before the getch thread begins
    std::atomic_bool end_thread;
    ...
};

最后,让我们实例化类并调用函数。

// somewhere not in a loop
LatestChar latest_char;
...
// somewhere is a loop
_x = latest_char.getLatestChar();

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。