Click here to Skip to main content
15,890,995 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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()); 
			
			







	}

}

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).
Posted
Updated 23-Mar-16 4:38am
v4

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)
 
Share this answer
 
Comments
Alya Wu 23-Mar-16 10:22am    
I added
std::vector<sf::rectangleshape>snake{ first,second,third,fourth }; (after deining first,second,third and fourth}
, but now the snake doesn't move when I try moving it with the keyboard buttons :/
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[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900