private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.W) { WPressed = true; } if (e.KeyCode == Keys.S) { SPressed = true; } if (e.KeyCode == Keys.Space && !isReloading) { if (!playSolo) { ClientUnit.Send(ClientUnit.socket, ClientUnit.PacketInfo.Shoot); isReloading = true; reloadProgress = 0; } else { shoot(shellID_Solo++, player.ID); isReloading = true; reloadProgress = 0; } } }
private void timer2_Tick(object sender, EventArgs e) { Random random = new Random(); if (isServer) { ClientUnit.Send(ClientUnit.socket, ClientUnit.PacketInfo.EnemyCreate, " " + random.Next(550, 700) + " " + random.Next(50, 350) + " " + (random.Next(1, 5) * 10) + " " + random.Next(1, 3) + " " + random.Next(1, 3)); } if (playSolo) { enemies.Add(createNewEnemy(enemyID_Solo++, random.Next(550, 700), random.Next(50, 350), random.Next(1, 5) * 10, random.Next(1, 3), random.Next(1, 3))); } }
private void Form1_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { if (firstConntect) { try { disconEvt.Reset(); ClientUnit.Send(ClientUnit.socket, ClientUnit.PacketInfo.Disconnect); disconEvt.WaitOne(); if (isServer) { threadServer.Abort(); } } catch { } try { ClientUnit.thread.Abort(); ClientUnit.socket.Shutdown(SocketShutdown.Both); ClientUnit.socket.Close(); } catch { } try { threadClient.Abort(); if (isServer) { for (int i = 0; i < Server.clients.Count; i++) { Server.clients[i].thread.Abort(); } } } catch { } e.Cancel = true; } Application.Exit(); } }
private void button2_Click(object sender, EventArgs e) { if (firstConntect) { try { disconEvt.Reset(); ClientUnit.Send(ClientUnit.socket, ClientUnit.PacketInfo.Disconnect); disconEvt.WaitOne(); if (isServer) { threadServer.Abort(); } } catch { } try { ClientUnit.thread.Abort(); ClientUnit.socket.Shutdown(SocketShutdown.Both); ClientUnit.socket.Close(); } catch { } try { threadClient.Abort(); if (isServer) { for (int i = 0; i < Server.clients.Count; i++) { Server.clients[i].thread.Abort(); } } } catch { } } Application.Exit(); }
private void timer1_Tick(object sender, EventArgs e) { if (playSolo && enemies.Count <= 0) { Random random = new Random(); enemies.Add(createNewEnemy(enemyID_Solo++, random.Next(550, 700), random.Next(50, 350), random.Next(1, 5) * 10, random.Next(1, 3), random.Next(1, 3))); timer2.Stop(); timer2.Start(); } if (playSolo) { time = time.AddMilliseconds(-25); if (time.Subtract(time1).TotalMilliseconds >= 0) { timerLlabel.Text = time.ToString("mm:ss.fffK"); } else { timerLlabel.Text = time1.ToString("mm:ss.fffK"); timer1.Stop(); timer2.Stop(); } } if (!playSolo && !firstConntect && Server.serverStarted) { firstConntect = true; ClientUnit cu = new ClientUnit(); threadClient = new Thread(cu.Main); //thread.Start(textBox1.Text); threadClient.Start(textBox2.Text + " " + portTextBox1.Text); } if (!timerBlock) { timer2.Start(); timerBlock = true; } List <Enemy> en = new List <Enemy>(); List <Shell> sh = new List <Shell>(); bool moveDone = false; Refresh(); //Произошла отрисовка столкновения. После чего удаляю пулю и врага for (int i = 0; i < shells.Count; i++) { for (int j = 0; j < enemies.Count; j++) { try { if ((shells[i].x + 20 >= enemies[j].x) && (shells[i].x <= enemies[j].x + 20) && (shells[i].y + 5 >= enemies[j].y) && (shells[i].y <= enemies[j].y + 20)) { enemies[j].killedBy = shells[i].whoShoot; Console.WriteLine("killed by " + enemies[j].killedBy + " player ID " + player.ID); if (enemies[j].killedBy == player.ID) { player.score += enemies[j].killBonus; } else { anotherPlayer.score += enemies[j].killBonus; } en.Add(enemies[j]); sh.Add(shells[i]); } } catch { Console.WriteLine(" !!!!!!!!!! Exception in enemies/shells cycle"); } } } if (en.Count > 0) { List <int> killedEnemiesIDs = new List <int>(); List <int> killedShellsIDs = new List <int>(); string package = ""; foreach (Enemy enemy in en) { enemies.Remove(enemy); killedEnemiesIDs.Add(enemy.enemyID); package += " " + enemy.enemyID + " " + enemy.killedBy; } foreach (Shell shell in sh) { shells.Remove(shell); killedShellsIDs.Add(shell.shellID); package += " " + shell.shellID; } if (!playSolo) { ClientUnit.Send(ClientUnit.socket, ClientUnit.PacketInfo.EnemyKilled, package); } } ///// MoveEvent?.Invoke(); if (player != null) { scoreLabel.Text = player.score.ToString(); if (anotherPlayer != null) { anotherPlayerScoreLabel.Text = anotherPlayer.score.ToString(); } if (WPressed) { player.y -= 10; moveDone = true; } if (APressed) { player.x -= 10; moveDone = true; } if (SPressed) { player.y += 10; moveDone = true; } if (DPressed) { player.x += 10; moveDone = true; } if (!playSolo && moveDone) { ClientUnit.Send(ClientUnit.socket, ClientUnit.PacketInfo.Position); } } if (player != null && anotherPlayer != null) { if (player.score >= 200 && anotherPlayer.score < 200) { timer1.Stop(); MessageBox.Show("You win! Congratulations!"); } if (anotherPlayer.score >= 200 && player.score < 200) { timer1.Stop(); MessageBox.Show("You lose =("); } if (player.score >= 200 && anotherPlayer.score >= 200) { timer1.Stop(); MessageBox.Show("Draw"); } } if (isReloading) { reloadProgress += 2; } }