int pingpong() { buffer = create_bitmap( 640, 480); BITMAP *BGpong = load_bitmap("C:\\Sanchit\\Game Pack\\Sprites\\BGpong.bmp", NULL); samSong = load_sample("C:\\Sanchit\\Game Pack\\Sprites\\pingpong.wav"); play_sample(samSong, 128, 128, 1000, 1); setupGame(); while( !key[KEY_ESC]) { draw_sprite(buffer, BGpong, 0,0); textprintf(buffer,font,60,2,makecol(255,255,255), "P1=%d", count1); textprintf(buffer,font,540,2,makecol(255,255,255), "P2=%d", count2); p1Move(); p2Move(); moveBall(); checkWin(); if(scored==1) pingpong_score(); if(key[KEY_BACKSPACE]) { count1=0; count2=0; ball_x=320; ball_y=240; p1_x = 20; p1_y = 210; p2_x = 620; p2_y = 210; stop_sample(samSong); openscreen(); } } return 0; }
int main(){ allegro_init(); install_keyboard(); set_color_depth(16); set_gfx_mode( GFX_AUTODETECT, 640, 480, 0, 0); buffer = create_bitmap( 640, 480); setupGame(); while( !key[KEY_ESC]){ p1Move(); p2Move(); moveBall(); checkWin(); } return 0; }
int main(int argc, char **argv) { OSScreenInit(); OSReport("Screen initted\n"); ProcUIInit(&SaveCallback); /****************************> Globals <****************************/ struct pongGlobals myPongGlobals; //Flag for restarting the entire game. myPongGlobals.restart = 1; //scale of game myPongGlobals.scale=1; //Default locations for paddles and ball location and movement dx/dy myPongGlobals.p1X_default=40*myPongGlobals.scale; myPongGlobals.p2X_default=340*myPongGlobals.scale; myPongGlobals.ballX_default=200*myPongGlobals.scale; myPongGlobals.p1Y_default=150*myPongGlobals.scale; myPongGlobals.p2Y_default=150*myPongGlobals.scale; myPongGlobals.ballY_default=120*myPongGlobals.scale; //Sizes of objects myPongGlobals.p1X_size=20*myPongGlobals.scale; myPongGlobals.p1Y_size=60*myPongGlobals.scale; myPongGlobals.ballX_size=8*myPongGlobals.scale; myPongGlobals.ballY_size=8*myPongGlobals.scale; myPongGlobals.p2X_size=20*myPongGlobals.scale; myPongGlobals.p2Y_size=60*myPongGlobals.scale; //Boundry of play area (screen) myPongGlobals.xMinBoundry=0*myPongGlobals.scale; myPongGlobals.xMaxBoundry=400*myPongGlobals.scale; myPongGlobals.yMinBoundry=0*myPongGlobals.scale; myPongGlobals.yMaxBoundry=240*myPongGlobals.scale; myPongGlobals.winX=11*2*myPongGlobals.scale; myPongGlobals.winY=5*2*myPongGlobals.scale; myPongGlobals.score1X=13*2*myPongGlobals.scale; myPongGlobals.score2X=15*2*myPongGlobals.scale; myPongGlobals.score1Y=0*myPongGlobals.scale; myPongGlobals.score2Y=0*myPongGlobals.scale; //Game engine globals myPongGlobals.direction = 1; myPongGlobals.button = 0; myPongGlobals.paddleColorR=0xFF; myPongGlobals.paddleColorG=0x00; myPongGlobals.paddleColorB=0x00; myPongGlobals.ballColorR=0x00; myPongGlobals.ballColorG=0xFF; myPongGlobals.ballColorB=0x00; myPongGlobals.ballTrailColorR=0x00; myPongGlobals.ballTrailColorG=0x00; myPongGlobals.ballTrailColorB=0xFF; myPongGlobals.backgroundColorR=0x00; myPongGlobals.backgroundColorG=0x00; myPongGlobals.backgroundColorB=0x00; myPongGlobals.count = 0; //Keep track of score myPongGlobals.score1 = 0; myPongGlobals.score2 = 0; myPongGlobals.scoreWin = 9; //Game engine globals myPongGlobals.direction = 1; myPongGlobals.button = 0; myPongGlobals.paddleColorR=0xFF; myPongGlobals.paddleColorG=0x00; myPongGlobals.paddleColorB=0x00; myPongGlobals.ballColorR=0x00; myPongGlobals.ballColorG=0xFF; myPongGlobals.ballColorB=0x00; myPongGlobals.ballTrailColorR=0x00; myPongGlobals.ballTrailColorG=0x00; myPongGlobals.ballTrailColorB=0xFF; myPongGlobals.backgroundColorR=0x00; myPongGlobals.backgroundColorG=0x00; myPongGlobals.backgroundColorB=0x00; myPongGlobals.count = 0; //Keep track of score myPongGlobals.score1 = 0; myPongGlobals.scoreWin = 9; //Used for collision myPongGlobals.flag = 0; //Flag to determine if p1 should be rendered along with p1's movement direction myPongGlobals.renderP1Flag = 0; //Flags for render states myPongGlobals.renderResetFlag = 0; myPongGlobals.renderBallFlag = 0; myPongGlobals.renderWinFlag = 0; myPongGlobals.renderScoreFlag = 0; OSReport("Globals initialized\n"); /****************************> VPAD Loop <****************************/ int error; VPADStatus vpad_data; while (AppRunning()) { if(!initialized) continue; VPADRead(0, &vpad_data, 1, &error); //Get the status of the gamepad myPongGlobals.button = vpad_data.hold; //If the game has been restarted, reset the game (we do this one time in the beginning to set everything up) if (myPongGlobals.restart == 1) { OSReport("Game reset\n"); reset(&myPongGlobals); myPongGlobals.restart = 0; } //Set old positions. updatePosition(&myPongGlobals); //Update location of player1 and 2 paddles p1Move(&myPongGlobals); p2Move(&myPongGlobals); //Update location of the ball moveBall(&myPongGlobals); //Check if their are any collisions between the ball and the paddles. checkCollision(&myPongGlobals); //Render the scene myPongGlobals.renderBallFlag = 1; render(&myPongGlobals); //Increment the counter (used for physicals calcuations) myPongGlobals.count+=1; } return 0; }
void _entryPoint() { /****************************> Get Handles <****************************/ //Get a handle to coreinit.rpl unsigned int coreinit_handle; OSDynLoad_Acquire("coreinit.rpl", &coreinit_handle); //Get a handle to vpad.rpl */ unsigned int vpad_handle; OSDynLoad_Acquire("vpad.rpl", &vpad_handle); /****************************> External Prototypes <****************************/ //VPAD functions int(*VPADRead)(int controller, VPADData *buffer, unsigned int num, int *error); //OS functions void(*_Exit)(); /****************************> Exports <****************************/ //VPAD functions OSDynLoad_FindExport(vpad_handle, 0, "VPADRead", &VPADRead); //OS functions OSDynLoad_FindExport(coreinit_handle, 0, "_Exit", &_Exit); /****************************> Globals <****************************/ struct pongGlobals myPongGlobals; //Flag for restarting the entire game. myPongGlobals.restart = 1; //scale of game myPongGlobals.scale=1; //Default locations for paddles and ball location and movement dx/dy myPongGlobals.p1X_default=40*myPongGlobals.scale; myPongGlobals.p2X_default=340*myPongGlobals.scale; myPongGlobals.ballX_default=200*myPongGlobals.scale; myPongGlobals.p1Y_default=150*myPongGlobals.scale; myPongGlobals.p2Y_default=150*myPongGlobals.scale; myPongGlobals.ballY_default=120*myPongGlobals.scale; //Sizes of objects myPongGlobals.p1X_size=20*myPongGlobals.scale; myPongGlobals.p1Y_size=60*myPongGlobals.scale; myPongGlobals.ballX_size=8*myPongGlobals.scale; myPongGlobals.ballY_size=8*myPongGlobals.scale; myPongGlobals.p2X_size=20*myPongGlobals.scale; myPongGlobals.p2Y_size=60*myPongGlobals.scale; //Boundry of play area (screen) myPongGlobals.xMinBoundry=0*myPongGlobals.scale; myPongGlobals.xMaxBoundry=400*myPongGlobals.scale; myPongGlobals.yMinBoundry=0*myPongGlobals.scale; myPongGlobals.yMaxBoundry=240*myPongGlobals.scale; myPongGlobals.winX=11*2*myPongGlobals.scale; myPongGlobals.winY=5*2*myPongGlobals.scale; myPongGlobals.score1X=13*2*myPongGlobals.scale; myPongGlobals.score2X=15*2*myPongGlobals.scale; myPongGlobals.score1Y=0*myPongGlobals.scale; myPongGlobals.score2Y=0*myPongGlobals.scale; //Game engine globals myPongGlobals.direction = 1; myPongGlobals.button = 0; myPongGlobals.paddleColorR=0xFF; myPongGlobals.paddleColorG=0x00; myPongGlobals.paddleColorB=0x00; myPongGlobals.ballColorR=0x00; myPongGlobals.ballColorG=0xFF; myPongGlobals.ballColorB=0x00; myPongGlobals.ballTrailColorR=0x00; myPongGlobals.ballTrailColorG=0x00; myPongGlobals.ballTrailColorB=0xFF; myPongGlobals.backgroundColorR=0x00; myPongGlobals.backgroundColorG=0x00; myPongGlobals.backgroundColorB=0x00; myPongGlobals.count = 0; //Keep track of score myPongGlobals.score1 = 0; myPongGlobals.score2 = 0; myPongGlobals.scoreWin = 9; //Game engine globals myPongGlobals.direction = 1; myPongGlobals.button = 0; myPongGlobals.paddleColorR=0xFF; myPongGlobals.paddleColorG=0x00; myPongGlobals.paddleColorB=0x00; myPongGlobals.ballColorR=0x00; myPongGlobals.ballColorG=0xFF; myPongGlobals.ballColorB=0x00; myPongGlobals.ballTrailColorR=0x00; myPongGlobals.ballTrailColorG=0x00; myPongGlobals.ballTrailColorB=0xFF; myPongGlobals.backgroundColorR=0x00; myPongGlobals.backgroundColorG=0x00; myPongGlobals.backgroundColorB=0x00; myPongGlobals.count = 0; //Keep track of score myPongGlobals.score1 = 0; myPongGlobals.scoreWin = 9; //Used for collision myPongGlobals.flag = 0; //Flag to determine if p1 should be rendered along with p1's movement direction myPongGlobals.renderP1Flag = 0; //Flags for render states myPongGlobals.renderResetFlag = 0; myPongGlobals.renderBallFlag = 0; myPongGlobals.renderWinFlag = 0; myPongGlobals.renderScoreFlag = 0; /****************************> VPAD Loop <****************************/ int error; VPADData vpad_data; while (1) { VPADRead(0, &vpad_data, 1, &error); //Get the status of the gamepad myPongGlobals.button = vpad_data.btn_hold; //If the game has been restarted, reset the game (we do this one time in the beginning to set everything up) if (myPongGlobals.restart == 1) { reset(&myPongGlobals); myPongGlobals.restart = 0; } //Set old positions. updatePosition(&myPongGlobals); //Update location of player1 and 2 paddles p1Move(&myPongGlobals); p2Move(&myPongGlobals); //Update location of the ball moveBall(&myPongGlobals); //Check if their are any collisions between the ball and the paddles. checkCollision(&myPongGlobals); //Render the scene myPongGlobals.renderBallFlag = 1; render(&myPongGlobals); //Increment the counter (used for physicals calcuations) myPongGlobals.count+=1; //To exit the game if (myPongGlobals.button&BUTTON_HOME) { break; } } //WARNING: DO NOT CHANGE THIS. YOU MUST CLEAR THE FRAMEBUFFERS AND IMMEDIATELY CALL EXIT FROM THIS FUNCTION. RETURNING TO LOADER CAUSES FREEZE. int ii=0; for(ii;ii<2;ii++) { fillScreen(0,0,0,0); flipBuffers(); } _Exit(); }
void _entryPoint() { struct Services services; /****************************> Get Handles <****************************/ //Get a handle to coreinit.rpl OSDynLoad_Acquire("coreinit.rpl", &services.coreinit_handle); //Get a handle to vpad.rpl */ unsigned int vpad_handle; OSDynLoad_Acquire("vpad.rpl", &vpad_handle); /****************************> External Prototypes <****************************/ //VPAD functions int(*VPADRead)(int controller, VPADData *buffer, unsigned int num, int *error); //OS functions void(*_Exit)(); /****************************> Exports <****************************/ //VPAD functions OSDynLoad_FindExport(vpad_handle, 0, "VPADRead", &VPADRead); // Draw functions OSDynLoad_FindExport(services.coreinit_handle, 0, "OSScreenPutPixelEx", &services.OSScreenPutPixelEx); OSDynLoad_FindExport(services.coreinit_handle, 0, "DCFlushRange", &services.DCFlushRange); OSDynLoad_FindExport(services.coreinit_handle, 0, "OSScreenFlipBuffersEx", &services.OSScreenFlipBuffersEx); OSDynLoad_FindExport(services.coreinit_handle, 0, "OSScreenGetBufferSizeEx", &services.OSScreenGetBufferSizeEx); OSDynLoad_FindExport(services.coreinit_handle, 0, "OSScreenPutFontEx", &services.OSScreenPutFontEx); OSDynLoad_FindExport(services.coreinit_handle, 0, "OSScreenClearBufferEx", &services.OSScreenClearBufferEx); //OS functions OSDynLoad_FindExport(services.coreinit_handle, 0, "_Exit", &_Exit); cleanSlate(&services); /****************************> Globals <****************************/ struct SpaceGlobals mySpaceGlobals; //Flag for restarting the entire game. mySpaceGlobals.restart = 1; mySpaceGlobals.services = &services; // initial state is title screen mySpaceGlobals.state = 1; mySpaceGlobals.titleScreenRefresh = 1; //Flags for render states mySpaceGlobals.renderResetFlag = 0; mySpaceGlobals.menuChoice = 0; // 0 is play, 1 is password // setup the password list unsigned int pwSeed = 27; int x; for (x=0; x<100; x++) mySpaceGlobals.passwordList[x] = (int)(prand(&pwSeed)*100000); // set the starting time int64_t (*OSGetTime)(); OSDynLoad_FindExport(services.coreinit_handle, 0, "OSGetTime", &OSGetTime); mySpaceGlobals.seed = OSGetTime(); /****************************> VPAD Loop <****************************/ int error; VPADData vpad_data; // decompress compressed things into their arrays, final argument is the transparent color in their palette decompress_sprite(3061, 200, 100, compressed_title, mySpaceGlobals.title, 39); decompress_sprite(511, 36, 36, compressed_ship, mySpaceGlobals.orig_ship, 14); decompress_sprite(206, 23, 23, compressed_enemy, mySpaceGlobals.enemy, 9); // setup palette and transparent index mySpaceGlobals.curPalette = ship_palette; mySpaceGlobals.transIndex = 14; // initialize starfield for this game initStars(&mySpaceGlobals); mySpaceGlobals.invalid = 1; while (1) { VPADRead(0, &vpad_data, 1, &error); //Get the status of the gamepad mySpaceGlobals.button = vpad_data.btn_hold; mySpaceGlobals.rstick = vpad_data.rstick; mySpaceGlobals.lstick = vpad_data.lstick; mySpaceGlobals.touched = vpad_data.tpdata.touched; if (mySpaceGlobals.touched == 1) { mySpaceGlobals.touchX = ((vpad_data.tpdata.x / 9) - 11); mySpaceGlobals.touchY = ((3930 - vpad_data.tpdata.y) / 16); } if (mySpaceGlobals.restart == 1) { reset(&mySpaceGlobals); mySpaceGlobals.restart = 0; } if (mySpaceGlobals.state == 1) // title screen { displayTitle(&mySpaceGlobals); doMenuAction(&mySpaceGlobals); } else if (mySpaceGlobals.state == 2) // password screen { displayPasswordScreen(&mySpaceGlobals); doPasswordMenuAction(&mySpaceGlobals); } else if (mySpaceGlobals.state == 3) // pause screen { displayPause(&mySpaceGlobals); doMenuAction(&mySpaceGlobals); } else if (mySpaceGlobals.state == 4) // game over screen { displayGameOver(&mySpaceGlobals); doMenuAction(&mySpaceGlobals); } else // game play { //Update location of player1 and 2 paddles p1Move(&mySpaceGlobals); // perform any shooting p1Shoot(&mySpaceGlobals); // handle any collisions handleCollisions(&mySpaceGlobals); // do explosions handleExplosions(&mySpaceGlobals); // if we're out of lives, break if (mySpaceGlobals.lives <= 0 && mySpaceGlobals.state == 4) continue; // add any new enemies addNewEnemies(&mySpaceGlobals); //Render the scene render(&mySpaceGlobals); // check for pausing checkPause(&mySpaceGlobals); } //To exit the game if (mySpaceGlobals.button&BUTTON_HOME) { break; } } cleanSlate(mySpaceGlobals.services); _Exit(); }