2017-07-19 74 views
-5
#include <iostream> 
#include <string> 
#include <vector> 
#include <cstdlib> 
#include "mcigraph.hpp" 

using namespace std; 

class Entity { 
private: 
    int _x, _y;     // Current postition 
    int _xsize, _ysize;   // Image size in pixels 
    int _xnbfields, _ynbfields; // Number of fields in x/y 
    string _img;    // Image to draw 

public: 
    Entity(int x, int y, string img, int xsize = 16, int ysize = 16, 
     int xres = 1024, int yres = 768) { 
    // Assign values 
    _x = x; 
    _y = y; 
    _img = img; 
    _xsize = xsize; 
    _ysize = ysize; 
    _xnbfields = xres/xsize; 
    _ynbfields = yres/ysize; 
    // Enforce Invariances 
    if (_x < 0) 
     _x = 0; 
    if (_y < 0) 
     _y = 0; 
    if (_x > _xnbfields - 1) 
     _x = _xnbfields - 1; 
    if (_y > _ynbfields - 1) 
     _y = _ynbfields - 1; 
    } 


    //background entity 

    Entity(string img, int xsize = 16, int ysize = 16, 
     int xres = 1024, int yres = 768) { 
     // Assign values 
     _img = img; 
     _xsize = xsize; 
     _ysize = ysize; 
     _xnbfields = xres/xsize; 
     _ynbfields = yres/ysize; 
     // Enforce Invariances 
     if (_x < 0) 
      _x = 0; 
     if (_y < 0) 
      _y = 0; 
     if (_x > _xnbfields - 1) 
      _x = _xnbfields - 1; 
     if (_y > _ynbfields - 1) 
      _y = _ynbfields - 1; 


     /* 
     vector <Entity> b; 
    for (int i = 0; i < _xnbfields; i++){ 

     for (int j = 0; j < _ynbfields; j++){ 

     b.push_back(Entity(i*xsize, j*ysize, _img)); 
     } 
     } 
     */ 
    } 



    void moveleft() { 
    if (_x > 0) 
     _x--; 
    } 
    void moveright() { 
    if (_x < _xnbfields - 1) 
     _x++; 
    } 
    void movedown() { 
    if (_y < _ynbfields - 1) 
     _y++; 
    } 
    void moveup() { 
    if (_y > 0) 
     _y--; 
    } 

    void draw() { draw_image(_img, _x * _xsize, _y * _ysize); } 

    int get_x() { return _x; } 
    int get_y() { return _y; } 
}; 

int main(int argc, char *argv[]) { 
    Entity e1(8, 8, "./char1.bmp"); 
    Entity e2(16, 16, "./char2.bmp"); 




    //Background 
    vector<Entity> b; 
    for (int i = 0; i < 64; i++){ 

     for (int j = 0; j < 48; j++){ 

      b.push_back(Entity(i,j, "./backg2.bmp")); 
     } 
    } 

    //Monster 

    vector<Entity> m; 
     m.push_back(Entity(16, 16, "./monster.bmp")); 



    //Group Entity 
    /* 
    vector<Entity> v; 
    for (int i = 0; i < 4; i++) { 
     v.push_back(Entity(16, 16, "./monster.bmp")); 
    } 
    */ 

    //DeadMan Walking 
    int steps = 0; 

    ___MCILOOPSTART___ 





     //background draw 
     for (auto &x : b){ 
      x.draw(); 
     } 


    //Monster 
    for (auto &q : m) { 
     int r = rand() % 4; 

     if (r == 0) 
      q.moveleft(); 
     if (r == 1) 
      q.moveright(); 
     if (r == 2) 
      q.moveup(); 
     if (r == 3) 
      q.movedown(); 

     q.draw(); 
    } 





    //group entity draw 

    /* 
     for (auto &e : v) { 
      int r = rand() % 4; 

      if (r == 0) 
       e.moveleft(); 
      if (r == 1) 
       e.moveright(); 
      if (r == 2) 
       e.moveup(); 
      if (r == 3) 
       e.movedown(); 

      e.draw(); 
     } 

     */ 





    //DeadMan Walking Count 
    bool flag_done = false; 

    //Player 1 
    if (is_pressed(KEY_LEFT)){ 
     e1.moveleft(); 
     flag_done = true; 
    } 

    if (is_pressed(KEY_RIGHT)){ 
    e1.moveright(); 
    flag_done = true; 
} 

    if (is_pressed(KEY_UP)){ 
     e1.moveup(); 
     flag_done = true; 
    } 

    if (is_pressed(KEY_DOWN)){ 
     e1.movedown(); 
     flag_done = true; 
    } 

    if (flag_done == true) 
     steps++; 


    //Player 2 
    if (is_pressed(KEY_A)) 
    e2.moveleft(); 
    if (is_pressed(KEY_D)) 
    e2.moveright(); 
    if (is_pressed(KEY_W)) 
    e2.moveup(); 
    if (is_pressed(KEY_S)) 
    e2.movedown(); 


    if (steps <30) //Max steps 
    e1.draw(); 



    e2.draw(); 





    ___MCILOOPEND___ 
    return 0; 
} 

我想实现,玩家可以摧毁怪物。功能key_space识别空格键何时被按下。但是确定怪物位置的最简单方法是什么?我怎样才能在攻击期间为玩家制作动画?C++游戏,找怪物的位置摧毁它

非常感谢!

game

+0

你在哪里存放怪物?对于我来说,这是太多的代码。 – Carcigenicate

+0

“确定怪物位置的最简单方法”? *但是如果你还没有怪物的位置,你甚至还没有绘制它?* – meowgoesthedog

+1

请看看如何发布[MCVE]以获得更好的帮助,这是很多代码。 (例如,从我们的角度来看,注释代码是完全不相关的。) – Peri461

回答

0

既然你有自己的向量的怪物,你可以调用m.front().get_x()m.front().get_y()以获取其位置。