0

How to make my bullet goes form start of X axis to y axis randomized by +3 -3 pixels

public void UpdateBullets()
{       
    //For each bullet in our bulletlist: Update the movment and if the bulet hits side of the screen remove it form the list
    foreach(Bullet bullet in bulletList)
    {
        //set movment for bullet
        bullet.position.X = bullet.position.X + bullet.speed;

        //if bullet hits side of screen, then make it visible to false
        if (bullet.position.X >= 800)
            bullet.isVisible = false;
    }
    //Go thru bulletList and see if any of the bullets are not visible, if they aren't  then remove bullet form bullet list
    for (int i = 0; i < bulletList.Count; i++)
    {
        if(!bulletList[i].isVisible)
        {
            bulletList.RemoveAt(i);
            i--;
        }
    }         
}

When i hold space bullets just go forwards and i want it to strafe little by Y axis as well.

Here is http://www.afrc.af.mil/shared/media/photodb/photos/070619-F-3849K-041.JPG to what i want to do.

Never to hit only in 1 spot. Just to randomize Y axis a bit. What i want to change is

//set movment for bullet
bullet.position.X = bullet.position.X + bullet.speed;

to something like

BUllet.position.X = Bullet.position.X + Bullet.random.next(-3,3).Y + bullet.speed.

Something like that.

javac
  • 2,431
  • 4
  • 17
  • 26
Vladan899
  • 29
  • 1
  • 9
  • bullet.position.Y += random.Next(-2, 2); Makes this possible but Bullets should go straight not wiggle on Y axis. When i add this bullets fly's like some bugs instead of going straight forward. – Vladan899 Sep 01 '14 at 19:32

1 Answers1

0

You need to define a constant value for each bullet representing the change in the y axis over time:

Change your Bullet class

public Class Bullet 
{
    // ....
    public int Ychange;
    System.Random rand;
    public Bullet()
    {
        // ....
        rand = new Random();
        Ychange = rand.Next(-3, 3);
    }

    // Add the above stuff to your class and constructor 
}

New UpdateBullets Method

public void UpdateBullets()
{       
    //For each bullet in our bulletlist: Update the movment and if the bulet hits side of the screen remove it form the list
    foreach(Bullet bullet in bulletList)
    {
        //set movment for bullet
        bullet.position.X += bullet.speed;
        bullet.position.Y += bullet.Ychange; 
        // Above will change at a constant value

        //if bullet hits side of screen, then make it visible to false
        if (bullet.position.X >= 800)
            bullet.isVisible = false;
    }
    //Go thru bulletList and see if any of the bullets are not visible, if they aren't  then remove bullet form bullet list
    for (int i = 0; i < bulletList.Count; i++)
    {
        if(!bulletList[i].isVisible)
        {
            bulletList.RemoveAt(i);
            i--;
        }
    }         
}

And that should work.

Monacraft
  • 6,510
  • 2
  • 17
  • 29