@OlgakRain

Как осуществить правильное передвижение персонажа по игровому полю?

Здравствуйте, помогите мне, пожалуйста. Я пишу игру и сейчас я на этапе обработки столкновений. То есть я хочу сделать так, чтобы персонаж не мог проходить сквозь стенки, чтобы он передвигался правильно.
Вот мой код, но он выдает ошибку:
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;
}


Помогите, пожалуйста, очень Вас прошу.
Заранее большое спасибо!
  • Вопрос задан
  • 104 просмотра
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
19 апр. 2024, в 05:01
999999 руб./за проект
19 апр. 2024, в 03:52
1000 руб./за проект
19 апр. 2024, в 03:01
1000 руб./за проект