0

Currently making a simple game in C# and I have my object appearing in the middle of the screen, now I have made a list for that object but was wondering how do I make the object appear like 10 times randomly on the screen? I am guessing a for each loop of some kind?

object1 = new List<Gem>();
object2 = new List<Gem>();

Above is just where I have made a list for that class where the object is stored. So again just trying to figure out how to make this object appear in random positions on the screen x10.

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
RonTheOld
  • 777
  • 2
  • 14
  • 30
  • Is `Gem` the object you're trying to display? And you want to display 10 of those? – MikeH Mar 26 '14 at 22:38
  • yes thats correct but i want to set the postion ramdoely,. so thoese 10 gems tha appear are just in ramdom locations – RonTheOld Mar 26 '14 at 22:40
  • 1
    how are you setting their locations? I don't see anything in your code so far the give them locations. – Kevin Mar 26 '14 at 22:41
  • At the moment its just manual , so its in the middle of the screen – RonTheOld Mar 26 '14 at 22:44
  • 1
    [How to generate random int number? (C#)](http://stackoverflow.com/a/2706537/880990) – Olivier Jacot-Descombes Mar 26 '14 at 22:45
  • Manually displaying a gem on the screen. That's humorous. I understand this is your first question but as of right now I don't know how to help you because I don't know what UI technology you are using or how the location of the gems gets set. – Kevin Mar 26 '14 at 22:46
  • @user3466372 post the code you're using to locate the Gem. Even if it's hard coded it'll be helpful. – MikeH Mar 26 '14 at 22:46
  • Side note: "duplicate" is rarely used as word related to objects in C#. You either "clone" (making identical copy, usually "deep copy") or just create new instance (likely what you are looking for - collection of new items with unique locations). – Alexei Levenkov Mar 26 '14 at 23:18

2 Answers2

0

Assuming your Gem class like:

class Gem
{
  public int X {get;set;}     
  public int Y {get;set;}
  // information about color/type/whatever
}

You can do something like:

//make sure to have single instance of `Random` for class/whole program
Random rnd = new Random(); 
var gems = Enumerable.Range(1,10) // 10 items
  .Select(i => new Gem {  // create new Gem
     X = rnd.Next(1, 100), // set position to random value 1-100, adjust to 
     Y = rnd.Next(1, 100), // desired width/height ranges
  })
  .ToList();// convert enumerable to list.
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
0

so lets say I have an IMAGE object that can be rendered to the screen with an XNA style call:

 renderer.draw(IMAGE,COORD,COLOR);

use:

for(int x = 0; x < 10; x++)
{
      renderer.draw(IMAGE,new COORD(rand.Next(IMAGE.WIDTH,SCREEN.SIZE.WIDTH - IMAGE.width),rand.Next(IMAGE.HEIGHT,SCREEN.SIZE.HEIGHT - IMAGE.HEIGHT)),COLOR);
}

you only need one instance of image but maybe a list of coordinates.

your logic object 'GEM' should not contain the image itself but be used to tell the render where to draw your one instance of the gem image.

if you can produce some of your rendering code and some of the GEM code I could help you a lot more

and the funky math will keep your gem from rendering outside the screen

RadioSpace
  • 953
  • 7
  • 15