Advertisement
Guest User

Arena

a guest
Apr 20th, 2013
684
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 30.78 KB | None | 0 0
  1. #include "SDL.h"
  2. #include <cstdlib>
  3. #include <ctime>
  4. //#include "SDL_image.h"
  5. #include <string>
  6. #include <conio.h>
  7. #include <winsock2.h>
  8. #include <iostream>
  9. #pragma comment(lib,"ws2_32.lib")
  10.  
  11. //Screen attributes
  12. const int SCREEN_WIDTH = 700;
  13. const int SCREEN_HEIGHT = 630;
  14. const int SCREEN_BPP = 32;
  15. float PLAYER_1_SPEED_X = 1;
  16. float PLAYER_1_SPEED_Y = .5;
  17. float PLAYER_2_SPEED_X = 1;
  18. float PLAYER_2_SPEED_Y = .5;
  19.  
  20. //The surfaces
  21. SDL_Surface *red = NULL;
  22. SDL_Surface *yellow = NULL;
  23. SDL_Surface *green = NULL;
  24. SDL_Surface *blue = NULL;
  25. SDL_Surface *Hero = NULL;
  26. SDL_Surface *hwall = NULL;
  27. SDL_Surface *hwall2 = NULL;
  28. SDL_Surface *vwall = NULL;
  29. SDL_Surface *vwall2 = NULL;
  30. SDL_Surface *period = NULL;
  31. SDL_Surface *One = NULL;
  32. SDL_Surface *Two = NULL;
  33. SDL_Surface *Three = NULL;
  34. SDL_Surface *Four = NULL;
  35. SDL_Surface *Five = NULL;
  36. SDL_Surface *Six = NULL;
  37. SDL_Surface *Seven = NULL;
  38. SDL_Surface *Eight = NULL;
  39. SDL_Surface *Nine = NULL;
  40. SDL_Surface *Zero = NULL;
  41. SDL_Surface *Blank = NULL;
  42. SDL_Surface *JoinHost = NULL;
  43. SDL_Surface *ServerConnect = NULL;
  44. SDL_Surface *IPConnectMessage = NULL;
  45. SDL_Surface *Player1 = NULL;
  46. SDL_Surface *Player_2 = NULL;
  47. SDL_Surface *background = NULL;
  48. SDL_Surface *gbackground = NULL;
  49. SDL_Surface *screen = NULL;
  50. SDL_Event event;
  51.  
  52. int IPpositionX = 50;
  53. int IPpositionY = 245;
  54. int BorderX [32] = {0,100,200,300,400,500,600,700,0,100,200,250,450,500,600,700,0,0,0,0,0,0,0,0,780,780,780,780,780,780,780,780};
  55. int BorderY [32] = {780,780,780,780,780,780,780,780,0,0,0,0,0,0,0,0,0,100,200,300,400,500,600,700,0,100,200,300,400,500,600,700};
  56. int WallX [87];
  57. int WallY [87];
  58. bool WallXY [87];
  59. bool BorderXY [32] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
  60. bool getInput(char *c)
  61. {
  62.     if (kbhit())
  63.     {
  64.          *c = getch();
  65.          return true; // Key Was Hit
  66.     }
  67.     return false; // No keys were pressed
  68. }
  69.  
  70. SDL_Surface *load_image( std::string filename )
  71. {
  72.     //The image that's loaded
  73.     SDL_Surface* loadedImage = NULL;
  74.    
  75.     //The optimized image that will be used
  76.     SDL_Surface* optimizedImage = NULL;
  77.    
  78.     //Load the image
  79.     loadedImage = SDL_LoadBMP( filename.c_str() );
  80.    
  81.     //If the image loaded
  82.     if( loadedImage != NULL )
  83.     {
  84.         //Create an optimized image
  85.         optimizedImage = SDL_DisplayFormat( loadedImage );
  86.        
  87.         //Free the old image
  88.         SDL_FreeSurface( loadedImage );
  89.     }
  90.    
  91.     //Return the optimized image
  92.     return optimizedImage;
  93. }
  94.  
  95. void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
  96. {
  97.     //Temporary rectangle to hold the offsets
  98.     SDL_Rect offset;
  99.    
  100.     //Get the offsets
  101.     offset.x = x;
  102.     offset.y = y;
  103.    
  104.     //Blit the surface
  105.     SDL_BlitSurface( source, clip, destination, &offset );
  106. }
  107.  
  108. void LevelGen()
  109. {
  110.     //clear screen
  111.     apply_surface(0,0,background,screen);
  112.     //room 1 bot walls
  113.     apply_surface(0,780,hwall,screen);
  114.     WallX [71] = 0;WallY [71] = 780; WallXY [71] = 0;
  115.     apply_surface(100,780,hwall,screen);
  116.     WallX [72] = 100;WallY [72] = 780; WallXY [72] = 0;
  117.     apply_surface(200,780,hwall,screen);
  118.     WallX [73] = 200;WallY [73] = 780; WallXY [73] = 0;
  119.     apply_surface(300,780,hwall,screen);
  120.     WallX [74] = 300;WallY [74] = 780; WallXY [74] = 0;
  121.     apply_surface(400,780,hwall,screen);
  122.     WallX [75] = 400;WallY [75] = 780; WallXY [75] = 0;
  123.     apply_surface(500,780,hwall,screen);
  124.     WallX [76] = 500;WallY [76] = 780; WallXY [76] = 0;
  125.     apply_surface(600,780,hwall,screen);
  126.     WallX [77] = 600;WallY [77] = 780; WallXY [77] = 0;
  127.     apply_surface(700,780,hwall,screen);
  128.     WallX [78] = 700;WallY [78] = 780; WallXY [78] = 0;
  129.     //room 1 top walls
  130.     apply_surface(0,0,hwall,screen);
  131.     WallX [79] = 0;WallY [79] = 0; WallXY [79] = 0;
  132.     apply_surface(100,0,hwall,screen);
  133.     WallX [80] = 100;WallY [80] = 0; WallXY [80] = 0;
  134.     apply_surface(200,0,hwall,screen);
  135.     WallX [81] = 200;WallY [81] = 0; WallXY [81] = 0;
  136.     apply_surface(250,0,hwall,screen);
  137.     WallX [82] = 250;WallY [82] = 0; WallXY [82] = 0;
  138.     apply_surface(450,0,hwall,screen);
  139.     WallX [83] = 450;WallY [83] = 0; WallXY [83] = 0;
  140.     apply_surface(500,0,hwall,screen);
  141.     WallX [84] = 500;WallY [84] = 0; WallXY [84] = 0;
  142.     apply_surface(600,0,hwall,screen);
  143.     WallX [85] = 600;WallY [85] = 0; WallXY [85] = 0;
  144.     apply_surface(700,0,hwall,screen);
  145.     WallX [86] = 700;WallY [86] = 0; WallXY [86] = 0;
  146.     //room 1 left walls
  147.     apply_surface(0,0,vwall,screen);
  148.     apply_surface(0,100,vwall,screen);
  149.     apply_surface(0,200,vwall,screen);
  150.     apply_surface(0,300,vwall,screen);
  151.     apply_surface(0,400,vwall,screen);
  152.     apply_surface(0,500,vwall,screen);
  153.     apply_surface(0,600,vwall,screen);
  154.     apply_surface(0,700,vwall,screen);
  155.     //room 1 right walls
  156.     apply_surface(780,0,vwall,screen);
  157.     apply_surface(780,100,vwall,screen);
  158.     apply_surface(780,200,vwall,screen);
  159.     apply_surface(780,300,vwall,screen);
  160.     apply_surface(780,400,vwall,screen);
  161.     apply_surface(780,500,vwall,screen);
  162.     apply_surface(780,600,vwall,screen);
  163.     apply_surface(780,700,vwall,screen);
  164.     //player
  165.     apply_surface(400,700,Hero,screen);
  166.  
  167.     //Set Walls
  168.     srand (time(NULL));
  169.     for (int y = 0; y < 70; y++)
  170.     {
  171.    
  172.         int rando = rand() % 2;
  173.         int randomvx = rand() % 800;
  174.         int randomvy = rand() % 800;
  175.         int randomhx = rand() % 800;
  176.         int randomhy = rand() % 800;
  177.         if (rando == 1)
  178.         {
  179.             apply_surface(randomvx, randomvy, vwall, screen);
  180.             WallX [y] = randomvx;
  181.             WallY [y] = randomvy;
  182.             WallXY [y] = 1;
  183.         }
  184.         else if (rando == 0)
  185.         {
  186.             apply_surface(randomhx, randomhy, hwall, screen);
  187.             WallX [y] = randomhx;
  188.             WallY [y] = randomhy;
  189.             WallXY [y] = 0;
  190.         }
  191.     }
  192.     for (int x = 0; x < 70; x++)
  193.     {
  194.         if (WallXY [x] == 1)
  195.         {
  196.             apply_surface( WallX [x], WallY [x], vwall, screen);
  197.         }
  198.  
  199.         if (WallXY [x] == 0)
  200.         {
  201.             apply_surface( WallX [x], WallY [x], hwall, screen);
  202.         }
  203.     }
  204.    
  205.     SDL_Flip(screen);
  206.     SDL_Delay(500);
  207. }
  208.  
  209. bool init()
  210. {
  211.     //Initialize all SDL subsystems
  212.     if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
  213.     {
  214.         return false;    
  215.     }
  216.    
  217.     //Set up the screen
  218.     screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
  219.    
  220.     //If there was an error in setting up the screen
  221.     if( screen == NULL )
  222.     {
  223.         return false;    
  224.     }
  225.  
  226.     //Set the window caption
  227.     SDL_WM_SetCaption( "Arena", NULL );
  228.    
  229.     //If everything initialized fine
  230.     return true;
  231. }
  232.  
  233. bool load_files()
  234. {
  235.     //Load the image
  236.   red = load_image("images/red.bmp");
  237.   blue = load_image("images/blue.bmp");
  238.   green = load_image("images/green.bmp");
  239.   yellow = load_image("images/yellow.bmp");
  240.   Hero = load_image("images/Player.bmp");
  241.   hwall = load_image("images/HWall.bmp");
  242.   hwall2 = load_image("images/HWallBlack.bmp");
  243.   vwall = load_image("images/VWall.bmp");
  244.   vwall2 = load_image("images/VWallBlack.bmp");
  245.   period = load_image("images/period.bmp");
  246.   Zero = load_image("images/0.bmp");
  247.   One = load_image("images/1.bmp");
  248.   Two = load_image("images/2.bmp");
  249.   Three = load_image("images/3.bmp");
  250.   Four = load_image("images/4.bmp");
  251.   Five = load_image("images/5.bmp");
  252.   Six = load_image("images/6.bmp");
  253.   Seven = load_image("images/7.bmp");
  254.   Eight = load_image("images/8.bmp");
  255.   Nine = load_image("images/9.bmp");
  256.   Blank = load_image("images/blank.bmp");
  257.   JoinHost = load_image("images/JoinHost.bmp");
  258.   ServerConnect = load_image("images/ServerConnecting.bmp");
  259.   IPConnectMessage = load_image("images/IPConnectMessage.bmp");
  260.   Player1 = load_image("images/Player 1.bmp");
  261.   Player_2 = load_image("images/Player 2.bmp");
  262.   background = load_image("images/background.bmp");    
  263.   gbackground = load_image("images/game_background.bmp");
  264.  
  265.     return true;    
  266. }
  267.  
  268. void clean_up()
  269. {
  270.     //Quit SDL
  271.     SDL_Quit();    
  272. }
  273.  
  274. SOCKET Client()
  275. {
  276.     WSADATA WsaDat;
  277.     if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
  278.     {
  279.         std::cout<<"WSA Initialization failed!\r\n";
  280.         WSACleanup();
  281.         system("PAUSE");
  282.         return 0;
  283.     }
  284.                    
  285.     SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  286.     if(Socket==INVALID_SOCKET)
  287.     {
  288.         std::cout<<"Socket creation failed.\r\n";
  289.         WSACleanup();
  290.         system("PAUSE");
  291.         return 0;
  292.     }
  293.     apply_surface(150,175,IPConnectMessage,screen);
  294.     SDL_Flip(screen);
  295.      bool quit = false;
  296.      std::string str;
  297.       SDL_EnableUNICODE( SDL_ENABLE );    
  298.     //While the user hasn't quit
  299.     while( quit == false )
  300.     {//1
  301.          //While there's an event to handle
  302.         if( SDL_PollEvent( &event ))
  303.         {
  304.             //If a key was pressed
  305.             if( event.type == SDL_KEYDOWN )
  306.             {
  307.                  //If the key is a number
  308.             if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
  309.                 {
  310.                     //Append the character
  311.                     str += (char)event.key.keysym.unicode;
  312.                     if (event.key.keysym.unicode == (Uint16)48)
  313.                     {
  314.                         apply_surface(IPpositionX,IPpositionY,Zero,screen);
  315.                         IPpositionX += 50;
  316.                         SDL_Flip(screen);
  317.                     }
  318.                     else if (event.key.keysym.unicode == (Uint16)49)
  319.                     {
  320.                         apply_surface(IPpositionX,IPpositionY,One,screen);
  321.                         IPpositionX += 50;
  322.                         SDL_Flip(screen);
  323.                     }
  324.                     else if (event.key.keysym.unicode == (Uint16)50)
  325.                     {
  326.                         apply_surface(IPpositionX,IPpositionY,Two,screen);
  327.                         IPpositionX += 50;
  328.                         SDL_Flip(screen);
  329.                     }
  330.                     else if (event.key.keysym.unicode == (Uint16)51)
  331.                     {
  332.                         apply_surface(IPpositionX,IPpositionY,Three,screen);
  333.                         IPpositionX += 50;
  334.                         SDL_Flip(screen);
  335.                     }
  336.                     else if (event.key.keysym.unicode == (Uint16)52)
  337.                     {
  338.                         apply_surface(IPpositionX,IPpositionY,Four,screen);
  339.                         IPpositionX += 50;
  340.                         SDL_Flip(screen);
  341.                     }
  342.                     else if (event.key.keysym.unicode == (Uint16)53)
  343.                     {
  344.                         apply_surface(IPpositionX,IPpositionY,Five,screen);
  345.                         IPpositionX += 50;
  346.                         SDL_Flip(screen);
  347.                     }
  348.                     else if (event.key.keysym.unicode == (Uint16)54)
  349.                     {
  350.                         apply_surface(IPpositionX,IPpositionY,Six,screen);
  351.                         IPpositionX += 50;
  352.                         SDL_Flip(screen);
  353.                     }
  354.                     else if (event.key.keysym.unicode == (Uint16)55)
  355.                     {
  356.                         apply_surface(IPpositionX,IPpositionY,Seven,screen);
  357.                         IPpositionX += 50;
  358.                         SDL_Flip(screen);
  359.                     }
  360.                     else if (event.key.keysym.unicode == (Uint16)56)
  361.                     {
  362.                         apply_surface(IPpositionX,IPpositionY,Eight,screen);
  363.                         IPpositionX += 50;
  364.                         SDL_Flip(screen);
  365.                     }
  366.                     else if (event.key.keysym.unicode == (Uint16)57)
  367.                     {
  368.                             apply_surface(IPpositionX,IPpositionY,Nine,screen);
  369.                         IPpositionX += 50;
  370.                         SDL_Flip(screen);
  371.                     }
  372.                 }
  373.                     if ( ( event.key.keysym.unicode == (Uint16)46 ))
  374.                         {
  375.                             //Append the character
  376.                             str += (char)event.key.keysym.unicode;
  377.                             apply_surface(IPpositionX,IPpositionY,period,screen);
  378.                             IPpositionX += 50;
  379.                             SDL_Flip(screen);
  380.                         }
  381.                     //If backspace was pressed and the string isn't blank
  382.                     if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
  383.                     {
  384.                      //Remove a character from the end
  385.                      str.erase( str.length() - 1 );
  386.                      IPpositionX -= 50;
  387.                      apply_surface(IPpositionX,IPpositionY,Blank,screen);
  388.                      SDL_Flip(screen);
  389.                     }
  390.                     if ( ( event.key.keysym.sym == (Uint16)13))
  391.                     {
  392.                         quit = true;
  393.                     }
  394.         }
  395.  
  396.             //If the user has Xed out the window
  397.             if( event.type == SDL_QUIT )
  398.             {
  399.                 //Quit the program
  400.                  clean_up();
  401.                  return 0;
  402.             }
  403.         }
  404.     }//1
  405.     char IP[15];
  406.     strcpy(IP,str.c_str());
  407.     // Resolve IP address for hostname
  408.     struct hostent *host;
  409.     if((host=gethostbyname(IP))==NULL)
  410.     {
  411.         //std::cout<<"Failed to resolve hostname.\r\n";
  412.         WSACleanup();
  413.         //system("PAUSE");
  414.         return 0;
  415.     }
  416.  
  417.     // Setup our socket address structure
  418.     SOCKADDR_IN SockAddr;
  419.     SockAddr.sin_port=htons(8888);
  420.     SockAddr.sin_family=AF_INET;
  421.     SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);
  422.  
  423.     // Attempt to connect to server
  424.     if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
  425.     {
  426.         //std::cout<<"Failed to establish connection with server\r\n";
  427.         WSACleanup();
  428.         //system("PAUSE");
  429.         return 0;
  430.     }
  431.     u_long iMode=1;
  432.     ioctlsocket(Socket,FIONBIO,&iMode);
  433.     return Socket;
  434. }
  435.  
  436. SOCKET Server()
  437. {
  438.     WSADATA WsaDat;
  439.     if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
  440.     {
  441.         std::cout<<"WSA Initialization failed!\r\n";
  442.         WSACleanup();
  443.         system("PAUSE");
  444.         return 0;
  445.     }
  446.                    
  447.     SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
  448.     if(Socket==INVALID_SOCKET)
  449.     {
  450.         std::cout<<"Socket creation failed.\r\n";
  451.         WSACleanup();
  452.         system("PAUSE");
  453.         return 0;
  454.     }
  455.                    
  456.     SOCKADDR_IN serverInf;
  457.     serverInf.sin_family=AF_INET;
  458.     serverInf.sin_addr.s_addr=INADDR_ANY;
  459.     serverInf.sin_port=htons(8888);
  460.                    
  461.     if(bind(Socket,(SOCKADDR*)(&serverInf),sizeof(serverInf))==SOCKET_ERROR)
  462.     {
  463.         std::cout<<"Unable to bind socket!\r\n";
  464.         WSACleanup();
  465.         system("PAUSE");
  466.         return 0;
  467.     }
  468.        
  469.     listen(Socket,1);
  470.                    
  471.     SOCKET TempSock=SOCKET_ERROR;
  472.     while(TempSock==SOCKET_ERROR)
  473.     {
  474.         //std::cout<<"Waiting for incoming connections...\r\n";
  475.         apply_surface(0,0,ServerConnect,screen);
  476.         SDL_Flip(screen);
  477.         TempSock=accept(Socket,NULL,NULL);
  478.     }
  479.                
  480.     // If iMode!=0, non-blocking mode is enabled.
  481.     u_long iMode=1;
  482.     ioctlsocket(Socket,FIONBIO,&iMode);
  483.                    
  484.     Socket=TempSock;
  485.  
  486.     //std::cout<<"Client connected!\r\n\r\n";
  487.     //std::cout<<"\a";
  488. return Socket;
  489. }
  490.  
  491. /////////////////////
  492. class Player
  493. {
  494.     private:
  495.     //The X and Y offsets of the dot
  496.    
  497.    
  498.     //The velocity of the dot
  499.     float xVel, yVel;
  500.    
  501.     public:
  502.     //Initializes the variables
  503.     Player();
  504.     float x, y;
  505.     //Takes key presses and adjusts the dot's velocity
  506.     void handle_input(SOCKET Socket);
  507.     void handle_input(int in);
  508.    
  509.     //Moves the dot
  510.     void move();
  511.    
  512.     //Shows the dot on the screen
  513.     void show();
  514. };
  515.  
  516. Player::Player()
  517. {
  518.     //SDL_Surface *Player = NULL;
  519.     //Player = load_image("images/Player.bmp");
  520.     //Initialize the offsets
  521.    
  522.     x = 10;
  523.     y = 40;
  524.    
  525.     //Initialize the velocity
  526.     xVel = 0;
  527.     yVel = 0;
  528. }
  529.  
  530. void Player::handle_input(SOCKET Socket)
  531. {
  532.     char output[1];
  533.     //If a key was pressed
  534.     if( event.type == SDL_KEYDOWN )
  535.     {
  536.         //Adjust the velocity
  537.         switch( event.key.keysym.sym ) 
  538.         {
  539.             case SDLK_UP: yVel -= PLAYER_1_SPEED_Y; memset(output,1,0); send(Socket,output,1,0); break;
  540.             case SDLK_DOWN: yVel += PLAYER_1_SPEED_Y; memset(output,2,0); send(Socket,output,1,0); break;
  541.             case SDLK_LEFT: xVel -= PLAYER_1_SPEED_X; memset(output,3,0); send(Socket,output,1,0); break;
  542.             case SDLK_RIGHT: xVel += PLAYER_1_SPEED_X; memset(output,4,0); send(Socket,output,1,0); break;    
  543.         }
  544.     }
  545.  
  546.      //If a key was released
  547.     else if( event.type == SDL_KEYUP )
  548.     {
  549.         //Adjust the velocity
  550.         switch( event.key.keysym.sym )
  551.         {
  552.             case SDLK_UP: yVel += PLAYER_1_SPEED_Y; memset(output,5,0); send(Socket,output,1,0); break;
  553.             case SDLK_DOWN: yVel -= PLAYER_1_SPEED_Y; memset(output,6,0); send(Socket,output,1,0); break;
  554.             case SDLK_LEFT: xVel += PLAYER_1_SPEED_X; memset(output,7,0); send(Socket,output,1,0); break;
  555.             case SDLK_RIGHT: xVel -= PLAYER_1_SPEED_X; memset(output,8,0); send(Socket,output,1,0); break;  
  556.         }        
  557.     }
  558. }
  559.  
  560. void Player::handle_input(int in)
  561. {
  562.     switch(in)
  563.     {
  564.             //keydown
  565.         //up
  566.         case 1: yVel -= PLAYER_1_SPEED_Y; break;
  567.         //down
  568.         case 2: yVel += PLAYER_1_SPEED_Y; break;
  569.         //left
  570.         case 3: xVel -= PLAYER_1_SPEED_X; break;
  571.         //right
  572.         case 4: xVel += PLAYER_1_SPEED_X; break;
  573.             //keyup
  574.         //up
  575.         case 5: yVel += PLAYER_1_SPEED_Y; break;
  576.         //down
  577.         case 6: yVel -= PLAYER_1_SPEED_Y; break;
  578.         //left
  579.         case 7: xVel += PLAYER_1_SPEED_X; break;
  580.         //right
  581.         case 8: xVel -= PLAYER_1_SPEED_X; break;
  582.     }
  583. }
  584.  
  585. void Player::move()
  586. {
  587.     //Move the dot left or right
  588.    // x += xVel;
  589.  
  590.     /*for (int z = 16; z < 32; z++)
  591.     {
  592.         if (BorderXY [z] == 1)
  593.         {
  594.             //x
  595.             if (x > BorderX [z] - 15 && x < BorderX [z] + 20 && y > BorderY [z] && y < BorderY [z] + 100)
  596.             {
  597.                 x -= xVel;  
  598.             }
  599.         }
  600.  
  601.         if (BorderXY [z] == 0)
  602.         {
  603.             //x
  604.             if (x > BorderX [z] - 15 && x < BorderX [z] + 100 && y > BorderY [z] - 15 && y < BorderY [z] + 20)
  605.             {
  606.                 x -= xVel;  
  607.             }
  608.         }
  609.     }
  610.    
  611.     for (int z = 0; z < 16; z++)
  612.     {
  613.         if (BorderXY [z] == 1)
  614.         {
  615.             if (y > BorderY [z] - 15 && y < BorderY [z] + 100 && x > BorderX [z] - 15 && x < BorderX [z] + 20)
  616.             {
  617.                 y -= yVel;  
  618.             }
  619.         }
  620.  
  621.         if (BorderXY [z] == 0)
  622.         {
  623.             if (y > BorderY [z] - 15 && y < BorderY [z] + 20 && x > BorderY [z] - 15 && x < BorderY [z] + 100)
  624.             {
  625.                 y -= yVel;  
  626.             }
  627.         }
  628.     }
  629.  
  630.     //If the dot went too far to the left or right
  631.     for (int q = 0; q < 87; q++)
  632.     {
  633.         if (WallXY [q] == 1)
  634.         {
  635.             //x
  636.             if (x > WallX [q] - 15 && x < WallX [q] + 20 && y > WallY [q] - 15 && y < WallY [q] + 100)
  637.             {
  638.                 x -= xVel;  
  639.             }
  640.         }
  641.  
  642.         if (WallXY [q] == 0)
  643.         {
  644.             //x
  645.             if (x > WallX [q] - 15 && x < WallX [q] + 100 && y > WallY [q] - 15 && y < WallY [q] + 20)
  646.             {
  647.                 x -= xVel;  
  648.             }
  649.         }
  650.     }
  651.    
  652.     /*if( ( x < 0 ) || ( x + 34 > 800 ) )
  653.     {
  654.         //move back
  655.         x -= xVel;    
  656.     }*/
  657.    
  658.     //Move the dot up or down
  659.    // y += yVel;
  660.    /* for (int q = 0; q < 87; q++)
  661.     {
  662.     if (WallXY [q] == 1)
  663.         {
  664.             if (y > WallY [q] - 15 && y < WallY [q] + 100 && x > WallX [q] - 15 && x < WallX [q] + 20)
  665.             {
  666.                 y -= yVel;  
  667.             }
  668.         }
  669.  
  670.     if (WallXY [q] == 0)
  671.         {
  672.             if (y > WallY [q] - 15 && y < WallY [q] + 20 && x > WallX [q] - 15 && x < WallX [q] + 100)
  673.             {
  674.                 y -= yVel;  
  675.             }
  676.         }
  677.     }
  678.     //If the dot went too far up or down
  679.     /*if( ( y < 0 ) || ( y + 34 > 800 ) )
  680.     {
  681.         //move back
  682.         y -= yVel;    
  683.     }*/
  684. }
  685.  
  686. void Player::show()
  687. {
  688.     x += xVel;
  689.     y += yVel;
  690.     apply_surface(0, 0, gbackground, screen);
  691.     apply_surface( x, y, Player1, screen );
  692.  
  693.     /*for (int q = 0; q < 87; q++)
  694.     {
  695.         if (WallXY [q] == 1)
  696.         {
  697.             //x
  698.             if (x > WallX [q] - 15 && x < WallX [q] + 19 && y > WallY [q] - 15 && y < WallY [q] + 99)
  699.             {
  700.                 x -= 15;  
  701.             }
  702.         }
  703.  
  704.         if (WallXY [q] == 0)
  705.         {
  706.             //x
  707.             if (x > WallX [q] - 15 && x < WallX [q] + 99 && y > WallY [q] - 15 && y < WallY [q] + 19)
  708.             {
  709.                 x -= 15;  
  710.             }
  711.         }
  712.     }
  713.  
  714.      for (int q = 0; q < 87; q++)
  715.     {
  716.     if (WallXY [q] == 1)
  717.         {
  718.             if (y > WallY [q] - 15 && y < WallY [q] + 99 && x > WallX [q] - 15 && x < WallX [q] + 19)
  719.             {
  720.                 y -= 15;  
  721.             }
  722.         }
  723.  
  724.     if (WallXY [q] == 0)
  725.         {
  726.             if (y > WallY [q] - 15 && y < WallY [q] + 19 && x > WallX [q] - 15 && x < WallX [q] + 99)
  727.             {
  728.                 y -= 15;  
  729.             }
  730.         }
  731.     }
  732.  
  733.     for (int x = 0; x < 70; x++)
  734.     {
  735.         if (WallXY [x] == 1)
  736.         {
  737.             apply_surface( WallX [x], WallY [x], vwall2, screen);
  738.         }
  739.  
  740.         if (WallXY [x] == 0)
  741.         {
  742.             apply_surface( WallX [x], WallY [x], hwall2, screen);
  743.         }
  744.     }*/
  745.    
  746.     /*//room 1 bot walls
  747.     apply_surface(0,780,hwall,screen);
  748.     apply_surface(100,780,hwall,screen);
  749.     apply_surface(200,780,hwall,screen);
  750.     apply_surface(300,780,hwall,screen);
  751.     apply_surface(400,780,hwall,screen);
  752.     apply_surface(500,780,hwall,screen);
  753.     apply_surface(600,780,hwall,screen);
  754.     apply_surface(700,780,hwall,screen);
  755.     //room 1 top walls
  756.     apply_surface(0,0,hwall,screen);
  757.     apply_surface(100,0,hwall,screen);
  758.     apply_surface(200,0,hwall,screen);
  759.     apply_surface(250,0,hwall,screen);
  760.     apply_surface(450,0,hwall,screen);
  761.     apply_surface(500,0,hwall,screen);
  762.     apply_surface(600,0,hwall,screen);
  763.     apply_surface(700,0,hwall,screen);
  764.     //room 1 left walls
  765.     apply_surface(0,0,vwall,screen);
  766.     apply_surface(0,100,vwall,screen);
  767.     apply_surface(0,200,vwall,screen);
  768.     apply_surface(0,300,vwall,screen);
  769.     apply_surface(0,400,vwall,screen);
  770.     apply_surface(0,500,vwall,screen);
  771.     apply_surface(0,600,vwall,screen);
  772.     apply_surface(0,700,vwall,screen);
  773.     //room 1 right walls
  774.     apply_surface(780,0,vwall,screen);
  775.     apply_surface(780,100,vwall,screen);
  776.     apply_surface(780,200,vwall,screen);
  777.     apply_surface(780,300,vwall,screen);
  778.     apply_surface(780,400,vwall,screen);
  779.     apply_surface(780,500,vwall,screen);
  780.     apply_surface(780,600,vwall,screen);
  781.     apply_surface(780,700,vwall,screen);
  782.    
  783.    
  784.     apply_surface( x, y, Hero, screen );
  785.     */
  786. }
  787. ///////////////////////////
  788.  
  789. class Player2
  790. {
  791.     private:
  792.     //The X and Y offsets of the dot
  793.    
  794.    
  795.     //The velocity of the dot
  796.     float xVel, yVel;
  797.    
  798.     public:
  799.     //Initializes the variables
  800.     Player2();
  801.     float x, y;
  802.     //Takes key presses and adjusts the dot's velocity
  803.     void handle_input(SOCKET Socket);
  804.     void handle_input(char in);
  805.    
  806.     //Moves the dot
  807.     void move();
  808.    
  809.     //Shows the dot on the screen
  810.     void show();
  811. };
  812.  
  813. Player2::Player2()
  814. {
  815.     //SDL_Surface *Player = NULL;
  816.     //Player = load_image("images/Player.bmp");
  817.     //Initialize the offsets
  818.    
  819.     x = 640;
  820.     y = 570;
  821.    
  822.     //Initialize the velocity
  823.     xVel = 0;
  824.     yVel = 0;
  825. }
  826.  
  827. void Player2::handle_input(char in)
  828. {
  829.     switch(in)
  830.     {
  831.             //keydown
  832.         //up
  833.         case 1: yVel -= PLAYER_2_SPEED_Y; break;
  834.         //down
  835.         case 2: yVel += PLAYER_2_SPEED_Y; break;
  836.         //left
  837.         case 3: xVel -= PLAYER_2_SPEED_X; break;
  838.         //right
  839.         case 4: xVel += PLAYER_2_SPEED_X; break;
  840.             //keyup
  841.         //up
  842.         case 5: yVel += PLAYER_2_SPEED_Y; break;
  843.         //down
  844.         case 6: yVel -= PLAYER_2_SPEED_Y; break;
  845.         //left
  846.         case 7: xVel += PLAYER_2_SPEED_X; break;
  847.         //right
  848.         case 8: xVel -= PLAYER_2_SPEED_X; break;
  849.     }
  850. }
  851.  
  852. void Player2::handle_input(SOCKET Socket)
  853. {
  854.     char output[1];
  855.     //If a key was pressed
  856.     if( event.type == SDL_KEYDOWN )
  857.     {
  858.         //Adjust the velocity
  859.         switch( event.key.keysym.sym )
  860.         {
  861.             case SDLK_UP: yVel -= PLAYER_2_SPEED_Y; memset(output,1,0); send(Socket,output,1,0); break;
  862.             case SDLK_DOWN: yVel += PLAYER_2_SPEED_Y; memset(output,2,0); send(Socket,output,1,0); break;
  863.             case SDLK_LEFT: xVel -= PLAYER_2_SPEED_X; memset(output,3,0); send(Socket,output,1,0); break;
  864.             case SDLK_RIGHT: xVel += PLAYER_2_SPEED_X; memset(output,4,0); send(Socket,output,1,0); break;    
  865.         }
  866.     }
  867.  
  868.      //If a key was released
  869.     else if( event.type == SDL_KEYUP )
  870.     {
  871.         //Adjust the velocity
  872.         switch( event.key.keysym.sym )
  873.         {
  874.             case SDLK_UP: yVel += PLAYER_2_SPEED_Y; memset(output,5,0); send(Socket,output,1,0); break;
  875.             case SDLK_DOWN: yVel -= PLAYER_2_SPEED_Y; memset(output,6,0); send(Socket,output,1,0); break;
  876.             case SDLK_LEFT: xVel += PLAYER_2_SPEED_X; memset(output,7,0); send(Socket,output,1,0); break;
  877.             case SDLK_RIGHT: xVel -= PLAYER_2_SPEED_X; memset(output,8,0); send(Socket,output,1,0); break;  
  878.         }        
  879.     }
  880. }
  881.  
  882. void Player2::move()
  883. {
  884.     //Move the dot left or right
  885.    // x += xVel;
  886.  
  887.     /*for (int z = 16; z < 32; z++)
  888.     {
  889.         if (BorderXY [z] == 1)
  890.         {
  891.             //x
  892.             if (x > BorderX [z] - 15 && x < BorderX [z] + 20 && y > BorderY [z] && y < BorderY [z] + 100)
  893.             {
  894.                 x -= xVel;  
  895.             }
  896.         }
  897.  
  898.         if (BorderXY [z] == 0)
  899.         {
  900.             //x
  901.             if (x > BorderX [z] - 15 && x < BorderX [z] + 100 && y > BorderY [z] - 15 && y < BorderY [z] + 20)
  902.             {
  903.                 x -= xVel;  
  904.             }
  905.         }
  906.     }
  907.    
  908.     for (int z = 0; z < 16; z++)
  909.     {
  910.         if (BorderXY [z] == 1)
  911.         {
  912.             if (y > BorderY [z] - 15 && y < BorderY [z] + 100 && x > BorderX [z] - 15 && x < BorderX [z] + 20)
  913.             {
  914.                 y -= yVel;  
  915.             }
  916.         }
  917.  
  918.         if (BorderXY [z] == 0)
  919.         {
  920.             if (y > BorderY [z] - 15 && y < BorderY [z] + 20 && x > BorderY [z] - 15 && x < BorderY [z] + 100)
  921.             {
  922.                 y -= yVel;  
  923.             }
  924.         }
  925.     }
  926.  
  927.     //If the dot went too far to the left or right
  928.     for (int q = 0; q < 87; q++)
  929.     {
  930.         if (WallXY [q] == 1)
  931.         {
  932.             //x
  933.             if (x > WallX [q] - 15 && x < WallX [q] + 20 && y > WallY [q] - 15 && y < WallY [q] + 100)
  934.             {
  935.                 x -= xVel;  
  936.             }
  937.         }
  938.  
  939.         if (WallXY [q] == 0)
  940.         {
  941.             //x
  942.             if (x > WallX [q] - 15 && x < WallX [q] + 100 && y > WallY [q] - 15 && y < WallY [q] + 20)
  943.             {
  944.                 x -= xVel;  
  945.             }
  946.         }
  947.     }
  948.    
  949.     /*if( ( x < 0 ) || ( x + 34 > 800 ) )
  950.     {
  951.         //move back
  952.         x -= xVel;    
  953.     }*/
  954.    
  955.     //Move the dot up or down
  956.     y += yVel;
  957.    /* for (int q = 0; q < 87; q++)
  958.     {
  959.     if (WallXY [q] == 1)
  960.         {
  961.             if (y > WallY [q] - 15 && y < WallY [q] + 100 && x > WallX [q] - 15 && x < WallX [q] + 20)
  962.             {
  963.                 y -= yVel;  
  964.             }
  965.         }
  966.  
  967.     if (WallXY [q] == 0)
  968.         {
  969.             if (y > WallY [q] - 15 && y < WallY [q] + 20 && x > WallX [q] - 15 && x < WallX [q] + 100)
  970.             {
  971.                 y -= yVel;  
  972.             }
  973.         }
  974.     }
  975.     //If the dot went too far up or down
  976.     /*if( ( y < 0 ) || ( y + 34 > 800 ) )
  977.     {
  978.         //move back
  979.         y -= yVel;    
  980.     }*/
  981. }
  982.  
  983. void Player2::show()
  984. {
  985.     x += xVel;
  986.     y += yVel;
  987.     //apply_surface(0, 0, gbackground, screen);
  988.     apply_surface( x, y, Player_2, screen );
  989.  
  990.     /*for (int q = 0; q < 87; q++)
  991.     {
  992.         if (WallXY [q] == 1)
  993.         {
  994.             //x
  995.             if (x > WallX [q] - 15 && x < WallX [q] + 19 && y > WallY [q] - 15 && y < WallY [q] + 99)
  996.             {
  997.                 x -= 15;  
  998.             }
  999.         }
  1000.  
  1001.         if (WallXY [q] == 0)
  1002.         {
  1003.             //x
  1004.             if (x > WallX [q] - 15 && x < WallX [q] + 99 && y > WallY [q] - 15 && y < WallY [q] + 19)
  1005.             {
  1006.                 x -= 15;  
  1007.             }
  1008.         }
  1009.     }
  1010.  
  1011.      for (int q = 0; q < 87; q++)
  1012.     {
  1013.     if (WallXY [q] == 1)
  1014.         {
  1015.             if (y > WallY [q] - 15 && y < WallY [q] + 99 && x > WallX [q] - 15 && x < WallX [q] + 19)
  1016.             {
  1017.                 y -= 15;  
  1018.             }
  1019.         }
  1020.  
  1021.     if (WallXY [q] == 0)
  1022.         {
  1023.             if (y > WallY [q] - 15 && y < WallY [q] + 19 && x > WallX [q] - 15 && x < WallX [q] + 99)
  1024.             {
  1025.                 y -= 15;  
  1026.             }
  1027.         }
  1028.     }
  1029.  
  1030.     for (int x = 0; x < 70; x++)
  1031.     {
  1032.         if (WallXY [x] == 1)
  1033.         {
  1034.             apply_surface( WallX [x], WallY [x], vwall2, screen);
  1035.         }
  1036.  
  1037.         if (WallXY [x] == 0)
  1038.         {
  1039.             apply_surface( WallX [x], WallY [x], hwall2, screen);
  1040.         }
  1041.     }*/
  1042.    
  1043.     /*//room 1 bot walls
  1044.     apply_surface(0,780,hwall,screen);
  1045.     apply_surface(100,780,hwall,screen);
  1046.     apply_surface(200,780,hwall,screen);
  1047.     apply_surface(300,780,hwall,screen);
  1048.     apply_surface(400,780,hwall,screen);
  1049.     apply_surface(500,780,hwall,screen);
  1050.     apply_surface(600,780,hwall,screen);
  1051.     apply_surface(700,780,hwall,screen);
  1052.     //room 1 top walls
  1053.     apply_surface(0,0,hwall,screen);
  1054.     apply_surface(100,0,hwall,screen);
  1055.     apply_surface(200,0,hwall,screen);
  1056.     apply_surface(250,0,hwall,screen);
  1057.     apply_surface(450,0,hwall,screen);
  1058.     apply_surface(500,0,hwall,screen);
  1059.     apply_surface(600,0,hwall,screen);
  1060.     apply_surface(700,0,hwall,screen);
  1061.     //room 1 left walls
  1062.     apply_surface(0,0,vwall,screen);
  1063.     apply_surface(0,100,vwall,screen);
  1064.     apply_surface(0,200,vwall,screen);
  1065.     apply_surface(0,300,vwall,screen);
  1066.     apply_surface(0,400,vwall,screen);
  1067.     apply_surface(0,500,vwall,screen);
  1068.     apply_surface(0,600,vwall,screen);
  1069.     apply_surface(0,700,vwall,screen);
  1070.     //room 1 right walls
  1071.     apply_surface(780,0,vwall,screen);
  1072.     apply_surface(780,100,vwall,screen);
  1073.     apply_surface(780,200,vwall,screen);
  1074.     apply_surface(780,300,vwall,screen);
  1075.     apply_surface(780,400,vwall,screen);
  1076.     apply_surface(780,500,vwall,screen);
  1077.     apply_surface(780,600,vwall,screen);
  1078.     apply_surface(780,700,vwall,screen);
  1079.    
  1080.    
  1081.     apply_surface( x, y, Hero, screen );
  1082.     */
  1083. }
  1084. //////////////////////
  1085.  
  1086. int main( int argc, char* argv[] )
  1087. {
  1088.     SOCKET Socket;
  1089.     bool RedOrBlue;
  1090.     //Make sure the program waits for a quit
  1091.     bool quit = false;
  1092.     Player player1;
  1093.     Player2 player2;
  1094.     apply_surface(0,0,background,screen);
  1095.    
  1096.  
  1097.  //Initialize
  1098.     if( init() == false )
  1099.     {
  1100.         return 1;    
  1101.     }
  1102.    
  1103.     //Load the files
  1104.     if( load_files() == false )
  1105.     {
  1106.         return 1;    
  1107.     }
  1108.     SDL_EnableUNICODE( SDL_ENABLE );  
  1109.    
  1110.     //Connect to partner
  1111.     apply_surface(210, 210,JoinHost,screen);
  1112.     SDL_Flip( screen );
  1113.     bool hmm = false;
  1114.     //While the user hasn't quit
  1115.      while( hmm == false )
  1116.     {//1
  1117.          //While there's an event to handle
  1118.         if( SDL_PollEvent( &event ))
  1119.         {
  1120.  
  1121.             //If a key was pressed
  1122.             if( event.type == SDL_KEYDOWN )
  1123.             {
  1124.                  //If the key is a number
  1125.                 if( event.key.keysym.unicode == (Uint16)106 )
  1126.                 {
  1127.                     apply_surface(0,0,background,screen);
  1128.                     SDL_Flip( screen );
  1129.                     if (Socket = Client())
  1130.                     {
  1131.                         return 0;
  1132.                     }
  1133.                     RedOrBlue = true;
  1134.                     hmm = true;
  1135.                 }
  1136.                 else if( event.key.keysym.unicode == (Uint16)104 )
  1137.                 {
  1138.                     apply_surface(0,0,background,screen);
  1139.                     SDL_Flip( screen );
  1140.                     if (Socket = Server())
  1141.                     {
  1142.                         return 0;
  1143.                     }
  1144.                     RedOrBlue = false;
  1145.                     hmm = true;
  1146.                 }
  1147.             }
  1148.             //If the user has Xed out the window
  1149.             if( event.type == SDL_QUIT )
  1150.             {
  1151.                 //Quit the program
  1152.                  clean_up();
  1153.                  return 0;  
  1154.             }
  1155.         }
  1156.     }
  1157.  
  1158.      //Client****************************************************
  1159.      if (RedOrBlue == true)
  1160.      {
  1161.     apply_surface(0,0,gbackground,screen); 
  1162.     apply_surface(player1.x,player1.y,Player1,screen);
  1163.     apply_surface(player2.x,player2.y,Player_2,screen);
  1164.     //Update the screen
  1165.     if( SDL_Flip( screen ) == -1 )
  1166.     {
  1167.         return 1;    
  1168.     }
  1169.     SDL_EnableUNICODE( SDL_DISABLE );
  1170.  
  1171.     //While the user hasn't quit
  1172.     while( quit == false )
  1173.     {//1
  1174.          //While there's an event to handle
  1175.         if( SDL_PollEvent( &event ))
  1176.         {
  1177.             char buffer[1];
  1178.             memset(buffer,0,0);
  1179.             int inDataLength=recv(Socket,buffer,1,0);
  1180.             int input = buffer[0];
  1181.              player1.handle_input(input);
  1182.              player2.handle_input(Socket);
  1183.  
  1184.             //If the user has Xed out the window
  1185.             if( event.type == SDL_QUIT )
  1186.             {
  1187.                 //Quit the program
  1188.                 quit = true;
  1189.             }  
  1190.         }
  1191.              
  1192.          player1.move();
  1193.          player2.move();
  1194.          player1.show();
  1195.          player2.show();
  1196.  
  1197.         //Update the screen
  1198.         if( SDL_Flip( screen ) == -1 )
  1199.         {
  1200.             return 1;    
  1201.         }
  1202.        
  1203.        
  1204.     }//1
  1205.     //Free the surface and quit SDL
  1206.     clean_up();
  1207.        
  1208.     return 0;    
  1209. }
  1210.     //Server***********************************************************
  1211.  
  1212.      else if (RedOrBlue == false)
  1213.      {
  1214.          char buffer[1];
  1215.         // char input;
  1216.          apply_surface(0,0,gbackground,screen);
  1217.     apply_surface(player1.x,player1.y,Player1,screen);
  1218.     apply_surface(player2.x,player2.y,Player_2,screen);
  1219.     //Update the screen
  1220.     if( SDL_Flip( screen ) == -1 )
  1221.     {
  1222.         return 1;    
  1223.     }
  1224.     SDL_EnableUNICODE( SDL_DISABLE );
  1225.  
  1226.     //While the user hasn't quit
  1227.     while( quit == false )
  1228.     {//1
  1229.          //While there's an event to handle
  1230.         if( SDL_PollEvent( &event ))
  1231.         {
  1232.            
  1233.             memset(buffer,0,0);
  1234.             int inDataLength=recv(Socket,buffer,1,0);
  1235.            
  1236.             //input = buffer[0];
  1237.              player1.handle_input(Socket);
  1238.              player2.handle_input(buffer[0]);
  1239.  
  1240.             //If the user has Xed out the window
  1241.             if( event.type == SDL_QUIT )
  1242.             {
  1243.                 //Quit the program
  1244.                 quit = true;
  1245.             }  
  1246.         }
  1247.              
  1248.          player1.move();
  1249.          player2.move();
  1250.          player1.show();
  1251.          player2.show();
  1252.  
  1253.         //Update the screen
  1254.         if( SDL_Flip( screen ) == -1 )
  1255.         {
  1256.             return 1;    
  1257.         }
  1258.        
  1259.        
  1260.     }//1
  1261.     //Free the surface and quit SDL
  1262.     clean_up();
  1263.        
  1264.     return 0;
  1265.      }
  1266.  
  1267.      else
  1268.      {
  1269.          clean_up();
  1270.        
  1271.         return 0;
  1272.      }
  1273.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement