Random Objects falling from the Sky
Monkey Forums/Monkey Beginners/Random Objects falling from the Sky
| ||
I would like to know what could I add to my code to get random rectangles to keep falling from the sky to where even I wouldn't know which rectangle would fall out of the sky next. That is, I'm trying to create the effect of the game Asteroids or Tetris. Thus, what do I add to my code below to do that. Thanks!!Strict Import mojo Function Main:Int() New MyApp() Return True End Class MyApp Extends App Field RectX: Int = 300 Field RectY: Int = -400 Field RectW: Int = 100 Field RectH: Int = 100 Field RectX2: Int = 300 Field RectY2: Int = -70 Field RectW2: Int = 50 Field RectH2: Int = 50 Field RectX3: Int = 300 Field RectY3: Int = -30 Field RectW3: Int = 20 Field RectH3: Int = 20 Field RectX4: Int = 300 Field RectY4: Int = -200 Field RectW4: Int = 110 Field RectH4: Int = 110 Field RectX5: Int = 300 Field RectY5: Int = -100 Field RectW5: Int = 5 Field RectH5: Int = 5 Method OnCreate:Int() SetUpdateRate(60) Return True End Method OnUpdate:Int() 'I want the Asteroids to keep falling out of the sky (like with the first "Rect" I made below, but 'I want random ones to fall to where even I wouldn't know WHICH block will fall next like in the game 'Asteroids or Tetris RectY +=1 Return True End Method OnRender:Int() Cls 'These are the different types of blocks that I wanted to keep falling out of the sky. 'Blue SetColor (0,38,255) DrawRect(RectX, RectY, RectW, RectH) 'Red SetColor (255,0,0) DrawRect(RectX2, RectY2, RectW2, RectH2) 'Green SetColor (76,255,0) DrawRect(RectX3, RectY3, RectW3, RectH3) 'Purple SetColor (178,0,255) DrawRect(RectX4, RectY4, RectW4, RectH4) 'Orange SetColor (255,106,0) DrawRect(RectX5, RectY5, RectW5, RectH5) Return True End End |
| ||
Local choice:Int = Int( Rnd( 5.0 ) ) ' Will give a random value from 0..4 that you can use to pick your rectangle. It shouldn't be possible to get 5, but if you are paranoid about that, use Int( Rnd( 4.99999 ) ) to be absolutely certain. You'll find if simpler if you use an array of rectangle objects. Lots of individually named fields makes for long-winded code. But you can also use plain arrays. I haven't tested the following, but it ought to work: ' Two arrays of arrays Field rects:[][] = [ [ 300, -400, 100, 100 ], ' And four more ] Field colours[][] = [ [ 0, 38, 255 ], ' And four more ] Field rectChoice:Int ' Which rectangle Field currentRect:Int = New Int[ 4 ] ' Working copy of it, so I can change y without affecting original Field readyForNewRect:Bool = True ' Ready for a new rect? Method OnUpdate:Int() If readyForNewRect rectChoice = Int( Rnd( 5.0 ) ) For Local i:Int = 0 Until 4 currentRect[ i ] = rects[ rectChoice ][ i ] Next readyForNewRect = False Else currentRect[ 1 ] += 1 If currentRect[ 1 ] > 1000 readyForNewRect = True End End End Method OnRender:Int() SetColor( colours[ rectChoice][ 0 ], colours[ rectChoice][ 1 ], colours[ rectChoice][ 2 ] ) DrawRect( currentRect[ 0 ], currentRect[ 1 ], currentRect[ 2 ], currentRect[ 3 ] ) End Hope that gives you some ideas |
| ||
Thanks Gerry, I have one thing. I incorporated the code you wrote above, but with the array line as an error in an and I don't know how to fix it. I wrote a note inside the code below to note where the error message has been mentioned:Strict Import mojo Function Main:Int() New MyApp() Return True End Class MyApp Extends App ' Two arrays of arrays Field rects: Int [][] = [ 'I added in the 4 arrays (I know they are all in the same coordinates now, I'm just writing this for testing purposes) [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], ] ' And four more Field colours: Int [][] = [ [ 0, 38, 255 ], [ 0, 38, 255 ], [ 0, 38, 255 ], [ 0, 38, 255 ], [ 0, 38, 255 ], ' And four more ] Field rectChoice:Int ' Which rectangle Field currentRect:Int = New Int[ 4 ] ' Working copy of it, so I can change y without affecting original Field readyForNewRect:Bool = True ' Ready for a new rect? Method OnUpdate:Int() If readyForNewRect rectChoice = Int( Rnd( 5.0 ) ) For Local i:Int = 0 Until 4 currentRect[ i ] = rects[ rectChoice ][ i ] Next readyForNewRect = False Else currentRect[ 1 ] += 1 If currentRect[ 1 ] > 1000 readyForNewRect = True End End End Method OnRender:Int() SetColor( colours[ rectChoice][ 0 ], colours[ rectChoice][ 1 ], colours[ rectChoice][ 2 ] ) DrawRect( currentRect[ 0 ], currentRect[ 1 ], currentRect[ 2 ], currentRect[ 3 ] ) End |
| ||
Thanks Gerry, I have one thing. I incorporated the code you wrote above, but with the array line as an error in an and I don't know how to fix it. I wrote a note inside the code below to note where the error message has been mentioned:Strict Import mojo Function Main:Int() New MyApp() Return True End Class MyApp Extends App ' Two arrays of arrays Field rects: Int [][] = [ 'I added in the 4 arrays (I know they are all in the same coordinates now, I'm just writing this for testing purposes) [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], '----------------Here is where I got the error message. I don't know how to fix it. Thanks!------------------------------------ ] Field colours: Int [][] = [ [ 0, 38, 255 ], [ 0, 38, 255 ], [ 0, 38, 255 ], [ 0, 38, 255 ], [ 0, 38, 255 ], ] Field rectChoice:Int ' Which rectangle Field currentRect:Int = New Int[ 4 ] ' Working copy of it, so I can change y without affecting original Field readyForNewRect:Bool = True ' Ready for a new rect? Method OnUpdate:Int() If readyForNewRect rectChoice = Int( Rnd( 5.0 ) ) For Local i:Int = 0 Until 4 currentRect[ i ] = rects[ rectChoice ][ i ] Next readyForNewRect = False Else currentRect[ 1 ] += 1 If currentRect[ 1 ] > 1000 readyForNewRect = True End End End Method OnRender:Int() SetColor( colours[ rectChoice][ 0 ], colours[ rectChoice][ 1 ], colours[ rectChoice][ 2 ] ) DrawRect( currentRect[ 0 ], currentRect[ 1 ], currentRect[ 2 ], currentRect[ 3 ] ) End |
| ||
Maybe the comma at the end of the last Rect ist to muchField rects: Int [][] = [ 'I added in the 4 arrays (I know they are all in the same coordinates now, I'm just writing this for testing purposes) [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [ 300, -400, 100, 100 ], [300, -400, 100, 100]] '----------------Here is where I got the error message. I don't know how to fix it. Thanks!------------------------------------ and Field currentRect:Int has to be :Int[] and end at the end ;-) is missing. (end of class MyApp) |
| ||
here is an alternative if you are interested: |
| ||
Thanks a lot Gerry and Jesse. I got both code examples to work. That's what I wanted. Thanks again!! |
| ||
Great, hope they are of some help. As you get experienced you'll probably move towards using a Rect class like Jesse, or a class specifically for your object that could contain the colours as well), but I sense you are just getting started with the basics. You won't really see the benefit of objects until you are writing programs of a certain size. |
| ||
Here are some corrections |