Connecting Tech Pros Worldwide Forums | Help | Site Map

Draw item to random x,y position

Familiar Sight
 
Join Date: Nov 2006
Location: PJ,Malaysia
Posts: 255
#1: Jan 2 '07
i have to design a simple game.
I asked some question before in this forum.
Now the game version is the new version with more items in it.

now, i have a 11 items to draw, '$'.
and i have a function in a header file called utils.h, GetRandom() function.
code:
Expand|Select|Wrap|Line Numbers
  1. inline unsigned GetRandom(unsigned min, unsigned max) {
  2.         return rand() % (max - min + 1) + min;
  3.     }
  4.  
i have to draw that 11 items(which is the same character '$' ) randomly to the screen(specified width and height, which is 25wdth, 11 in height) using that function above.

What my examiner said :
Quote:
suppose you have decided to have 11 powerups. Where are you
going to put them? So you'll neeed to use GetRandom at least 22 times to
get random x and y positions. The min and max will depend on the size of
the screen that you'll have.
I have no idea what it means by min & max argument in GetRandom() function will depend on the size of the screen.
So, what should i put in for the min & max argument? 11 and 25 (the height and width) ?

Please could someone show the example ? because i can't figure it out.
What i have tried (which is failed ) :
Expand|Select|Wrap|Line Numbers
  1.      unsigned xPos;
  2.      unsigned yPos;
  3.  
  4.      for(unsigned i = 0; i <= max; i++) {
  5.       xPos = GetRandom(1, 275);
  6.           yPos = GetRandom(1, 275);
  7.                 screen->Insert(xPos, yPos, '$');   // draw 11 of $ into screen
  8.      }
  9.  
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,165
#2: Jan 4 '07

re: Draw item to random x,y position


Quote:

Originally Posted by nickyeng

I have no idea what it means by min & max argument in GetRandom() function will depend on the size of the screen.
So, what should i put in for the min & max argument? 11 and 25 (the height and width) ?

Please could someone show the example ? because i can't figure it out.
What i have tried (which is failed )

When it says screeen size it is not talking about the physical dimensions of you monitor (19" for instance) but the resolution of you display. For instance at a resolution of 800x600 then the max x would be 800 but at 1024x768 the max x would be 1024. Also it is likely that yougame screen will be smaller then the actual available space and that there may be margins hence the need to a minimum too.


You example at the end is unlike to be correct because it is unlikely (but not impossible) that the game screen is square.
Reply