By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,137 Members | 1,024 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,137 IT Pros & Developers. It's quick & easy.

Draw item to random x,y position

100+
P: 254
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 :
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.  
Jan 2 '07 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
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.
Jan 4 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.