TX Library Help – Version: 00173a, Revision: 173
|
00001 //{=========================================================================== 00014 //}=========================================================================== 00015 00016 // client.cpp 00017 00018 #include "proto.h" 00019 00020 #define SERVER_IP "127.0.0.1" // Измените на IP-адрес сервера 00021 00022 void RunGame (TX_SOCKET peer_sock); 00023 void DrawPlayer (Player_t player, COLORREF color); 00024 void DrawBall (Ball_t ball); 00025 00026 int main() 00027 { 00028 txCreateWindow (1024, 768); 00029 txBegin(); 00030 00031 txSetColor (TX_LIGHTGREEN); 00032 txSetTextAlign (TA_CENTER); 00033 txTextOut (500, 380, "I am a server!"); 00034 txTextOut (500, 450, "Waiting for client..."); 00035 txSleep (0); 00036 00037 TX_SOCKET client = txCreateSocket (TX_CLIENT, SERVER_IP, TX_STD_PORT, TX_BLOCK, false); 00038 00039 if (txnAssert (client) == TXN_NOT_CREATED) 00040 { 00041 printf ("ERROR: BAD SOCKET!!!"); 00042 return -1; 00043 } 00044 00045 RunGame (client); 00046 00047 return 0; 00048 } 00049 00050 void RunGame (TX_SOCKET peer_sock) 00051 { 00052 Player_t me = {512, 748, 0}; 00053 Player_t peer = {512, 20, 0}; 00054 00055 Ball_t ball = {100, 100, 15, 15}; 00056 00057 DWORD timer = 0; 00058 00059 while (!GetAsyncKeyState (VK_SPACE) && peer.action != -1) 00060 { 00061 txSetFillColor (TX_BLACK); 00062 txClear(); 00063 00064 txSetColor (TX_LIGHTGREEN); 00065 txTextOut (500, 380, "I am a client!"); 00066 txTextOut (500, 450, "Use [A], [D]. [Space] to exit."); 00067 00068 if (GetAsyncKeyState ('A') && me.x > 50) me.x -= 15; 00069 if (GetAsyncKeyState ('D') && me.x < 1019) me.x += 15; 00070 00071 DrawBall (ball); 00072 DrawPlayer (me, TX_LIGHTGREEN); 00073 DrawPlayer (peer, TX_PINK); 00074 00075 timer = GetTickCount(); 00076 00077 txRecvFrom (peer_sock, &peer, sizeof (peer)); 00078 txSendTo (peer_sock, &me, sizeof (me)); 00079 txRecvFrom (peer_sock, &ball, sizeof (ball)); 00080 00081 while (GetTickCount() - timer < 15); 00082 txSleep (0); 00083 } 00084 00085 me.action = -1; 00086 txSendTo (peer_sock, &me, sizeof (me)); 00087 } 00088 00089 void DrawPlayer (Player_t player, COLORREF color) 00090 { 00091 txSetColor (color); 00092 txSetFillColor (color); 00093 00094 txRectangle (player.x - 100, player.y - 10, player.x + 100, player.y + 10); 00095 } 00096 00097 void DrawBall (Ball_t ball) 00098 { 00099 txSetColor (TX_LIGHTCYAN); 00100 txSetFillColor (TX_LIGHTCYAN); 00101 00102 txCircle (ball.x, ball.y, 20); 00103 } 00104