python

Example game_object

Jan 2nd, 2012
330
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. ///Mock up code:
  2. /*Warning, this code has not been compile tested. however it is based off of working code.
  3. May contain typos. Note: you will need to fill in the blanks [...] with your code to make this work*/
  4. ...
  5.  
  6. //Include whatever else you need
  7. #include <vector>
  8. #include "game_object.h"
  9. ...
  10.  
  11. vector<game_object*> objects; // making it a global var is a bad idea.
  12. // "objects" should be put inside of a class, function or namespace
  13.  
  14. //Function Definitions
  15.  
  16. void add_object(game_object * gm_object)
  17. {
  18.     objects.push_back(gm_object);
  19. }
  20.  
  21. void remove_object(game_object * gm_object)
  22. {
  23.     for (unsigned int i=0;i < objects.size();i++)
  24.     {
  25.         if (objects.at(i)==gm_object)
  26.         {
  27.             delete objects.at(i);
  28.             objects.at(i)==NULL;
  29.         }
  30.     }
  31. }
  32.  
  33. // Class Definitions
  34.  
  35. //you really, really need to split up your code into multiple files
  36. //eg. game_object.h - class prototype
  37. //and game_object.cpp - class member functions (code)
  38.  
  39. class game_object
  40. {
  41.     public:
  42.         game_object() { }
  43.         virtual void draw() { }
  44.         virtual void update() { }
  45.         virtual ~game_object() { }
  46.     protected:
  47.     private:
  48. }
  49.  
  50. ...
  51.  
  52. // More Class Definitions
  53.  
  54. /* note to read variables from the ship class you need to make a "getter" function
  55. --- game_object.h
  56.  
  57. class game_object
  58. {
  59.     public:
  60.         game_object() { }
  61.  
  62.     virtual void get(string name, bool &return_value) { } //<- new
  63.     virtual void get(string name, int &return_value) { } //<- new
  64.     virtual void get(string name, float &return_value) { } //<- new
  65.     virtual void get(string name, string &return_value) { } //<- new
  66.  
  67.         virtual void draw() { }
  68.         virtual void update() { }
  69.         virtual ~ai_ship() { }
  70.     protected:
  71.     private:
  72. }
  73.  
  74. --- Ship
  75. class ship : public game_object
  76. {
  77.     public:
  78.         ship(float x, float y);
  79.  
  80.     void get(string name, float &return_value); //<- new
  81.     //add the other get functions defined in game_object to support other data types such as "int"
  82.  
  83.         void draw();
  84.         void update();
  85.         ~ai_ship();
  86.     protected:
  87.     private:
  88.         SDL_Surface * sprite;
  89.         SDL_Rect * rect;
  90.         float x,y;
  91. };
  92.  
  93. void ship::get(string name, float &return_value)
  94. {
  95.         if (name=="x")
  96.         {
  97.                 return_value=x;
  98.         return;
  99.         }
  100.  
  101.         if (name=="y")
  102.         {
  103.                 return_value=y;
  104.         return;
  105.         }
  106. }
  107.  
  108. --example usage
  109.  
  110. ship myship(10,20); //make ship at x: 10 y: 20
  111. float ship_x;
  112. myship.get("x", ship_x); //ship_x now equals 10
  113.  
  114. To set variables to need to make "setter" functions, I think you
  115. can figure out how to to that using "get" as an example
  116. */
  117.  
  118.  
  119. // This is the ship class which inherits from game_object
  120. class ship : public game_object
  121. {
  122.     public:
  123.         ship(float x, float y);
  124.         void draw();
  125.         void update();
  126.         ~ai_ship();
  127.     protected:
  128.     private:
  129.         SDL_Surface * sprite;
  130.         SDL_Rect * rect;
  131.         float x,y;
  132. };
  133.  
  134. ...
  135.  
  136. int main()
  137. {
  138.         ...
  139.  
  140.         ship * myship(10,20);
  141.     ship * myship2(20,10);
  142.  
  143.         add_object(myship); // add objects to the game loop
  144.     add_object(myship2);
  145.  
  146.     remove_object(myship2); //myship2 is now removed
  147.         ...
  148.  
  149.         while( game_is_running )
  150.         {
  151.                 ...
  152.  
  153.                 for(unsigned int i; i < object.size(); i++)
  154.                 {
  155.             if (objects.at(i)==NULL)
  156.                 continue; // skip NULL objects
  157.  
  158.                         object.at(i)->update(); //go through the array of objects and run their logic
  159.                         object.at(i)->draw(); //then draw
  160.                 }
  161.  
  162.         //check for NULL objects and remove
  163.         vector<game_object*> new_objects;
  164.  
  165.         for (unsigned int i=0;i < objects.size();i++)
  166.         {
  167.             if (objects.at(i)!=NULL)
  168.                 new_objects.push_back(objects.at(i));//all non-NULL objects
  169.                 //get put onto a new vector, which objects is set equal to
  170.         }
  171.  
  172.         objects=new_objects;
  173.  
  174.                 ...
  175.         }
  176.    
  177.     //end of game
  178.  
  179.     objects.clear(); //don't get into the habit of modifying "objects" directly
  180. }
  181.  
  182. ///End of mock up code
Advertisement
Add Comment
Please, Sign In to add comment