Advertisement
frostbytes89

brokenCode :(

May 23rd, 2011
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.25 KB | None | 0 0
  1. //Errors.. C2504: 'Sprite' : base class undefined        character.h  line 8
  2. //         C2061: syntax error: identifier 'DrawEngine'  character.h  line 10
  3.            C2143: syntax error: missing ';' before*      game.h       line 40
  4.  
  5.  
  6. #ifndef CHARACTER_H
  7. #define CHARACTER_H
  8.  
  9. #include "sprite.h"
  10.  
  11.  
  12. class Character : public Sprite
  13. {
  14. public:
  15.     Character(DrawEngine * de, int s_index, float x = 1, float y = 1,
  16.               int lives = 3, char up_key = 'w', char down_key = 's', char left_key = 'a',
  17.               char right_key = 'd');
  18.  
  19.     virtual bool keyPress(char c);
  20.  
  21. protected:
  22.     char upKey;
  23.     char downKey;
  24.     char leftKey;
  25.     char rightKey;
  26.  
  27. private:
  28. };
  29.  
  30.  
  31. #endif
  32.  
  33. //***************************************************************************************
  34. //CHARACTER.CPP
  35. #include "character.h"
  36.  
  37. Character::Character(DrawEngine *de, int s_index, float x, float y,
  38.               int lives, char upKey, char downKey, char leftKey,
  39.               char rightKey) : Sprite(de, s_index, x, y, lives)
  40. {
  41.     upKey = upKey;
  42.     downKey = downKey;
  43.     leftKey = leftKey;
  44.     rightKey = rightKey;
  45.  
  46.     classID = CHARACTER_CLASSID;
  47. }
  48.  
  49. bool Character::keyPress(char c)
  50. {
  51.     if(c == upKey)
  52.     {
  53.         return move(0, -1);
  54.     }
  55.     else if(c == downKey)
  56.     {
  57.         return move(0, 1);
  58.     }
  59.         else if(c == rightKey)
  60.     {
  61.         return move(1, 0);
  62.     }
  63.         else if(c == leftKey)
  64.     {
  65.         return move(-1, 0);
  66.     }
  67.         return false;
  68. }
  69. //****************************************************************************************
  70. #ifndef DRAWENGINE_H
  71. #define DRAWENGINE_H
  72.  
  73. #include "level.h"
  74. #include "game.h"
  75. #include "character.h"
  76.  
  77.  
  78.  
  79. class DrawEngine
  80. {
  81. public:
  82.     DrawEngine(int xSize = 30, int ySize = 20);
  83.    
  84.     //cursorVisibilty will be OFF in the coACnstructor
  85.     //cursorVisibilty will be ON in the destructor
  86.     ~DrawEngine();
  87.  
  88.     //select sprite, sprite representation character (appearance)
  89.     int createSprite(int index, char c);
  90.     void deleteSprite(int index);
  91.  
  92. //space character drawn at xy to erase sprite
  93.     void eraseSprite(int posx, int posy);
  94.                     //selected sprite, drawn at posx, with posy
  95.     void drawSprite(int index, int posx, int posy);
  96.  
  97.     void createBackgroundTile(int index, char c);
  98.  
  99.     void setMap(char **);
  100.     void drawBackground(void);
  101. protected:
  102.  
  103.     char **map;
  104.     int screenWidth, screenHeight;
  105.     char spriteImage[16];
  106.     char tileImage[16];
  107.    
  108. private:
  109.     void gotoxy(int x, int y);
  110.  
  111.     //false is hidden, true is visible
  112.     void cursorVisibilty(bool visibility);
  113.    
  114.  
  115. };
  116. #endif
  117. //**************************************************************
  118. //drawEngine.cpp
  119. #include "drawEngine.h"
  120.  
  121. #include <Windows.h>
  122. #include<iostream>
  123.  
  124. //DrawEngine Constructor
  125. DrawEngine::DrawEngine(int xSize, int ySize)
  126. {
  127.     screenWidth = xSize;
  128.     screenHeight = ySize;
  129.    
  130.     //set cursor visibility to false
  131.     cursorVisibilty(false);
  132.  
  133.     map = 0;
  134. }
  135.  
  136. //Draw Engine Destructor
  137. DrawEngine::~DrawEngine()
  138. {
  139.     //set cursor visibility to true
  140.     cursorVisibilty(true);
  141.  
  142. }
  143.  
  144.  
  145.  
  146.  
  147. int DrawEngine::createSprite(int index, char c)
  148. {
  149.     if(index >= 0 && index < 16)
  150.     {
  151.     spriteImage[index] = c;
  152.     return index;
  153.     }
  154.     //error
  155.     return -1;
  156. }
  157.  
  158. void DrawEngine::deleteSprite(int index)
  159. {
  160.     //this implementation we don't need it
  161.  
  162. }
  163.  
  164.  
  165. void DrawEngine::drawSprite(int index, int posx, int posy)
  166. {
  167.     //go to correct location
  168.     gotoxy(posx, posy);
  169.  
  170.     //draw the image
  171.     std::cout << spriteImage[index];
  172. }
  173.  
  174. void DrawEngine::eraseSprite(int posx, int posy)
  175. {
  176.  
  177.     gotoxy(posx, posy);
  178.     std::cout << ' ' ;
  179. }
  180.  
  181. void DrawEngine::setMap(char **data)
  182. {
  183.     map = data;
  184. }
  185.  
  186. void DrawEngine::createBackgroundTile(int index, char c)
  187. {
  188.     if ( index >= 0 && index < 16)
  189.     {
  190.         tileImage[index] = c;
  191.     }
  192. }
  193.  
  194. void DrawEngine::drawBackground(void)
  195. {
  196.     if (map)
  197.     {
  198.         for (int y = 0; y < screenHeight; y++)
  199.         {
  200.             gotoxy(0, y);
  201.  
  202.             for(int x = 0; x < screenWidth; x++)
  203.             {
  204.                 std::cout << tileImage[map[x][y]];
  205.  
  206.  
  207.             }
  208.         }
  209.     }
  210. }
  211.  
  212. void DrawEngine::gotoxy(int x, int y)
  213. {
  214.     //A handle in windows is an ID that asscociates with something
  215.     //in the system
  216.     HANDLE output_handle;
  217.  
  218.     //built in structure
  219.     COORD pos;
  220.  
  221.     pos.X = x;
  222.     pos.Y = y;
  223.  
  224.     // This will get us the output_handle which is the ID
  225.     output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  226.  
  227.     //what windows ID has the cursor, what position to put the cursor
  228.     SetConsoleCursorPosition(output_handle, pos);
  229. }
  230.  
  231. //gets called in drawEngine Constructor and destructor
  232. // my draw engine is called drawArea
  233. void DrawEngine::cursorVisibilty(bool visibility)
  234. {
  235.     HANDLE output_handle;
  236.     CONSOLE_CURSOR_INFO cciInfo;
  237.  
  238.     //sets cursor size
  239.     cciInfo.dwSize = 1;
  240.     cciInfo.bVisible = visibility;
  241.  
  242.     output_handle = GetStdHandle(STD_OUTPUT_HANDLE);
  243.  
  244.     SetConsoleCursorInfo(output_handle, &cciInfo);
  245. }
  246. //*****************************************************************************
  247. #ifndef GAME_H
  248. #define GAME_H
  249. #include"drawEngine.h"
  250. #include"level.h"
  251. #include "character.h"
  252. class Game
  253. {
  254.  
  255. public:
  256.                      //This function will determine if the game is running
  257.     bool run(void);  //Will not get out of this function until the game ends
  258.  
  259.  
  260. protected:                  
  261.  
  262.     /****************************************************
  263.      *             BOOL GETINPUT
  264.      *      @param char *c  
  265.      * This will see if the user presses a key
  266.      *
  267.      * If the user presses a key, it returns TRUE and put the character pressed inside of *c's address.
  268.      * While they aren't pressing a key it returns FALSE
  269.      ****************************************************/
  270.     bool getInput(char *key);
  271.  
  272.  
  273.     /**********************************************************
  274.      *            VOID TIMERUPDATE
  275.      * Called constantly
  276.      * Contains the game code
  277.      * Runs constantly
  278.      * Controls the speed at which the game plays
  279.      **********************************************************/
  280.     void timerUpdate(void);
  281.  
  282.  
  283.    
  284. private:
  285.     //35:04
  286.     Level *level;
  287.     Character *player;
  288.  
  289.     double frameCount;
  290.     double startTime;
  291.     double lastTime;
  292.     double timeRunning;
  293.     int posx;
  294.  
  295.     DrawEngine drawArea;
  296. };
  297.  
  298.  
  299.  
  300. #endif
  301. //*************************************************************
  302. //game.cpp
  303. #include "game.h"
  304. #include"drawEngine.h"
  305.  
  306.  
  307. #include <iostream>
  308. #include <conio.h>
  309. #include <Windows.h>
  310.  
  311.  
  312.  
  313.  
  314. #define GAME_SPEED 33.3      //This will give us roughly 30 fps
  315.  
  316.  
  317. bool Game::run(void)
  318. {
  319.     level = new Level(&drawArea, 30,20);
  320.  
  321.     drawArea.createBackgroundTile(TILE_EMPTY, ' ');
  322.     drawArea.createBackgroundTile(TILE_WALL, '+');
  323.    
  324.     //Draw Area defined in game.h private:
  325.     //create sprite and send it index and the character representation
  326.     // of the sprite
  327.     drawArea.createSprite(0, '$');
  328.     player = new Character(&drawArea, 0);
  329.  
  330.     level->draw();
  331.    
  332.    
  333.  
  334.  
  335.  
  336.     //initialize key and position at x
  337.     char key = ' ';
  338.  
  339.  
  340.     startTime = timeGetTime();
  341.  
  342.     frameCount = 0;
  343.     lastTime = 0;
  344.  
  345.     //if the key you pressed doesn't == q, then loop
  346.     while (key != 'q')
  347.     {
  348.         //while getInput is returning false
  349.         //pass getInput() the address of char key ; & = address
  350.         //call timerUpdate
  351.         //exits loop when key is pressed
  352.         while(!Game::getInput(&key))
  353.         {
  354.             timerUpdate();
  355.  
  356.         }
  357.         //send keypress()->inside of player the new value of key
  358.         player->keyPress(key);
  359.  
  360.     }
  361.  
  362.     //prevent memory leak
  363.     delete player;
  364.     //**************************NOTES****************************************
  365.     // if i start the program at 10:10:10 (startTime) and end it at 10:10:20,
  366.     //the difference is how long we have been running
  367.     //obviously bigger value minus the smaller value
  368.  
  369.     timeRunning = (timeGetTime() - startTime);
  370.  
  371.     std::cout << frameCount /  (timeRunning /1000) << " fps" << "\n";
  372.  
  373.     std::cout << "Frame Count: " << frameCount << "\n";
  374.     std::cout << "End of the game" << "\n";
  375.     return true;
  376. }
  377.  
  378. /*****************************************************
  379. *              GET INPUT
  380. * Recieves the variable ADDRESS from &key
  381. * Checks to see if the player hits a key
  382. *
  383. * If the player hits a key then replace the data in
  384. * address &key with the players new key and RETURN TRUE
  385. *
  386. * If no key is hit, return false
  387. *****************************************************/
  388. bool Game::getInput(char *key)
  389. {
  390.     if(kbhit())
  391.     {
  392.         *key = getch();
  393.         return true;
  394.     }
  395.  
  396.     return false;
  397. }
  398.  
  399. /*********************************************************
  400. *
  401. *********************************************************/
  402. void Game::timerUpdate(void)
  403. {
  404.  
  405.     double currentTime = timeGetTime() - lastTime;
  406.  
  407.     if(currentTime < GAME_SPEED)
  408.     {
  409.         return;
  410.     }
  411.  
  412.     frameCount++;
  413.  
  414.     lastTime = timeGetTime();
  415.  
  416. }
  417. //**********************************************************
  418. #ifndef LEVEL_H
  419. #define LEVEL_H
  420.  
  421.  
  422. enum
  423. {
  424.     TILE_EMPTY,
  425.     TILE_WALL
  426. };
  427.  
  428.  
  429. #include "drawEngine.h"
  430. class Character;
  431.  
  432. class Level
  433. {
  434. public:
  435.     Level(DrawEngine *de, int width = 30, int height = 20);
  436.     ~Level();
  437.  
  438.     void addPlayer(Character *p);
  439.     void update(void);
  440.     void draw(void);
  441.     bool keyPress(char c);
  442. protected:
  443.     void createLevel(void);
  444.    
  445. private:
  446.     int width;
  447.     int height;
  448.  
  449.     char **level;
  450.  
  451.     Character *player;
  452.     DrawEngine *drawArea;
  453.  
  454. };
  455.  
  456. #endif
  457. //************************************************************************
  458. //level.cpp
  459. #include "level.h"
  460.  
  461. #include <stdlib.h>
  462.  
  463. Level::Level(DrawEngine *de, int w, int h)
  464. {
  465.     drawArea = de;
  466.  
  467.     width = w;
  468.     height = h;
  469.  
  470.     player = 0;
  471.  
  472.     //char ** map data
  473.     level = new char *[width];
  474.  
  475.  
  476.     //create memory for our level
  477.     for(int x = 0; x < width; x++)
  478.     {
  479.         level[x] = new char[height];
  480.     }
  481.  
  482.     //create the level
  483.     createLevel();
  484.  
  485.     drawArea->setMap(level);
  486.  
  487. }
  488.  
  489. Level::~Level()
  490. {
  491.     for(int x = 0; x < width; x++)
  492.     {
  493.         delete[] level[x];
  494.     }
  495.     delete [] level;
  496. }
  497.  
  498. void Level::createLevel(void)
  499. {
  500.     for(int x = 0; x < width; x++)
  501.     {
  502.         for (int y = 0; y < height; y++)
  503.         {
  504.             int random = rand() %100;
  505.  
  506.  
  507.             if(y == 0 || y == height - 1 || x == 0 || x == width - 1)
  508.             {
  509.                 level[x][y] = TILE_WALL;
  510.             }
  511.             else
  512.             {
  513.                 if (random < 90 || (x < 3 && y < 3))           
  514.                     level[x][y] = TILE_EMPTY;
  515.                 else
  516.                     level[x][y] = TILE_WALL;
  517.             }
  518.         }
  519.     }
  520. }
  521. void Level::draw(void)
  522. {
  523.     drawArea->drawBackground();
  524. }
  525.  
  526. void Level::addPlayer(Character *p)
  527. {
  528.     player = p;
  529. }
  530. //*************************************************************************************
  531. #ifndef SPRITE_H
  532. #define SPRITE_H
  533.  
  534. #include"drawEngine.h"
  535. enum
  536. {
  537.     SPRITE_CLASSID,
  538.     CHARACTER_CLASSID
  539. };
  540.  
  541.  
  542. typedef struct _Vector
  543. {
  544.     float x;
  545.     float y;
  546. } _Vector;
  547.  
  548. class Sprite
  549. {
  550. public:
  551.     //constructor
  552.     Sprite(DrawEngine * de, int s_index, float x = 1, float y = 1, int i_lives = 1);
  553.     //destructor
  554.     ~Sprite();
  555.  
  556.     int classID;
  557.  
  558.     _Vector getPosition(void);
  559.  
  560.     float getX(void);
  561.     float getY(void);
  562.  
  563.     virtual void addLives(int num = 1);
  564.  
  565.     int getLives(void);
  566.     bool isAlive(void);
  567.  
  568.     virtual bool move(float x, float y);
  569.  
  570. protected:
  571.  
  572.     void draw(float x, float y);
  573.     void erase(float x, float y);
  574.  
  575.     DrawEngine *drawArea;
  576.  
  577.     _Vector pos;
  578.  
  579.     int spriteIndex;
  580.     int numLives;
  581.  
  582.     _Vector facingDirection;
  583.  
  584. };
  585. #endif
  586. //***********************************************************
  587. //sprite.cpp
  588. #include "sprite.h"
  589. #include"drawEngine.h"
  590. Sprite::Sprite(DrawEngine *de, int s_index, float x, float y, int i_lives)
  591. {
  592.     drawArea = de;
  593.     pos.x  = x;
  594.     pos.y = y;
  595.  
  596.     spriteIndex = s_index;
  597.  
  598.     numLives = i_lives;
  599.  
  600.     facingDirection.x = 1;
  601.     facingDirection.y = 0;
  602.     classID = SPRITE_CLASSID;
  603. }
  604.  
  605. Sprite::~Sprite()
  606. {
  607.     //erase the dying sprite
  608.     erase(pos.x, pos.y);
  609. }
  610.  
  611. _Vector Sprite::getPosition(void)
  612. {
  613.     return pos;
  614. }
  615.  
  616. float Sprite::getX(void)
  617. {
  618.     return pos.x;
  619. }
  620.  
  621. float Sprite::getY(void)
  622. {
  623.     return pos.y;
  624. }
  625.  
  626. void Sprite::addLives(int num )
  627. {
  628.     numLives += num;
  629. }
  630.  
  631. int Sprite::getLives(void)
  632. {
  633.     return numLives;
  634. }
  635.  
  636. bool Sprite::isAlive(void)
  637. {
  638.  
  639.     return (numLives > 0);
  640.     /*
  641.     if(numLives <= 0)
  642.     return false;
  643.  
  644.     return 0;
  645.     */
  646. }
  647.  
  648. bool Sprite::move(float x, float y)
  649. {
  650.     //erase Sprite
  651.     erase(pos.x, pos.y);
  652.  
  653.     pos.x += x;
  654.     pos.y += y;
  655.  
  656.     facingDirection.x = x;
  657.     facingDirection.y = y;
  658.  
  659.     //draw sprite
  660.     draw(pos.x,pos.y);
  661.  
  662.  
  663.     return true;
  664.    
  665. }
  666.  
  667.     void Sprite::draw(float x, float y)
  668.     {
  669.         drawArea->drawSprite(spriteIndex, (int)x, (int)y);
  670.     }
  671.  
  672.     void Sprite::erase(float x, float y)
  673.     {
  674.         drawArea->eraseSprite((int)x, (int)y);
  675.     }
  676. //************************************************
  677. //main.cpp
  678. #include "game.h"
  679.  
  680. int main()
  681. {
  682.     Game gameHeart;
  683.  
  684.     //start the game loop
  685.  
  686.     gameHeart.run();
  687.  
  688.     return 0;
  689. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement