private void playerMovement_Tick(object sender, EventArgs e) // Timer handles all player movements and inputs { if (Keyboard.IsKeyDown(Key.Left) || Keyboard.IsKeyDown(Key.A)) // Move left { if (player.Location.X > 20) // Limit movement within game area { player.Location = new Point(player.Location.X - 3, player.Location.Y); p1.SetPos(player.Location); } } else if (Keyboard.IsKeyDown(Key.Right) || Keyboard.IsKeyDown(Key.D)) // Move right { if (player.Location.X < this.Width - rightSideDifference) // Limit movement within game area { player.Location = new Point(player.Location.X + 3, player.Location.Y); p1.SetPos(player.Location); } } if (Keyboard.IsKeyDown(Key.Up) || Keyboard.IsKeyDown(Key.W)) // Shoot projectile { if (!p1.IsFired()) { p1.Fire(true); playerProjectile.Location = new Point(p1.GetPos('x') + (25), p1.GetPos('y')); playerProjectile.Visible = playerProj.SetVisibility(true); playerProjectile.Visible = true; playerProj.SetPos(p1.GetPos('x') + 25, 'x'); playerProj.SetPos(p1.GetPos('y'), 'y'); PlaySound(3); } } if (p1.IsFired()) { playerProjectileGhost.Location = new Point(playerProj.GetPos('x') - projectileGhostOffset, playerProj.GetPos('y') - projectileSpeed); playerProjectile.Location = new Point(playerProj.GetPos('x'), playerProj.GetPos('y') - projectileSpeed); playerProj.SetPos(playerProj.GetPos('y') - projectileSpeed, 'y'); p1.Fire(OutOfBoundsCheck()); } }
private bool OutOfBoundsCheck(Projectile p) // Checks for out of bounds projectile { // Player projectile check if (p.GetPos('y') < 67) { p.SetVisibility(false); return(false); } else { return(true); } }
private void playerMovement_Tick(object sender, EventArgs e) // Timer handles all player movements and inputs { // Player 1 movement keys if (Keyboard.IsKeyDown(Key.A)) // Move left { if (player1.Location.X > 20) // Limit movement within game area { player1.Location = new Point(player1.Location.X - 3, player1.Location.Y); p1.SetPos(player1.Location); } } else if (Keyboard.IsKeyDown(Key.D)) // Move right { //if (player1.Location.X < this.Width - rightSideDifference) { // Limit movement within game area if (player1.Location.X < this.Width / 2 - player1DivideDifference) { player1.Location = new Point(player1.Location.X + 3, player1.Location.Y); p1.SetPos(player1.Location); } } if (Keyboard.IsKeyDown(Key.W) && p1.GetLives() > 0) // Shoot projectile { if (!p1.IsFired()) { p1.Fire(true); player1Projectile.Location = new Point(p1.GetPos('x') + (25), p1.GetPos('y')); player1Projectile.Visible = player1Proj.SetVisibility(true); player1Projectile.Visible = true; player1Proj.SetPos(p1.GetPos('x') + 25, 'x'); player1Proj.SetPos(p1.GetPos('y'), 'y'); PlaySound(3); } } // If player 1 fired... if (p1.IsFired()) { player1ProjectileGhost.Location = new Point(player1Proj.GetPos('x') - projectileGhostOffset, player1Proj.GetPos('y') - projectileSpeed); player1Projectile.Location = new Point(player1Proj.GetPos('x'), player1Proj.GetPos('y') - projectileSpeed); player1Proj.SetPos(player1Proj.GetPos('y') - projectileSpeed, 'y'); p1.Fire(OutOfBoundsCheck(player1Proj)); } // Player 2 movement keys if (Keyboard.IsKeyDown(Key.Left)) // Move left { if (player2.Location.X > this.Width / 2) // Limit movement within game area { player2.Location = new Point(player2.Location.X - 3, player2.Location.Y); p2.SetPos(player2.Location); } } else if (Keyboard.IsKeyDown(Key.Right)) // Move right { if (player2.Location.X < this.Width - rightSideDifference) { player2.Location = new Point(player2.Location.X + 3, player2.Location.Y); p2.SetPos(player2.Location); } } if (Keyboard.IsKeyDown(Key.Up) && p2.GetLives() > 0) // Shoot projectile { if (!p2.IsFired()) { p2.Fire(true); player2Projectile.Location = new Point(p2.GetPos('x') + (25), p2.GetPos('y')); player2Projectile.Visible = player2Proj.SetVisibility(true); player2Projectile.Visible = true; player2Proj.SetPos(p2.GetPos('x') + 25, 'x'); player2Proj.SetPos(p2.GetPos('y'), 'y'); PlaySound(3); } } // If player 2 fired... if (p2.IsFired()) { player2ProjectileGhost.Location = new Point(player2Proj.GetPos('x') - projectileGhostOffset, player2Proj.GetPos('y') - projectileSpeed); player2Projectile.Location = new Point(player2Proj.GetPos('x'), player2Proj.GetPos('y') - projectileSpeed); player2Proj.SetPos(player2Proj.GetPos('y') - projectileSpeed, 'y'); p2.Fire(OutOfBoundsCheck(player2Proj)); } }