PDA

View Full Version : Allegro problem...


erroroccured
02-07-2007, 06:00 AM
Hi, I have an little problem with allegro (like title says). I am trying to make a letter fall automatically down and so that you can move it. That worked well. But it usually goes off-screen. So, when I tried to fix this, I didn`t succeed.
What is wrong with my code? Here it is:


#include <allegro.h>
#include <time.h>

int x = 10;
int y = 10;
int dir=1;

int main(){

allegro_init();
install_keyboard();
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

while ( !key[KEY_ESC] ){

clear_keybuf();

acquire_screen();

textout_ex( screen, font, " ", x+49, y+49, makecol( 0, 0, 0), makecol( 0, 0, 0) );
if (key[KEY_RIGHT] && x < 420) ++x;
else if (key[KEY_LEFT]&& x > 0) --x;
else if (key[KEY_UP]&& y > 0) ----y;
else
allegro_message("An error has occured");

time_t seconds;
time(&seconds);
if (seconds>=0 && y < 420)
++y;
else
allegro_message ("An error has occured, and program must be quit. Please, inform us of this problem.");

textout_ex( screen, font, "@", x+50, y+50, makecol( 255, 0, 0), makecol( 0, 0, 0) );

release_screen();
acquire_screen();

textout_ex( screen, font, " ", x, y+9, makecol( 0, 0, 0), makecol( 0, 0, 0) );
if (key[KEY_RIGHT] && x < 420) ++x;
else if (key[KEY_LEFT]&& x > 0) --x;
else if (key[KEY_UP]&& y > 0) ----y;
else
allegro_message("An error has occured");

if (seconds>=0 && y < 420)
++y;
else
allegro_message ("An error has occured, and program must be quit. Please, inform us of this problem.");

textout_ex( screen, font, "b", x, y+10, makecol( 255, 0, 0), makecol( 0, 0, 0) );

release_screen();

rest(50);

}

return 0;
}

END_OF_MAIN();

Reedbeta
02-07-2007, 07:46 AM
Hi, please use [ code ] [ /code ] tags for posting code samples.

erroroccured
02-12-2007, 08:34 AM
Updatet.

#include <allegro.h>

int x = 10;
int y = 10;
int fall = 0;

int main(){

allegro_init();
install_keyboard();
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

while ( !key[KEY_ESC] ){

clear_keybuf();

acquire_screen();

textout_ex( screen, font, " ", x, y, makecol( 0, 0, 0), makecol( 0, 0, 0) );

if (key[KEY_UP]&& y > 0) ----y;
else if (key[KEY_RIGHT]&& x < 420) ++x;
else if (key[KEY_LEFT]&& x > 0) --x;
time_t seconds;
time(&seconds);
if (fall==0 && y < 420)
++y;
else
++fall;

textout_ex( screen, font, "@", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );

release_screen();

rest(50);

}

return 0;

}
END_OF_MAIN();

SamuraiCrow
02-12-2007, 10:21 AM
For one thing you have too many minuses in front of y in your predecrement of y (unless you are trying to decrement twice which should be notated y-=2; ). For another thing you haven't used many curly braces on your if...else constructs. Lastly you haven't got a separate subroutine to process your key-codes and you don't have the code in main indented correctly.

Clean up your syntax first before posting here. These are simple mistakes that any beginner should be able to see without posting to the forum. I think your problem isn't with Allegro, it's with C programming itself.

erroroccured
02-15-2007, 10:35 AM
Samuraicrow, please note, that your 21 years older than me (if your age is what you say it is). And I think, that even if you started at the same age, You didn`t take as complex language as C/C++ to your starting language. Anyway, I got it working myself, but here is the unfixed code:

#include <allegro.h>

int x = 10;
int y = 10;
int fall = 0;

int main(){

allegro_init();
install_keyboard();
set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0);

while ( !key[KEY_ESC] ){

clear_keybuf();

acquire_screen();

textout_ex( screen, font, " ", x, y, makecol( 0, 0, 0), makecol( 0, 0, 0) );

if (key[KEY_UP]&& y > 0){
y-=2;
}
else if (key[KEY_RIGHT]&& x < 420){
++x;
}
else if (key[KEY_LEFT]&& x > 0){
--x;
}
else if (key[KEY_DOWN]&& fall > 0){
--fall;
}
while (fall==0){
if (y < 420){
++y;
}
else {
++fall;
}
}

textout_ex( screen, font, "@", x, y, makecol( 255, 0, 0), makecol( 0, 0, 0) );

release_screen();

rest(50);

}

return 0;

}
END_OF_MAIN();

SamuraiCrow
02-16-2007, 10:04 AM
I started programming in BASIC on my Commodore 64 when I was 9. I'm glad that I didn't start with C (C++ wasn't even invented yet) because then I would have missed out on the opportunity to learn Assembly language to optimize my code in junior-high.

You're really doing ok for what you've started with. I'm sorry I snapped at you.

What books and websites are you reading to get started?

erroroccured
02-19-2007, 05:02 AM
Well, for the websites, I use www.cppgameprogramming.com and www.cprogramming.com. And I use any book I can find.

SamuraiCrow
02-23-2007, 09:17 AM
I guess that's ok. I think you should either stick to C++ and drop C or switch to another object-oriented language. You can always fall-back to C after you've learned C++. There is one series of free books you can download that will teach C++ in a parallel fashion so that you can learn Python or Java if you want to on http://www.greenteapress.com/ .

erroroccured
03-07-2007, 05:59 AM
Ok. Thank you, very much. Maybe I will stick to C++ only, then.