Randomly Placing Objects?

BlitzMax Forums/BlitzMax Beginners Area/Randomly Placing Objects?

bookworm99(Posted 2008) [#1]
Hi all,

I have a game with all the main code inside a while loop. What I want to do is generate a set of random x and y coordinates for say, 5 rectangles, and then redraw those rectangles in the same position each time the screen is cleared and everything's updated. How do I do that without it recalculating the random values each time?

I tried using something like
If gen = 0 
[all the random code] 
gen = 1 
EndIf 


But that caused the program to freeze when I tried to run it (I'm using OS X Leopard 1.5.2, if that's important).

Any help anybody has would be appreciated.

Bookworm


tonyg(Posted 2008) [#2]



ImaginaryHuman(Posted 2008) [#3]
Put some random values into an array at the start, outside the loop, and then just call the array instead of Rand().

ie
Local X:Float[10]
Local Y:Float[10]
For Local Obj:Int=0 to 9
   X:Obj=Rand(0,ScreenWidth-1)
   Y:Obj=Rand(0,ScreenHeight-1)
Next
While GameRunning=True
   For Local Count:Int=0 to 9
      Plot X[Count],Y[Count]
   Next
Wend



bookworm99(Posted 2008) [#4]
I will try both of those, thanks for your help - I should be able to tell you if it works by later today.


bookworm99(Posted 2008) [#5]
Wellp, here's my code:
Graphics 800,600,0

'first, set the variables to make things nice and tidy
Global x = 30, y = 30, bugGen = True, killed:Int = 0


bugCounter = 0

Function execMainGame(kill:Int)

'create bugs 
Type CBugs
	Global list:TList=CreateList()
	Global saved:TList=CreateList() 
	Field cx:Int
	Field cy:Int
	Field size:Int
	' Create a new bug at the position and size specified.

	Function Create(ccx:Int,ccy:Int,size:Int=30)
		Local temp:Cbugs = New CBugs
		temp.cx=ccx
		temp.cy=ccy
		temp.size=size
		ListAddLast list,temp
	End Function

	Function draw()
		For Local all:Cbugs = EachIn list
			DrawRect all.cx,all.cy,all.size,all.size
		Next
	End Function 
End Type
Local gw:Int=800
Local gh:Int=600
Graphics gw,gh
SeedRnd MilliSecs()
' Create our rectangles with random X, Y positions.

For Local bugCounter:Int=0 To 5
	Local xpos:Int=Rand(0,gw)
	Local ypos:Int=Rand(0,gh)
	Cbugs.Create(xpos,ypos)
Next

While Not KeyHit(KEY_ESCAPE) And killed = 0 
	#main 
	'show us the info 
	SetColor 0,0,255
	DrawText "Use the arrow keys (up, down, left, right) to move the Pac-A-Dot", 0, 0
	DrawText "Use the spacebar to reset the Pac-A-Dot to the top right corner of the screen; hit ESC to quit.", 0, 10
	
	SetColor 255,255,255
	
	'make the divider bar 
	DrawRect 1,25,800,10   
	
	'make our little pac-a-dot - he's green!
	SetColor 0,255,0
	DrawOval x, y, 30, 30 
	
	'make the first set of bugs - red
	SetColor 255,0,0
	Cbugs.draw() 
	
	If Cbugs.draw = True 
	kill = 1
	EndIf 
	
	'move him around
	x = MouseX ()
	y = MouseY () 
	DrawOval x,y,30,30 
	
	'kill key for suicide
	If KeyDown(KEY_ESC) 
	kill = 1
	EndIf 
	
	'make sure he doesn't hit that text
	If y<= 30
	y = 31
	EndIf 
	
	Flip;Cls 

Wend

If kill = 1 Return 1
If kill = 0 Return 0  

End Function 

Cls 

'starting or finishing?
If(killed = 0) 
execMainGame(killed) 
EndIf 

'so, you died - too bad, but you can start again 
If(killed = 1) 
DrawText "You were killed! Sorry. Press any key to exit.",300,100
Flip 

WaitKey

EndIf 

End  


I'm getting an error which says "Types 'Int()' and 'Int' are unrelated" when I try to compile... any ideas?


Perturbatio(Posted 2008) [#6]
	If Cbugs.draw = True 


should be
	If Cbugs.draw() = True 



pmc(Posted 2008) [#7]

Function Create(x:Int,y:Int,size:Int=50)
Function draw()



I have a noob question about this OOP example that illustrates a gap in my understanding on the topic that's been grating at me. :)

Why the choice of a function for both create and draw? Why not a method for draw? Pros and cons? I've done it both ways and it works fine for me. But I'm not sure which is more proper and why.

TIA

-pmc