So as you can see, i have a class organism and a class child. Child is born when organism eats one food (when coordinates are the same as foods coordinates function eat gets called) but before that happens, organism has to sense food, i do this with explaining how far he can sense (coordinates). Now, one organism can move and eat with no problem, but it gets tricky when child is born. He can move around freely as organism does but he only eats the same food as organism does and vica versa. I think the problem is in the part where i inheritated from class organism all the functions. Movements works well, eating too, but sense is not working properly. It might had to do smomething with the variable i or with variable senseFood. But i don't know how to fix it.
Basically i would like to make child that will eat freely (movement already works) and i think the problem lies in sense part and it has some connection with int i or bool senseFood.
here is link to load VS project: http://www.mediafire...8056mx4bru873vf
EDIT: if mods want to move topic somewhere else please do so
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <string>
#include <sstream>
#include <time.h>
////////////////////////////// VARIABLES //////////////////////////////
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
const int SCREEN_BPP = 32;
const int FRAMES_PER_SECOND = 200;
const int ORGANISM_WIDTH = 20;
const int ORGANISM_HEIGHT = 20;
SDL_Surface *food = NULL;
SDL_Surface *organism = NULL;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Event event;
SDL_Rect foodPlaces[6];
int i;
////////////////////////////// CLASSES //////////////////////////////
class Timer
{
private:
int startTicks;
int pausedTicks;
bool paused;
bool started;
public:
Timer();
void start();
void stop();
void pause();
void unpause();
int get_ticks();
bool is_started();
bool is_paused();
};
class Organism
{
private:
int xVel, yVel, xOrganism, yOrganism;
bool diffusion;
int diffusionDowntime;
int diffusionStep;
bool senseFood;
public:
Organism();
void show();
void move();
void sense();
void eat();
void extract();
void grow();
void reproduct();
static bool makeChild;
};
class Food
{
private:
public:
bool startPosition;
static bool spawnNew;
static int xFood, yFood;
Food();
void show();
void spawnFood(int x);
};
class Child: public Organism
{
private:
int xVel, yVel, xOrganism, yOrganism;
bool diffusion;
int diffusionDowntime;
int diffusionStep;
bool senseFood;
public:
Child();
};
bool Organism::makeChild;
bool Food::spawnNew;
////////////////////////////// GENERAL //////////////////////////////
SDL_Surface *load_image(std::string filename)
{
SDL_Surface *loadedImage = NULL;
SDL_Surface *optimizedImage = NULL;
loadedImage = IMG_Load(filename.c_str());
if(loadedImage != NULL)
{
optimizedImage = SDL_DisplayFormat(loadedImage);
SDL_FreeSurface(loadedImage);
if(optimizedImage != NULL)
{
SDL_SetColorKey(optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB(optimizedImage->format, 0, 0xFF, 0xFF));
}
}
return optimizedImage;
}
void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL)
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, clip, destination, &offset );
}
bool init()
{
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
return false;
}
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(screen == NULL)
{
return false;
}
SDL_WM_SetCaption("KRNEKI", NULL);
return true;
}
bool load_files()
{
background = load_image("background.png");
food = load_image("food.png");
organism = load_image("organism.png");
if (background == NULL)
{
return false;
}
if (food == NULL)
{
return false;
}
if (organism == NULL)
{
return false;
}
return true;
}
void clean_up()
{
SDL_FreeSurface(background);
SDL_FreeSurface(organism);
SDL_FreeSurface(food);
SDL_Quit;
}
////////////////////////////// ORGANISM //////////////////////////////
Organism::Organism()
{
xOrganism = 380;
yOrganism = 280;
diffusionStep=0;
diffusion=true;
diffusionDowntime=40;
senseFood=false;
}
void Organism::sense()
{
if(foodPlaces.x < xOrganism+20+100 && foodPlaces.x > xOrganism-100 && foodPlaces.y < yOrganism+20+100 && foodPlaces.y > yOrganism-100)
{
senseFood=true;
}
else
{
senseFood=false;
i++;
if(i==6)
{
i=0;
}
}
}
void Organism::move()
{
if(senseFood==false)
{
if(diffusion==true)
{
xVel=(rand()%3)-1;
yVel=(rand()%3)-1;
diffusionStep=0;
diffusion=false;
}
if(diffusionStep < diffusionDowntime)
{
diffusionStep++;
}
if(diffusionStep==diffusionDowntime)
{
diffusion=true;
}
xOrganism += xVel;
yOrganism += yVel;
if(xOrganism + ORGANISM_WIDTH > SCREEN_WIDTH || xOrganism < 0)
{
xVel = - xVel;
}
if(yOrganism + ORGANISM_HEIGHT > SCREEN_HEIGHT || yOrganism < 0)
{
yVel = - yVel;
}
}
if(senseFood==true)
{
if(xOrganism < foodPlaces.x)
{
xVel = 1;
}
if(xOrganism > foodPlaces.x)
{
xVel = - 1;
}
if(xOrganism == foodPlaces.x)
{
xVel = 0;
}
if(yOrganism < foodPlaces.y)
{
yVel = 1;
}
if(yOrganism > foodPlaces.y)
{
yVel = - 1;
}
if(yOrganism == foodPlaces.y)
{
yVel = 0;
}
xOrganism += xVel;
yOrganism += yVel;
}
}
void Organism::show()
{
apply_surface(xOrganism, yOrganism, organism, screen);
}
void Organism::eat()
{
Food foodObj;
if(xOrganism == foodPlaces.x && yOrganism == foodPlaces.y)
{
foodObj.spawnNew=true;
makeChild=true;
}
else
{
foodObj.spawnNew=false;
}
}
////////////////////////////// FOOD //////////////////////////////
Food::Food()
{
spawnNew=false;
startPosition=true;
}
void Food::show()
{
Organism organismObj;
if(spawnNew==true)
{
spawnFood(i);
apply_surface(foodPlaces.x, foodPlaces.y, food, screen);
spawnNew=false;
}
if(startPosition==true)
{
spawnFood(0);
spawnFood(1);
spawnFood(2);
spawnFood(3);
spawnFood(4);
spawnFood(5);
startPosition=false;
}
apply_surface(foodPlaces[0].x, foodPlaces[0].y, food, screen);
apply_surface(foodPlaces[1].x, foodPlaces[1].y, food, screen);
apply_surface(foodPlaces[2].x, foodPlaces[2].y, food, screen);
apply_surface(foodPlaces[3].x, foodPlaces[3].y, food, screen);
apply_surface(foodPlaces[4].x, foodPlaces[4].y, food, screen);
apply_surface(foodPlaces[5].x, foodPlaces[5].y, food, screen);
}
void Food::spawnFood(int x)
{
foodPlaces[x].x=1+(rand()%600);
foodPlaces[x].y=1+(rand()%400);
foodPlaces[x].w=20;
foodPlaces[x].h=20;
}
////////////////////////////// CHILD //////////////////////////////
Child::Child()
{
diffusionStep=0;
diffusion=true;
diffusionDowntime=40;
}
////////////////////////////// TIMER //////////////////////////////
Timer::Timer()
{
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
started = true;
paused = false;
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
started = false;
paused = false;
}
void Timer::pause()
{
if((started == true) && (paused == false))
{
paused = true;
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
if(paused == true)
{
paused = false;
startTicks = SDL_GetTicks() - pausedTicks;
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
if(started == true)
{
if(paused == true)
{
return pausedTicks;
}
else
{
return SDL_GetTicks() - startTicks;
}
}
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
////////////////////////////// MAIN //////////////////////////////
int main(int argc, char *args[])
{
Timer fps;
Organism organismObj;
Food foodObj;
Child childObj;
srand(time(0));
bool quit = false;
if(init() == false)
{
return 1;
}
if(load_files() == false)
{
return 1;
}
while (quit == false)
{
fps.start();
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
quit = true;
}
}
apply_surface(0, 0, background, screen);
organismObj.move();
organismObj.sense();
organismObj.eat();
organismObj.show();
foodObj.show();
if(organismObj.makeChild==true)
{
childObj.move();
childObj.sense();
childObj.eat();
childObj.show();
}
if(SDL_Flip(screen) == -1)
{
return 1;
}
if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
clean_up();
return 0;
}
I am sorry if a code is too long, i hope you read it through :S