Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL.h"
- #include <cstdlib>
- #include <ctime>
- //#include "SDL_image.h"
- #include <string>
- #include <conio.h>
- #include <winsock2.h>
- #include <iostream>
- #pragma comment(lib,"ws2_32.lib")
- //Screen attributes
- const int SCREEN_WIDTH = 700;
- const int SCREEN_HEIGHT = 630;
- const int SCREEN_BPP = 32;
- float PLAYER_1_SPEED_X = 1;
- float PLAYER_1_SPEED_Y = .5;
- float PLAYER_2_SPEED_X = 1;
- float PLAYER_2_SPEED_Y = .5;
- //The surfaces
- SDL_Surface *red = NULL;
- SDL_Surface *yellow = NULL;
- SDL_Surface *green = NULL;
- SDL_Surface *blue = NULL;
- SDL_Surface *Hero = NULL;
- SDL_Surface *hwall = NULL;
- SDL_Surface *hwall2 = NULL;
- SDL_Surface *vwall = NULL;
- SDL_Surface *vwall2 = NULL;
- SDL_Surface *period = NULL;
- SDL_Surface *One = NULL;
- SDL_Surface *Two = NULL;
- SDL_Surface *Three = NULL;
- SDL_Surface *Four = NULL;
- SDL_Surface *Five = NULL;
- SDL_Surface *Six = NULL;
- SDL_Surface *Seven = NULL;
- SDL_Surface *Eight = NULL;
- SDL_Surface *Nine = NULL;
- SDL_Surface *Zero = NULL;
- SDL_Surface *Blank = NULL;
- SDL_Surface *JoinHost = NULL;
- SDL_Surface *ServerConnect = NULL;
- SDL_Surface *IPConnectMessage = NULL;
- SDL_Surface *Player1 = NULL;
- SDL_Surface *Player_2 = NULL;
- SDL_Surface *background = NULL;
- SDL_Surface *gbackground = NULL;
- SDL_Surface *screen = NULL;
- SDL_Event event;
- int IPpositionX = 50;
- int IPpositionY = 245;
- 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};
- 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};
- int WallX [87];
- int WallY [87];
- bool WallXY [87];
- 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};
- bool getInput(char *c)
- {
- if (kbhit())
- {
- *c = getch();
- return true; // Key Was Hit
- }
- return false; // No keys were pressed
- }
- SDL_Surface *load_image( std::string filename )
- {
- //The image that's loaded
- SDL_Surface* loadedImage = NULL;
- //The optimized image that will be used
- SDL_Surface* optimizedImage = NULL;
- //Load the image
- loadedImage = SDL_LoadBMP( filename.c_str() );
- //If the image loaded
- if( loadedImage != NULL )
- {
- //Create an optimized image
- optimizedImage = SDL_DisplayFormat( loadedImage );
- //Free the old image
- SDL_FreeSurface( loadedImage );
- }
- //Return the optimized image
- return optimizedImage;
- }
- void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
- {
- //Temporary rectangle to hold the offsets
- SDL_Rect offset;
- //Get the offsets
- offset.x = x;
- offset.y = y;
- //Blit the surface
- SDL_BlitSurface( source, clip, destination, &offset );
- }
- void LevelGen()
- {
- //clear screen
- apply_surface(0,0,background,screen);
- //room 1 bot walls
- apply_surface(0,780,hwall,screen);
- WallX [71] = 0;WallY [71] = 780; WallXY [71] = 0;
- apply_surface(100,780,hwall,screen);
- WallX [72] = 100;WallY [72] = 780; WallXY [72] = 0;
- apply_surface(200,780,hwall,screen);
- WallX [73] = 200;WallY [73] = 780; WallXY [73] = 0;
- apply_surface(300,780,hwall,screen);
- WallX [74] = 300;WallY [74] = 780; WallXY [74] = 0;
- apply_surface(400,780,hwall,screen);
- WallX [75] = 400;WallY [75] = 780; WallXY [75] = 0;
- apply_surface(500,780,hwall,screen);
- WallX [76] = 500;WallY [76] = 780; WallXY [76] = 0;
- apply_surface(600,780,hwall,screen);
- WallX [77] = 600;WallY [77] = 780; WallXY [77] = 0;
- apply_surface(700,780,hwall,screen);
- WallX [78] = 700;WallY [78] = 780; WallXY [78] = 0;
- //room 1 top walls
- apply_surface(0,0,hwall,screen);
- WallX [79] = 0;WallY [79] = 0; WallXY [79] = 0;
- apply_surface(100,0,hwall,screen);
- WallX [80] = 100;WallY [80] = 0; WallXY [80] = 0;
- apply_surface(200,0,hwall,screen);
- WallX [81] = 200;WallY [81] = 0; WallXY [81] = 0;
- apply_surface(250,0,hwall,screen);
- WallX [82] = 250;WallY [82] = 0; WallXY [82] = 0;
- apply_surface(450,0,hwall,screen);
- WallX [83] = 450;WallY [83] = 0; WallXY [83] = 0;
- apply_surface(500,0,hwall,screen);
- WallX [84] = 500;WallY [84] = 0; WallXY [84] = 0;
- apply_surface(600,0,hwall,screen);
- WallX [85] = 600;WallY [85] = 0; WallXY [85] = 0;
- apply_surface(700,0,hwall,screen);
- WallX [86] = 700;WallY [86] = 0; WallXY [86] = 0;
- //room 1 left walls
- apply_surface(0,0,vwall,screen);
- apply_surface(0,100,vwall,screen);
- apply_surface(0,200,vwall,screen);
- apply_surface(0,300,vwall,screen);
- apply_surface(0,400,vwall,screen);
- apply_surface(0,500,vwall,screen);
- apply_surface(0,600,vwall,screen);
- apply_surface(0,700,vwall,screen);
- //room 1 right walls
- apply_surface(780,0,vwall,screen);
- apply_surface(780,100,vwall,screen);
- apply_surface(780,200,vwall,screen);
- apply_surface(780,300,vwall,screen);
- apply_surface(780,400,vwall,screen);
- apply_surface(780,500,vwall,screen);
- apply_surface(780,600,vwall,screen);
- apply_surface(780,700,vwall,screen);
- //player
- apply_surface(400,700,Hero,screen);
- //Set Walls
- srand (time(NULL));
- for (int y = 0; y < 70; y++)
- {
- int rando = rand() % 2;
- int randomvx = rand() % 800;
- int randomvy = rand() % 800;
- int randomhx = rand() % 800;
- int randomhy = rand() % 800;
- if (rando == 1)
- {
- apply_surface(randomvx, randomvy, vwall, screen);
- WallX [y] = randomvx;
- WallY [y] = randomvy;
- WallXY [y] = 1;
- }
- else if (rando == 0)
- {
- apply_surface(randomhx, randomhy, hwall, screen);
- WallX [y] = randomhx;
- WallY [y] = randomhy;
- WallXY [y] = 0;
- }
- }
- for (int x = 0; x < 70; x++)
- {
- if (WallXY [x] == 1)
- {
- apply_surface( WallX [x], WallY [x], vwall, screen);
- }
- if (WallXY [x] == 0)
- {
- apply_surface( WallX [x], WallY [x], hwall, screen);
- }
- }
- SDL_Flip(screen);
- SDL_Delay(500);
- }
- bool init()
- {
- //Initialize all SDL subsystems
- if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
- {
- return false;
- }
- //Set up the screen
- screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
- //If there was an error in setting up the screen
- if( screen == NULL )
- {
- return false;
- }
- //Set the window caption
- SDL_WM_SetCaption( "Arena", NULL );
- //If everything initialized fine
- return true;
- }
- bool load_files()
- {
- //Load the image
- red = load_image("images/red.bmp");
- blue = load_image("images/blue.bmp");
- green = load_image("images/green.bmp");
- yellow = load_image("images/yellow.bmp");
- Hero = load_image("images/Player.bmp");
- hwall = load_image("images/HWall.bmp");
- hwall2 = load_image("images/HWallBlack.bmp");
- vwall = load_image("images/VWall.bmp");
- vwall2 = load_image("images/VWallBlack.bmp");
- period = load_image("images/period.bmp");
- Zero = load_image("images/0.bmp");
- One = load_image("images/1.bmp");
- Two = load_image("images/2.bmp");
- Three = load_image("images/3.bmp");
- Four = load_image("images/4.bmp");
- Five = load_image("images/5.bmp");
- Six = load_image("images/6.bmp");
- Seven = load_image("images/7.bmp");
- Eight = load_image("images/8.bmp");
- Nine = load_image("images/9.bmp");
- Blank = load_image("images/blank.bmp");
- JoinHost = load_image("images/JoinHost.bmp");
- ServerConnect = load_image("images/ServerConnecting.bmp");
- IPConnectMessage = load_image("images/IPConnectMessage.bmp");
- Player1 = load_image("images/Player 1.bmp");
- Player_2 = load_image("images/Player 2.bmp");
- background = load_image("images/background.bmp");
- gbackground = load_image("images/game_background.bmp");
- return true;
- }
- void clean_up()
- {
- //Quit SDL
- SDL_Quit();
- }
- SOCKET Client()
- {
- WSADATA WsaDat;
- if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
- {
- std::cout<<"WSA Initialization failed!\r\n";
- WSACleanup();
- system("PAUSE");
- return 0;
- }
- SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(Socket==INVALID_SOCKET)
- {
- std::cout<<"Socket creation failed.\r\n";
- WSACleanup();
- system("PAUSE");
- return 0;
- }
- apply_surface(150,175,IPConnectMessage,screen);
- SDL_Flip(screen);
- bool quit = false;
- std::string str;
- SDL_EnableUNICODE( SDL_ENABLE );
- //While the user hasn't quit
- while( quit == false )
- {//1
- //While there's an event to handle
- if( SDL_PollEvent( &event ))
- {
- //If a key was pressed
- if( event.type == SDL_KEYDOWN )
- {
- //If the key is a number
- if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
- {
- //Append the character
- str += (char)event.key.keysym.unicode;
- if (event.key.keysym.unicode == (Uint16)48)
- {
- apply_surface(IPpositionX,IPpositionY,Zero,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)49)
- {
- apply_surface(IPpositionX,IPpositionY,One,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)50)
- {
- apply_surface(IPpositionX,IPpositionY,Two,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)51)
- {
- apply_surface(IPpositionX,IPpositionY,Three,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)52)
- {
- apply_surface(IPpositionX,IPpositionY,Four,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)53)
- {
- apply_surface(IPpositionX,IPpositionY,Five,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)54)
- {
- apply_surface(IPpositionX,IPpositionY,Six,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)55)
- {
- apply_surface(IPpositionX,IPpositionY,Seven,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)56)
- {
- apply_surface(IPpositionX,IPpositionY,Eight,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- else if (event.key.keysym.unicode == (Uint16)57)
- {
- apply_surface(IPpositionX,IPpositionY,Nine,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- }
- if ( ( event.key.keysym.unicode == (Uint16)46 ))
- {
- //Append the character
- str += (char)event.key.keysym.unicode;
- apply_surface(IPpositionX,IPpositionY,period,screen);
- IPpositionX += 50;
- SDL_Flip(screen);
- }
- //If backspace was pressed and the string isn't blank
- if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( str.length() != 0 ) )
- {
- //Remove a character from the end
- str.erase( str.length() - 1 );
- IPpositionX -= 50;
- apply_surface(IPpositionX,IPpositionY,Blank,screen);
- SDL_Flip(screen);
- }
- if ( ( event.key.keysym.sym == (Uint16)13))
- {
- quit = true;
- }
- }
- //If the user has Xed out the window
- if( event.type == SDL_QUIT )
- {
- //Quit the program
- clean_up();
- return 0;
- }
- }
- }//1
- char IP[15];
- strcpy(IP,str.c_str());
- // Resolve IP address for hostname
- struct hostent *host;
- if((host=gethostbyname(IP))==NULL)
- {
- //std::cout<<"Failed to resolve hostname.\r\n";
- WSACleanup();
- //system("PAUSE");
- return 0;
- }
- // Setup our socket address structure
- SOCKADDR_IN SockAddr;
- SockAddr.sin_port=htons(8888);
- SockAddr.sin_family=AF_INET;
- SockAddr.sin_addr.s_addr=*((unsigned long*)host->h_addr);
- // Attempt to connect to server
- if(connect(Socket,(SOCKADDR*)(&SockAddr),sizeof(SockAddr))!=0)
- {
- //std::cout<<"Failed to establish connection with server\r\n";
- WSACleanup();
- //system("PAUSE");
- return 0;
- }
- u_long iMode=1;
- ioctlsocket(Socket,FIONBIO,&iMode);
- return Socket;
- }
- SOCKET Server()
- {
- WSADATA WsaDat;
- if(WSAStartup(MAKEWORD(2,2),&WsaDat)!=0)
- {
- std::cout<<"WSA Initialization failed!\r\n";
- WSACleanup();
- system("PAUSE");
- return 0;
- }
- SOCKET Socket=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
- if(Socket==INVALID_SOCKET)
- {
- std::cout<<"Socket creation failed.\r\n";
- WSACleanup();
- system("PAUSE");
- return 0;
- }
- SOCKADDR_IN serverInf;
- serverInf.sin_family=AF_INET;
- serverInf.sin_addr.s_addr=INADDR_ANY;
- serverInf.sin_port=htons(8888);
- if(bind(Socket,(SOCKADDR*)(&serverInf),sizeof(serverInf))==SOCKET_ERROR)
- {
- std::cout<<"Unable to bind socket!\r\n";
- WSACleanup();
- system("PAUSE");
- return 0;
- }
- listen(Socket,1);
- SOCKET TempSock=SOCKET_ERROR;
- while(TempSock==SOCKET_ERROR)
- {
- //std::cout<<"Waiting for incoming connections...\r\n";
- apply_surface(0,0,ServerConnect,screen);
- SDL_Flip(screen);
- TempSock=accept(Socket,NULL,NULL);
- }
- // If iMode!=0, non-blocking mode is enabled.
- u_long iMode=1;
- ioctlsocket(Socket,FIONBIO,&iMode);
- Socket=TempSock;
- //std::cout<<"Client connected!\r\n\r\n";
- //std::cout<<"\a";
- return Socket;
- }
- /////////////////////
- class Player
- {
- private:
- //The X and Y offsets of the dot
- //The velocity of the dot
- float xVel, yVel;
- public:
- //Initializes the variables
- Player();
- float x, y;
- //Takes key presses and adjusts the dot's velocity
- void handle_input(SOCKET Socket);
- void handle_input(int in);
- //Moves the dot
- void move();
- //Shows the dot on the screen
- void show();
- };
- Player::Player()
- {
- //SDL_Surface *Player = NULL;
- //Player = load_image("images/Player.bmp");
- //Initialize the offsets
- x = 10;
- y = 40;
- //Initialize the velocity
- xVel = 0;
- yVel = 0;
- }
- void Player::handle_input(SOCKET Socket)
- {
- char output[1];
- //If a key was pressed
- if( event.type == SDL_KEYDOWN )
- {
- //Adjust the velocity
- switch( event.key.keysym.sym )
- {
- case SDLK_UP: yVel -= PLAYER_1_SPEED_Y; memset(output,1,0); send(Socket,output,1,0); break;
- case SDLK_DOWN: yVel += PLAYER_1_SPEED_Y; memset(output,2,0); send(Socket,output,1,0); break;
- case SDLK_LEFT: xVel -= PLAYER_1_SPEED_X; memset(output,3,0); send(Socket,output,1,0); break;
- case SDLK_RIGHT: xVel += PLAYER_1_SPEED_X; memset(output,4,0); send(Socket,output,1,0); break;
- }
- }
- //If a key was released
- else if( event.type == SDL_KEYUP )
- {
- //Adjust the velocity
- switch( event.key.keysym.sym )
- {
- case SDLK_UP: yVel += PLAYER_1_SPEED_Y; memset(output,5,0); send(Socket,output,1,0); break;
- case SDLK_DOWN: yVel -= PLAYER_1_SPEED_Y; memset(output,6,0); send(Socket,output,1,0); break;
- case SDLK_LEFT: xVel += PLAYER_1_SPEED_X; memset(output,7,0); send(Socket,output,1,0); break;
- case SDLK_RIGHT: xVel -= PLAYER_1_SPEED_X; memset(output,8,0); send(Socket,output,1,0); break;
- }
- }
- }
- void Player::handle_input(int in)
- {
- switch(in)
- {
- //keydown
- //up
- case 1: yVel -= PLAYER_1_SPEED_Y; break;
- //down
- case 2: yVel += PLAYER_1_SPEED_Y; break;
- //left
- case 3: xVel -= PLAYER_1_SPEED_X; break;
- //right
- case 4: xVel += PLAYER_1_SPEED_X; break;
- //keyup
- //up
- case 5: yVel += PLAYER_1_SPEED_Y; break;
- //down
- case 6: yVel -= PLAYER_1_SPEED_Y; break;
- //left
- case 7: xVel += PLAYER_1_SPEED_X; break;
- //right
- case 8: xVel -= PLAYER_1_SPEED_X; break;
- }
- }
- void Player::move()
- {
- //Move the dot left or right
- // x += xVel;
- /*for (int z = 16; z < 32; z++)
- {
- if (BorderXY [z] == 1)
- {
- //x
- if (x > BorderX [z] - 15 && x < BorderX [z] + 20 && y > BorderY [z] && y < BorderY [z] + 100)
- {
- x -= xVel;
- }
- }
- if (BorderXY [z] == 0)
- {
- //x
- if (x > BorderX [z] - 15 && x < BorderX [z] + 100 && y > BorderY [z] - 15 && y < BorderY [z] + 20)
- {
- x -= xVel;
- }
- }
- }
- for (int z = 0; z < 16; z++)
- {
- if (BorderXY [z] == 1)
- {
- if (y > BorderY [z] - 15 && y < BorderY [z] + 100 && x > BorderX [z] - 15 && x < BorderX [z] + 20)
- {
- y -= yVel;
- }
- }
- if (BorderXY [z] == 0)
- {
- if (y > BorderY [z] - 15 && y < BorderY [z] + 20 && x > BorderY [z] - 15 && x < BorderY [z] + 100)
- {
- y -= yVel;
- }
- }
- }
- //If the dot went too far to the left or right
- for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 20 && y > WallY [q] - 15 && y < WallY [q] + 100)
- {
- x -= xVel;
- }
- }
- if (WallXY [q] == 0)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 100 && y > WallY [q] - 15 && y < WallY [q] + 20)
- {
- x -= xVel;
- }
- }
- }
- /*if( ( x < 0 ) || ( x + 34 > 800 ) )
- {
- //move back
- x -= xVel;
- }*/
- //Move the dot up or down
- // y += yVel;
- /* for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 100 && x > WallX [q] - 15 && x < WallX [q] + 20)
- {
- y -= yVel;
- }
- }
- if (WallXY [q] == 0)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 20 && x > WallX [q] - 15 && x < WallX [q] + 100)
- {
- y -= yVel;
- }
- }
- }
- //If the dot went too far up or down
- /*if( ( y < 0 ) || ( y + 34 > 800 ) )
- {
- //move back
- y -= yVel;
- }*/
- }
- void Player::show()
- {
- x += xVel;
- y += yVel;
- apply_surface(0, 0, gbackground, screen);
- apply_surface( x, y, Player1, screen );
- /*for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 19 && y > WallY [q] - 15 && y < WallY [q] + 99)
- {
- x -= 15;
- }
- }
- if (WallXY [q] == 0)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 99 && y > WallY [q] - 15 && y < WallY [q] + 19)
- {
- x -= 15;
- }
- }
- }
- for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 99 && x > WallX [q] - 15 && x < WallX [q] + 19)
- {
- y -= 15;
- }
- }
- if (WallXY [q] == 0)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 19 && x > WallX [q] - 15 && x < WallX [q] + 99)
- {
- y -= 15;
- }
- }
- }
- for (int x = 0; x < 70; x++)
- {
- if (WallXY [x] == 1)
- {
- apply_surface( WallX [x], WallY [x], vwall2, screen);
- }
- if (WallXY [x] == 0)
- {
- apply_surface( WallX [x], WallY [x], hwall2, screen);
- }
- }*/
- /*//room 1 bot walls
- apply_surface(0,780,hwall,screen);
- apply_surface(100,780,hwall,screen);
- apply_surface(200,780,hwall,screen);
- apply_surface(300,780,hwall,screen);
- apply_surface(400,780,hwall,screen);
- apply_surface(500,780,hwall,screen);
- apply_surface(600,780,hwall,screen);
- apply_surface(700,780,hwall,screen);
- //room 1 top walls
- apply_surface(0,0,hwall,screen);
- apply_surface(100,0,hwall,screen);
- apply_surface(200,0,hwall,screen);
- apply_surface(250,0,hwall,screen);
- apply_surface(450,0,hwall,screen);
- apply_surface(500,0,hwall,screen);
- apply_surface(600,0,hwall,screen);
- apply_surface(700,0,hwall,screen);
- //room 1 left walls
- apply_surface(0,0,vwall,screen);
- apply_surface(0,100,vwall,screen);
- apply_surface(0,200,vwall,screen);
- apply_surface(0,300,vwall,screen);
- apply_surface(0,400,vwall,screen);
- apply_surface(0,500,vwall,screen);
- apply_surface(0,600,vwall,screen);
- apply_surface(0,700,vwall,screen);
- //room 1 right walls
- apply_surface(780,0,vwall,screen);
- apply_surface(780,100,vwall,screen);
- apply_surface(780,200,vwall,screen);
- apply_surface(780,300,vwall,screen);
- apply_surface(780,400,vwall,screen);
- apply_surface(780,500,vwall,screen);
- apply_surface(780,600,vwall,screen);
- apply_surface(780,700,vwall,screen);
- apply_surface( x, y, Hero, screen );
- */
- }
- ///////////////////////////
- class Player2
- {
- private:
- //The X and Y offsets of the dot
- //The velocity of the dot
- float xVel, yVel;
- public:
- //Initializes the variables
- Player2();
- float x, y;
- //Takes key presses and adjusts the dot's velocity
- void handle_input(SOCKET Socket);
- void handle_input(char in);
- //Moves the dot
- void move();
- //Shows the dot on the screen
- void show();
- };
- Player2::Player2()
- {
- //SDL_Surface *Player = NULL;
- //Player = load_image("images/Player.bmp");
- //Initialize the offsets
- x = 640;
- y = 570;
- //Initialize the velocity
- xVel = 0;
- yVel = 0;
- }
- void Player2::handle_input(char in)
- {
- switch(in)
- {
- //keydown
- //up
- case 1: yVel -= PLAYER_2_SPEED_Y; break;
- //down
- case 2: yVel += PLAYER_2_SPEED_Y; break;
- //left
- case 3: xVel -= PLAYER_2_SPEED_X; break;
- //right
- case 4: xVel += PLAYER_2_SPEED_X; break;
- //keyup
- //up
- case 5: yVel += PLAYER_2_SPEED_Y; break;
- //down
- case 6: yVel -= PLAYER_2_SPEED_Y; break;
- //left
- case 7: xVel += PLAYER_2_SPEED_X; break;
- //right
- case 8: xVel -= PLAYER_2_SPEED_X; break;
- }
- }
- void Player2::handle_input(SOCKET Socket)
- {
- char output[1];
- //If a key was pressed
- if( event.type == SDL_KEYDOWN )
- {
- //Adjust the velocity
- switch( event.key.keysym.sym )
- {
- case SDLK_UP: yVel -= PLAYER_2_SPEED_Y; memset(output,1,0); send(Socket,output,1,0); break;
- case SDLK_DOWN: yVel += PLAYER_2_SPEED_Y; memset(output,2,0); send(Socket,output,1,0); break;
- case SDLK_LEFT: xVel -= PLAYER_2_SPEED_X; memset(output,3,0); send(Socket,output,1,0); break;
- case SDLK_RIGHT: xVel += PLAYER_2_SPEED_X; memset(output,4,0); send(Socket,output,1,0); break;
- }
- }
- //If a key was released
- else if( event.type == SDL_KEYUP )
- {
- //Adjust the velocity
- switch( event.key.keysym.sym )
- {
- case SDLK_UP: yVel += PLAYER_2_SPEED_Y; memset(output,5,0); send(Socket,output,1,0); break;
- case SDLK_DOWN: yVel -= PLAYER_2_SPEED_Y; memset(output,6,0); send(Socket,output,1,0); break;
- case SDLK_LEFT: xVel += PLAYER_2_SPEED_X; memset(output,7,0); send(Socket,output,1,0); break;
- case SDLK_RIGHT: xVel -= PLAYER_2_SPEED_X; memset(output,8,0); send(Socket,output,1,0); break;
- }
- }
- }
- void Player2::move()
- {
- //Move the dot left or right
- // x += xVel;
- /*for (int z = 16; z < 32; z++)
- {
- if (BorderXY [z] == 1)
- {
- //x
- if (x > BorderX [z] - 15 && x < BorderX [z] + 20 && y > BorderY [z] && y < BorderY [z] + 100)
- {
- x -= xVel;
- }
- }
- if (BorderXY [z] == 0)
- {
- //x
- if (x > BorderX [z] - 15 && x < BorderX [z] + 100 && y > BorderY [z] - 15 && y < BorderY [z] + 20)
- {
- x -= xVel;
- }
- }
- }
- for (int z = 0; z < 16; z++)
- {
- if (BorderXY [z] == 1)
- {
- if (y > BorderY [z] - 15 && y < BorderY [z] + 100 && x > BorderX [z] - 15 && x < BorderX [z] + 20)
- {
- y -= yVel;
- }
- }
- if (BorderXY [z] == 0)
- {
- if (y > BorderY [z] - 15 && y < BorderY [z] + 20 && x > BorderY [z] - 15 && x < BorderY [z] + 100)
- {
- y -= yVel;
- }
- }
- }
- //If the dot went too far to the left or right
- for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 20 && y > WallY [q] - 15 && y < WallY [q] + 100)
- {
- x -= xVel;
- }
- }
- if (WallXY [q] == 0)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 100 && y > WallY [q] - 15 && y < WallY [q] + 20)
- {
- x -= xVel;
- }
- }
- }
- /*if( ( x < 0 ) || ( x + 34 > 800 ) )
- {
- //move back
- x -= xVel;
- }*/
- //Move the dot up or down
- y += yVel;
- /* for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 100 && x > WallX [q] - 15 && x < WallX [q] + 20)
- {
- y -= yVel;
- }
- }
- if (WallXY [q] == 0)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 20 && x > WallX [q] - 15 && x < WallX [q] + 100)
- {
- y -= yVel;
- }
- }
- }
- //If the dot went too far up or down
- /*if( ( y < 0 ) || ( y + 34 > 800 ) )
- {
- //move back
- y -= yVel;
- }*/
- }
- void Player2::show()
- {
- x += xVel;
- y += yVel;
- //apply_surface(0, 0, gbackground, screen);
- apply_surface( x, y, Player_2, screen );
- /*for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 19 && y > WallY [q] - 15 && y < WallY [q] + 99)
- {
- x -= 15;
- }
- }
- if (WallXY [q] == 0)
- {
- //x
- if (x > WallX [q] - 15 && x < WallX [q] + 99 && y > WallY [q] - 15 && y < WallY [q] + 19)
- {
- x -= 15;
- }
- }
- }
- for (int q = 0; q < 87; q++)
- {
- if (WallXY [q] == 1)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 99 && x > WallX [q] - 15 && x < WallX [q] + 19)
- {
- y -= 15;
- }
- }
- if (WallXY [q] == 0)
- {
- if (y > WallY [q] - 15 && y < WallY [q] + 19 && x > WallX [q] - 15 && x < WallX [q] + 99)
- {
- y -= 15;
- }
- }
- }
- for (int x = 0; x < 70; x++)
- {
- if (WallXY [x] == 1)
- {
- apply_surface( WallX [x], WallY [x], vwall2, screen);
- }
- if (WallXY [x] == 0)
- {
- apply_surface( WallX [x], WallY [x], hwall2, screen);
- }
- }*/
- /*//room 1 bot walls
- apply_surface(0,780,hwall,screen);
- apply_surface(100,780,hwall,screen);
- apply_surface(200,780,hwall,screen);
- apply_surface(300,780,hwall,screen);
- apply_surface(400,780,hwall,screen);
- apply_surface(500,780,hwall,screen);
- apply_surface(600,780,hwall,screen);
- apply_surface(700,780,hwall,screen);
- //room 1 top walls
- apply_surface(0,0,hwall,screen);
- apply_surface(100,0,hwall,screen);
- apply_surface(200,0,hwall,screen);
- apply_surface(250,0,hwall,screen);
- apply_surface(450,0,hwall,screen);
- apply_surface(500,0,hwall,screen);
- apply_surface(600,0,hwall,screen);
- apply_surface(700,0,hwall,screen);
- //room 1 left walls
- apply_surface(0,0,vwall,screen);
- apply_surface(0,100,vwall,screen);
- apply_surface(0,200,vwall,screen);
- apply_surface(0,300,vwall,screen);
- apply_surface(0,400,vwall,screen);
- apply_surface(0,500,vwall,screen);
- apply_surface(0,600,vwall,screen);
- apply_surface(0,700,vwall,screen);
- //room 1 right walls
- apply_surface(780,0,vwall,screen);
- apply_surface(780,100,vwall,screen);
- apply_surface(780,200,vwall,screen);
- apply_surface(780,300,vwall,screen);
- apply_surface(780,400,vwall,screen);
- apply_surface(780,500,vwall,screen);
- apply_surface(780,600,vwall,screen);
- apply_surface(780,700,vwall,screen);
- apply_surface( x, y, Hero, screen );
- */
- }
- //////////////////////
- int main( int argc, char* argv[] )
- {
- SOCKET Socket;
- bool RedOrBlue;
- //Make sure the program waits for a quit
- bool quit = false;
- Player player1;
- Player2 player2;
- apply_surface(0,0,background,screen);
- //Initialize
- if( init() == false )
- {
- return 1;
- }
- //Load the files
- if( load_files() == false )
- {
- return 1;
- }
- SDL_EnableUNICODE( SDL_ENABLE );
- //Connect to partner
- apply_surface(210, 210,JoinHost,screen);
- SDL_Flip( screen );
- bool hmm = false;
- //While the user hasn't quit
- while( hmm == false )
- {//1
- //While there's an event to handle
- if( SDL_PollEvent( &event ))
- {
- //If a key was pressed
- if( event.type == SDL_KEYDOWN )
- {
- //If the key is a number
- if( event.key.keysym.unicode == (Uint16)106 )
- {
- apply_surface(0,0,background,screen);
- SDL_Flip( screen );
- if (Socket = Client())
- {
- return 0;
- }
- RedOrBlue = true;
- hmm = true;
- }
- else if( event.key.keysym.unicode == (Uint16)104 )
- {
- apply_surface(0,0,background,screen);
- SDL_Flip( screen );
- if (Socket = Server())
- {
- return 0;
- }
- RedOrBlue = false;
- hmm = true;
- }
- }
- //If the user has Xed out the window
- if( event.type == SDL_QUIT )
- {
- //Quit the program
- clean_up();
- return 0;
- }
- }
- }
- //Client****************************************************
- if (RedOrBlue == true)
- {
- apply_surface(0,0,gbackground,screen);
- apply_surface(player1.x,player1.y,Player1,screen);
- apply_surface(player2.x,player2.y,Player_2,screen);
- //Update the screen
- if( SDL_Flip( screen ) == -1 )
- {
- return 1;
- }
- SDL_EnableUNICODE( SDL_DISABLE );
- //While the user hasn't quit
- while( quit == false )
- {//1
- //While there's an event to handle
- if( SDL_PollEvent( &event ))
- {
- char buffer[1];
- memset(buffer,0,0);
- int inDataLength=recv(Socket,buffer,1,0);
- int input = buffer[0];
- player1.handle_input(input);
- player2.handle_input(Socket);
- //If the user has Xed out the window
- if( event.type == SDL_QUIT )
- {
- //Quit the program
- quit = true;
- }
- }
- player1.move();
- player2.move();
- player1.show();
- player2.show();
- //Update the screen
- if( SDL_Flip( screen ) == -1 )
- {
- return 1;
- }
- }//1
- //Free the surface and quit SDL
- clean_up();
- return 0;
- }
- //Server***********************************************************
- else if (RedOrBlue == false)
- {
- char buffer[1];
- // char input;
- apply_surface(0,0,gbackground,screen);
- apply_surface(player1.x,player1.y,Player1,screen);
- apply_surface(player2.x,player2.y,Player_2,screen);
- //Update the screen
- if( SDL_Flip( screen ) == -1 )
- {
- return 1;
- }
- SDL_EnableUNICODE( SDL_DISABLE );
- //While the user hasn't quit
- while( quit == false )
- {//1
- //While there's an event to handle
- if( SDL_PollEvent( &event ))
- {
- memset(buffer,0,0);
- int inDataLength=recv(Socket,buffer,1,0);
- //input = buffer[0];
- player1.handle_input(Socket);
- player2.handle_input(buffer[0]);
- //If the user has Xed out the window
- if( event.type == SDL_QUIT )
- {
- //Quit the program
- quit = true;
- }
- }
- player1.move();
- player2.move();
- player1.show();
- player2.show();
- //Update the screen
- if( SDL_Flip( screen ) == -1 )
- {
- return 1;
- }
- }//1
- //Free the surface and quit SDL
- clean_up();
- return 0;
- }
- else
- {
- clean_up();
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement