//JTBlocks 1.10. Windows version //Written by Justin Todd (Gameprogrammer@excite.com) //Enjoy my fully commented code :) #define TOPSPEED 10 //Maximum speed in pixels that the paddle can travel #define FRICTION 0.98 //The amount of friction applied to the paddle. (This is what gives it that sliding effect) #define ROWS 5 //The number of rows of blocks #define COLUMNS 20 //The number of columns of blocks #define BLOCKSIZE 32 //The size of the blocks in pixels #include //The allegro graphics routines #include //sprintf() #include //time() //FUNCTION PROTOTYPES void incSpeedCtr(void); int Check_Win(void); void initialize(void); void gameInit(void); void processKey(void); void updateObjects(void); void updateGraphics(void); void deInitialize(void); //GLOBALS volatile int speedCtr=0; //Variable. Allows game to run at uniform rate regardless of computers speed BITMAP *virtScreen; //The buffer screen. Used for smooth animation int blockField[COLUMNS][ROWS]; //An array to hold the state of the blocks, (1 = block is present, 0 = block is not present) int points=0; //Holds the amount of points the player currently has char string[35]; //String to hold the score struct BALL{ //Holds all information dealing with the bouncing ball int XPos; //The x position of the ball int YPos; //The y position of the ball int XVelocity; //The horizontal velocity (speed) int YVelocity; //The vertical velocity (speed) int Radius; //The radius of the ball int Colour; //The color of the ball }Ball={320,240,1,2,7,10}; //Initialize the ball with these values struct PADDLE{ //Holds all information about the paddle float Position; //The horizontal positon of the center of the paddle float Velocity; //The speed of the paddle int Height; //The height of the paddle int Width; //The width of the paddle int Colour; //The color of the paddle }Paddle={320,0,10,100,7}; //Initialize the paddle with the following values void initialize(void){ //This function does all the initializations: changes to graphics mode, installs keyboard... allegro_init(); //Initialize Allegro install_keyboard(); //Install the Allegro keyboard routines install_timer(); //Install the timer. set_gfx_mode(GFX_DIRECTX,640,480,0,0); //Set the graphics mode to 640x480 (BITMAP *)virtScreen=create_bitmap(SCREEN_W,SCREEN_H); //Create a bitmap as big as the current screen resolution srand(time(NULL)); //Create a random seed text_mode(-1); } void gameInit(void){ //This function is used to reset all the game variables. int x,y; //counters for(y=0;yTOPSPEED) Paddle.Velocity=TOPSPEED; //Don't let the paddle go faster than its top speed else if(Paddle.Velocity<-TOPSPEED) Paddle.Velocity=-TOPSPEED; //Don't let the paddle go faster than its top speed if(Paddle.Position<0){ //If the paddle leaves the screen Paddle.Position=0; //Put the paddle back on the screen Paddle.Velocity*=-1; //Bounce it off the wall } else if((Paddle.Position+Paddle.Width)>SCREEN_W){ //If the paddle leaves the screen to the right Paddle.Position=SCREEN_W-Paddle.Width; //Put the paddle back on the screen Paddle.Velocity*=-1; //Bounce the screen off the wall } //BALL Ball.XPos+=Ball.XVelocity; //Move the ball horizontally based on its velocity Ball.YPos+=Ball.YVelocity; //Move the ball vertically based on its vertical speed if(Ball.YPos-Ball.Radius<=0) Ball.YVelocity*=-1; //If the ball attempts to leave the screen at the top, bounce it off the roof if(Ball.XPos-Ball.Radius<=0 || Ball.XPos+Ball.Radius>=SCREEN_W) //If the ball attempts to leave the screen on either the left or the right... Ball.XVelocity*=-1; //...bounce it in the opposite direction if(Ball.YPos+Ball.Radius>=SCREEN_H){ //If the ball hits the floor... gameInit(); //...then reset the game points=0; //And reset the points } //(Below) If the ball hits or drops below the surface of the paddle... if((Ball.YPos+Ball.Radius>=SCREEN_H-Paddle.Height)&&(Ball.XPos-Ball.RadiusPaddle.Position)){ Ball.YVelocity*=-1; //...Then bounce it off Ball.YPos=SCREEN_H-Paddle.Height-Ball.Radius; //and make sure the ball isn't stuck underneath the paddle (for example, the ball is hit with the side of the paddle) } //Check for a block/ball collision. If the ball comes in contact with a block... //Note, x<<5 is simply x*32, shifting bits is just faster than multiplication for(y=0;y(x<<5))){ blockField[x][y]=0; //Destroy the block Ball.YVelocity=1+rand()%2; //Bounce the ball back down at a random speed points+=(ROWS-y)*5; //Give player points. The higher the row level, the higher the points break; //As soon as the ball destroys the block, allow it to destroy no others } sprintf(string,"Points: %d",points); //Copy the points into a string so it can be displayed with textout } void updateGraphics(void){ //Update all the graphics int x,y; //Counters textout((BITMAP *)virtScreen,font,string,0,SCREEN_H-30,0); //Display how many points the user has for(y=0;y0){ //Update game logic processKey(); //Process the user input updateObjects(); //Check collisions and move objects if(checkWin()) gameInit(); //If a win was detected, then reset the game --speedCtr; } updateGraphics(); //Update the graphics } deInitialize(); //Deinitialize everything return 0; //exit main } END_OF_MAIN() //end of main()