Visual c++ 跟随Chili'时在屏幕周围移动十字线时出现问题;现在开始DirectX教程

Visual c++ 跟随Chili'时在屏幕周围移动十字线时出现问题;现在开始DirectX教程,visual-c++,directx-9,Visual C++,Directx 9,首先,我将在这里下载第1-15课的Chili框架: 我正在运行WindowsXPSP3的旧笔记本电脑上使用DirectX9。为了运行框架,我已经将Direct3D渲染设置为软件。我使用VisualStudioExpress C++ 2010,安装了第一个服务包。 这是我遇到问题的代码: // Start moving reticle code DrawReticle(itemLocX, itemLocY, 255, 255, 255); if(itemLocX == pointA &

首先,我将在这里下载第1-15课的Chili框架:

我正在运行WindowsXPSP3的旧笔记本电脑上使用DirectX9。为了运行框架,我已经将Direct3D渲染设置为软件。我使用VisualStudioExpress 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轴移动。我可能会在以后发布,但没有现成的。您可以在本视频的开头看到:

然而,我希望这样做符合逻辑,就好像我在沿着盒子里一堵看不见的墙走边界一样。我想让事情变得有意义。但是光标不会移动,我看不出为什么它不移动。这是我的游戏。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

            /********************************/
        };
这是我的游戏。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
}
#包括“Game.h”
游戏:游戏(HWND HWND,常量键盘服务器和kServer)
:gfx(hWnd),
kbd(kServer),
itemLocX(100),//项目在x轴上的初始位置
itemLocY(100),//项目在y轴上的初始位置
十字线(400),//十字线在x轴上的初始位置
十字线(300),//y轴上十字线的初始位置
点A(100),//从0移动到A,在A处停止
pointAb(700),//从A移动到b,在b处停止
点BC(500),//从B移动到c,在c处停止
点CD(700),//从C移动到d,在d处停止
pointDa(500)//从D移动到a,在a处停止
{}
void游戏::Go()
{
gfx.BeginFrame();
复合框架();
gfx.EndFrame();
}
虚空游戏:抽划十字线(int xP,int yP,int cR,int cG,int cB)
/*
xP=x位置,
yP=y位置,
cR=红色,
cG=绿色,
cB=蓝色
*/
{
像素(xP-5,yP,cR,cG,cB);
绿色像素(xP-4,yP,cR,cG,cB);
像素(xP-3,yP,cR,cG,cB);
像素(xP+3,yP,cR,cG,cB);
像素(xP+4,yP,cR,cG,cB);
像素(xP+5,yP,cR,cG,cB);
像素(xP、yP、cR、cG、cB);
像素(xP,yP-5,cR,cG,cB);
像素(xP,yP-4,cR,cG,cB);
像素(xP,yP-3,cR,cG,cB);
像素(xP,yP+3,cR,cG,cB);
像素(xP,yP+4,cR,cG,cB);
像素(xP,yP+5,cR,cG,cB);
}
void Game::ComposeFrame()
{
//开始绘制十字线代码
牵引十字线(十字线,十字线,100155255);
//端画十字线代码
//启动颜色更改代码
int yT=200;//边框距顶部200像素
int yB=400;//边框距底部200像素
int xL=300;//左起200像素边框
int xR=500;//右起200像素边框
if(网线xR)//定义X的颜色变化区域
{
牵引十字线(十字线、十字线、255、255);
}
if(网线yB)//定义Y的颜色变化区域
{
牵引十字线(十字线、十字线、255、255);
}
//结束颜色更改代码
//开始移动十字线代码
牵引十字线(itemLocX、itemLocY、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;
}
//末端移动十字线编码
//起始边界代码
if(网线<6)
{
网线=6;
}
否则如果(网线>794)
{
网线=794;
}
if(网线<6)
{
瑞克利=6;
}
否则,如果(网线>594)
{
瑞克利=594;
}
//结束边界代码
//起动速度变化代码
int cSpeed=4;//默认光标速度
if(kbd.enteripressed())//更改为高速
{
cSpeed=8;
}
if(kbd.SpaceIsPressed())//更改为低速
{
cSpeed=1;
}
if(kbd.RightIsPressed())
{
网线+=cSpeed;
}
if(kbd.LeftIsPressed())
{
网线-=cSpeed;
}
if(kbd.UpIsPressed())
{
网纹纸-=cSpeed;
}
if(kbd.downipressed())
{
网线+=cSpeed;
}
//结束速度变化代码
}
现在我要注意的是,这应该是没有函数的,只有基本C++操作符。这就是Chili教授到这一点的内容。这是我自己第二次尝试解决这个问题,在花了几个小时思考并在纸上工作之后。我卡住了。只是看不见而已。我认为这是我的一个逻辑错误。我想知道我的想法可能错在哪里,但更重要的是,如何像计算机一样正确地思考这个问题

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


谢谢你的帮助-非常感谢

我知道你是怎么做的。就个人而言,你把它搞得太复杂了

1:你不需要这个!=if语句中的运算符

2:试试这个:

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

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

我相信你的回答,尽管我还没有测试过。感谢您抽出时间来到这里并给出答案,尽管这条线已经很旧了。这对我来说意义重大,我真的很感激!谢谢:)。我个人说要一直尝试编写最简单的代码