Working on a video game using visual studio and I am having trouble with the sound effect when the player shoots their weapon. When the player shoots multiple times, the sound overlaps. Ive tried many things and this was my last attempt. Can someone please help me? I am stuck.
if (e.KeyCode == Keys.Space)
{
while(true)
{
bossTimer.Stop();
space = true;
Bullet();
fire = new SoundPlayer("shooting.wav");
fire.Play();
bossTimer.Start();
}
Below is my program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace boss_fight
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
bool right, left, up, down, space, A;
int miss = 10;//number of missiles
void HitorMiss()
{
foreach (Control j in this.Controls)
{
foreach (Control i in this.Controls)
{
if (j is PictureBox && j.Tag == "shot" || j is PictureBox && j.Tag == "missile")
{
if (i is PictureBox && i.Tag == "boss")
{
if(j.Bounds.IntersectsWith(i.Bounds))
{
if (enemyhealth.Value == 0)
{
PictureBox bullet = new PictureBox();
bullet.SizeMode = PictureBoxSizeMode.AutoSize;
bullet.Image = Properties.Resources.Shoot;
bullet.BackColor = System.Drawing.Color.Transparent;
((PictureBox)j).Image = Properties.Resources.explosion;
}
else
enemyhealth.Value -= 10;
}
}
}
}
if (Player.Bounds.IntersectsWith(e1laser.Bounds))//Player loses health when hit by boss laser
{
if (healthbar.Value == 0)
{
PictureBox bullet = new PictureBox();
bullet.SizeMode = PictureBoxSizeMode.AutoSize;
bullet.Image = Properties.Resources.Shoot;
bullet.BackColor = System.Drawing.Color.Transparent;
((PictureBox)Player).Image = Properties.Resources.explosion;
}
else
healthbar.Value -= 10;
}
}
}
void Shot()//positions where players laser shows on the screen
{
PictureBox ammo = new PictureBox();
ammo.SizeMode = PictureBoxSizeMode.AutoSize;
ammo.Image = Properties.Resources.Shoot;
ammo.BackColor = System.Drawing.Color.Transparent;
ammo.Tag = "shot";
ammo.Left = Player.Left + 100;//moves bullet left or right
ammo.Top = Player.Top + 55;//55 is perfect
this.Controls.Add(ammo);
ammo.BringToFront();
}
void MoveShot()
{
foreach (Control x in this.Controls)
{
if (x is PictureBox && x.Tag == "shot")
{
x.Left += 100;//x.Top -= 10;
if (x.Top > 900)//<100
{
this.Controls.Remove(x);
}
}
}
shoot.Left -= 30;
e1laser.Left -= 30;
if (e1laser.Left < 0)
{
e1laser.Left = BOSS.Left;
e1laser.Top = BOSS.Top + 25;
}
zmissile1.Left = zmissile1.Left -= 100;
zmissile1.Top = zmissile1.Top - 100;
if(zmissile1.Left < 0)
{
zmissile1.Left = BOSS.Left;
zmissile1.Top = BOSS.Top + 10;//+10
}
zmissile2.Left -= 100;
if(zmissile2.Left < 0)
{
zmissile2.Left = BOSS.Left;
zmissile2.Top = BOSS.Top + 50;//+10
}
zmissile3.Left = zmissile3.Left -= 100;
zmissile3.Top = zmissile3.Top + 100;
if (zmissile3.Left < 0)
{
zmissile3.Left = BOSS.Left;
zmissile3.Top = BOSS.Top + 70;//+10
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
right = false;
}
if (e.KeyCode == Keys.Left)
{
left = false;
}
if (e.KeyCode == Keys.Up)
{
up = false;
}
if (e.KeyCode == Keys.Down)
{
down = false;
}
if (e.KeyCode == Keys.Space)
{
space = false;
}
if (e.KeyCode == Keys.A)
{
A = false;
}
}
void Playermovement()
{
if (right == true)
{
if (Player.Left < 900)
{
Player.Left += 20;
}
}
if (left == true)
{
if (Player.Left > 5)
{
Player.Left -= 15;
}
}
if (up == true)
{
if (Player.Top > 20)
{
Player.Top -= 20;
}
}
if (down == true)
{
if (Player.Top < 550)
{
Player.Top += 20;
}
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Right)
{
right = true;
}
if (e.KeyCode == Keys.Left)
{
left = true;
}
if (e.KeyCode == Keys.Up)
{
up = true;
}
if (e.KeyCode == Keys.Down)
{
down = true;
}
if (e.KeyCode == Keys.Space)
{
space = true;
Shot();
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Playermovement();
MoveShot();
HitorMiss();
}
}
}