На главную Наши проекты:
Журнал   ·   Discuz!ML   ·   Wiki   ·   DRKB   ·   Помощь проекту
ПРАВИЛА FAQ Помощь Участники Календарь Избранное RSS
msm.ru
! Правила раздела Visual C++ / MFC / WTL (далее Раздела)
1) На Раздел распространяются все Правила Форума.
2) Перед тем, как создать новый топик, убедитесь, что Вы читали Правила создания тем в Разделе.
3) Вопросы, не связанные с программированием (настройки MS Visual Studio, книги, библиотеки и т.д.),
обсуждаются в разделе C/C++: Прочее
4) Вопросы разработки .NET (Windows Form, C++/CLI и т.п.) приложений на Visual C++/C# обсуждаются в разделе .NET.
5) Нарушение Правил может повлечь наказание со стороны модераторов.

Полезные ссылки:
user posted image FAQ Раздела user posted image Обновления для FAQ Раздела user posted image Поиск по Разделу user posted image MSDN Library Online
Модераторы: ElcnU
  
> Игра на С++ наследование SW
    уже есть объекты с наследованием Герои и оружие
    хотелось бы собрать из этого игру но пока не ясно направление дальнейших действий
    суть игры: игрок Jedi появляется в левом верхнем углу поля у него есть оружие.также на поле есть:стоящие на месте клоны,попадая на клетку к которым герой сражается с ними,эндорцы бегающие за героем и наносящие ему незначительный урон с помощью kick,Джаба медленно ползущий за героем способный нанести серьезный урон спомощью Sword.Цель игры убить всех врагов.
    Есть недописанная функция Battle которая генерит кто наносит удар случайным образом.

    Собственно вопрос: можно ли собрать функционал игру с ООП объектами,или как можно собрать подобную игру?

    ExpandedWrap disabled
      #include <iostream>
      #include <conio.h>
       #include <time.h>
       #include <vector>
       #include <math.h>
       
       #define SIZE_X 20
       #define SIZE_Y 10
       using namespace std;
      ///////////////////////////////////////////////////////////////////
       enum GameState
       {Menu,Game_Move,Game_Shoot};
       
       enum Directions
       {Left,Right,Up,Down};
       
      ///////////////////////////////////////////////////////////////////
       class Weapon
       {
       public:
       int bullets;
       char name[20];
       Weapon():bullets(0)
       {
       }
       Weapon(int power,int range,int bullets)
       {
       this->power = power;
       this->range = range;
       this->bullets = bullets;
       }
       unsigned int getRange() { return range;}
       unsigned int getPower() { return power; }
       virtual char * getName() { return name; }
       private:
       unsigned int power;
       unsigned int range;
       };
       
       
      class Blaster:public Weapon
      {
       public:
       Blaster():
       Weapon(5,4,10)
       {
       
       }
       virtual char * getName() { return name; }
       char name[20];
      };
       
      class Sword:public Weapon
      {
      public:
          Sword():
          Weapon(10,1,-1)
          {
       
          }
          virtual char * getName() { return name; }
          char name[20];
      };
       
      class Power_of_the_Jedi:public Weapon
      {
       public:
       Power_of_the_Jedi():
       Weapon(50,2,1)
       {
       
       }
       virtual char * getName() { return name; }
       char name[20];
      };
       
      class Kick:public Weapon
      {
      public:
          Kick():
          Weapon(2,2,-1)
          {
       
          }
          virtual char * getName() { return name; }
          char name[20];
      };
      ///////////////////////////////////////////////////////////////////
      struct Coord
      {
          int x;
          int y;
          Coord()
          {
       
          }
          Coord(int x,int y)
          {
              this->x = x;
              this->y = y;
          }
          bool operator==(const Coord& coord)
          {
              return (x==coord.x)&&(y==coord.y);
          }
      };
       
      template<class Stream>
      Stream& operator<<(Stream& out,Coord p)
      {
          out<<"["<<p.x<<","<<p.y<<"]";
          return out;
      }
       
      //////////////////////////////////////////////////////////////////
       
      class Creature;
      Creature * checkCollisions(Coord);
      bool die(void*);
      bool sectorIsFree(Coord);
      void setMenu();
       
      class Creature
      {
      public:
          char correlation;
          int power;
          Weapon* weapon;
          Weapon** weapons;
          char weapon_index;
          Creature( int h, int x, int y)
          : health (h), position (x,y)
          {
          }
          ~Creature()
          {
          }
          Coord getPos()
          {
              return position;
          }
          void addWeapons(Weapon* w1,Weapon* w2,Weapon* w3,Weapon* w4)
          {
              weapons[0] = w1;
              weapons[1] = w2;
              weapons[2] = w3;
              weapons[3] = w4;
              weapon = weapons[0];
              weapon_index = 0;
          }
          void changeWeapon()
          {
              if(weapon_index = 0)
              {
                  weapon = weapons[1];
                  weapon_index = 1;
              }
              if(weapon_index = 1)
              {
                  weapon = weapons[2];
                  weapon_index = 2;
              }
              if((weapon_index = 2))
              {
                  weapon = weapons[3];
                  weapon_index = 3;
              }
              if(weapon_index = 3)
              {
                  weapon = weapons[0];
                  weapon_index = 0;
              }
          }
          int getHealth()
          {
              return health;
          }
          bool goLeft()
          {
              if(position.x)
              {
                  Coord p(position);
                  p.x--;
                  if(checkCollisions(p)!=NULL)
                      return false;
                  position.x--;
                  return true;
              }
              return false;
          }
          bool goRight()
          {
              Coord p(position);
              p.x++;
              if(checkCollisions(p)!=NULL)
                  return false;
              if(position.x<SIZE_X-1)
              {
                  position.x++;
                  return true;
              }
              return false;
          }
          bool goUp()
          {
              if(position.y)
              {
                  Coord p(position);
                  p.y--;
                  if(checkCollisions(p)!=NULL)
                      return false;
                  position.y--;
                  return true;
              }
              return false;
          }
          bool goDown()
          {
              if(position.y<SIZE_Y-1)
              {
                  Coord p(position);
                  p.y++;
                  if(checkCollisions(p)!=NULL)
                      return false;
                  position.y++;
                  return true;
              }
              return false;
          }
          virtual bool damage(int power)
          {
              health-=power;
             // if(health<=0)
                 // return !die(this);//??????????????????????????????????????????????????????
              return true;
          }
       
          bool shoot(Directions direction)
           {
              int sx = position.x;
              int sy = position.y;
              if(!weapon->bullets)
                  return false;
              if(direction == Right){
                  sx++;
                  for(unsigned int i=sx;i<sx+weapon->getRange();++i){
                      if(i>SIZE_X-1)
                          break;
                      Coord p;
                      p.x = i;
                      p.y = sy;
                      Creature * aim = (Creature*)checkCollisions(p);
       
                      if(aim!=NULL){
                          aim->damage(weapon->getPower());
                          break;
                      }
                  }
              }else if(direction == Left){
                  sx--;
                  for(unsigned int i=sx;i>sx-weapon->getRange();--i){
                      if(i<0)
                          break;
                      Coord p;
                      p.x = i;
                      p.y = sy;
                      Creature * aim = (Creature*)checkCollisions(p);
       
                      if(aim!=NULL){
                          aim->damage(weapon->getPower());
                          break;
                      }
                  }
              }else if(direction == Up){
                  sy--;
                  for(unsigned int i=sy;i>sy-weapon->getRange();--i){
                      if(i<0)
                          break;
                      Coord p;
                      p.x = sx;
                      p.y = i;
                      Creature * aim = (Creature*)checkCollisions(p);
       
                      if(aim!=NULL){
                          aim->damage(weapon->getPower());
                          break;
                      }
                  }
              }else{
                  sy++;
                  for(unsigned int i=sy;i<sy+weapon->getRange();++i){
                      if(i>SIZE_Y-1)
                          break;
                      Coord p;
                      p.x = sx;
                      p.y = i;
                      Creature * aim = checkCollisions(p);
       
                      if(aim!=NULL){
                          aim->damage(weapon->getPower());
                          break;
                      }
                  }
              }
              weapon->bullets--;
              return true;
          }
          void kick(Creature* target)
           {
              target->damage(power);
          }
         /* void moveTo(Creature* target)
           {
              int repetitions = 0;
              Coord p = target->getPos();
              for(int i=0;i<speed;i++){
                  if(sqrt((float)abs(position.x - p.x)*abs(position.x - p.x) + abs(position.y - p.y)*abs(position.y - p.y))>1){
                      int shx = position.x - p.x;
                      int shy = position.y - p.y;
                      bool ok;
                      if((rand() % 10) >=5){
                          if(shx>0)
                              ok = goLeft();
                          else
                              ok = goRight();
                      }else{
                          if(shy>0)
                              ok = goUp();
                          else
                              ok = goDown();
                      }
                      if(!ok){
                          if(repetitions<3){
                              repetitions++;
                              i--;
                          }else{
                              repetitions = 0;
                              break;
                          }
                      }
                  }else{
                      kick(target);
                      //cout<<"KICK"<<target->getPos()<<getPos()<<endl;
                      break;
                  }
              }
          }*/
       
      protected:
          int speed;
          int health;
          Coord position;
      };
       
      class Player: public Creature
      {
      public:
          Player():
          Creature(100,0,0)
          {
              correlation = '@';
              weapons = new Weapon*[2];
          }
       
          ~Player()
          {
              for(int i=0;i<2;++i)
              {
                  delete weapons[i];
              }
              delete[] weapons;
          }
          virtual bool damage(int power)
          {
              health-=power;
              if(health<=0)
              {
                  //setMenu();//?????????????????????????????????????????????????????
                  return false;
              }
              return true;
          }
      };
       
      class Clones:public Creature
      {
      public:
          Clones(int cx,int cy):
          Creature(5,cx,cy)
          {
              speed = 0;
              power = 4;
              correlation = 'с';
          }
      };
       
      class Endorses:public Creature
      {
      public:
          Endorses(int cx,int cy):
          Creature(1,cx,cy)
          {
              speed = 6;
              power = 1;
              correlation = 'e';
          }
      };
       
      class Djabba:public Creature
      {
      public:
          Djabba(int cx,int cy):
          Creature(20,cx,cy)
          {
              speed = 1;
              power = 30;
              correlation = 'd';
          }
      };
      /////////////////////////////////////////////
      int Battle (int c)
      {   srand((unsigned)time(NULL));
          int s = rand()%3;
          if (s==1)
          {
          }
          if (s==2)
          {
          }
          if (s==3)
          {
          }
       
      }
       
      int main()
      {
          char c[SIZE_X][SIZE_Y];
          for(int i=0;i<SIZE_Y;++i)
              for(int j=0;j<SIZE_X;++j)
                  c[i][j]='.';
          for(int i=0;i<SIZE_Y;++i)
          {cout<<endl;
              for(int j=0;j<SIZE_X;++j)
                  cout<<c[i][j];
          }
          cout<<endl;
       
       
          Player* player = new Player();
          Sword* sword = new Sword();
          Blaster* blaster = new Blaster();
          Power_of_the_Jedi* power_of_the_Jedi = new Power_of_the_Jedi();
          Kick* kick = new Kick();
       
          player->addWeapons(sword,blaster,power_of_the_Jedi,kick);
       
      }
      Направление -- модульность, вынос реализаций отдельных классов по соответствующим модулям. В этом должна остаться точка входа и, возможно, игровой цикл
      0 пользователей читают эту тему (0 гостей и 0 скрытых пользователей)
      0 пользователей:


      Рейтинг@Mail.ru
      [ Script execution time: 0,0307 ]   [ 18 queries used ]   [ Generated: 29.03.24, 14:33 GMT ]