lundi 17 août 2015

Random variable selects same values despite single instance

I am working on a game using Visual C# (Winforms Application) which has missiles coming from all 4 sides of a Panel, with random speed, damage and location upon an axis values. All of the random Next() functions used when initializing the variable use the same Random variable contained in another class and is used for all random usages.

However, with an array of 20 missiles, the same value is chosen for coordinates multiple times so that only 3 or 4 missiles are visually present due to overlap of images. This also occurs with the speed and side variables, and presumably with the damage variable however I have yet to check.

The missiles are Rectangles with Images contained within them. The X and Y coordinates are affected by a rotation move value contained within the GameObject class which Missile extends, meaning I must divide and multiple by a scale value to switch between pixel coords and texal coords.

The following is the code which regenerates the missile once it reaches the side of the panel without hitting the player.

side = game.rand.Next(1, 4);

    switch (side)
    {
            case 1:
                angle = 90;
                x = game.rand.Next((int)(30 / xScale), (int)(990 / xScale));
                y = (int)(30 / yScale);
                break;
            case 2:
                x = (int)(1000 / xScale);
                y = game.rand.Next((int)(30 / yScale), (int)(610 / yScale));
                break;
            case 3:
                angle = 180;
                x = game.rand.Next((int)(30 / xScale), (int)(990 / xScale));
                y = (int)(610 / yScale);
                break;
            case 4:
                angle = -90;
                x = (int)(30 / xScale);
                y = game.rand.Next((int)(30 / yScale), (int)(610 / yScale));
                break;
        }

game.rand is the common random variable stored in another class. It should select random variables down the y axis or across the x axis, but often only chooses 2 different values despite this method being called 20 times.

Any and all suggestions welcome. Thank you very much for reading.




Aucun commentaire:

Enregistrer un commentaire