From: "K Morrison" Newsgroups: comp.os.msdos.djgpp Subject: Allegro game souce code Lines: 355 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 5.00.2919.6600 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 Message-ID: <3KC95.603$gho7.5570823@news.xtra.co.nz> Date: Sat, 8 Jul 2000 21:45:44 +1200 NNTP-Posting-Host: 210.55.34.81 X-Complaints-To: newsadmin AT xtra DOT co DOT nz X-Trace: news.xtra.co.nz 963049471 210.55.34.81 (Sat, 08 Jul 2000 21:44:31 NZST) NNTP-Posting-Date: Sat, 08 Jul 2000 21:44:31 NZST Organization: Xtra To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com I was working on my first program and I have decided nto stop work on the project and start on something more complex. Thus I am posting my source code so someone else might wish to borrow my code as crappy as it may be , but hey you gotta start somewhere right :P /* * Game program for the Allegro library, by Kevin Morrison. * * */ #include #include #include "allegro.h" // Globals and Types const int BOX_SIZE = 12; const int BOX_SPEED = 100; int BOX_COLOR = 3; enum DIRECTION { STOPPED, UP, DOWN, LEFT, RIGHT }; enum MAP_OBJ { EMPTY, WALL , PICKUP_01 }; MAP_OBJ map [50][50]; BITMAP *buffer; // ProtoTypes void init_map (void); void draw_arena (void); void draw_map (void); // QUAZAR class declaration class Quazar { public: // Constructors Quazar ():x(15), y(15), dx(1), dy(0), itsDirection(RIGHT), itsSpeed(BOX_SPEED){} Quazar (int x_position, int y_position, int speed, DIRECTION a_direction); ~Quazar (){} // Accessors void setx (int atx){ x = atx; } void sety (int aty){ y = aty; } void setxy (int atx, int aty){ x = atx; y = aty; } void setdirection (DIRECTION a_direction){ itsDirection = a_direction; } void setspeed (int speed){ itsSpeed = speed; } int getx (){ return x; } int gety (){ return y; } int getsx (){ return sx; } int getsy (){ return sy; } int getdx (){ return dx; } int getdy (){ return dy; } DIRECTION getdirection (){ return itsDirection; } int getspeed (){ return itsSpeed; } // Other Methods void draw (void); void change_direction (DIRECTION a_direction); void move (void); private: // Data int x, y; // map x,y coordinates int sx, sy; // relative screen coordinates of x, y int itsSpeed; // delay in milliseconds DIRECTION itsDirection; // direction object is travelling int dx, dy; // }; Quazar::Quazar (int x_pos, int y_pos, int speed, DIRECTION a_direction) { x = x_pos; y = y_pos; itsSpeed = speed; itsDirection = a_direction; dx = 0; dy = 0; } void Quazar::draw () { // convert map x y to screen coordinates. sx = (x + 2) * (BOX_SIZE + 1); sy = (y + 2) * (BOX_SIZE + 1); rectfill (buffer, sx, sy, sx + BOX_SIZE, sy + BOX_SIZE, BOX_COLOR); } void Quazar::change_direction (DIRECTION a_direction) { // making sure you cant move backwards if ((itsDirection == UP) && (a_direction == DOWN)) a_direction = UP; else if ((itsDirection == DOWN) && (a_direction == UP)) a_direction = DOWN; else if ((itsDirection == LEFT) && (a_direction == RIGHT)) a_direction = LEFT; else if ((itsDirection == RIGHT) && (a_direction == LEFT)) a_direction = RIGHT; // set new direction and // reset dx and dy given the direction itsDirection = a_direction; switch (itsDirection) { case UP: { dx = 0; dy = -1; } break; case DOWN: { dx = 0; dy = 1; } break; case LEFT: { dx = -1; dy = 0; } break; case RIGHT: { dx = 1; dy = 0; }; break; case STOPPED: { dx = 0; dy = 0; } default: break; } // end switch } void Quazar::move () { char debug_message[20]; DIRECTION newdirection; // decide which way to move if x y is about to hit wall // defaults to moving clock wise . if ( map[x+dx][y+dy] == WALL ) { if ( (itsDirection == UP) || (itsDirection == DOWN) ) { // newdirection = RIGHT; for (int lx = (x - 1); lx >= (x - 15); lx-- ) if ( (lx >= 0) && (map[lx][y] == WALL) ) { newdirection = RIGHT; break; } for (int rx = (x + 1); rx <= (x + 15); rx++ ) if ( (rx <= 50) && (map[rx][y] == WALL) ) { newdirection = LEFT; break; } change_direction (newdirection); } else if ( (itsDirection == RIGHT) || (itsDirection == LEFT) ) { if (map[x][y-1] == WALL) change_direction (DOWN); else if (map[x][y+1] == WALL) change_direction (UP); else change_direction (DOWN); } sprintf (debug_message, "on wall "); } // if walls surround x y then game over if ( (map[x-1][y] == WALL) && (map[x+1][y] == WALL) && (map[x][y-1] == WALL) && (map[x][y+1] == WALL) ) { itsDirection = STOPPED; dx = 0; dy = 0; BOX_COLOR = 8; } // increase x and y by the direction x = x + dx; y = y + dy; // check to see if collision with wall if ( map[x][y] == EMPTY ) sprintf (debug_message, "empty "); // check to see if collision with pick up item if ( map[x][y] == PICKUP_01 ) sprintf (debug_message, "pickup 01 "); // display message.. for debug purposes textout (buffer, font, debug_message, 10, 400, 1); // update map with wall map[x][y] = WALL; } // MAIN PROGRAM CODE ******* int main (int argc, char **argv) { PALETTE pal; char buf[64]; allegro_init (); install_keyboard (); install_timer (); if (argc > 1) { if (set_gfx_mode (GFX_VGA, 320, 200, 0, 0) != 0) { printf ("error setting VGA 320x200x256\n"); return 1; } } else { if (set_gfx_mode (GFX_AUTODETECT, 640, 480, 0, 0) != 0) { printf ("error setting SVGA 640x480x256\nTry giving a parameter to set 320x200x256 mode\n"); return 1; } } // create palette pal = desktop_palette; pal[0].r = pal[0].g = pal[0].b = 0; // set 0 to black set_pallete (pal); // create bitmap to store screen output buffer = create_bitmap (SCREEN_W, SCREEN_H); clear (buffer); // Create Player Quazar Object Quazar player; // Init Map Array init_map (); draw_map (); // Draw Game Arena draw_arena (); // main keyboard input loop for (;;) { while (!(keypressed())) { // draw memory bitmap to screen rest (player.getspeed ()); player.move (); player.draw (); vsync (); blit (buffer, screen, 0, 0, 0, 0, SCREEN_W, SCREEN_H); // *** BEGIN DEBUG sprintf (buf, "speed[%i] dx[%i] dy[%i] x[%i] y[%i] sx[%i] sy[%i] ", player.getspeed (), player.getdx (), player.getdy (), player.getx (), player.gety (), player.getsx (), player.getsy ()); textout (buffer, font, buf, 10, 450, 17); // *** END DEBUG } // end of while loop // read in key pressed int a_key = readkey (); if ((a_key >> 8) == KEY_UP) player.change_direction (UP); else if ((a_key >> 8) == KEY_DOWN) player.change_direction (DOWN); else if ((a_key >> 8) == KEY_LEFT) player.change_direction (LEFT); else if ((a_key >> 8) == KEY_RIGHT) player.change_direction (RIGHT); else if ((a_key >> 8) == KEY_ESC) break; } // endof infinite for loop destroy_bitmap (buffer); return 0; } // end Main void init_map () { // set entire map to WALL for (int x = 0; x <= 40; x++) for (int y = 0; y <= 30; y++) map[x][y] = WALL; // set playing area to EMPTY for (int x = 1; x <= 39; x++) for (int y = 1; y <= 29; y++) map[x][y] = EMPTY; for (int i = 0; i <= 5; i++) map [(random() % 39) + 1][(random() % 29) + 1] = PICKUP_01; } void draw_map () { // displays map on screen for (int x = 0; x <= 50; x++) for (int y = 0; y <= 50; y++) { if (map[x][y] == WALL) rectfill (buffer, (x + 2) * (BOX_SIZE + 1), (y + 2) * (BOX_SIZE + 1), (x + 2) * (BOX_SIZE + 1) + BOX_SIZE, (y + 2) * (BOX_SIZE + 1) + BOX_SIZE, 2); else if (map[x][y] == PICKUP_01) triangle(buffer, (x + 2) * (BOX_SIZE + 1) + (BOX_SIZE/2), (y + 2) * (BOX_SIZE + 1), (x + 2) * (BOX_SIZE + 1), (y + 2) * (BOX_SIZE + 1) + BOX_SIZE, (x + 2) * (BOX_SIZE + 1) + BOX_SIZE, (y + 2) * (BOX_SIZE + 1) + BOX_SIZE, 3); } } void draw_arena () { // textout (buffer, font, "Quazar 2 (c) 2000 : Written By Kevin Morrison", 10, 10, 1); }