@OlgakRain

Почему выводит ошибку string out of range?

Здравствуйте, помогите, пожалуйста, исправить код, чтобы не выдавало ошибку string out of range.
#include <SFML/Graphics.hpp>
#include <string>
using namespace sf;
using namespace std;

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#",
		"##########"
};
string charSt;

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 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++) {
				charSt.assign(mapOne[i], j, 1);
				if ((charSt == "#") || (charSt == "Q") || (charSt == "O")
					|| (charSt == "Z") || (charSt == "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;
					}
				}
			}
		}
	}

	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;
	}	
};

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

	Texture levelBackground, borderBlock, noChangeBlock,
		noChangeBlockTop, changeBlock, changeBlockTop, gridTexture;
	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");
	gridTexture.loadFromFile("sprites/grid.png");
	Sprite level, block, grid;
	level.setTexture(levelBackground);
	level.setPosition(56 + positionToDraw, 56);
	grid.setTexture(gridTexture);
	grid.setPosition(-4, -3);

	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 (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);
		window.draw(grid);

		for (int i = 0; i < HEIGHT; i++) {
			for (int j = 0; j < WIDTH; j++) {
				charSt.assign(mapOne[i], j, 1);
				if (charSt == "#") {
					block.setTexture(borderBlock);
				}
				if (charSt == "Q") {
					block.setTexture(changeBlockTop);
				}
				if (charSt == "O") {
					block.setTexture(changeBlock);
				}
				if (charSt == "X") {
					block.setTexture(noChangeBlock);
				}
				if (charSt == "Z") {
					block.setTexture(noChangeBlockTop);
				}
				if (charSt == " ") {
					continue;
				}
				block.setPosition(j * 67 + positionToDraw, i * 67);
				window.draw(block);
			}
		}
		window.draw(player.player);
		window.display();		
	}
	return 0;
}
  • Вопрос задан
  • 102 просмотра
Пригласить эксперта
Ваш ответ на вопрос

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

Похожие вопросы