6 Replies - 631 Views - Last Post: 16 June 2010 - 03:56 PM Rate Topic: -----

#1 SuperStyler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-May 10

[Need Help] Related To Drawing

Posted 15 June 2010 - 04:00 PM

Okay so I've been trying to make a 2d target shooting game. I've made it so that target wuld appear in random positions of the form. But for some reason the targets are always in a row and the only difference everytime I run it is the distance between each target.

Random npcX = new Random(); int npcStoreX;
        Random npcY = new Random(); int npcStoreY;

for (int i = 0; i <= 7; i++)
            {
                npcStoreX = npcX.Next(25, 1015);
                npcStoreY = npcY.Next(268, 609);
                g.DrawImage(npcAlive, npcStoreX, npcStoreY, 19, 26);
                npcStoreX = 0;
                npcStoreY = 0;
            }


That is the part of my code that should make the targets fully random in a certain space. But why doesn't it?

Is This A Good Question/Topic? 0
  • +

Replies To: [Need Help] Related To Drawing

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: [Need Help] Related To Drawing

Posted 15 June 2010 - 04:26 PM

Just some tips:

Random npcX = new Random(); int npcStoreX; 
Random npcY = new Random(); int npcStoreY;

There is no need to create two random number generators. They aren't 'more random', it just wastes a little bit of memory. Create one and use it everywhere.

npcStoreX = 0; 
npcStoreY = 0;

These lines do nothing for you, remove them.

Question for you: Is this a cut/paste from your code? If it is, you have the strangest indenting style I've ever seen. If it isn't, then why isn't it?

Run your code in the debugger and see what your x/y values are. You'll find the typo then.

This post has been edited by Momerath: 15 June 2010 - 04:26 PM

Was This Post Helpful? 0
  • +
  • -

#3 SuperStyler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-May 10

Re: [Need Help] Related To Drawing

Posted 15 June 2010 - 04:37 PM

i tried the "= 0" because i thought it might help cus i got the problem without it but it didnt

its straight from my code and its random but for some reason the randoms end up in a row and its just difference of spacing
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [Need Help] Related To Drawing

Posted 15 June 2010 - 04:50 PM

This could and probably should be done with a single random. For instance:
static Random rand;

static void Main(string[] args)
{
    rand = new Random();
    var ret = GetRands();
    var listX = ret.Item1;
    var listY = ret.Item2;
    for (int i = 0; i < listX.Count; i++)
        Console.WriteLine("{0}, {1}", listX[i], listY[i]);
    Console.ReadKey();
}

static Tuple<List<int>, List<int>> GetRands()
{
    List<int> listX = new List<int>()
        , listY = new List<int>();

    for (int i = 0; i <= 7; i++)
    {
        listX.Add(rand.Next(25, 1015));
        listY.Add(rand.Next(268, 609));
    }
    return Tuple.Create(listX, listY);
}


Will produce distributed, random output each time you run it.
Was This Post Helpful? 0
  • +
  • -

#5 SuperStyler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-May 10

Re: [Need Help] Related To Drawing

Posted 16 June 2010 - 07:43 AM

o_o i dont know most of the things in that code

so is that code suppose to show me what numbers are generated?
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: [Need Help] Related To Drawing

Posted 16 June 2010 - 08:03 AM

Sorry about that. The important section is this:
    List<int> listX = new List<int>()
        , listY = new List<int>();

    for (int i = 0; i <= 7; i++)
    {
        listX.Add(rand.Next(25, 1015));
        listY.Add(rand.Next(268, 609));
    }


Instead of drawing anything, it just adds the random numbers to a list of X and Y coordinates. Yes, it was for the purposes of displaying the coordinates.

I'm suggesting that you use one class-level Random object. Initialize it in the constructor, and just use that one random object for your needs. No need to make a new Random object every time you need to make a random number.
Was This Post Helpful? 0
  • +
  • -

#7 SuperStyler   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-May 10

Re: [Need Help] Related To Drawing

Posted 16 June 2010 - 03:56 PM

alright, i dont know why but my image keeps being drawn in a certain row just different spacing thats why my numbers are still random
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1