image array with random help

Monkey Forums/Monkey Programming/image array with random help

dubbsta(Posted 2017) [#1]
i have 3 images i want to call in random order and set at 2000 px apart recursively. cant figure it out, i get a cant convert to int error. pls help


Xaron(Posted 2017) [#2]
Global images:List<Image>

'...
'Inside your function/method, create the images/load them
'I use a list here because we want to select every image only once and avoid that way to do loops with random numbers and array stuff...
images = New List<Image>()
images.AddLast( LoadImage( ... ) )
images.AddLast( LoadImage( ... ) )
images.AddLast( LoadImage( ... ) )

'...
'Now select them in random order. Remember, we want to avoid that we display one more than once, that's why I've put them in a list
'We could do a for loop, but as we only have three images it's easier that way:
Local imageNumber:Int
imageNumber = Rnd( 0, images.Count() )  ' equals Rnd( 0, 3 ) which creates a number from 0...2
Local image1:Image = images.ToArray()[imageNumber]  'select that image out of that list
images.Remove( image1 )  'and remove it, we don't want to choose it again
imageNumber = Rnd( 0, images.Count() )  ' equals Rnd( 0, 2 ) which creates a number from 0...1
Local image2:Image = images.ToArray()[imageNumber]
images.Remove( image2 )
Local image3:Image = images.First()  ' as there is only one image left, we directly take it

'...
'Display the 3 randomly selected images 2000px apart 
DrawImage( image1, 0, 0 )
DrawImage( image2, 2000, 0 )
DrawImage( image3, 4000, 0 )



dubbsta(Posted 2017) [#3]
thx Xaron appreciate your help, learned something new but dont think its what im looking for. :( let me try to better explain, im making a space shooter for practice of course, i have 3 pictures of nebulas and want to randomly draw them at 2000px apart which you did, i would like to keep drawing them over till game over. the game i vertical so up coming down. if you have time and not to much to ask could you give me both list and loop example, it would really be useful
if it help here is the code, its in its own class
thx again!!

edit: i have a bunch of planets as well figure i could do the same, but the planets are in a sheet so i would just change frames i guess,
ps. nebula doesnt move with the y update on this

pps. i havent implemented a timer yet but for future reference would timed drawing be better in this case than distance or just a matter of taste?

Global  images:List<Image>

Class nebulas

  Field purpneb:Image
  Field rainbow:Image                    
  Field seahorse:Image
 
  
  Field x:Float = 0.0
  Field y:Float = -900.0
  Field speed:Float = 0.1
  Field nebs:Image[3]
  Field gameon:Int = 1 

	  		Method New()	  			
	  				images = New List<Image>()
	  				images.AddLast( LoadImage("nebula/purpleneb.png") )
					images.AddLast( LoadImage("nebula/rainbow.png") )
					images.AddLast( LoadImage("nebula/seahorse.png") )	
	  		End 
	  		
	  		Method Update:Int()
	  		
	  			y+=speed
		  		
	  			Return 0
	  		End 
	  		Method Draw:Int()
	  		
	  			Local imageNumber:Int
		  			imageNumber = Rnd( 0, images.Count() )
		  			
		  		Local image1:Image = images.ToArray()[imageNumber] 
			  		images.Remove( image1 )
			  		imageNumber = Rnd( 0, images.Count() )
			  		 
		  		Local image2:Image = images.ToArray()[imageNumber]
					images.Remove( image2 )
					
				Local image3:Image = images.First()
	  			DrawImage( image1, x, y+0.0  )
				DrawImage( image2, x, y+2000.0 )
				DrawImage( image3, x, y+4000.0 )
	  		
	  			Return 0 
	  		End 
End 



dubbsta(Posted 2017) [#4]
if youre feeling brave or bored here is my planets class, caution it may be messy and at the moment not working correctly, but basically the same but in sprite sheet so frames. if youre to busy for this no worry thx


Class Planets

  Field sx:Int'[10] 
  Field sy:float'[10] 
  Field mx:Int =0 
  Field my:Int =0 
  Field lx:Int =0
  Field ly:Int =0
  
  Field sframe:Int'[10]
  Field mframe:Int
  Field speed:Float = .5
  Field size:int
  
  Field small:Image
  Field med:Image
  Field large:Image
  
  			Method New() 
  				
  				small = LoadImage("planets/small.png",872/2,1588/4,8,Image.MidHandle)
  				med = LoadImage("planets/med.png",660,4620/7,7,Image.MidHandle)
  				'large = LoadImage("planets/large.png",807/6,538/2,6,Image.MidHandle)
  				For Local i:Int = 0 until 10
   					'sx[i] = Rnd(640)  
  					'sy[i] = Rnd(-1000-2000)
  					'sframe[i] = Rnd(7) 
  					
  				End 
  			End

  		 	
  			Method Update:Int()
  				' For Local i:Int = 0 until 10
  				 'sy[i]+= speed
  				' End 
  				Return 0 
  			End 
  			
  			Method Draw:Int() 
  			
  				For Local i:Int = 0 until 10
  					'DrawImage(small,sx[i] ,sy[i]+i*-1000 ,0,.5,.5,sframe[i])
  				End 	
  				
  				Return 0 
  			End 
End 



Xaron(Posted 2017) [#5]
Hmm ok, let's see if I find some time to wrap my head around this...


dubbsta(Posted 2017) [#6]
K I'll try to find a solution in the mean time


dubbsta(Posted 2017) [#7]
ok! so after playing around i came up with something! only problem is i cant figure out how to draw random image in the draw method i tried like 10 different things and nothing. someone pls help

right now this works. but only for 1 image
Class nebulas
  Field x:Float = 0.0  
  Field y:Float = -1000.0 
  Field speed:Float = 0.2
  Field nebs:Image[] 
  Field gameon:Int = 1 
  Field lastpos:Int = 0
   
	  		Method New()
	  		    nebs = New Image[3]	   
	  			nebs[0]= LoadImage("nebula/rainbowneb.png")
	  			nebs[1]= LoadImage("nebula/purpleneb.png")
	  			nebs[2]= LoadImage("nebula/seahorse.png")  		
	  		End 			     
	  				    
	  		Method Update:Int()  
	 	  		y+=speed	  			
		  		If y > screenHeight
		  		y -= 2000
		  		End 
	  			Return 0
	  		End  
	  		 
	  		Method Draw:Int()
 			DrawImage(nebs[0],x,y) 
	  			Return 0 
	  		End 
End 



Phil7(Posted 2017) [#8]
I would do it like this, if I wanted a easy solution without any extra classes
Class nebulas
	Field x:Float = 0.0
	Field speed:Float = 15.0 'Just sped it up to see the results faster ;-)
	Field nebs:Image[]
	
	Field nebsImg:Image[]
	Field nebsY:Float[] =[-1000.0, -3000.0, -5000.0]
	
	Field gameon:Int = 1
	Field lastpos:Int = 0
   
	Method New()
		nebs = New Image[3]
		nebsImg = New Image[3] 'These are just the containers for the images.
		nebsImg[0] = LoadImage("nebula/rainbowneb.png")
		nebsImg[1] = LoadImage("nebula/purpleneb.png")
		nebsImg[2] = LoadImage("nebula/seahorse.png")
		
		'All the nebs get their random image at the beginning.
		nebs[0] = nebsImg[Int(Rnd(0, 3))]
		nebs[1] = nebsImg[Int(Rnd(0, 3))]
		nebs[2] = nebsImg[Int(Rnd(0, 3))]
		
	End
	  				    
	Method Update:Int()
		
		For Local i = 0 Until nebs.Length()
			nebsY[i] += speed
			
			'the current neb gets it new random image, when it gets out of the screen.
			If nebsY[i] > screenHeight
				nebs[0] = nebsImg[Int(Rnd(0, 3))]
				nebsY[i] -= 6000
			End
		End

		Return 0
	End
	  		 
	Method Draw:Int()
		For Local i = 0 Until nebs.Length()
			DrawImage(nebs[i], x, nebsY[i])
		End
		
		Return 0
	End
End



dubbsta(Posted 2017) [#9]
works perfect!!! thanks! small but great community thx guys...now, to make more problems...

edit: can you explain this part for me, seems like a reset but not sure how to read it
why only nebs[0]?

                                        If nebsY[i] > screenHeight
						nebs[0] = nebsImg[Int(Rnd(0, 3))]
						nebsY[i] -= 6000
					End



Gerry Quinn(Posted 2017) [#10]
It should be nebs[i], I think. The nebula with index i has gone off the bottom of screen, so it is given a fresh random image and its position is set somewhere above the screen, where it will appear in due course.


Phil7(Posted 2017) [#11]
@dubbsta: yes, that was a bug of mine ;-)


dubbsta(Posted 2017) [#12]
ahhh ok thanks guys