![]() |
TX Library Help – Version: 00173a, Revision: 174
|
00001 //{=========================================================================== 00012 //}=========================================================================== 00013 // server.cpp 00014 00015 #include "proto.h" 00016 00017 void RunGame (TX_SOCKET peer_sock); 00018 void DrawPlayer (Player_t player, COLORREF color); 00019 void DrawBall (Ball_t ball); 00020 void MoveBall (Ball_t* ball); 00021 void BallInterPlayers (Ball_t* ball, Player_t player1, Player_t player2); 00022 00023 int main() 00024 { 00025 txCreateWindow (1024, 768); 00026 txBegin(); 00027 00028 txSetColor (TX_LIGHTGREEN); 00029 txSetTextAlign (TA_CENTER); 00030 txTextOut (500, 380, "I am a server!"); 00031 txTextOut (500, 450, "Waiting for client..."); 00032 txSleep (0); 00033 00034 TX_SOCKET client = txCreateSocket (TX_SERVER, "", TX_STD_PORT, TX_BLOCK, false); 00035 00036 if (txnAssert (client) == TXN_NOT_CREATED) 00037 { 00038 printf ("ERROR: BAD SOCKET!!!"); 00039 return -1; 00040 } 00041 00042 RunGame (client); 00043 00044 return 0; 00045 } 00046 00047 void RunGame (TX_SOCKET peer_sock) 00048 { 00049 Player_t me = {512, 20, 0}; 00050 Player_t peer = {512, 748, 0}; 00051 00052 Ball_t ball = {100, 100, 15, 15}; 00053 00054 DWORD timer = 0; 00055 00056 while (!GetAsyncKeyState (VK_ESCAPE) && peer.action != -1) 00057 { 00058 txSetFillColor (TX_BLACK); 00059 txClear(); 00060 00061 txSetColor (TX_LIGHTGREEN); 00062 txTextOut (500, 380, "I am a server!"); 00063 txTextOut (500, 450, "Use [Left], [Right]. [Escape] to exit."); 00064 00065 if (GetAsyncKeyState (VK_LEFT) && me.x > 50) me.x -= 15; 00066 if (GetAsyncKeyState (VK_RIGHT) && me.x < 1019) me.x += 15; 00067 00068 MoveBall (&ball); 00069 00070 DrawBall (ball); 00071 DrawPlayer (me, TX_LIGHTGREEN); 00072 DrawPlayer (peer, TX_PINK); 00073 00074 BallInterPlayers (&ball, peer, me); 00075 00076 timer = GetTickCount(); 00077 00078 txSendTo (peer_sock, &me, sizeof (me)); 00079 txRecvFrom (peer_sock, &peer, sizeof (peer)); 00080 txSendTo (peer_sock, &ball, sizeof (ball)); 00081 00082 while (GetTickCount() - timer < 15); 00083 txSleep (0); 00084 } 00085 00086 me.action = -1; 00087 txSendTo (peer_sock, &me, sizeof (me)); 00088 } 00089 00090 void DrawPlayer (Player_t player, COLORREF color) 00091 { 00092 txSetColor (color); 00093 txSetFillColor (color); 00094 00095 txRectangle (player.x - 100, player.y - 10, player.x + 100, player.y + 10); 00096 } 00097 00098 void DrawBall (Ball_t ball) 00099 { 00100 txSetColor (TX_LIGHTCYAN); 00101 txSetFillColor (TX_LIGHTCYAN); 00102 00103 txCircle (ball.x, ball.y, 20); 00104 } 00105 00106 void MoveBall (Ball_t* ball) 00107 { 00108 if (ball->x < 0) { ball->vx = -ball->vx; ball->x = 0; } 00109 if (ball->x > 1024) { ball->vx = -ball->vx; ball->x = 1024; } 00110 if (ball->y < 0) { ball->vy = -ball->vy; ball->y = 0; } 00111 if (ball->y > 768) { ball->vy = -ball->vy; ball->y = 768; } 00112 00113 ball->x += ball->vx; 00114 ball->y += ball->vy; 00115 } 00116 00117 void BallInterPlayers (Ball_t* ball, Player_t player1, Player_t player2) 00118 { 00119 if (player1.x - 100 < ball->x && ball->x < player1.x + 100 && player1.y - 25 < ball->y) 00120 ball->vy = -ball->vy; 00121 00122 if (player2.x - 100 < ball->x && ball->x < player2.x + 100 && player2.y + 25 > ball->y) 00123 ball->vy = -ball->vy; 00124 }