help on allegro

Started by
2 comments, last by 23yrold3yrold 17 years, 10 months ago
here's the deal, i can compile this pong code, but i cannot make it work, an error message pop out when i try to run it... what's the problem???
#include<allegro.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>

#define PAL_LENGTH 100
#define PAL_WIDTH 10
#define BALL_RAD 20
#define SCREENX 300
#define SCREENY 150

volatile int t=0;
int auxt=0;
int lastscore=1;
BITMAP* buffer=create_bitmap(SCREENX, SCREENY);

void tiempo(void);
void drawball(int, int);
void drawplayer(int, int);
int pos_ball_pal(int, int);

int main()
{
	int vb[2], pb[2], vp[2];		/*vb/pb=ball's speed/position */
	int score[2]={0 , 0};		/*p1y y p2y player's posittions*/
	int i, p1y=(SCREENY/2)-(PAL_LENGTH/2);	
	int p2y;
	p2y = p1y;
	srand(time(NULL));
	allegro_init();
	install_keyboard();			/*iniciando allegro*/
	install_timer();
	set_color_depth(8);
	set_gfx_mode(GFX_AUTODETECT, SCREENX, SCREENY, 0, 0);
	floodfill(screen, 0,0,0);
	while(!(key[KEY_F7]))
	{
		PALLETE paleta;
		BITMAP* intro=create_bitmap(SCREENX, SCREENY);
		textprintf_ex(intro, font, (SCREENX/2)-20, (SCREENY/2)-10, 12, -1, "PONG!!");
		blit(intro, screen, 0, 0, 0, 0, SCREENX, SCREENY);
		readkey();
		destroy_bitmap(intro);
		
		while(!(key[KEY_ESC]))
		{
                              LOCK_VARIABLE(t);
                              LOCK_FUNCTION(tiempo);
                              install_int(tiempo, 500);
                              if(t!=auxt)
                              {
                                    clear(buffer);
                                    if(key[KEY_UP])
                                         vp[1]-=4;       /*commands*/
                                    if(key[KEY_DOWN])
                                         vp[1]+=4;
                                     if(key[KEY_W])
                                         vp[2]-=4;                              
                                    if(key[KEY_S])
                          vp[2]+=4;                                             
                                         if(lastscore)
                                         {
                                                      pb[0]=SCREENX/2;
      /*reset position*/                              pb[1]=SCREENY/2;
                                                      for(i=0; i<2; i++)
                                                      {
                                                          vb=(rand()%12)-6;
                                                               if(vb>=0)
                                                                     vb=12;
                                                               else
                                                                      vb=-12;
                                                      }
                                                      lastscore=0;
                                                      rest(2000);
                                         }
                                        drawplayer(p2y, SCREENX-(PAL_WIDTH+5));
                                        drawplayer(p1y, 5);                                              drawball(pb[0], pb[1]);
/*solves when ball is near player's 1 edge*/if(pb[0]-BALL_RAD<=(PAL_WIDTH+5))
                                         {
                                                        switch(pos_ball_pal(pb[1], p1y))
                                                        {
                                                            case 1:
                                                                vb[0]*= (-1/2);
                                                                vb[1]*= -2;
                                                                break;
                                                            case 2:
                                                                vb[0]*=(-5/4);
                                                                vb[1]-=(5/4);
                                                                break;
                                                            case 3:
                                                                vb[0]*=(-3/2);
                                                                break;
                                                            case 4:
                                                                vb[0]*=(-5/4);
                                                                vb[1]+=(5/4);
                                                                break;
                                                            case 5:
                                                                vb[0]*=(-1/2);
                                                                vb[1]*=-2;
                                                                break;
                                                            case 0:
                                                                score[1]++;
                                                                lastscore++;
                                                                break;
                                                            default:
                                                                break;
                                                        }
                                         }
  /*resuelve encuentro con jug 2*/       if(pb[0]+BALL_RAD>=SCREENX-(PAL_WIDTH+5))                    
                                           {
                                                        switch(pos_ball_pal(pb[1], p2y))
                                                        {
                                                            case 1:
                                                               vb[0]*= (-1/2);
                                                               vb[1]*= -2;
                                                               break;
                                                            case 2:
                                                               vb[0]*=(-5/4);
                                                               vb[1]-=(5/4);
                                                               break;
                                                            case 3:
                                                               vb[0]*=(-3/2);
                                                               break;
                                                            case 4:
                                                               vb[0]*=(-5/4);
                                                               vb[1]+=(5/4);
                                                               break;
                                                            case 5:
                                                               vb[0]*=(-1/2);
                                                               vb[1]*=-2;
                                                               break;
                                                            case 0:
                                                               score[0]++;
                                                               lastscore++;
                                                               break;
                                                            default:
                                                               break;
                                                        }
                                         }
      /*resuelve rebotes laterales*/     if((pb[1]+BALL_RAD>=SCREENY)||(pb[1]-BALL_RAD<=0))
                                                        vb[1]*=-1;
                                         pb[0]+= vb[0];
                                         pb[1]+= vb[1];
                                         for (i=0; i<2; i++)
  /*detiene movimiento de las paletas*/         vp*=(-15/16);
                                         p1y+=vp[0];
                                         p2y+=vp[1];
                                         textprintf_ex(buffer, font,  (SCREENX/2)-10, 0, 15, -1, "%d - %d", score[1], score[0]);
                                         blit(buffer, screen, 0, 0, 0, 0, SCREENX, SCREENY);
                                         auxt = t;
                               } /*fin loop de tiempo*/
                               if((score[0]>=7)||(score[1]>=7))
                               {
                                           if(score[0]>score[1])
                                           {
                                                  clear(screen);
                                                  textprintf_ex(screen, font, (SCREENX/2)-20, (SCREENY/2)-10, 13, -1, "%s", "Jugador 1 Gana");
                                           }
                                           else
                                           {
                                                  clear(screen);
                                                  textprintf_ex(screen, font, (SCREENX/2)-20, (SCREENY/2)-10, 13, -1, "%s", "Jugador 2 Gana");
                                           }
                                           break;
                                   }          
                      }                            /* fin de loop ESC*/                                   

         }                               /*fin de loop F7*/
  allegro_exit();
  return 0;
}
END_OF_MAIN();

void tiempo(void)
{
     t++;
}

void drawball(int x, int y)
{
     circlefill(buffer, x, y, BALL_RAD, 5);
}

void drawplayer(int y, int x)
{
     rectfill(buffer, x, y, x+PAL_WIDTH, y+PAL_LENGTH, 15);
}

int pos_ball_pal(int by, int py)
{
    if((by+BALL_RAD>=py)&&(by-BALL_RAD<=py+PAL_LENGTH))
    {
             if(by<py+(PAL_LENGTH/7))
                                     return 1;
             else{if(by<py+(PAL_LENGTH*(3/7)))
                                     return 2;
             else{if(by<py+(PAL_LENGTH*(4/7)))
                                     return 3;
             else{if(by<py+(PAL_LENGTH*(6/7)))
                                     return 4;
             else
                                     return 5;
             }}}
    }
    else
                return 0;
}


[Edited by - 23yrold3yrold on May 23, 2006 7:52:18 PM]
Advertisement
Quote:Original post by rangua
... what's the problem???

There are several, actually.
- You didn't enclose the code in your post in [source] and [/source] tags for readability.
- You didn't specify which compiler you're using
- You didn't provide the contents of the error message that popped up.

Once you've given all the necessary information, chances are you'll get help right away.
{[JohnE, Chief Architect and Senior Programmer, Twilight Dragon Media{[+++{GCC/MinGW}+++{Code::Blocks IDE}+++{wxWidgets Cross-Platform Native UI Framework}+++
What's the error message? When does it uccor?

Please use [ source ] and [ /source ] tags around your code(no spaces, obviously). And please don't just dump code, even a pong game is alot of code to sift through. Have you tried debugging it yet and narrowed it down to a specific function or line? If you don't give us any other hints than there's an error when you run it, we aren't going to be able (or willing) to help much.

If you aren't familiar with your IDE's debugger, either get familiar with it(an excellent use of your time), or use simple printf()s or couts to determine on what line the program crashes. Eg: print out some text after you've initialized Allegro, if the text isn't in the file after the crash, then the crash was caused by initializing Allegro.

And a probably cause for an error is not having the correct .dll files in the folder with the .exe. You should have something like Allegro.dll in the same folder.
Source tags added. And yes, we need more info if you want help with this please.

Jesus saves ... the rest of you take 2d4 fire damage.

This topic is closed to new replies.

Advertisement