Ostatnio zacząłem uczyć się SDL. Napisałem grę w stylu "Pickin' sticks". Jedyna rzecz, która nie działa to kolizja bądź koordynaty umiejscowionych na mapie przedmiotów. Wygląda to tak - włączam program, podnoszę jedzenie z podłogi (poruszając się strzałkami), w incie "foodamount" liczone są podniosione przedmioty. Czasem działa, otrzymuję +1 do foodamount, jedzenie znika i aktywuje się dźwięk jedzenia. Jednak niekiedy nie działa - kiedy staram się podnieść przedmiot, dźwięk jedzenia powtarza się, przedmioty pojawiają się non stop w tym samym miejscu, a licznik wzrasta np. z 5 do 300. Nie mam pojęcia, jak to naprawić. Byłbym wdzięczny, gdybyście mogli mi pomóc. Screeny są pod kodem. Oto kod:

#include "SDL/SDL.h"
#include "SDL/SDL_ttf.h"
#include "SDL/SDL_mixer.h"
#include "SDL/SDL_image.h"
#include <windows.h>
#include <stdbool.h>
#define randx rand()*100%1175
#define randy rand()*100%585
SDL_Surface * background;
SDL_Surface * player;
SDL_Surface * screen;
//ITEMS
SDL_Surface * hamburger;
SDL_Surface * banana;
SDL_Surface * fries;
SDL_Surface * donut;
SDL_Surface * cake;
SDL_Surface * item;
//ITEMS
SDL_Surface * koniec;
SDL_Event event;
SDL_Rect playerlocation;
//ITEMS LOCATIONS
SDL_Rect hamburgerlocation;
SDL_Rect bananalocation;
SDL_Rect frieslocation;
SDL_Rect donutlocation;
SDL_Rect cakelocation;
SDL_Rect itemlocation;
//ITEMS LOCATIONS
int fullscreen = 0;
int randomfood;
int foodamount;
void putfood()
{
srand( time( NULL ) );
int itemx,itemy;
itemx = randx;
itemy = randy;
randomfood = (rand()%(5+1)) + 1;
if(randomfood == 1)
{
hamburgerlocation.x = itemx;
hamburgerlocation.y = itemy;
hamburgerlocation.w = 40;
hamburgerlocation.h = 40;
item = hamburger;
itemlocation = hamburgerlocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 2)
{
bananalocation.x = itemx;
bananalocation.y = itemy;
bananalocation.w = 40;
bananalocation.h = 40;
item = banana;
itemlocation = bananalocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 3)
{
frieslocation.x = itemx;
frieslocation.y = itemy;
frieslocation.w = 40;
frieslocation.h = 40;
item = fries;
itemlocation = frieslocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 4)
{
donutlocation.x = itemx;
donutlocation.y = itemy;
donutlocation.w = 40;
donutlocation.h = 40;
item = donut;
itemlocation = donutlocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
if(randomfood == 5)
{
cakelocation.x = itemx;
cakelocation.y = itemy;
cakelocation.w = 40;
cakelocation.h = 40;
item = cake;
itemlocation = cakelocation;
SDL_BlitSurface(item, NULL, screen, &itemlocation);
//SDL_Flip(screen);
}
}
int collision(SDL_Rect* rect1,SDL_Rect* rect2)
{
if(rect1->y >= rect2->y + rect2->h)
return 0;
if(rect1->x >= rect2->x + rect2->w)
return 0;
if(rect1->y + rect1->h <= rect2->y)
return 0;
if(rect1->x + rect1->w <= rect2->x)
return 0;
return 1;
}
bool checkCollision(SDL_Rect *player, SDL_Rect *obj)
{
if(player->x < obj->x + obj->w && player->x + player->w > obj->x && player->y < obj->y + obj->h && player->y + player->h > obj->y)
{
return true;
}
else
{
return false;
}
}
int main(int argc, char * args[])
{
bool jump = false;
Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096);
Mix_Chunk *eat = Mix_LoadWAV("sound/eat.wav");
Mix_Chunk *intro = Mix_LoadWAV("sound/layout.wav");
Mix_Chunk *step = Mix_LoadWAV("sound/step.wav");
Mix_VolumeChunk(intro, 64);
Mix_VolumeChunk(step, 10);
Mix_VolumeChunk(eat, 70);
Mix_PlayChannel(-1, intro, 0);
int x,y;
int exit = 0;
SDL_Init(SDL_INIT_EVERYTHING);
background = IMG_Load("images/grass.jpg");
player = IMG_Load("images/player.png");
screen = SDL_SetVideoMode(1280,720,32,SDL_DOUBLEBUF|SDL_SWSURFACE);
//items
hamburger = IMG_Load("images/food.png");
banana = IMG_Load("images/banana.png");
fries = IMG_Load("images/fries.png");
donut = IMG_Load("images/donut.png");
cake = IMG_Load("images/cake.png");
//items
koniec = SDL_LoadBMP("images/koniec.bmp");
SDL_WM_SetCaption("Program testowy SDL v2 (by Selve)", NULL);
Uint8 * keystate = SDL_GetKeyState( NULL );
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_Flip(screen);
x = 500;
y = 250;
playerlocation.w = 40;
playerlocation.h = 40;
playerlocation.x = x;
playerlocation.y = y;
TTF_Init();
TTF_Font * font = TTF_OpenFont("font/font.ttf", 45);
SDL_Surface * text;
SDL_Color text_color = {255, 255, 255};
char buf[50];
sprintf(buf, "Food amount: %d", foodamount);
SDL_Surface * textSurface = TTF_RenderText_Solid(font, buf, text_color);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
SDL_Flip(screen);
putfood();
while (exit == 0)
{
char buf[50];
sprintf(buf, "Food amount: %d", foodamount);
SDL_Surface * textSurface = TTF_RenderText_Solid(font, buf, text_color);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
{
SDL_Quit();
return 0;
}
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_q)
{
SDL_FreeSurface(background);
SDL_Flip( screen );
Mix_CloseAudio();
SDL_BlitSurface( koniec, NULL, screen, NULL );
SDL_Flip( screen );
SDL_Delay(2500);
SDL_Quit();
return 0;
}
if(event.key.keysym.sym == SDLK_RETURN && fullscreen == 0)
{
SDL_SetVideoMode( 1280, 720, 32, SDL_DOUBLEBUF|SDL_SWSURFACE|SDL_FULLSCREEN);
fullscreen = 1;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(player, NULL, screen, &playerlocation);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_Flip( screen );
break;
}
if(event.key.keysym.sym == SDLK_RETURN && fullscreen == 1)
{
SDL_SetVideoMode( 1280, 720, 32, SDL_DOUBLEBUF|SDL_SWSURFACE );
fullscreen = 0;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(player, NULL, screen, &playerlocation);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_Flip( screen );
break;
}
}
}
if(keystate[SDLK_RIGHT])
{
if(playerlocation.x < 1175)
{
playerlocation.x = playerlocation.x + 4;
Mix_PlayChannel(-1, step, 0);
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
if(keystate[SDLK_LEFT])
{
if(playerlocation.x > 10)
{
Mix_PlayChannel(-1, step, 0);
playerlocation.x = playerlocation.x - 4;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
if(keystate[SDLK_UP])
{
if(playerlocation.y > 10)
{
Mix_PlayChannel(-1, step, 0);
playerlocation.y = playerlocation.y - 4;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
if(keystate[SDLK_DOWN])
{
if(playerlocation.y < 585)
{
Mix_PlayChannel(-1, step, 0);
playerlocation.y = playerlocation.y + 4;
SDL_BlitSurface(background, NULL, screen, NULL);
SDL_BlitSurface(item, NULL, screen, &itemlocation);
SDL_BlitSurface(textSurface, NULL, screen, NULL);
}
}
SDL_BlitSurface(player, NULL, screen, &playerlocation);
SDL_Flip(screen);
if(checkCollision(&playerlocation, &itemlocation))
{
putfood();
Mix_PlayChannel(1, eat, 0);
foodamount++;
}
}
} 

Screeny:
user image
I dalej:
user image