-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++遊戲,找怪物的位置摧毀它
非常感謝!
你在哪裏存放怪物?對於我來說,這是太多的代碼。 – Carcigenicate
「確定怪物位置的最簡單方法」? *但是如果你還沒有怪物的位置,你甚至還沒有繪製它?* – meowgoesthedog
請看看如何發佈[MCVE]以獲得更好的幫助,這是很多代碼。 (例如,從我們的角度來看,註釋代碼是完全不相關的。) – Peri461