首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在阅读Chili的DirectX入门教程时,在屏幕上移动标线时遇到了困难

在阅读Chili的DirectX入门教程时,在屏幕上移动标线时遇到了困难
EN

Stack Overflow用户
提问于 2013-05-19 09:10:17
回答 1查看 254关注 0票数 1

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

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

这是我遇到问题的代码:

代码语言:javascript
复制
// 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的解决方案是沿着y轴移动,同时检查x,和x,同时检查y,我稍后可能会发布,但没有现成的。你可以在这段视频的开头看到:http://youtu.be/JEmwkQsi8l0

我想让它明白到底发生了什么。

代码语言:javascript
复制
        #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:

代码语言:javascript
复制
#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++运算符。到目前为止,Chili只教了这么多。我被卡住了。只是看不到而已。我认为我这里有一个逻辑错误。

我也乐于接受关于我的编码风格的建议。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-26 14:59:08

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

2:试试这个:

代码语言:javascript
复制
if(itemLocX < 700)
{
    itemLocX += 2;
}

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16630529

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档