1

首先,我在第 1-15 课中使用 Chili 框架,可在此处下载: http ://www.planetchili.net/

我在运行 Windows XP SP3 的旧笔记本电脑上使用 DirectX 9。为了运行框架,我已将 Direct3D 渲染设置为软件。我正在使用安装了第一个服务包的 Visual Studio Express C++ 2010。

这是我遇到问题的代码:

// Start moving reticle code

DrawReticle(itemLocX, itemLocY, 255, 255, 255);

if(itemLocX == pointA && itemLocX != pointAb)
{
    itemLocX += 2;
}
else if(itemLocX == pointBc && itemLocX != pointDa)
{
    itemLocX -= 2;
}

if(itemLocY == pointAb && itemLocY != pointBc)
{
    itemLocY += 2;
}
else if(itemLocY == pointDa && itemLocX != pointA)
{
    itemLocY -= 2;
}

// End moving reticle code

现在 Chili 的解决方案是在检查 x 时沿 y 轴移动,在检查 y 时沿 x 移动。我可能会稍后发布,没有现成的。你可以在这个视频的开头看到它:http: //youtu.be/JEmwkQsi8l0

然而,我想在逻辑上做到这一点,就好像我在一个盒子里沿着一堵看不见的墙走着边界。我想让它弄清楚发生了什么。但是光标不会移动,我看不出它为什么不移动。这是我的game.h:

        #pragma once

        #include "D3DGraphics.h"
        #include "Keyboard.h"

        class Game
        {
        public:
            Game( HWND hWnd,const KeyboardServer& kServer );
            void Go();
        private:
            void ComposeFrame();
            /********************************/
            /*  User Functions              */

            void DrawReticle(int xP, int yP, int cR, int cG, int cB);
            /*
                xP = x position,
                yP = y position,
                cR = color red,
                cG = color green,
                cB = color blue
            */

            // TODO: User functions go here

            /********************************/
        private:
            D3DGraphics gfx;
            KeyboardClient kbd;
            /********************************/
            /*  User Variables              */

            int pointA;  // Starting at pointA (100, 100) - the top left
            int pointAb; // Move from pointA to pointAb (700, 100) - the top right
            int pointBc; // Move from pointAb to pointBc (700, 500) - the bottom right
            int pointCd; // Move from pointBc to pointCd (100,500) - the bottom left
            int pointDa; // Move from pointCd to pointDa (100,100) - the top left


/*
    These points describe the process of starting, then four movements. The four points  are A, B, C, D. We start at A, then go to B (pointAb, read as A to b), then go to C (pointBc, read as B to c), then go to D (pointCd, read as C to d) then go to A (pointDa, read as D to a).

        This can be very confusing, because there are five varibles used. But if we drew it out there would only four points, as well as only four movements. The best way to think of it is that starting is itself a movement, and as you need a place to start from, it itself must have a point. Since you start at A, but haven't yet gone anywhere, pointA is our starting point. Once you start moving, you go from pointA to pointB. Now if we used pointB as our variable it would be confusing,because we would have to move from pointA to pointB to pointC to pointD and then back to pointA. Still five variables, one is repeating, but the first pointA describes where you start, and the last where you end. Since these are two different actions on the same point, I have elected to use two letter names for each of the points you move to, while the point you start at has a single letter name. It was the best way I could clearly think about this process.
                */

            int itemLocX; // Initial position of item on the x axis
            int itemLocY; // Initial position of item on the y axis
            int reticleX; // Initial position of reticle on the x axis
            int reticleY; // Initial position of reticle on the y axis

            // TODO: User variables go here

            /********************************/
        };

这是我的game.cpp:

#include "Game.h"

Game::Game( HWND hWnd,const KeyboardServer& kServer )
:   gfx(hWnd),
    kbd(kServer),
    itemLocX(100), // Initial position of item on the x axis
    itemLocY(100), // Initial position of item on the y axis
    reticleX(400), // Initial position of reticle on the x axis
    reticleY(300), // Initial position of reticle on the y axis
    pointA(100),  // Movement from 0 to A, stopping at A
    pointAb(700), // Movement from A to b, stopping at B
    pointBc(500), // Movement from B to c, stopping at C
    pointCd(700), // Movement from C to d, stopping at D
    pointDa(500)  // Movement from D to a, stopping at A
{}

void Game::Go()
{
    gfx.BeginFrame();
    ComposeFrame();
    gfx.EndFrame();
}

void Game::DrawReticle(int xP, int yP, int cR, int cG, int cB)
/*
    xP = x position,
    yP = y position,
    cR = color red,
    cG = color green,
    cB = color blue
*/
{
    gfx.PutPixel(xP-5,yP,cR,cG,cB);
    gfx.PutPixel(xP-4,yP,cR,cG,cB);
    gfx.PutPixel(xP-3,yP,cR,cG,cB);
    gfx.PutPixel(xP+3,yP,cR,cG,cB);
    gfx.PutPixel(xP+4,yP,cR,cG,cB);
    gfx.PutPixel(xP+5,yP,cR,cG,cB);
    gfx.PutPixel(xP,yP,cR,cG,cB);
    gfx.PutPixel(xP,yP-5,cR,cG,cB);
    gfx.PutPixel(xP,yP-4,cR,cG,cB);
    gfx.PutPixel(xP,yP-3,cR,cG,cB);
    gfx.PutPixel(xP,yP+3,cR,cG,cB);
    gfx.PutPixel(xP,yP+4,cR,cG,cB);
    gfx.PutPixel(xP,yP+5,cR,cG,cB);
}

void Game::ComposeFrame()
{
    // Start draw reticle code

    DrawReticle(reticleX, reticleY, 100, 155, 255);

    // End draw reticle code

    // Start color change code

    int yT = 200; // Border 200 pixels from top
    int yB = 400; // Border 200 pixels from bottom
    int xL = 300; // Border 200 pixels from left
    int xR = 500; // Border 200 pixels from right

    if(reticleX < xL || reticleX > xR) // Defining color change area for X
    {
        DrawReticle(reticleX, reticleY, 255, 255, 255);
    }

    if(reticleY < yT || reticleY > yB) // Defining color change area for Y
    {
        DrawReticle(reticleX, reticleY, 255, 255, 255);
    }

    // End color change code

    // Start moving reticle code

    DrawReticle(itemLocX, itemLocY, 255, 255, 255);

    if(itemLocX == pointA && itemLocX != pointAb)
    {
        itemLocX += 2;
    }
    else if(itemLocX == pointBc && itemLocX != pointDa)
    {
        itemLocX -= 2;
    }

    if(itemLocY == pointAb && itemLocY != pointBc)
    {
        itemLocY += 2;
    }
    else if(itemLocY == pointDa && itemLocX != pointA)
    {
        itemLocY -= 2;
    }

    // End moving reticle code

    // Start border code

    if(reticleX < 6)
    {
        reticleX = 6;
    }
    else if(reticleX > 794)
    {
        reticleX = 794;
    }

    if(reticleY < 6)
    {
        reticleY = 6;
    }
    else if(reticleY > 594)
    {
        reticleY = 594;
    }

    // End border code

    // Start speed change code

    int cSpeed = 4; // Default cursor speed

    if(kbd.EnterIsPressed()) // Change to high speed
    {
        cSpeed = 8;
    }

    if(kbd.SpaceIsPressed()) // Change to low speed
    {
        cSpeed = 1;
    }

    if(kbd.RightIsPressed())
    {
        reticleX += cSpeed;
    }

    if(kbd.LeftIsPressed())
    {
        reticleX -= cSpeed;
    }

    if(kbd.UpIsPressed())
    {
        reticleY -= cSpeed;
    }

    if(kbd.DownIsPressed())
    {
        reticleY += cSpeed;
    }

    // End speed change code
}

现在我应该在这里指出,这应该在没有函数的情况下完成,而只有基本的 C++ 运算符。这就是辣椒到目前为止所教的。这是我第二次尝试自己解决这个问题,经过数小时的思考和纸上谈兵。我被困住了。只是没看到。我认为我这里有一个逻辑错误。我想了解我的想法在哪里可能是错误的,但更重要的是,如何正确地思考,就像电脑一样,关于这个。

我也愿意接受有关我的编码风格的建议。如果我不够清楚,或者正在做一些不应该成为坏习惯的事情 - 基本上,如果在编写代码时我应该做一些不同的事情,我想知道它。

感谢您的帮助 - 非常感谢!

4

1 回答 1

2

我知道你是如何尝试这样做的。就个人而言,你已经过度复杂了。

1:您的 if 语句中不需要 != 运算符。

2:试试这个:

if(itemLocX < 700)
{
    itemLocX += 2;
}

3:这在测试期间运行良好。另一点是 if 语句的顺序可能是错误的。我将它更改为它在屏幕上移动的顺序。我有 XYXY,你有 XXY Y。(未确认)它按顺序执行 if 语句。我已经硬编码了答案。如果你真的想要,将它们设置为变量。希望这有帮助!

于 2014-04-26T06:59:07.627 回答