decna12255

help with pong.

Feb 21st, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.41 KB | None | 0 0
  1. below is my code for pong that i am currently working on but have come to a bit of a dead end i cant firgure out how to write collision for my code any advice or sample code would be nice have fun reading i tried to keep my code neat.
  2.  
  3. // SDL_App.cpp : Defines the entry point for our Project
  4. //
  5. //#include "stdafx.h"
  6. #include <Windows.h>
  7. #include "GL_Functions.h"
  8. #include "pctimer.h"
  9. #include <string>
  10. #include <cmath>
  11.  
  12. #include <crtdbg.h>
  13.  
  14. // theme of project is pong from a metroid perspective
  15. //{ make this a function later =D
  16.      
  17.     enum STATE
  18.     {
  19.       MAINMENU,  // will take user to main menu at start of game or during
  20.       SINGLEPLAYER,  // one player mode
  21.       MULTIPLAYER, // player vs player mode
  22.       HIGHSCORES, //lists high score
  23.       OPTIONS, // opens the options menu
  24.       QUIT // when user presses escape key program closes
  25.     };
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34. int main(int argc, char* argv[])
  35. {
  36.  
  37.     //Lets open the window and initialise opengl
  38.     InitGL(1024,768);
  39.     int background = LoadTexture ("./images/background.png");
  40.     int mball = LoadTexture ("./images/mball.png");
  41.     int leftpaddle_2 = LoadTexture ("./images/paddle_2.png");
  42.     int rightpaddle_1 = LoadTexture ("./images/paddle_1.png");
  43.     //int Menu = LoadTexture ("./images/MENU.png");
  44.  
  45.     //stuff to complete.......!!!!
  46.     //STATE currentstate;
  47.    
  48.     //bool singeplayer()
  49.    
  50.         //{
  51.             //currentState = MAINMENU
  52.             //while(bContinueGame)
  53.             //{
  54.                 //case
  55.     //}
  56.     float backgroundx = 0;
  57.     float backgroundy = 0;
  58.     float mballx = 492; // 'M' stand for metroid.
  59.     float mbally = 384;
  60.     float ballxspeed = 4;
  61.     float ballyspeed = 2;
  62.     float rightpaddle_1x = 990;
  63.     float rightpaddle_1y = 20;
  64.     float leftpaddle_2x = 15;
  65.     float leftpaddle_2y = 450;
  66.     float ballrside = mballx + 40;
  67.     float position = 0;
  68.     float paddle_width = 5;
  69.     float paddle_length = 15;
  70.     float ball_width = 2.5;
  71.     float ball_length = 5;
  72.    
  73.     //while true()
  74.     //{ make this a function later =D
  75.     //  enum STATE
  76.     //  MAINMENU  // will take user to main menu at start of game or during
  77.     //  SINGLEPLAYER   // one player mode
  78.     //  MULTIPLAYER  // player vs player mode
  79.     //  HIGHSCORES  //lists high score
  80.     //  OPTIONS // opens the options menu
  81.     //  QUIT  // when user selects 'quit' program closes
  82.     //}
  83.    
  84.  
  85.      do
  86.     {
  87.         //Clear the screen, so previous frames don't build up
  88.         ClearScreen();
  89.        
  90.         mballx = mballx + ballxspeed;
  91.         mbally = mbally + ballyspeed;
  92.        
  93.    
  94. //////////draws images to screen////////////////////////////////////////////////////
  95.         DrawSprite(background, backgroundx, backgroundy, 1024, 768);  //////////////
  96.         DrawLine(512,0,512,768);                                      //////////////
  97.         DrawSprite(mball, mballx, mbally, 40, 40);                    //////////////
  98.         DrawSprite(leftpaddle_2, leftpaddle_2x,  leftpaddle_2y, 20, 100); //////////
  99.         DrawSprite(rightpaddle_1, rightpaddle_1x, rightpaddle_1y, 20, 100); ////////
  100. ////////////////////////////////////////////////////////////////////////////////////
  101.        
  102.        
  103.         //highscores
  104.         //{
  105.         //  int iHighscore[6}
  106.         //  int iScore = 0
  107.         //  char scorestring[] = "score"
  108.         //  while (iscore < 6)
  109.         //  printf("Highscore: %i %i/n"), (iScore+1), iHighscore[iScore]);
  110.         //  ++iScore;
  111.         //   for{
  112.         //     (int iHighscore = 0;, iHighscore < 10, iHighscore++)
  113.         //  printf(
  114.         //  }
  115.         //}
  116.  
  117.  
  118.         // ball speed/control
  119.         //
  120.         if (mballx > 984)  
  121.         {
  122.             mballx = 492;
  123.             mbally = 384;
  124.             ballxspeed *= -1;
  125.         }
  126.  
  127.         if ( mballx < 0)   
  128.         {
  129.             mballx = 492;
  130.             mbally = 384;
  131.             ballxspeed *= -1;
  132.         }
  133.        
  134.         if (mbally > 1024 || mbally < 0)
  135.                 ballyspeed *= -.5;
  136.  
  137.         // controls for players one and two
  138.         // player one's controls ( 'w' and 's')
  139.         // player two's controls ( 'up' and 'down')
  140.  
  141.        
  142.         if (IsKeyDown(KEY_UP))
  143.         {
  144.             rightpaddle_1y = rightpaddle_1y -5;
  145.             if( rightpaddle_1y < 0 )
  146.             {
  147.                 rightpaddle_1y = 0;
  148.             }
  149.         }
  150.  
  151.         if (IsKeyDown(KEY_DOWN) && rightpaddle_1 < 668)
  152.             rightpaddle_1y = rightpaddle_1y +5;
  153.  
  154.         if (IsKeyDown('w') && leftpaddle_2 > 0)
  155.             leftpaddle_2y = leftpaddle_2y -5;
  156.        
  157.         if (IsKeyDown('s') && leftpaddle_2 < 668)
  158.             leftpaddle_2y = leftpaddle_2y +5;
  159.        
  160.         // closes program down atfer user presses escape!
  161.         if (IsKeyDown(KEY_ESCAPE))
  162.             CloseDown();
  163.    
  164.  
  165.         //limit paddles so that stay with in boundaries placed
  166.         // well they would if i could program
  167.         {
  168.         if(leftpaddle_2y < 0)
  169.             leftpaddle_2y = 0;
  170.          
  171.         if(leftpaddle_2y + paddle_width > 668)
  172.             leftpaddle_2y = 668 - paddle_width;
  173.        
  174.         if(rightpaddle_1y + paddle_width > 668)
  175.            rightpaddle_1y = 668 - paddle_width;  
  176.         }
  177.         //ball impact values below
  178.         //makes ball go bouncy bouncy
  179.         //ball collision should be here....
  180.         {
  181.         if(ball_length + ball_width > paddle_width + paddle_length)
  182.         mballx,mbally = 0;
  183.            
  184.         // these do stuff
  185.         // without them game crashes....
  186.         if(mballx > rightpaddle_1x + rightpaddle_1y)
  187.        
  188.         if(mballx = leftpaddle_2x - position && leftpaddle_2y > ball_width)
  189.            
  190.         if(mbally = rightpaddle_1x + position && rightpaddle_1x > ball_width)
  191.         return(0);
  192.         }
  193.         //Stop it from running too fast! Sleep ZZzzz
  194.         Sleep(5);
  195.        
  196.         } while (FrameworkUpdate()); //Do some secret stuff,
  197.  
  198.        
  199.         FreeTexture(leftpaddle_2);
  200.         FreeTexture(rightpaddle_1);
  201.         FreeTexture(background);
  202.         FreeTexture(mball);
  203.  
  204.  
  205.  
  206.         //Close down
  207.         CloseDown();
  208.  
  209.         _CrtDumpMemoryLeaks();
  210.  
  211.         //Quit!
  212.         return 0;
  213.  }
Advertisement
Add Comment
Please, Sign In to add comment