Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Errors.. C2504: 'Sprite' : base class undefined character.h line 8
- // C2061: syntax error: identifier 'DrawEngine' character.h line 10
- C2143: syntax error: missing ';' before* game.h line 40
- #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);
- protected:
- char upKey;
- char downKey;
- char leftKey;
- char rightKey;
- private:
- };
- #endif
- //***************************************************************************************
- //CHARACTER.CPP
- #include "character.h"
- Character::Character(DrawEngine *de, int s_index, float x, float y,
- int lives, char upKey, char downKey, char leftKey,
- char rightKey) : Sprite(de, s_index, x, y, lives)
- {
- upKey = upKey;
- downKey = downKey;
- leftKey = leftKey;
- rightKey = rightKey;
- classID = CHARACTER_CLASSID;
- }
- bool Character::keyPress(char c)
- {
- if(c == upKey)
- {
- return move(0, -1);
- }
- else if(c == downKey)
- {
- return move(0, 1);
- }
- else if(c == rightKey)
- {
- return move(1, 0);
- }
- else if(c == leftKey)
- {
- return move(-1, 0);
- }
- return false;
- }
- //****************************************************************************************
- #ifndef DRAWENGINE_H
- #define DRAWENGINE_H
- #include "level.h"
- #include "game.h"
- #include "character.h"
- class DrawEngine
- {
- public:
- DrawEngine(int xSize = 30, int ySize = 20);
- //cursorVisibilty will be OFF in the coACnstructor
- //cursorVisibilty will be ON in the destructor
- ~DrawEngine();
- //select sprite, sprite representation character (appearance)
- int createSprite(int index, char c);
- void deleteSprite(int index);
- //space character drawn at xy to erase sprite
- void eraseSprite(int posx, int posy);
- //selected sprite, drawn at posx, with posy
- void drawSprite(int index, int posx, int posy);
- void createBackgroundTile(int index, char c);
- void setMap(char **);
- void drawBackground(void);
- protected:
- char **map;
- int screenWidth, screenHeight;
- char spriteImage[16];
- char tileImage[16];
- private:
- void gotoxy(int x, int y);
- //false is hidden, true is visible
- void cursorVisibilty(bool visibility);
- };
- #endif
- //**************************************************************
- //drawEngine.cpp
- #include "drawEngine.h"
- #include <Windows.h>
- #include<iostream>
- //DrawEngine Constructor
- DrawEngine::DrawEngine(int xSize, int ySize)
- {
- screenWidth = xSize;
- screenHeight = ySize;
- //set cursor visibility to false
- cursorVisibilty(false);
- map = 0;
- }
- //Draw Engine Destructor
- DrawEngine::~DrawEngine()
- {
- //set cursor visibility to true
- cursorVisibilty(true);
- }
- int DrawEngine::createSprite(int index, char c)
- {
- if(index >= 0 && index < 16)
- {
- spriteImage[index] = c;
- return index;
- }
- //error
- return -1;
- }
- void DrawEngine::deleteSprite(int index)
- {
- //this implementation we don't need it
- }
- void DrawEngine::drawSprite(int index, int posx, int posy)
- {
- //go to correct location
- gotoxy(posx, posy);
- //draw the image
- std::cout << spriteImage[index];
- }
- void DrawEngine::eraseSprite(int posx, int posy)
- {
- gotoxy(posx, posy);
- std::cout << ' ' ;
- }
- void DrawEngine::setMap(char **data)
- {
- map = data;
- }
- void DrawEngine::createBackgroundTile(int index, char c)
- {
- if ( index >= 0 && index < 16)
- {
- tileImage[index] = c;
- }
- }
- void DrawEngine::drawBackground(void)
- {
- if (map)
- {
- for (int y = 0; y < screenHeight; y++)
- {
- gotoxy(0, y);
- for(int x = 0; x < screenWidth; x++)
- {
- std::cout << tileImage[map[x][y]];
- }
- }
- }
- }
- void DrawEngine::gotoxy(int x, int y)
- {
- //A handle in windows is an ID that asscociates with something
- //in the system
- HANDLE output_handle;
- //built in structure
- COORD pos;
- pos.X = x;
- pos.Y = y;
- // This will get us the output_handle which is the ID
- output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
- //what windows ID has the cursor, what position to put the cursor
- SetConsoleCursorPosition(output_handle, pos);
- }
- //gets called in drawEngine Constructor and destructor
- // my draw engine is called drawArea
- void DrawEngine::cursorVisibilty(bool visibility)
- {
- HANDLE output_handle;
- CONSOLE_CURSOR_INFO cciInfo;
- //sets cursor size
- cciInfo.dwSize = 1;
- cciInfo.bVisible = visibility;
- output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
- SetConsoleCursorInfo(output_handle, &cciInfo);
- }
- //*****************************************************************************
- #ifndef GAME_H
- #define GAME_H
- #include"drawEngine.h"
- #include"level.h"
- #include "character.h"
- class Game
- {
- public:
- //This function will determine if the game is running
- bool run(void); //Will not get out of this function until the game ends
- protected:
- /****************************************************
- * BOOL GETINPUT
- * @param char *c
- * This will see if the user presses a key
- *
- * If the user presses a key, it returns TRUE and put the character pressed inside of *c's address.
- * While they aren't pressing a key it returns FALSE
- ****************************************************/
- bool getInput(char *key);
- /**********************************************************
- * VOID TIMERUPDATE
- * Called constantly
- * Contains the game code
- * Runs constantly
- * Controls the speed at which the game plays
- **********************************************************/
- void timerUpdate(void);
- private:
- //35:04
- Level *level;
- Character *player;
- double frameCount;
- double startTime;
- double lastTime;
- double timeRunning;
- int posx;
- DrawEngine drawArea;
- };
- #endif
- //*************************************************************
- //game.cpp
- #include "game.h"
- #include"drawEngine.h"
- #include <iostream>
- #include <conio.h>
- #include <Windows.h>
- #define GAME_SPEED 33.3 //This will give us roughly 30 fps
- bool Game::run(void)
- {
- level = new Level(&drawArea, 30,20);
- drawArea.createBackgroundTile(TILE_EMPTY, ' ');
- drawArea.createBackgroundTile(TILE_WALL, '+');
- //Draw Area defined in game.h private:
- //create sprite and send it index and the character representation
- // of the sprite
- drawArea.createSprite(0, '$');
- player = new Character(&drawArea, 0);
- level->draw();
- //initialize key and position at x
- char key = ' ';
- startTime = timeGetTime();
- frameCount = 0;
- lastTime = 0;
- //if the key you pressed doesn't == q, then loop
- while (key != 'q')
- {
- //while getInput is returning false
- //pass getInput() the address of char key ; & = address
- //call timerUpdate
- //exits loop when key is pressed
- while(!Game::getInput(&key))
- {
- timerUpdate();
- }
- //send keypress()->inside of player the new value of key
- player->keyPress(key);
- }
- //prevent memory leak
- delete player;
- //**************************NOTES****************************************
- // if i start the program at 10:10:10 (startTime) and end it at 10:10:20,
- //the difference is how long we have been running
- //obviously bigger value minus the smaller value
- timeRunning = (timeGetTime() - startTime);
- std::cout << frameCount / (timeRunning /1000) << " fps" << "\n";
- std::cout << "Frame Count: " << frameCount << "\n";
- std::cout << "End of the game" << "\n";
- return true;
- }
- /*****************************************************
- * GET INPUT
- * Recieves the variable ADDRESS from &key
- * Checks to see if the player hits a key
- *
- * If the player hits a key then replace the data in
- * address &key with the players new key and RETURN TRUE
- *
- * If no key is hit, return false
- *****************************************************/
- bool Game::getInput(char *key)
- {
- if(kbhit())
- {
- *key = getch();
- return true;
- }
- return false;
- }
- /*********************************************************
- *
- *********************************************************/
- void Game::timerUpdate(void)
- {
- double currentTime = timeGetTime() - lastTime;
- if(currentTime < GAME_SPEED)
- {
- return;
- }
- frameCount++;
- lastTime = timeGetTime();
- }
- //**********************************************************
- #ifndef LEVEL_H
- #define LEVEL_H
- enum
- {
- TILE_EMPTY,
- TILE_WALL
- };
- #include "drawEngine.h"
- class Character;
- class Level
- {
- public:
- Level(DrawEngine *de, int width = 30, int height = 20);
- ~Level();
- void addPlayer(Character *p);
- void update(void);
- void draw(void);
- bool keyPress(char c);
- protected:
- void createLevel(void);
- private:
- int width;
- int height;
- char **level;
- Character *player;
- DrawEngine *drawArea;
- };
- #endif
- //************************************************************************
- //level.cpp
- #include "level.h"
- #include <stdlib.h>
- Level::Level(DrawEngine *de, int w, int h)
- {
- drawArea = de;
- width = w;
- height = h;
- player = 0;
- //char ** map data
- level = new char *[width];
- //create memory for our level
- for(int x = 0; x < width; x++)
- {
- level[x] = new char[height];
- }
- //create the level
- createLevel();
- drawArea->setMap(level);
- }
- Level::~Level()
- {
- for(int x = 0; x < width; x++)
- {
- delete[] level[x];
- }
- delete [] level;
- }
- void Level::createLevel(void)
- {
- for(int x = 0; x < width; x++)
- {
- for (int y = 0; y < height; y++)
- {
- int random = rand() %100;
- if(y == 0 || y == height - 1 || x == 0 || x == width - 1)
- {
- level[x][y] = TILE_WALL;
- }
- else
- {
- if (random < 90 || (x < 3 && y < 3))
- level[x][y] = TILE_EMPTY;
- else
- level[x][y] = TILE_WALL;
- }
- }
- }
- }
- void Level::draw(void)
- {
- drawArea->drawBackground();
- }
- void Level::addPlayer(Character *p)
- {
- player = p;
- }
- //*************************************************************************************
- #ifndef SPRITE_H
- #define SPRITE_H
- #include"drawEngine.h"
- enum
- {
- SPRITE_CLASSID,
- CHARACTER_CLASSID
- };
- typedef struct _Vector
- {
- float x;
- float y;
- } _Vector;
- class Sprite
- {
- public:
- //constructor
- Sprite(DrawEngine * de, int s_index, float x = 1, float y = 1, int i_lives = 1);
- //destructor
- ~Sprite();
- int classID;
- _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:
- void draw(float x, float y);
- void erase(float x, float y);
- DrawEngine *drawArea;
- _Vector pos;
- int spriteIndex;
- int numLives;
- _Vector facingDirection;
- };
- #endif
- //***********************************************************
- //sprite.cpp
- #include "sprite.h"
- #include"drawEngine.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 dying 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);
- /*
- if(numLives <= 0)
- return false;
- return 0;
- */
- }
- 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);
- }
- //************************************************
- //main.cpp
- #include "game.h"
- int main()
- {
- Game gameHeart;
- //start the game loop
- gameHeart.run();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement