Rand, Rnd, Random Generators

BlitzPlus Forums/BlitzPlus Beginners Area/Rand, Rnd, Random Generators

En929(Posted 2012) [#1]
One thing I haven't learned how to do well is make things keep appearing at random times . I'm trying to find out the best way to make it so that duplicated copies of the "Man" character below keeps randomly coming in the same way the enemies do in the old NES game called Kung Fu. Thanks.





Graphics 1640, 1000 
SetBuffer BackBuffer()  
SeedRnd MilliSecs() 



Global MAN_IMAGE = LoadImage ("Enemy1.png")



Type MAN
	Field x,y
	Field image
	
End Type 



M.MAN = New  MAN      
m\x =  1000              
m\y = 300 					     
m\image = Man_Image                  





While Not KeyDown (1)


Cls


DrawImage (M\image, M\x,M\y)
M\x = M\x - 3


Flip
Wend 


Last edited 2012


RemiD(Posted 2012) [#2]
You can use 2 variables to track the time :

Graphics(800,600,32,2) 
SetBuffer(BackBuffer())
SeedRnd(MilliSecs())

MillisecCur% = MilliSecs()
MillisecOld% = MilliSecs()
CharactersCount% = 0

Repeat

 Cls()

 MillisecCur% = MilliSecs()
 If(MillisecCur% >= MillisecOld% + 1000)
  ;create your character here
  CharactersCount% = CharactersCount% + 1
  CharacterX% = Rand(0,799)
  CharacterY% = Rand(0,599)
  CharacterLife# = 100.0
  MillisecOld% = MillisecCur%
 EndIf
 
 Text(20,20,"CharactersCount% : "+CharactersCount%)

 Flip(True)

Until(KeyDown(1)=True)

End



En929(Posted 2012) [#3]
Thanks RemCoder. The Randomness in which the characters appears worked but they disappear the second that they appear. Thus, how do I get it so that the duplicated characters stay on screen and not disappear after appearing. Thanks again.

Last edited 2012


dynaman(Posted 2012) [#4]
in this section you need to loop through the type.

DrawImage (M\image, M\x,M\y)
M\x = M\x - 3


I forget the exact syntax but something like.

for i.man = each.man
drawimage (i\image, i\x,u\y)
i\x = i\x - 3
next


dynaman(Posted 2012) [#5]
Here is the manual entry on types, I forgot to include it.

http://www.blitzbasic.com/b3ddocs/command.php?name=Type&ref=2d_a-z


En929(Posted 2012) [#6]
Thanks dynaman. That worked. And thanks for the link too.

Last edited 2012