View Single Post
  #6 (permalink)  
Old 07-09-2008, 08:02 PM
Aereshaa's Avatar   
Aereshaa Aereshaa is offline
Guru
 
Join Date: Apr 2008
Posts: 498
Rep Power: 9
Aereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the roughAereshaa is a jewel in the rough
Default Re: Allegro Game Library

Okay, that's pretty much the basic stuff, for extra features and lists of functions see here.
Now, I'll just share some examples:
Aereshaa's Pong Reloaded. Undefine reloaded for classic pong.
Code:
#include "stdio.h"
#include "unistd.h"
#include "time.h"
#include "allegro.h"
#include "stdlib.h"
#include "math.h"
#define scrH 128
#define scrW 256
#define color int
#define triple long double
#define reloaded
typedef struct MASS_ {
 int xpos;
 int ypos;
 double xspd;
 double yspd;
} MASS;
#define mass MASS *


int main(int argc, char** argv){
 allegro_init();//setup of graphics & stuff.
 install_keyboard();
 install_mouse();
 set_color_depth(24);
 set_gfx_mode(GFX_AUTODETECT_WINDOWED, scrW * 2 , scrH * 2 ,0,0);
 BITMAP *buffer = create_bitmap(scrW, scrH);//will speed things up.
 MASS P1 = {64,64,0.0,0.0};
 MASS P2 = {192,64,0.0,0.0};
 MASS BL = {128,64,-2.0,0.0};
 char P1_size = 8;
 char P2_size = 8;
#ifdef reloaded
 long P1_timeanom_pow = 0;
 long P2_timeanom_pow = 0;
 long P1_gravanom_pow = 0;
 long P2_gravanom_pow = 0;
 long P1_gravanom_dur = 0;
 long P2_gravanom_dur = 0;
 int gravdir = 0;
 long P1_reflanom_pow = 0;
 long P2_reflanom_pow = 0;
#endif
 int P1_score = 0;
 int P2_score = 0;
 int winner = 0;
 color white = makecol24(255,255,255);
 color grey = makecol24(127,127,127);
 color green = makecol24(0,255,0);
 color red = makecol24(255,0,0);
 color blue = makecol24(0,0,255);
 color cyan = makecol24(0,255,255);
 int show = 0;
 while(!key[KEY_G] && !key[KEY_ESC]){
  START:
  clear(buffer);
  textprintf_centre_ex(buffer,font,scrW / 2,24,blue,-1,"AERESHAA\'S");
  textprintf_centre_ex(buffer,font,scrW / 2 + 1,24,cyan,-1,"AERESHAA\'S");
#ifdef reloaded
  textprintf_centre_ex(buffer,font,scrW / 2,36,white,-1,"\"PONG RELOADED\"");
#else
  textprintf_centre_ex(buffer,font,scrW / 2,36,white,-1,"\"PONG\"");
#endif
  if(time(0) % 2)textprintf_centre_ex(buffer,font,scrW / 2 + 1,48,grey,-1,"press G to continue");
  if(!(time(0) % 2) && show != 1)textprintf_centre_ex(buffer,font,scrW / 2 + 1,56,grey,-1,"press H to show controls");
  if(key[KEY_H])show = 1;
  if(show == 1){
   textprintf_centre_ex(buffer,font,scrW / 2 + 1,68,grey,-1,"P1: W = up, S = down");
#ifdef reloaded
   textprintf_centre_ex(buffer,font,scrW / 2 + 1,76,grey,-1,"anomalies: Z,X,C");
#endif
   textprintf_centre_ex(buffer,font,scrW / 2 + 1,86,grey,-1,"P2: ^ = up, v = down");
#ifdef reloaded
   textprintf_centre_ex(buffer,font,scrW / 2 + 1,94,grey,-1,"anomalies: I,O,P");
#endif
  }
  stretch_blit(buffer,screen,0,0,scrW,scrH,0,0,scrW*2,scrH*2);
 }
 while(!key[KEY_ESC]){
  /*               /TIMING PHASE/                    * /
   *   Timing is very important, so it will be
   *   handled by using the nanosleep() function.
   */
  usleep(25000);
#ifdef reloaded
  P1_timeanom_pow++;
  P2_timeanom_pow++;
  P1_reflanom_pow++;
  P2_reflanom_pow++;
  P1_gravanom_pow++;
  P2_gravanom_pow++;
  if(P2_gravanom_dur){P2_gravanom_dur--;gravdir = 2;}
  else if(P1_gravanom_dur){P1_gravanom_dur--;gravdir = 1;}
  else if((P1_gravanom_dur) && (P2_gravanom_dur)){P2_gravanom_dur--;P1_gravanom_dur--;gravdir = 0;}
  else{gravdir = 0;}
#endif
  /*                /CONTROL PHASE/                  * /
   *    Control phase begins here. Both players will
   *    have access to easy controls for warps, time
   *    anomalies, gravity introduction, etc.
   */
  if(key[KEY_S])P1.yspd += 0.5;
  if(key[KEY_W])P1.yspd -= 0.5;
  if(key[KEY_DOWN])P2.yspd += 0.5;
  if(key[KEY_UP])P2.yspd -= 0.5;
  if(key[KEY_Y]){ //reset
  P1_score = 0;
  P2_score = 0;
  P1 = (MASS) {64,64,0.0,0.0};
  P2 = (MASS) {192,64,0.0,0.0};
  BL = (MASS) {128,64,-2.0,0.0};
  winner = 0;
#ifdef reloaded
  P1_timeanom_pow = 0;
  P2_timeanom_pow = 0;
  P1_gravanom_pow = 0;
  P2_gravanom_pow = 0;
  P1_gravanom_dur = 0;
  P2_gravanom_dur = 0;
  gravdir = 0;
  P1_reflanom_pow = 0;
  P2_reflanom_pow = 0;
#endif
  goto START;
  }
#ifdef reloaded
  if(key[KEY_Z] && P1_timeanom_pow > 200){
   P1.yspd = 0 - P1.yspd;
   P2.yspd = 0 - P2.yspd;
   BL.yspd = 0 - BL.yspd;
   BL.xspd = 0 - BL.xspd;
   P1_timeanom_pow = 0;
  }
  if(key[KEY_I] && P2_timeanom_pow > 200){
   P1.yspd = 0 - P1.yspd;
   P2.yspd = 0 - P2.yspd;
   BL.yspd = 0 - BL.yspd;
   BL.xspd = 0 - BL.xspd;
   P2_timeanom_pow = 0;
  }
  if(key[KEY_X] && P1_gravanom_pow > 400){
   P1_gravanom_dur = 50;
   P1_gravanom_pow = 0;
  }
  if(key[KEY_O] && P2_gravanom_pow > 400){
   P2_gravanom_dur = 50;
   P2_gravanom_pow = 0;
  }
  if(key[KEY_P] && P2_reflanom_pow > 200){
   BL.yspd = 0 - BL.yspd;
   BL.xspd = 0 - BL.xspd;
   BL.ypos = (128 - BL.ypos);
   BL.xpos = (256 - BL.xpos);
   P2_reflanom_pow = 0;
  }
  if(key[KEY_C] && P1_reflanom_pow > 200){
   BL.yspd = 0 - BL.yspd;
   BL.xspd = 0 - BL.xspd;
   BL.ypos = (128 - BL.ypos);
   BL.xpos = (256 - BL.xpos);
   P1_reflanom_pow = 0;
  }
#endif
  /*                 /PHYSICS PHASE/                 * /
   *   This game is very physics-intensive, so I will
   *   use the simplest algorithms I know. Time is the
   *   most complicated part.
   */
   /*Player One*/
  P1.ypos += (int) P1.yspd;
  if(P1.ypos > scrH){//bounce off walls
   P1.yspd = 0.0 - P1.yspd;
   P1.ypos = scrH;}
  if(P1.ypos < 0){//bounce off walls
   P1.yspd = 0.0 + -P1.yspd;
   P1.ypos = 0;}
  P1.yspd += (P1.yspd > 0.0)?(-0.2):
                           (P1.yspd < 0.0)?(0.2):(0.0);
   /*Player Two*/
  P2.ypos += (int) P2.yspd; color grey = makecol24(127,127,127);
  if(P2.ypos > scrH){//bounce off walls
   P2.yspd = 0.0 - P2.yspd;
   P2.ypos = scrH;}
  if(P2.ypos < 0){//bounce off walls
   P2.yspd = 0.0 + -P2.yspd;
   P2.ypos = 0;}
  P2.yspd += (P2.yspd > 0.0)?(-0.2):
                           (P2.yspd < 0.0)?(0.2):(0.0);
   /*Ball*/
  BL.ypos += (int) BL.yspd;
  BL.xpos += (int) BL.xspd;
  if(BL.ypos > scrH){ //bounce off walls
   BL.yspd = 0.0 - BL.yspd;
   BL.ypos = scrH;}
  if(BL.ypos < 0){ //bounce off walls
   BL.yspd = 0.0 + -BL.yspd;
   BL.ypos = 0;}
  if(BL.xpos < P1.xpos + 2 && BL.xpos > P1.xpos - 5
                           && BL.ypos > P1.ypos - (P1_size + 2)
                           && BL.ypos < P1.ypos + (P1_size + 2)){
   BL.xspd = 0.0 + -BL.xspd; // bounce off paddle 1.
   BL.xpos = P1.xpos + 2;
   BL.yspd += P1.yspd;}

  if(BL.xpos > P2.xpos - 2 && BL.xpos < P2.xpos + 5
                           && BL.ypos > P2.ypos - (P2_size + 2)
                           && BL.ypos < P2.ypos + (P2_size + 2)){
   BL.xspd = 0.0 + -BL.xspd; // bounce off paddle 2.
   BL.xpos = P2.xpos - 2;
   BL.yspd += P2.yspd;}
  if(BL.xpos > scrW){//score for P1
   BL.xspd = -2.0;
   BL.yspd = 0.0;
   P1_score++;
   BL.ypos = 64;
   BL.xpos = scrW / 2;}
  if(BL.xpos < 0){//score for P2
   BL.xspd = 2.0;
   BL.yspd = 0.0;  if(winner)textprintf_centre_ex(buffer,font,scrW / 2,0,red,-1,"!P%d Wins!",winner);
   P2_score++;
   BL.xpos = scrW / 2;
   BL.ypos = 64;}
#ifdef reloaded
   BL.xspd += (gravdir == 1)?(0.1):(gravdir == 2)?(-0.1):(0.0);
#endif
  /*                   /GAME PHASE/                   */
  if(P1_score == 15){
   winner = 1;}
  if(P2_score == 15){
   winner = 2;}
  /*                  /DRAWING PHASE/                 * /
   *   Drawing phase begins here. All game objects
   *   must be drawn, unless mitigating circumstances
   *   arise. I will use primitives wherever possible.
   */
  clear(buffer);
  textprintf_ex(buffer,font,0,0,grey,-1,"%d",P1_score);
#ifdef reloaded
  if(P1_timeanom_pow > 200)textprintf_ex(buffer,font,0,8,grey,-1,"time anomaly");
  if(P1_reflanom_pow > 200)textprintf_ex(buffer,font,0,24,grey,-1,"reflect anomaly");
  if(P1_gravanom_pow > 300)textprintf_ex(buffer,font,0,16,grey,-1,"gravity anomaly");
  if(P1_gravanom_dur)textprintf_ex(buffer,font,0,16,green,-1,"gravity anomaly");
#endif
  textprintf_right_ex(buffer,font,scrW,0,grey,-1,"%d",P2_score);
#ifdef reloaded
  if(P2_timeanom_pow > 200)textprintf_right_ex(buffer,font,scrW,8,grey,-1,"time anomaly");
  if(P2_reflanom_pow > 200)textprintf_right_ex(buffer,font,scrW,24,grey,-1,"reflect anomaly");
  if(P2_gravanom_pow > 300)textprintf_right_ex(buffer,font,scrW,16,grey,-1,"gravity anomaly");
  if(P2_gravanom_dur)textprintf_right_ex(buffer,font,scrW,16,green,-1,"gravity anomaly");
#endif
  textprintf_centre_ex(buffer,font,scrW / 2, 64, grey, -1, "%.3f", BL.yspd / BL.xspd);
  line(buffer,P1.xpos, P1.ypos + P1_size, P1.xpos, P1.ypos - P1_size, white);
  line(buffer,P2.xpos, P2.ypos + P2_size, P2.xpos, P2.ypos - P2_size, white);
  circle(buffer, BL.xpos, BL.ypos, 2, white);
  if(winner)textprintf_centre_ex(buffer,font,scrW / 2,0,red,-1,"!P%d Wins!",winner);
  stretch_blit(buffer,screen,0,0,scrW,scrH,0,0,scrW*2,scrH*2);
 }
 destroy_bitmap(buffer);
}END_OF_MAIN();

...Will add more.
Reply With Quote