Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 WindowsFormsApp17
    {
        public partial class Form1 : Form
        {
            public Form1()
            {


                InitializeComponent();
                CenterToScreen();
                Bullet.Visible = false;
            }
           ;
            Boolean Up, Lef, Righ, Down;
            private void Form1_KeyDown(object sender, KeyEventArgs e)
                    
        
            if (e.KeyCode == Keys.Z)
            {
               Bullet.Location = new Point(Gun.Location.X + 7,Gun.Location.Y - 20);
                Bullet.Visible = true;

            }
            if (e.KeyCode == Keys.Right)
            {
                Righ = true;
                Gun.Left = Gun.Left + 8;

            }
            if (e.KeyCode == Keys.Up)
            {
                Up = true;
                Gun.Top = Gun.Top + -8;
            }
            if (e.KeyCode == Keys.Down)
            {
                Down = true;
                Gun.Top =Gun.Top + 8;
            }
            if (e.KeyCode == Keys.Left)
            {
                Lef = true;
                Gun.Left = Gun.Left + -8;

            }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {

            Bullet.Top -= 20;
        }

well.....this is my code and I want to make this "Gun" shoot multiple bullets when you press a key.

What I have tried:

I've tried some scripts......but they only shoot one bullet at a time when you press and hold the key. I want my "Gun" to shoot lots of bullets like a fully automatic rifle when you press a key and hold it.
Posted
Updated 21-Apr-20 2:33am
v2
Comments
F-ES Sitecore 21-Apr-20 6:02am    
You're going to need the ability to have multiple bullets and every "x" milliseconds of the fire key being held you create a new bullet that has its own position. You can hold instances of the bullet in a List and every timer click go through the list of bullets and move them\detect hits etc.
THEmaker2 21-Apr-20 7:28am    
Is it alright if I ask you to give me an example code?
THEmaker2 21-Apr-20 19:25pm    
Thanks for the sample code! It works when the bullet represents a label. However, it makes an error if I change it to a picturebox. Also, whenever I change the timerControl interval or timerBullet interval...it stops the project and makes an error

Basic sample code of managing a dynamic collection of items

C#
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private List<Bullet> Bullets;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Bullets = new List<Bullet>();

            timerControls.Interval = 500;
            timerControls.Start();

            timerBullets.Interval = 100;
            timerBullets.Start();

        }

        private void timerBullets_Tick(object sender, EventArgs e)
        {
            // we'll process the bullets on it's own thread to keep the UI thread free for input
            System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(ProcessBullets));
            t.Start();
        }

        private void ProcessBullets()
        {
            // update all bullets
            foreach (var b in Bullets)
            {
                b.Update();
            }

            var deadBullets = Bullets.Where(b => b.Destroy).ToList();
            if (deadBullets.Count == 0)
            {
                return;
            }

            // We use Invoke to run the code on the same thread that created the bullet
            deadBullets.First().BulletUI.Invoke((MethodInvoker)delegate { deadBullets.ForEach(b => this.Controls.Remove(b.BulletUI)); });
            Bullets.RemoveAll(b => b.Destroy);
        }

        private void timerControls_Tick(object sender, EventArgs e)
        {
            System.Diagnostics.Debug.WriteLine(System.Threading.Thread.CurrentThread.GetHashCode());
            Bullet b = new Bullet();
            // hard-coded position, this will be the position of your gun
            b.Location = new Point(10, 200);
            b.Speed = 10;

            // I'm using a label to represent the bullet, you can use an image or anything else
            Label lbl = new Label { Text = "X", Left = b.Location.X, Top = b.Location.Y };
            b.BulletUI = lbl;
            this.Controls.Add(lbl);
            Bullets.Add(b);
        }
    }

    public class Bullet
    {
        public Point Location { get; set; }
        public Label BulletUI { get; set; }
        public DateTime Created { get; set; }
        public int Speed { get; set; }
        public bool Destroy { get; private set; }

        public Bullet()
        {
            Created = DateTime.Now;
        }

        public void Update()
        {
            Location = new Point(Location.X, Location.Y - Speed);

            if (Location.Y <= 0 || (DateTime.Now - Created).TotalMilliseconds >= 5000)
            {
                Destroy = true;
                return;
            }

            // We use Invoke to run the code on the same thread that created the bullet
            BulletUI.Invoke((MethodInvoker)delegate { BulletUI.Left = Location.X; BulletUI.Top = Location.Y; });
        }
    }
}
 
Share this answer
 
Comments
THEmaker2 21-Apr-20 8:55am    
Thanks!
I would do it like this :
With the Button of your choice you set a variable "ShotRepeater" to true with the KeyDown-Event and set it to false with the KeyUp-Event.
Inside your Timer-Event you check if the "ShotRepeater" is true and if yes release a shot every Timer-Tick ...
For this your Timer-Handler should only have the right Tick-rate for your purpose ...
 
Share this answer
 
Comments
THEmaker2 21-Apr-20 6:32am    
Well....I tried it...but it didn't work
Ralf Meier 22-Apr-20 2:06am    
In this case you should show your code which doesn't work ...
Basicly my Suggestion works as described ...
THEmaker2 22-Apr-20 2:11am    
Well....maybe I didn't understand what you were saying...can you give me a sample code?
Ralf Meier 23-Apr-20 3:39am    
Not in the moment - sorry ... but I think that my desciption is unique ...
If it doesn't work for you we should look at your code ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900