up and down are left and right

I made started a game using the tutorials from 3DBuzz, and all is fine- except that when I run it, up(w) moves me left, and down(s) moves me right. Please help!
I included the main.cpp, game.h, game.cpp, sprite.h, sprite.cpp, character.h, and character.cpp files. The draw engine is not on here. How do I make up be up, and down be down?

main.cpp
1
2
3
4
5
6
7
8
9
10
#include "game.h"
int main()
{//start of main

    Game gameHeart;

    gameHeart.run();

    return 0;
}//end of main 


game.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef GAME_H_INCLUDED
#define GAME_H_INCLUDED

#include "drawEngine.h"
#include "character.h"

class Game
{
public:
    bool run(void);
private:
    Character *player;

    double frameCount;
    double startTime;
    double lastTime;

    int posx;

    DrawEngine drawArea;

protected:
    bool getInput(char *c);
    void timerUpdate(void);
};



#endif // GAME_H_INCLUDED 


game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include "game.h"

#include <conio.h>
#include <iostream>
#include <windows.h>

using namespace std;


//this limits FPS. to find what number to use, divide 1000 by the target fps. use the answer here
const int GAME_SPEED = (1000/30);


bool Game::run(void)
{
    drawArea.createSprite(0, '$'); //the char is the evil monkey sprite

    player = new Character(&drawArea, 0);

    char key = ' ';

    startTime = timeGetTime();
    frameCount = 0;
    lastTime = 0;


    posx = 0;

    while ( key != 'q')
        {
            while (!getInput(&key))
            {
                timerUpdate();
            }
            player->keyPress(key);
        }
        delete player;

        cout << frameCount / ((timeGetTime() - startTime) / 1000)<< "fps" << endl;
        cout << "End of the game." << endl;
        return true;
}

bool Game::getInput(char *c)
{
    if (kbhit())
    {
        *c = getch();
        return true;
    }

   return false;
}

void Game::timerUpdate(void)
{
    int currentTime = timeGetTime() - lastTime;

    if (currentTime < GAME_SPEED)
        return;

       /* player ->move(1,1); //moves player 1 to right and 0 in y

        drawArea.eraseSprite(posx, 5); //erase sprite as it goes
        posx = (posx + 1) % 80;
        drawArea.drawSprite(0, posx, 5); //the last two numbers are the evil monkeys cordinates */

    frameCount ++;
    lastTime = timeGetTime();
}


sprite.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef SPRITE_H_INCLUDED
#define SPRITE_H_INCLUDED

#include "drawEngine.h"

enum
{
    SPRITE_CLASSID,
    CHARACTER_CLASSID,
};

struct vector
{
    float x;
    float y;
};

class Sprite
{
public:
    Sprite(DrawEngine *de, int s_index, float  x = 1, float y = 1, int i_lives = 1);
    ~Sprite();

    vector getPosition(void);
    float getX(void);
    float getY(void);

    virtual void addLives(int num = 1);
    int getLives(void);
    bool isAlive(void);

    virtual bool move(float x, float y);


protected:
    DrawEngine *drawArea;
    vector pos;
    int spriteIndex;
    int numLives;

    int classID;

    vector facingDirection;

    void draw(float x, float y);
    void erase(float x, float y);
};


#endif // SPRITE_H_INCLUDED 


Sprite.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "sprite.h"

Sprite::Sprite(DrawEngine *de, int s_index, float x, float y, int i_lives)
{
    drawArea = de;

    pos.x = x;
    pos.y = y;

    spriteIndex = s_index;

    numLives = i_lives;

    facingDirection.x = 1;
    facingDirection.y = 0;

    classID = SPRITE_CLASSID;
}

Sprite::~Sprite()
{
    //erase the dieing sprite
    erase(pos.x, pos.y);
}

vector Sprite::getPosition(void)
{
    return pos;
}

float Sprite::getX(void)
{
    return pos.x;
}

float Sprite::getY(void)
{
    return pos.y;
}

void Sprite::addLives(int num)
{
    numLives += num;
}
int Sprite::getLives(void)
{
    return numLives;
}
bool Sprite::isAlive(void)
{
    return (numLives > 0);
}

bool Sprite::move(float x, float y)
{
    //erase sprite
    erase(pos.x, pos.y);

    pos.x += x;
    pos.x += y;

    facingDirection.x = x;
    facingDirection.y = y;
    //draw sprite
    draw(pos.x, pos.y);

    return true;
}
    void Sprite::draw(float x, float y)
    {
        drawArea ->drawSprite(spriteIndex, (int)x, (int)y);
    }
    void Sprite::erase(float x, float y)
    {
        drawArea ->eraseSprite((int)x, (int)y);
    }


character.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef CHARACTER_H_INCLUDED
#define CHARACTER_H_INCLUDED

#include "sprite.h"

class Character : public Sprite
{
public:
    Character(DrawEngine *de, int s_index, float x = 1, float y = 1,
              int lives = 3, char up_key = 'w', char down_key = 's', char left_key = 'a', char right_key = 'd');

    virtual bool keyPress(char c);

protected:
    char up_key;
    char down_key;
    char right_key;
    char left_key;
};


#endif // CHARACTER_H_INCLUDED 


character.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "character.h"

Character::Character(DrawEngine *de, int s_index, float x, float y, int lives,
                     char u, char d, char l, char r)
                     : Sprite(de, s_index, x, y, lives)
{
    up_key = u;
    down_key = d;
    left_key = l;
    right_key = r;

    classID = CHARACTER_CLASSID;
}

bool Character::keyPress(char c)
{
    if (c == up_key)
    {
        return move(0, -1);
    }
    else if (c == down_key)
    {
        return move(0, 1);
    }
    else if ( c == right_key)
    {
        return move(1, 0);
    }
    else if (c == left_key)
    {
        return move(-1, 0);
    }

}
Have you thought about just changing the values in lines 19 to 31? It seems like you just mixed up the axes.
move(y-coordinate, x-coordinate)
I tried that, same effect. I think the error is in the Sprite class... either that or the draw engine.

drawEngine.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef DRAWENGINE_H_INCLUDED
#define DRAWENGINE_H_INCLUDED


class DrawEngine
{
public:
    DrawEngine(int xSize = 30, int ySize = 20);
    ~DrawEngine();

    int createSprite(int index, char c);
    void deleteSprite(int index);

    void eraseSprite(int posx, int posy);
    void drawSprite(int index, int posx, int posy);

protected:
    int screenWidth;
    int screenHeight;
    char spriteImage[16];

private:
    void gotoxy(int x, int y);
    void cursorVisibility(bool visibility);
};

#endif // DRAWENGINE_H_INCLUDED 


drawEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "drawEngine.h"
#include <windows.h>
#include <iostream>

using namespace std;

DrawEngine::DrawEngine(int xSize, int ySize)
{
    screenWidth = xSize;
    screenHeight = ySize;

    //set cursor visibility to false
    cursorVisibility(false);

}
DrawEngine::~DrawEngine()
{
    //set cursor visibility to true
    cursorVisibility(false);
}

int DrawEngine::createSprite(int index, char c)
{
    if(index >= 0 && index < 16)
    {
        spriteImage[index] = c;
        return index;
    }

    return -1;
}
void DrawEngine::deleteSprite(int index)
{
    //in this implementation I don't need it
}
void DrawEngine::drawSprite(int index, int posx, int posy)
{

    ;
    //go to the correct location
    gotoxy(posx, posy);
    //draw the image
    cout << spriteImage[index];
}
void DrawEngine::eraseSprite(int posx, int posy)
{
    gotoxy(posx, posy);
    cout << ' ';
}

void DrawEngine::gotoxy(int x, int y)
{
    HANDLE output_handle;
    COORD pos;
    pos.X = x;
    pos.Y = y;

    output_handle = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleCursorPosition(output_handle, pos);
}
void DrawEngine::cursorVisibility(bool visibility)
{
    HANDLE output_handle;
    CONSOLE_CURSOR_INFO cciInfo;

    cciInfo.dwSize = 1; //if inbetween 1 and 100 console cursor is invisible
    cciInfo.bVisible = visibility; //bVisible if true console cursor can be seen, if false it cant. we control this through bool visibility

    output_handle = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleCursorInfo(output_handle, &cciInfo);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool Sprite::move(float x, float y)
{
    //erase sprite
    erase(pos.x, pos.y);

    pos.x += x;
    pos.x += y;

    facingDirection.x = x;
    facingDirection.y = y;
    //draw sprite
    draw(pos.x, pos.y);

    return true;
}


Shouldn't that be pos.y?
Last edited on
that fixed it thank you very much :D !!!
Topic archived. No new replies allowed.