调试断言失败 - 向量下标超出范围。 (蛇游戏) [英] Debug assertion failed-vector subscript out of range. (Snake game)

查看:123
本文介绍了调试断言失败 - 向量下标超出范围。 (蛇游戏)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "stdafx.h"
#include <vector>
#include <limits>
#include <algorithm>


#include <SFML/Graphics.hpp>
#include <iostream>
int t = 0;
sf::RectangleShape addsnake();
std::vector<sf::RectangleShape>snake;


sf::RectangleShape addsnake(){
	std::vector<sf::RectangleShape>snake;
	snake[0].setFillColor(sf::Color::Red);
	snake[0].setPosition(100, 100);
	snake[0].setSize(sf::Vector2f(20, 20));
	return snake[t];

}
bool intersects(const sf::RectangleShape & r1, const sf::RectangleShape & r2){
	sf::FloatRect snake = r1.getGlobalBounds();
	sf::FloatRect spawnedFruit = r2.getGlobalBounds();
	return snake.intersects(spawnedFruit);

}

sf::RectangleShape generateFruit() {


	sf::RectangleShape fruit;
	fruit.setFillColor(sf::Color::Yellow);
	int fruitx = rand() % 400;
	int fruity = rand() % 400;
	fruit.setPosition(fruitx, fruity);
	fruit.setSize(sf::Vector2f(5, 5));

	return fruit;


}
int main()
{
	srand(time(NULL));
	int width = 400;
	int height = 400;
	sf::VideoMode videomode(width, height);
	sf::RenderWindow window(videomode, "Snake");
	
		
	

	
	sf::Clock clock;
	sf::Time t1 = sf::seconds(20);
	sf::RectangleShape spawnedFruit;
	while (window.isOpen()) {
		window.clear();
		window.draw(snake[0]);



		sf::Time elapsed1 = clock.getElapsedTime();
		if (elapsed1 >= t1) {


			spawnedFruit = generateFruit();

			clock.restart();

		}

		window.draw(spawnedFruit);

		window.display();
		sf::Event event;
		while (window.pollEvent(event))
		{
			if ((event.type == sf::Event::Closed) ||
				((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape)))
				window.close();
		}


		if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))

			snake[0].move(0, -0.1);
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
			snake[0].move(0, 0.1);
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
			snake[0].move(-0.1, 0);
		else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
			snake[0].move(0.1, 0);

		if (intersects(snake[0], spawnedFruit))
			
				t++;

				snake.push_back(addsnake()); 
			
			







	}

}



为什么我收到此Debug断言错误?请你帮助我好吗? (IDE Visual C ++ 2013)



我尝试过:



我已经尝试将snake.push_back(addsnake())改为snake.push_back(snake [t]),同样的问题。我也搜索了互联网的解决方案,但没有一个完全像我的问题,所以他们没有工作。我也尝试过使用类,但我还不擅长使用类(我是初学者)。


Why am I getting this Debug assertion error? Could you please help me? (IDE Visual C++ 2013)

What I have tried:

I've tried changing snake.push_back(addsnake()) to snake.push_back(snake[t]), same problem . I also searched the internet for solutions but none of them were exactly like my problem and so they didn't work. I've also tried using a class but I'm not good at using classes yet (I'm a beginner).

推荐答案

你的蛇是一个向量(如数组)你必须插入对象才能访问它们。



注意对象的范围。在addsnake中,你创建了蛇的本地实例 - 它隐藏了全局。 (所以你的实现是错误的)
Your snake is a vector (like array) you must insert objects, to access them.

Pay attention to the scope of your objects. In addsnake you create a local instance of the snake - it hides the global. (So it is a mistake in your implementation)


你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]


这篇关于调试断言失败 - 向量下标超出范围。 (蛇游戏)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆