Wednesday, December 24, 2014

Monkey getting started - Random rectangles using for loop example code


The code below fills a section of the screen with red rectangles. The rectangles only get drawn when the random value is 1. Two for loops are used.

Import mojo

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End
    Method OnUpdate()
    End
    Method OnRender()
        Cls(0,0,0)
        SetColor(255,0,0)
        For Local y:Int = 3 To 10
        For Local x:Int = 3 To 10
            If Int(Rnd(2)) = 1 Then DrawRect(x*16,y*16,16,16)
        End
        End
    End
End

Function Main()
    New MyGame()
End

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.