Onion13 31 Junior Poster in Training

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();

        }
    }
}

Dani AI

Generated

— overlapping shot sounds come from two things in your post: trying to loop inside a key event (the while(true) approach) and re-creating/playing the sound every frame without any rate limit. Don’t block the UI thread with loops in KeyDown. Use your existing game timer as the game loop, enforce a firing cooldown (milliseconds between shots), and load the sound once so you avoid file I/O every shot.

A simple, immediately-applicable pattern: preload a single SoundPlayer, track time since the last shot with a Stopwatch or timestamp, and in your timer tick fire only when the cooldown has passed. Play the sound on a background thread so the UI stays responsive and you don’t recreate the player for every shot.

private readonly System.Media.SoundPlayer shotSound;
private readonly Stopwatch fireTimer = Stopwatch.StartNew();
private int fireCooldownMs = 250;
private long lastFireMs = -1000;

public Form1()
{
    InitializeComponent();
    shotSound = new System.Media.SoundPlayer(Properties.Resources.shooting); // or load from file
    shotSound.Load();
}

private void gameTimer_Tick(object sender, EventArgs e)
{
    // existing movement/MoveShot()/HitorMiss() calls

    if (space && (fireTimer.ElapsedMilliseconds - lastFireMs) >= fireCooldownMs)
    {
        lastFireMs = fireTimer.ElapsedMilliseconds;
        CreateBullet(); // call whatever creates the PictureBox for a shot
        Task.Run(() => shotSound.PlaySync()); // PlaySync on background thread to avoid overlap on UI thread
    }
}

Notes and troubleshooting:

  • Remove any while(true) inside event handlers — that freezes the form.
  • Pick a cooldown >= sound length if you want no overlapping. If you want overlapping sounds (multiple simultaneous shots), use an audio library (e.g., NAudio) or create separate playback instances and mix them; SoundPlayer is limited.
  • Use resources or call Load() once to avoid repeated disk reads.
  • Tune fireCooldownMs to match gameplay feel and sound duration.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.