Jump to content


Allegro problem...


8 replies to this topic

#1 erroroccured

    New Member

  • Members
  • Pip
  • 5 posts

Posted 07 February 2007 - 02:00 PM

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(); 


#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4979 posts
  • LocationBellevue, WA

Posted 07 February 2007 - 03:46 PM

Hi, please use [ code ] [ /code ] tags for posting code samples.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 erroroccured

    New Member

  • Members
  • Pip
  • 5 posts

Posted 12 February 2007 - 04:34 PM

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(); 


#4 SamuraiCrow

    Senior Member

  • Members
  • PipPipPipPip
  • 459 posts

Posted 12 February 2007 - 06:21 PM

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.

#5 erroroccured

    New Member

  • Members
  • Pip
  • 5 posts

Posted 15 February 2007 - 06:35 PM

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(); 


#6 SamuraiCrow

    Senior Member

  • Members
  • PipPipPipPip
  • 459 posts

Posted 16 February 2007 - 06:04 PM

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?

#7 erroroccured

    New Member

  • Members
  • Pip
  • 5 posts

Posted 19 February 2007 - 01:02 PM

Well, for the websites, I use www.cppgameprogramming.com and www.cprogramming.com. And I use any book I can find.

#8 SamuraiCrow

    Senior Member

  • Members
  • PipPipPipPip
  • 459 posts

Posted 23 February 2007 - 05:17 PM

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/ .

#9 erroroccured

    New Member

  • Members
  • Pip
  • 5 posts

Posted 07 March 2007 - 01:59 PM

Ok. Thank you, very much. Maybe I will stick to C++ only, then.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users