1
General / Constant Crashs
« on: May 06, 2018, 10:26:43 pm »
Hello,
When i am running my program it will work for a bit and then it will crash, No error is appearing. This error has only just started happening recently.
Im running SFML on codeblock 16.01, on windows 10.
Thanks
When i am running my program it will work for a bit and then it will crash, No error is appearing. This error has only just started happening recently.
Im running SFML on codeblock 16.01, on windows 10.
(click to show/hide)
Main Class
Game Class
Game Header
Snake Class
Snake Header
Collectable Class
Collectable Header
Planets Class
Planets Header
#include "Game.h"
int main(){
Game snakeGame;
snakeGame.Run();
return 0;
}
int main(){
Game snakeGame;
snakeGame.Run();
return 0;
}
Game Class
#include "Game.h"
Game::Game()
{
//ctor
}
Game::~Game()
{
//dtor
}
void Game::getDefualtValues(){
}
void Game::Run(){
srand(time(NULL));
//Set output to console
std::cout << "Starting" << std::endl;
sf::Clock clock;
m_snakes.push_back(Snake("Player1"));
//m_snakes.push_back(Snake("Player2"));
Collectable m_collect[5];
Planets m_planets;
//Main loop that continues till we call close()
while(m_window.isOpen()){
while(isAlive){
if(sf::Event::Closed){
m_window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
m_window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::P)){
for(Snake& s : m_snakes){
s.popBack(1);
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::R)){
//m_window.restart();
}
//Check to see how many collectables there already are
int activeCollect = 0;
for(int i = 0; i<5; i++){
if(m_collect[i].getIsVisable() == true){
activeCollect++;
}
}
//Respawns the collectables
if(activeCollect < 5 && (rand()%100 + 1) == 5){
for(int i = 0; i<5; i++){
if(m_collect[i].getIsVisable() == false){
bool accepted = false;
do{
m_collect[i].respawn();
for(int j = 0; i<5; i++){
if(i != j){
if((m_collect[j].getSize()+m_collect[i].getSize())>sqrt(pow((m_collect[j].getPosition().x-m_collect[i].getPosition().x),2)+pow((m_collect[j].getPosition().y-m_collect[i].getPosition().y),2))){
m_collect[i].respawn();
}else{
accepted = true;
}
}
}
}while(!accepted);
break;
}
}
}
//Check the collison of the collectable and the snake
for(Snake& s : m_snakes){
for(int i = 0; i<5; i++){
if((s.getSize()+m_collect[i].getSize())>sqrt(pow((s.getPosition().x-m_collect[i].getPosition().x),2)+pow((s.getPosition().y-m_collect[i].getPosition().y),2))){
m_collect[i].setIsVisable(false);
s.addGrowAmount(1);
std::cout<<"Grow"<<std::endl;
break;
}
}
float resultantSize = s.getSize()+m_planets.getSize("sun");
float xResultant = sqrt(pow((s.getPosition().x-m_planets.GetPosition("sunX")),2));
float yResultant = sqrt(pow((s.getPosition().y-m_planets.GetPosition("sunY")),2));
if(resultantSize > xResultant + yResultant){
std::cout<<"Dead"<<std::endl;
//isAlive = false;
}
}
//move all the snakes
for (Snake& s : m_snakes){
s.Movement();
}
//We must clear the window at the end of each loop
m_window.clear();
//Render the snakes
for (Snake& s : m_snakes){
s.Render(m_window);
}
//Render the collecables
for (Collectable& c : m_collect){
c.Render(m_window);
}
//Render Planets
m_planets.Render(m_window);
//Get the window to display content
m_window.display();
//Delay
while(clock.getElapsedTime().asMilliseconds()<16.67);
clock.restart();
}
}
std::cout << "Finished" << std::endl;
}
Game::Game()
{
//ctor
}
Game::~Game()
{
//dtor
}
void Game::getDefualtValues(){
}
void Game::Run(){
srand(time(NULL));
//Set output to console
std::cout << "Starting" << std::endl;
sf::Clock clock;
m_snakes.push_back(Snake("Player1"));
//m_snakes.push_back(Snake("Player2"));
Collectable m_collect[5];
Planets m_planets;
//Main loop that continues till we call close()
while(m_window.isOpen()){
while(isAlive){
if(sf::Event::Closed){
m_window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)){
m_window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::P)){
for(Snake& s : m_snakes){
s.popBack(1);
}
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::R)){
//m_window.restart();
}
//Check to see how many collectables there already are
int activeCollect = 0;
for(int i = 0; i<5; i++){
if(m_collect[i].getIsVisable() == true){
activeCollect++;
}
}
//Respawns the collectables
if(activeCollect < 5 && (rand()%100 + 1) == 5){
for(int i = 0; i<5; i++){
if(m_collect[i].getIsVisable() == false){
bool accepted = false;
do{
m_collect[i].respawn();
for(int j = 0; i<5; i++){
if(i != j){
if((m_collect[j].getSize()+m_collect[i].getSize())>sqrt(pow((m_collect[j].getPosition().x-m_collect[i].getPosition().x),2)+pow((m_collect[j].getPosition().y-m_collect[i].getPosition().y),2))){
m_collect[i].respawn();
}else{
accepted = true;
}
}
}
}while(!accepted);
break;
}
}
}
//Check the collison of the collectable and the snake
for(Snake& s : m_snakes){
for(int i = 0; i<5; i++){
if((s.getSize()+m_collect[i].getSize())>sqrt(pow((s.getPosition().x-m_collect[i].getPosition().x),2)+pow((s.getPosition().y-m_collect[i].getPosition().y),2))){
m_collect[i].setIsVisable(false);
s.addGrowAmount(1);
std::cout<<"Grow"<<std::endl;
break;
}
}
float resultantSize = s.getSize()+m_planets.getSize("sun");
float xResultant = sqrt(pow((s.getPosition().x-m_planets.GetPosition("sunX")),2));
float yResultant = sqrt(pow((s.getPosition().y-m_planets.GetPosition("sunY")),2));
if(resultantSize > xResultant + yResultant){
std::cout<<"Dead"<<std::endl;
//isAlive = false;
}
}
//move all the snakes
for (Snake& s : m_snakes){
s.Movement();
}
//We must clear the window at the end of each loop
m_window.clear();
//Render the snakes
for (Snake& s : m_snakes){
s.Render(m_window);
}
//Render the collecables
for (Collectable& c : m_collect){
c.Render(m_window);
}
//Render Planets
m_planets.Render(m_window);
//Get the window to display content
m_window.display();
//Delay
while(clock.getElapsedTime().asMilliseconds()<16.67);
clock.restart();
}
}
std::cout << "Finished" << std::endl;
}
Game Header
#ifndef GAME_H
#define GAME_H
#include <iostream>
#include <vector>
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include "Snake.h"
#include "Collectable.h"
#include "Planets.h"
class Game
{
public:
Game();
~Game();
void Run();
void getDefualtValues();
protected:
private:
int m_screenWidth{1280};
int m_screenHeight{720};
std::vector<Snake> m_snakes;
bool isAlive = true;
// Initialise Window and title text
sf::RenderWindow m_window{sf::VideoMode(m_screenWidth, m_screenHeight), "C++ Snake ICA : T7048271"};
};
#endif // GAME_H
#define GAME_H
#include <iostream>
#include <vector>
#include <cstdlib>
#include <SFML/Graphics.hpp>
#include "Snake.h"
#include "Collectable.h"
#include "Planets.h"
class Game
{
public:
Game();
~Game();
void Run();
void getDefualtValues();
protected:
private:
int m_screenWidth{1280};
int m_screenHeight{720};
std::vector<Snake> m_snakes;
bool isAlive = true;
// Initialise Window and title text
sf::RenderWindow m_window{sf::VideoMode(m_screenWidth, m_screenHeight), "C++ Snake ICA : T7048271"};
};
#endif // GAME_H
Snake Class
#include "Snake.h"
Snake::Snake(std::string name) : m_name(name)
{
//ctor
//Set the shapes fill colour attribute to green
m_snakeHead.setFillColor(sf::Color::Magenta);
m_segmentList.push_front(m_snakePosition);
}
Snake::~Snake()
{
//dtor
}
void Snake::Movement(){
m_horDirection = sf::Keyboard::isKeyPressed(sf::Keyboard::Right) - sf::Keyboard::isKeyPressed(sf::Keyboard::Left);
m_angle += m_horDirection*m_angleDis;
m_snakePosition.x += m_mag*cos(m_angle);
m_snakePosition.y += m_mag*sin(m_angle);
m_segmentList.push_front(m_snakePosition);
if(m_growAmount>0){
m_growAmount = 0;
}else{
m_segmentList.pop_back();
}
}
void Snake::Render(sf::RenderWindow& m_window){
//draw our circle shape to the window
//m_window.draw(m_snakeHead);
for (auto& s: m_segmentList)
{
m_snakeHead.setPosition(s);
m_window.draw(m_snakeHead);
}
}
int Snake::getSize(){
return m_snakeSize;
}
sf::Vector2f Snake::getPosition(){
return m_snakePosition;
}
std::list<sf::Vector2f> Snake::getSegmentList(){
return m_segmentList;
}
void Snake::addGrowAmount(int amount){
m_growAmount += amount;
}
void Snake::popBack(int amount){
for(int i = 0; i < amount; i++){
m_segmentList.pop_back();
}
}
Snake::Snake(std::string name) : m_name(name)
{
//ctor
//Set the shapes fill colour attribute to green
m_snakeHead.setFillColor(sf::Color::Magenta);
m_segmentList.push_front(m_snakePosition);
}
Snake::~Snake()
{
//dtor
}
void Snake::Movement(){
m_horDirection = sf::Keyboard::isKeyPressed(sf::Keyboard::Right) - sf::Keyboard::isKeyPressed(sf::Keyboard::Left);
m_angle += m_horDirection*m_angleDis;
m_snakePosition.x += m_mag*cos(m_angle);
m_snakePosition.y += m_mag*sin(m_angle);
m_segmentList.push_front(m_snakePosition);
if(m_growAmount>0){
m_growAmount = 0;
}else{
m_segmentList.pop_back();
}
}
void Snake::Render(sf::RenderWindow& m_window){
//draw our circle shape to the window
//m_window.draw(m_snakeHead);
for (auto& s: m_segmentList)
{
m_snakeHead.setPosition(s);
m_window.draw(m_snakeHead);
}
}
int Snake::getSize(){
return m_snakeSize;
}
sf::Vector2f Snake::getPosition(){
return m_snakePosition;
}
std::list<sf::Vector2f> Snake::getSegmentList(){
return m_segmentList;
}
void Snake::addGrowAmount(int amount){
m_growAmount += amount;
}
void Snake::popBack(int amount){
for(int i = 0; i < amount; i++){
m_segmentList.pop_back();
}
}
Snake Header
#ifndef SNAKE_H
#define SNAKE_H
#include <SFML/Graphics.hpp>
#include <string>
#include <list>
class Snake
{
public:
Snake(std::string name);
virtual ~Snake();
void Movement();
void Render(sf::RenderWindow& m_window);
int getSize();
sf::Vector2f getPosition();
std::list<sf::Vector2f> getSegmentList();
void addGrowAmount(int amount);
void popBack(int amount);
private:
std::string m_name;
sf::Vector2f m_snakePosition{300,200};
std::list<sf::Vector2f> m_segmentList;
const int m_snakeSize{12};
//Create an instance of the SFMl Circle shape and initialise it
sf::CircleShape m_snakeHead{m_snakeSize};
int m_horDirection{0};
int m_growAmount{0};
float m_angle{0};
float m_mag{3};
float m_angleDis{0.1};
};
#endif // SNAKE_H
#define SNAKE_H
#include <SFML/Graphics.hpp>
#include <string>
#include <list>
class Snake
{
public:
Snake(std::string name);
virtual ~Snake();
void Movement();
void Render(sf::RenderWindow& m_window);
int getSize();
sf::Vector2f getPosition();
std::list<sf::Vector2f> getSegmentList();
void addGrowAmount(int amount);
void popBack(int amount);
private:
std::string m_name;
sf::Vector2f m_snakePosition{300,200};
std::list<sf::Vector2f> m_segmentList;
const int m_snakeSize{12};
//Create an instance of the SFMl Circle shape and initialise it
sf::CircleShape m_snakeHead{m_snakeSize};
int m_horDirection{0};
int m_growAmount{0};
float m_angle{0};
float m_mag{3};
float m_angleDis{0.1};
};
#endif // SNAKE_H
Collectable Class
#include "Collectable.h"
Collectable::Collectable()
{
//ctor
m_collectPosition.x = rand()%(800-10)+10;
m_collectPosition.y = rand()%(600-10)+10;
m_collect.setPosition(m_collectPosition);
m_collect.setFillColor(sf::Color::Red);
}
Collectable::~Collectable()
{
//dtor
}
void Collectable::Render(sf::RenderWindow& m_window){
//draw our circle shape to the window
if(isVisable == true){
m_window.draw(m_collect);
}
}
bool Collectable::getIsVisable(){
return isVisable;
}
void Collectable::setIsVisable(bool visability){
isVisable = visability;
}
int Collectable::getSize(){
return m_collectSize;
}
sf::Vector2f Collectable::getPosition(){
return m_collectPosition;
}
void Collectable::respawn(){
m_collectPosition.x = rand()%(800-20)+10;
m_collectPosition.y = rand()%(600-20)+10;
m_collect.setPosition(m_collectPosition);
isVisable = true;
}
Collectable::Collectable()
{
//ctor
m_collectPosition.x = rand()%(800-10)+10;
m_collectPosition.y = rand()%(600-10)+10;
m_collect.setPosition(m_collectPosition);
m_collect.setFillColor(sf::Color::Red);
}
Collectable::~Collectable()
{
//dtor
}
void Collectable::Render(sf::RenderWindow& m_window){
//draw our circle shape to the window
if(isVisable == true){
m_window.draw(m_collect);
}
}
bool Collectable::getIsVisable(){
return isVisable;
}
void Collectable::setIsVisable(bool visability){
isVisable = visability;
}
int Collectable::getSize(){
return m_collectSize;
}
sf::Vector2f Collectable::getPosition(){
return m_collectPosition;
}
void Collectable::respawn(){
m_collectPosition.x = rand()%(800-20)+10;
m_collectPosition.y = rand()%(600-20)+10;
m_collect.setPosition(m_collectPosition);
isVisable = true;
}
Collectable Header
#ifndef COLLECTABLE_H
#define COLLECTABLE_H
#include <SFML/Graphics.hpp>
#include <string>
class Collectable
{
public:
Collectable();
virtual ~Collectable();
void Render(sf::RenderWindow& m_window);
bool getIsVisable();
void setIsVisable(bool visability);
int getSize();
sf::Vector2f getPosition();
void respawn();
protected:
private:
sf::Vector2f m_collectPosition{400,300};
const int m_collectSize{9};
//Create an instance of the SFMl Circle shape and initialise it
sf::CircleShape m_collect{m_collectSize};
bool isVisable{false};
};
#endif // COLLECTABLE_H
#define COLLECTABLE_H
#include <SFML/Graphics.hpp>
#include <string>
class Collectable
{
public:
Collectable();
virtual ~Collectable();
void Render(sf::RenderWindow& m_window);
bool getIsVisable();
void setIsVisable(bool visability);
int getSize();
sf::Vector2f getPosition();
void respawn();
protected:
private:
sf::Vector2f m_collectPosition{400,300};
const int m_collectSize{9};
//Create an instance of the SFMl Circle shape and initialise it
sf::CircleShape m_collect{m_collectSize};
bool isVisable{false};
};
#endif // COLLECTABLE_H
Planets Class
#include "Planets.h"
Planets::Planets()
{
m_sun.setPosition(m_sunPosition);
m_sun.setFillColor(sf::Color::Yellow);
m_ast.setFillColor(sf::Color::Blue);
m_ast.setPosition(m_astPosition);
m_ast2.setFillColor(sf::Color::Green);
m_ast2.setPosition(m_ast2Position);
}
Planets::~Planets()
{
//dtor
}
void Planets::Render(sf::RenderWindow& m_window){
//draw our circle shape to the window
m_window.draw(m_sun);
m_window.draw(m_ast);
m_window.draw(m_ast2);
m_astPosition.x = (cos(m_astRotationAngle) * m_astRadius) + (640-m_astSize);
m_astPosition.y = (sin(m_astRotationAngle) * -m_astRadius) + (360-m_astSize);
m_ast.setPosition(m_astPosition);
m_astRotationAngle+= 0.01;
m_ast2Position.x = (cos(m_ast2RotationAngle) * m_ast2Radius) + (640-m_ast2Size);
m_ast2Position.y = (sin(m_ast2RotationAngle) * m_ast2Radius) + (360-m_ast2Size);
m_ast2.setPosition(m_ast2Position);
m_ast2RotationAngle+= 0.05;
}
int Planets::getSize(std::string planet){
if(planet == "sun"){
int size = m_sunSize;
return size;
}else if(planet == "ast"){
int size = m_sunSize;
return size;
}else if(planet == "ast2"){
int size = m_sunSize;
return size;
}
}
int Planets::GetPosition(std::string planet){
if(planet == "sunX"){
int posX = m_sunPosition.x;
return posX;
} else if(planet == "sunY"){
int posY = m_sunPosition.y;
return posY;
}
}
Planets::Planets()
{
m_sun.setPosition(m_sunPosition);
m_sun.setFillColor(sf::Color::Yellow);
m_ast.setFillColor(sf::Color::Blue);
m_ast.setPosition(m_astPosition);
m_ast2.setFillColor(sf::Color::Green);
m_ast2.setPosition(m_ast2Position);
}
Planets::~Planets()
{
//dtor
}
void Planets::Render(sf::RenderWindow& m_window){
//draw our circle shape to the window
m_window.draw(m_sun);
m_window.draw(m_ast);
m_window.draw(m_ast2);
m_astPosition.x = (cos(m_astRotationAngle) * m_astRadius) + (640-m_astSize);
m_astPosition.y = (sin(m_astRotationAngle) * -m_astRadius) + (360-m_astSize);
m_ast.setPosition(m_astPosition);
m_astRotationAngle+= 0.01;
m_ast2Position.x = (cos(m_ast2RotationAngle) * m_ast2Radius) + (640-m_ast2Size);
m_ast2Position.y = (sin(m_ast2RotationAngle) * m_ast2Radius) + (360-m_ast2Size);
m_ast2.setPosition(m_ast2Position);
m_ast2RotationAngle+= 0.05;
}
int Planets::getSize(std::string planet){
if(planet == "sun"){
int size = m_sunSize;
return size;
}else if(planet == "ast"){
int size = m_sunSize;
return size;
}else if(planet == "ast2"){
int size = m_sunSize;
return size;
}
}
int Planets::GetPosition(std::string planet){
if(planet == "sunX"){
int posX = m_sunPosition.x;
return posX;
} else if(planet == "sunY"){
int posY = m_sunPosition.y;
return posY;
}
}
Planets Header
#ifndef PLANETS_H
#define PLANETS_H
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
class Planets
{
public:
Planets();
~Planets();
void Render(sf::RenderWindow& m_window);
int getSize(std::string planet);
int GetPosition(std::string planet);
private:
const int m_sunSize = 50;
sf::Vector2f m_sunPosition{(640-m_sunSize),(360-m_sunSize)};
sf::CircleShape m_sun{m_sunSize};
float m_astRotationAngle = 0;
float m_astRadius = 200;
const int m_astSize = 15;
sf::Vector2f m_astPosition{0,0};
sf::CircleShape m_ast{m_astSize};
float m_ast2RotationAngle = 0;
float m_ast2Radius = 100;
const int m_ast2Size = 10;
sf::Vector2f m_ast2Position{0,0};
sf::CircleShape m_ast2{m_ast2Size};
};
#endif // PLANETS_H
#define PLANETS_H
#include <SFML/Graphics.hpp>
#include <string>
#include <iostream>
class Planets
{
public:
Planets();
~Planets();
void Render(sf::RenderWindow& m_window);
int getSize(std::string planet);
int GetPosition(std::string planet);
private:
const int m_sunSize = 50;
sf::Vector2f m_sunPosition{(640-m_sunSize),(360-m_sunSize)};
sf::CircleShape m_sun{m_sunSize};
float m_astRotationAngle = 0;
float m_astRadius = 200;
const int m_astSize = 15;
sf::Vector2f m_astPosition{0,0};
sf::CircleShape m_ast{m_astSize};
float m_ast2RotationAngle = 0;
float m_ast2Radius = 100;
const int m_ast2Size = 10;
sf::Vector2f m_ast2Position{0,0};
sf::CircleShape m_ast2{m_ast2Size};
};
#endif // PLANETS_H
Thanks