O
O
OlgakRain2020-11-30 20:31:46
C++ / C#
OlgakRain, 2020-11-30 20:31:46

How to carry out the correct movement of the character on the playing field?

Hello, help me please. I'm writing a game and I'm currently in the collision handling phase. That is, I want to make it so that the character cannot pass through the walls, so that he moves correctly.
Here is my code, but it throws an error:

spoiler
#include <SFML/Graphics.hpp>
using namespace sf;

int ground = 220;
const int positionToDraw = 621;
const int HEIGHT = 16;
const int WIDTH = 10;
String mapOne[HEIGHT] = {
    "##########",
    "#        #",
    "#   Q    #",
    "#  QO    #",
    "#    Z ZQ#",
    "#    O   #",
    "#    O   #",
    "#QQQQOQ Q#",
    "#        #",
    "#        #",
    "#QQQQ QQ #",
    "#        #",
    "#    Z   #",
    "#    XZ Q#",
    "#Q QQQQQO#",
    "##########"
};

class PLAYER {
public:
  float dx, dy;
  FloatRect rect;
  bool onGround = 1;
  Sprite player;
  float currentFrame;

  PLAYER(Texture& image) {
    player.setTexture(image);
    player.setTextureRect(IntRect(0, 0, 46, 26));
    rect = FloatRect(965, 0, 46, 26);
    dx = dy = 0;
    currentFrame = 0;
  }

  void update(float time) {
    rect.left += dx * time;
    Collision(0);
    if (!onGround) {
      dy = dy + 0.05 * time;
    }
    rect.top += dy * time;
    onGround = 0;
    Collision(1);
    currentFrame += 0.05 * time;
    if (currentFrame > 15) {
      currentFrame -= 15;
    }
    if (dx > 0) {
      player.setTextureRect(IntRect(46 * int(currentFrame) + 46, 0, -46, 26));
    }
    if (dx < 0) {
      player.setTextureRect(IntRect(46 * int(currentFrame), 0, 46, 26));
    }
    player.setPosition(rect.left, rect.top);

    dx = 0;
  }

  void Collision(int direction) {
    for (int i = rect.top / 67; i < (rect.top + rect.height) / 67; i++) {
      for (int j = rect.left / 67; j < (rect.left + rect.width) / 67; j++) {
        if ((mapOne[i][j] == '#') || (mapOne[i][j] == 'Q') || (mapOne[i][j] == 'O')
          || (mapOne[i][j] == 'Z') || (mapOne[i][j] == 'X')) {
          if ((dx > 0) && (direction == 0)) {
            rect.left = j * 67 - rect.width;
          }
          if ((dx < 0) && (direction == 0)) {
            rect.left = j * 67 + 67;
          }
          if ((dy > 0) && (direction == 1)) {
            rect.top = i * 67 - rect.height;
            dy = 0;
            onGround = 1;
          }
          if ((dy < 0) && (direction == 1)) {
            rect.top = i * 67 + 67;
            dy = 0;
          }
        }
      }
    }
  }
};

int main() {
  RenderWindow window(VideoMode(1920, 1080), "Lemmings", Style::None);

  Texture levelBackground, borderBlock, noChangeBlock,
    noChangeBlockTop, changeBlock, changeBlockTop;
  levelBackground.loadFromFile("sprites/levelBackground.png");
  borderBlock.loadFromFile("sprites/borderBlock.png");
  noChangeBlock.loadFromFile("sprites/noChangeBlock.png");
  noChangeBlockTop.loadFromFile("sprites/noChangeBlockTop.png");
  changeBlock.loadFromFile("sprites/changeBlock.png");
  changeBlockTop.loadFromFile("sprites/changeBlockTop.png");
  Sprite level, block;
  level.setTexture(levelBackground);

  level.setPosition(56 + positionToDraw, 56);

  float currentFrame = 0;

  Texture movePlayer;
  movePlayer.loadFromFile("sprites/movePlayer.png");

  PLAYER player(movePlayer);

  Clock clock;

  while (window.isOpen()) {
    float time = clock.getElapsedTime().asMicroseconds();
    clock.restart();
    time = time / 10000;

    Event event;
    while (window.pollEvent(event)) {
      if (Keyboard::isKeyPressed(Keyboard::Escape)) {
        window.clear();
        window.close();
      }
    }
    /*if (mainMenu) {
      menu(window);
    }
    else*/ 
      if (Keyboard::isKeyPressed(Keyboard::A)) {
        player.dx = -2;///////////////////////////////////////////////////-0.2			
      }
      if (Keyboard::isKeyPressed(Keyboard::D)) {
        player.dx = 2;////////////////////////////////////////////////////0.2
      }
      player.update(time);

      window.clear();
      window.draw(level);
      for (int i = 0; i < HEIGHT; i++) {
        for (int j = 0; j < WIDTH; j++) {
          if (mapOne[i][j] == '#') {
            block.setTexture(borderBlock);
          }
          if (mapOne[i][j] == 'Q') {
            block.setTexture(changeBlockTop);
          }
          if (mapOne[i][j] == 'O') {
            block.setTexture(changeBlock);
          }
          if (mapOne[i][j] == 'X') {
            block.setTexture(noChangeBlock);
          }
          if (mapOne[i][j] == 'Z') {
            block.setTexture(noChangeBlockTop);
          }
          if (mapOne[i][j] == ' ') {
            continue;
          }
          block.setPosition(j * 67 + positionToDraw, i * 67);
          window.draw(block);
        }
      }
      window.draw(player.player);
      window.display();
    
  }
  return 0;
}


Help, please, I beg you.
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question