Jump to content

  • Log In with Google      Sign In   
  • Create Account


#Actualselve

Posted 03 November 2012 - 06:28 AM

Hi guys. Recently I started learning SDL. I wrote something like "Pickin' sticks". The only thing that's not working is I think the collision or maybe random coordinates of food. It is like this - I launch the game, pick some food (moving by arrows) and it's counting by int foodamount (you can see on the screen). Sometimes it works - I get +1 to foodamount and there's a single sound crunch. However sometimes when I try to pick it up, the sound of crunch is repeating and repeating, food appears in the same place on the floor, the foodamount counter rises from 5 to 300. I don't know how to fix this. I would be really grateful if you can help me. Screens are under the code. Here's the code (the code fuction didn't work for me on the forum):

#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++;
}
}
}

Here's the photos:
Posted Image
And next:
Posted Image

#5selve

Posted 03 November 2012 - 06:25 AM

Hi guys. Recently I started learning SDL. I wrote something like "Pickin' sticks". The only thing that's not working is I think the collision or maybe random coordinates of food. It is like this - I launch the game, pick some food (moving by arrows) and it's counting by int foodamount (you can see on the screen). Sometimes it works - I get +1 to foodamount and there's a single sound crunch. However sometimes when I try to pick it up, the sound of crunch is repeating and repeating, food appears in the same place on the floor, the foodamount counter rises from 5 to 300. I don't know how to fix this. I would be really grateful if you can help me. Here's the code:

#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++;
}
}
}

Here's the photos:
Posted Image
And next:
Posted Image

#4selve

Posted 03 November 2012 - 06:23 AM

Hi guys. Recently I started learning SDL. I wrote something like "Pickin' sticks". The only thing that's not working is I think the collision or maybe random coordinates of food. It is like this - I launch the game, pick some food (moving by arrows) and it's counting by int foodamount (you can see on the screen). Sometimes it works - I get +1 to foodamount and there's a single sound crunch. However sometimes when I try to pick it up, the sound of crunch is repeating and repeating, food appears in the same place on the floor, the foodamount counter rises from 5 to 300. I don't know how to fix this. I would be really grateful if you can help me. Here's the code:

[source lang="cpp"]#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%585SDL_Surface * background;SDL_Surface * player;SDL_Surface * screen;//ITEMSSDL_Surface * hamburger;SDL_Surface * banana;SDL_Surface * fries;SDL_Surface * donut;SDL_Surface * cake;SDL_Surface * item;//ITEMSSDL_Surface * koniec;SDL_Event event;SDL_Rect playerlocation;//ITEMS LOCATIONSSDL_Rect hamburgerlocation;SDL_Rect bananalocation;SDL_Rect frieslocation;SDL_Rect donutlocation;SDL_Rect cakelocation;SDL_Rect itemlocation;//ITEMS LOCATIONSint 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++; } }}[/source]

Here's the photos:
Posted Image
And next:
Posted Image

#3selve

Posted 03 November 2012 - 06:18 AM

Hi guys. Recently I started learning SDL. I wrote something like "Pickin' sticks". The only thing that's not working is I think the collision or maybe random coordinates of food. It is like this - I launch the game, pick some food (moving by arrows) and it's counting by int foodamount (you can see on the screen). Sometimes it works - I get +1 to foodamount and there's a single sound crunch. However sometimes when I try to pick it up, the sound of crunch is repeating and repeating, food appears in the same place on the floor, the foodamount counter rises from 5 to 300. I don't know how to fix this. I would be really grateful if you can help me. Here's the photos:

Posted Image
And next:
Posted Image

The code:

[source lang="cpp"]#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%585SDL_Surface * background;SDL_Surface * player;SDL_Surface * screen;//ITEMSSDL_Surface * hamburger;SDL_Surface * banana;SDL_Surface * fries;SDL_Surface * donut;SDL_Surface * cake;SDL_Surface * item;//ITEMSSDL_Surface * koniec;SDL_Event event;SDL_Rect playerlocation;//ITEMS LOCATIONSSDL_Rect hamburgerlocation;SDL_Rect bananalocation;SDL_Rect frieslocation;SDL_Rect donutlocation;SDL_Rect cakelocation;SDL_Rect itemlocation;//ITEMS LOCATIONSint 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++; } }}[/source]

#2selve

Posted 03 November 2012 - 06:16 AM

Hi guys. Recently I started learning SDL. I wrote something like "Pickin' sticks". The only thing that's not working is I think the collision or maybe random coordinates of food. It is like this - I launch the game, pick some food (moving by arrows) and it's counting by int foodamount (you can see on the screen). Sometimes it works - I get +1 to foodamount and there's a single sound crunch. However sometimes when I try to pick it up, the sound of crunch is repeating and repeating, food appears in the same place on the floor, the foodamount counter rises from 5 to 300. I don't know how to fix this. I would be really grateful if you can help me. Here's the photos:

Posted Image
And next:
Posted Image

The code:
[source lang="cpp"]#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%585SDL_Surface * background;SDL_Surface * player;SDL_Surface * screen;//ITEMSSDL_Surface * hamburger;SDL_Surface * banana;SDL_Surface * fries;SDL_Surface * donut;SDL_Surface * cake;SDL_Surface * item;//ITEMSSDL_Surface * koniec;SDL_Event event;SDL_Rect playerlocation;//ITEMS LOCATIONSSDL_Rect hamburgerlocation;SDL_Rect bananalocation;SDL_Rect frieslocation;SDL_Rect donutlocation;SDL_Rect cakelocation;SDL_Rect itemlocation;//ITEMS LOCATIONSint 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++; } }}[/source]

#1selve

Posted 03 November 2012 - 06:14 AM

Hi guys. Recently I started learning SDL. I wrote something like "Pickin' sticks". The only thing that's not working is I think the collision or maybe random coordinates of food. It is like this - I launch the game, pick some food (moving by arrows) and it's counting by int foodamount (you can see on the screen). Sometimes it works - I get +1 to foodamount and there's a single sound crunch. However sometimes when I try to pick it up, the sound of crunch is repeating and repeating, food appears in the same place on the floor, the foodamount counter rises from 5 to 300. I don't know how to fix this. I would be really grateful if you can help me. Here's the photos:

Posted Image
And next:
Posted Image

The code:
[source lang="cpp"]#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%585SDL_Surface * background;SDL_Surface * player;SDL_Surface * screen;//ITEMSSDL_Surface * hamburger;SDL_Surface * banana;SDL_Surface * fries;SDL_Surface * donut;SDL_Surface * cake;SDL_Surface * item;//ITEMSSDL_Surface * koniec;SDL_Event event;SDL_Rect playerlocation;//ITEMS LOCATIONSSDL_Rect hamburgerlocation;SDL_Rect bananalocation;SDL_Rect frieslocation;SDL_Rect donutlocation;SDL_Rect cakelocation;SDL_Rect itemlocation;//ITEMS LOCATIONSint 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++; } }}[/source]

PARTNERS