Results 1 to 6 of 6
  1. #1
    Join Date
    Dec 2010
    Posts
    6

    C++ Character class tutorial problems

    I have been working on making the console game and am up to character class. When I debug and press any of the keys to move the character sprite only moves right and instead of erasing the last position it was at it leaves a copy + a space.

    Game Engine cpp file
    Code:
    #include "engine.h"
    #include <Windows.h>
    #include <iostream>
    
    using namespace std;
    
    drawEngine::drawEngine(int xsize, int ysize)
    {
    	screenWidth = xsize;
    	screenHeight = ysize;
    
    	// Set cursor vis to false
    	cursorVis(false);
    }
    
    drawEngine::~drawEngine()
    {
    	// Set vis to true
    	cursorVis(true);
    }
    int drawEngine::createSprite(int index, char c)
    {
    	if (index >= 0 && index < 16)
    	{
    		spriteImage[index] = c;
    		return index;
    	}
    	return -1;
    
    }
    
    void drawEngine::drawSprite(int index, int posx, int posy)
    {
    	// Go to location 
    	gotoxy(posx, posy);
    	
    	// Draw 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::cursorVis(bool vis)
    {
    	HANDLE output_handle;
    	CONSOLE_CURSOR_INFO cciInfo;
    
    	cciInfo.dwSize = 1;
    	cciInfo.bVisible = vis;
    
    	output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
    	SetConsoleCursorInfo(output_handle, &cciInfo);
    }
    Game engine header file
    Code:
    #ifndef ENGINE_H
    #define ENGINE_H
    
    
    class drawEngine
    {
    public:
    	drawEngine(int xsize = 100, int ysize = 50);
    	~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 cursorVis(bool vis);
    
    
    	};
    
    #endif
    Sprite cpp
    Code:
    #include "sprite.h"
    
    
    sprite::sprite(drawEngine *de, int s_index, int i_lives, float x, float y)
    {
    	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 dying sprite here
    	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)
    {
    	if (numLives <= 0)
    	{
    		return false;
    	}
    	return true;
    }
    
    bool sprite::move(float x, float y)
    {
    	// Erase Sprite
    	erase(pos.x, pos.y);
    	pos.x += x;
    	pos.y += 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);
    }
    Sprite header file
    Code:
    #ifndef SPRITE_H
    #define SPRITE_H
    
    #include "engine.h"
    
    enum classID
    {
    	SPRITE_CLASSID,
    	CHARACTER_CLASSID
    
    };
    
    struct vector
    {
    	float x;
    	float y;
    };
    
    class sprite
    {
    public:
    	sprite(drawEngine *de, int s_index, int i_lives, float x = 1, float y = 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);
    
    private:
    	
    	
    
    
    protected:
    	drawEngine *drawArea;
    	vector pos;
    	int spriteIndex;
    	int numLives;
    	vector facingDirection;
    	void draw(float x, float y);
    	void erase(float x, float y);
    	int classid;
    
    };
    Game loop cpp

    Code:
    #include "game.h"
    #include <conio.h>
    #include <iostream>
    #include <Windows.h>
    
    
    
    using namespace std;
    
    // Defines fps
    #define GAME_SPEED 33.33333
    
    
    bool game::run(void)
    {
    	
    drawArea.createSprite(0, '8');
    
    
    	player = new character(&drawArea, 0);
    
    	
    	char key = ' ';
    	startTime = timeGetTime();
    	frameCount = 0;
    
    	while (key != 27)
    	{
    
    		while (!getInput(&key))
    		{
    		timerUpdate();
    		}
    		
    		player->keyPress(key);
    	
    	}
    	
    	delete player;
    	return true;
    		}
    
    bool game::getInput(char *c)
    {
    	if (_kbhit())
    	{
    		*c = _getch();
    		return true;
    	}
    	return false;
    }
    
    void game::timerUpdate(void)
    {
    	double currentTime = timeGetTime() - lastTime;
    
    	if (currentTime < GAME_SPEED)
    		return;
    	frameCount++;
    	
    
    	lastTime = timeGetTime();
    }
    Game Loop header

    Code:
    #ifndef GAME_H
    #define GAME_H
    
    #include "character.h"
    #include "engine.h"
    
    class game
    {
    public:
    	bool run(void);
    	character *player;
    
    protected:
    	bool getInput(char *c);
    		void timerUpdate(void);
    		
    
    private:
    	double frameCount;
    	double lastTime;
    	double startTime;
    	drawEngine drawArea;
    
    };
    
    
    #endif
    character class cpp

    Code:
    #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, numLives)
    {
    	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 == left_key)
    		{
    			return move(-1, 0);
    		}
    		else if (c == right_key)
    		{
    			return move(1, 0);
    		}
    				return false;
    }
    character header

    Code:
    #ifndef CHARACTER_H
    #define CHARACTER_H
    
    #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);
    
    private:
    
    protected:
    	char up_key;
    	char down_key;
    	char left_key;
    	char right_key;
    };
    
    
    
    
    #endi
    and finally MAIN

    Code:
    #include <iostream>
    #include "game.h"
    #include "engine.h"
    
    
    using namespace std;
    
    int main()
    {
    	
    	system("color f3");
    
    	game gameMain;
    
    	gameMain.run();
    
    	return 0;
    
    }

  2. #2
    Join Date
    Sep 2008
    Location
    Salisbury, UK
    Posts
    2,812
    Just bumping... seeing as I suggested that bozo start this thread, I thought I ought to at least help keep it on the front forum page...

    Anyone out there know C++ ?

    I found
    my current avatar on google, so props to THIS GUY who created the original...

  3. #3
    Join Date
    Mar 2004
    Posts
    0
    You never set u, d, l, and r to any keys in your character constructor, or pass them in from the game class.

  4. #4
    Join Date
    Oct 2006
    Posts
    1,548
    Hint

    Code:
    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, numLives)
    [quote][\quote]

  5. #5
    Join Date
    Dec 2010
    Posts
    6
    thanks wforl
    i spent hours last night wondering what the problem was and forgot that i had moved the float x, and y in the sprite header from what the videos had because it didnt like it when i put float x = 1, float y = 1 in the middle of the:
    sprite(drawEngine *de, int s_index, int i_lives, float x = 1, float y = 1);

    so all i need to do was sprite(de, s_index, x, y, numLives) make that sprite(de, s_index, numLives, x, y)

    much appreciated

  6. #6
    Join Date
    Oct 2006
    Posts
    1,548
    You're also passing Sprite::numLives to the sprite classes constuctor.

    I believe you want that to be "lives"

    Code:
    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, numLives)
    [quote][\quote]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •