private void timer1_Tick(object sender, EventArgs e) { if (facing1 == "W") { _rect1.Y += 15; } if (facing1 == "S") { _rect1.Y -= 15; } if (facing1 == "A") { _rect1.X -= 15; } if (facing1 == "D") { _rect1.X += 15; } if (facing2 == "W") { _rect2.Y += 15; } if (facing2 == "S") { _rect2.Y -= 15; } if (facing2 == "A") { _rect2.X -= 15; } if (facing2 == "D") { _rect2.X += 15; } var snakeRect = new Rectangle(_rect1.Location.X, _rect1.Location.Y, _rect1.Width, _rect1.Height); var isCollision = snakeRect.Contains(_appleRect.Location.X, _appleRect.Location.Y); if (isCollision) { PacketData.ApplePos pos = new PacketData.ApplePos("1"); _client.TCPSend(pos); PacketData.Score scorePacket = new PacketData.Score("1.1"); _client.TCPSend(scorePacket); } snakeRect = new Rectangle(_rect2.Location.X, _rect2.Location.Y, _rect2.Width, _rect2.Height); isCollision = snakeRect.Contains(_appleRect.Location.X, _appleRect.Location.Y); if (isCollision) { PacketData.ApplePos pos = new PacketData.ApplePos("1"); _client.TCPSend(pos); PacketData.Score scorePacket = new PacketData.Score("1.2"); _client.TCPSend(scorePacket); } Invalidate(); }
void SnakeScore(int clientid1, int clientid2, PacketData.Score input, object clientObj) { Client client = (Client)clientObj; string inputStr = input.input.ToString(); string player = inputStr.Substring(inputStr.IndexOf(".") + 1); games[client.gameID].score(player); if (player == "1") { int score = games[client.gameID].ReturnScore(player); PacketData.Score scorePacket = new PacketData.Score(score.ToString() + "." + player); send(scorePacket, clients[clientid1]); send(scorePacket, clients[clientid2]); } else if (player == "2") { int score = games[client.gameID].ReturnScore(player); PacketData.Score scorePacket = new PacketData.Score(score + "." + player); send(scorePacket, clients[clientid1]); send(scorePacket, clients[clientid2]); } }
public void ProcessServerResponse() { int numOfIncomingBytes = 0; while ((numOfIncomingBytes = reader.ReadInt32()) != 0) { byte[] buffer = reader.ReadBytes(numOfIncomingBytes); memoryStream.Write(buffer, 0, numOfIncomingBytes); memoryStream.Position = 0; binaryFormatter.Binder = new MyBinder.MyBinder(); PacketData.Packet rawPacket = (PacketData.Packet)binaryFormatter.Deserialize(memoryStream); memoryStream.SetLength(0); switch (rawPacket.type) { case PacketData.PacketType.CHATMESSAGE: PacketData.ChatMessagePacket packet = (PacketData.ChatMessagePacket)rawPacket; messageForm.UpdateChatWindow(packet.message); break; case PacketData.PacketType.GAME: startSnakeGame(); break; case PacketData.PacketType.SNAKEGAME: Socket udpSocket = udpClient.Client; PacketData.LoginPacket loginpacket = new PacketData.LoginPacket((IPEndPoint)udpSocket.LocalEndPoint); TCPSend(loginpacket); PacketData.GameMovement gamemovement = (PacketData.GameMovement)rawPacket; snakeGame.UpdateSnake(gamemovement.input); break; case PacketData.PacketType.SCORE: PacketData.Score score = (PacketData.Score)rawPacket; snakeGame.UpdateScore(score.input); break; case PacketData.PacketType.APPLEPOS: PacketData.ApplePos pos = (PacketData.ApplePos)rawPacket; snakeGame.SetApplePos(pos.pos); break; case PacketData.PacketType.PLAYER: PacketData.Player player = (PacketData.Player)rawPacket; snakeGame.startingText(player.player); break; case PacketData.PacketType.STOPGAME: snakeGame.Close(); break; case PacketData.PacketType.QUIT: TCPStop(); //PacketData.QuitPacket quitPacket = (PacketData.QuitPacket)rawPacket; //messageForm.UpdateChatWindow(quitPacket.message); break; default: break; } } }
void UDPServerResponse() { //IPEndPoint remoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0); //Byte[] receiveBytes = udpClient.Receive(ref remoteIpEndPoint); int numOfIncomingBytes = 0; byte[] bytes = new byte[256]; while ((numOfIncomingBytes = reader.ReadInt32()) != 0) { byte[] buffer = reader.ReadBytes(numOfIncomingBytes); memoryStream.Write(buffer, 0, numOfIncomingBytes); memoryStream.Position = 0; binaryFormatter.Binder = new MyBinder.MyBinder(); PacketData.Packet rawPacket = (PacketData.Packet)binaryFormatter.Deserialize(memoryStream); memoryStream.SetLength(0); switch (rawPacket.type) { case PacketData.PacketType.LOGIN: PacketData.LoginPacket loginPacket = (PacketData.LoginPacket)rawPacket; HandlePacket(loginPacket.endPoint); break; case PacketData.PacketType.CHATMESSAGE: PacketData.ChatMessagePacket packet = (PacketData.ChatMessagePacket)rawPacket; messageForm.UpdateChatWindow(packet.message); break; case PacketData.PacketType.GAME: startSnakeGame(); break; case PacketData.PacketType.SNAKEGAME: PacketData.GameMovement gameMovement = (PacketData.GameMovement)rawPacket; snakeGame.UpdateSnake(gameMovement.input); break; case PacketData.PacketType.SCORE: PacketData.Score score = (PacketData.Score)rawPacket; snakeGame.UpdateScore(score.input); break; case PacketData.PacketType.APPLEPOS: PacketData.ApplePos pos = (PacketData.ApplePos)rawPacket; snakeGame.SetApplePos(pos.pos); break; case PacketData.PacketType.PLAYER: PacketData.Player player = (PacketData.Player)rawPacket; snakeGame.startingText(player.player); break; case PacketData.PacketType.STOPGAME: snakeGame.CloseForm(); //PacketData.StopGame stopgame = new PacketData.StopGame(); //TCPSend(stopgame); //snakeGame.Close(); break; case PacketData.PacketType.QUIT: UDPStop(); break; } } }
void TCPClientMethod(object clientObj) { int numOfIncomingBytes = 0; Client client = (Client)clientObj; while ((numOfIncomingBytes = client.reader.ReadInt32()) != 0) { byte[] buffer = client.reader.ReadBytes(numOfIncomingBytes); memoryStream.Write(buffer, 0, numOfIncomingBytes); memoryStream.Position = 0; binaryFormatter.Binder = new MyBinder.MyBinder(); PacketData.Packet rawPacket = (PacketData.Packet)binaryFormatter.Deserialize(memoryStream); memoryStream.SetLength(0); switch (rawPacket.type) { case PacketData.PacketType.CHATMESSAGE: PacketData.ChatMessagePacket packet = (PacketData.ChatMessagePacket)rawPacket; if (packet.message.Substring(0, 1) == "/") { Commands(packet, client); } if (client.gameID != -1 && games[client.gameID].state == GameState.GS_ACTIVE) { if (games[client.gameID].type == GameType.GT_RPS) { if (client.id == games[client.gameID].clientID1 && games[client.gameID].client1Input == null) { games[client.gameID].client1Input = packet.message; } if (client.id == games[client.gameID].clientID2 && games[client.gameID].client2Input == null) { games[client.gameID].client2Input = packet.message; } PlayRPS(games[client.gameID].clientID1, games[client.gameID].clientID2); } } else { foreach (Client element in clients) { if (element.gameID == -1) { PacketData.ChatMessagePacket packet1 = new PacketData.ChatMessagePacket(client.name + ": " + packet.message); send(packet1, element); } } } break; case PacketData.PacketType.NICKNAME: PacketData.NickNamePakcet nickNamePacket = (PacketData.NickNamePakcet)rawPacket; PacketData.ChatMessagePacket newPacket = new PacketData.ChatMessagePacket(client.Rename(client, nickNamePacket.nickName)); foreach (Client element in clients) { send(newPacket, element); } break; case PacketData.PacketType.SNAKEGAME: foreach (Game element in games) { if (element.clientID1 == client.id) { PacketData.GameMovement gamePacket = (PacketData.GameMovement)rawPacket; PlaySnake(element.clientID1, element.clientID2, gamePacket.input, "1"); } else if (element.clientID2 == client.id) { PacketData.GameMovement gamePacket = (PacketData.GameMovement)rawPacket; PlaySnake(element.clientID1, element.clientID2, gamePacket.input, "2"); } } break; case PacketData.PacketType.SCORE: foreach (Game element in games) { if (element.clientID1 == client.id || element.clientID2 == client.id) { PacketData.Score scorepacket = (PacketData.Score)rawPacket; SnakeScore(element.clientID1, element.clientID2, scorepacket, client); } } break; case PacketData.PacketType.APPLEPOS: foreach (Game element in games) { if (element.clientID1 == client.id || element.clientID2 == client.id) { SnakeGameApple(element.clientID1, element.clientID2); } } break; case PacketData.PacketType.PLAYER: foreach (Game element in games) { if (element.clientID1 == client.id) { PacketData.Player playerPacket = new PacketData.Player(1); send(playerPacket, client); } else if (element.clientID2 == client.id) { PacketData.Player playerPacket = new PacketData.Player(2); send(playerPacket, client); } } break; case PacketData.PacketType.LOGIN: PacketData.LoginPacket login = (PacketData.LoginPacket)rawPacket; HandlePacket(client, login.endPoint); break; case PacketData.PacketType.STOPGAME: PacketData.StopGame stop = (PacketData.StopGame)rawPacket; send(stop, client); break; case PacketData.PacketType.QUIT: PacketData.QuitPacket quit = (PacketData.QuitPacket)rawPacket; string message = client.name + " " + quit.message; PacketData.ChatMessagePacket newMessage = new PacketData.ChatMessagePacket(message); //PacketData.QuitPacket quitMessage = new PacketData.QuitPacket(client.name + quit.message); //send(quitMessage, client); client.tcpSocket.Close(); clients.Remove(client); foreach (Client element in clients) { send(newMessage, element); } break; } } }