Thursday, February 19, 2015

Got flash applets input working!.

You need to press on them a couple of times I noticed. I will start creating the flash applets and be putting them with the posts. This will take a while since I have almost 90 posts with code.

edit:
Now almost every post has a flash applet on the top showing the program.

Wednesday, February 18, 2015

Monkey-X - Beginners - Vectors code example 5 (space ship method B)


Here another vectors code example. (use cursor keys and space to control) Thanks to t-c's blitz tutorials. 
This is another method to get the spaceship flying.

here a video of it : http://youtu.be/dFQNdTAatss


Code below :

Import mojo

Class player
    Field x:Float,y:Float
    Field thrustmax:Float=0.1
    Field speedmax:Float=5
    Field rotatemax:Float=4
    Field thrustx:Float,thrusty:Float,angle:Int
    Field velocityx:Float,velocityy:Float
    Field thrusting:Bool=False
    Field speed:Float,heading:int
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y
    End Method
    Method update()
        If KeyDown(KEY_LEFT) Then angle-=rotatemax
        If KeyDown(KEY_RIGHT) Then angle+=rotatemax
        If angle<0 Then angle=angle+360
        If angle>359 Then angle=angle-360
        thrusting = False
        If KeyDown(KEY_UP) 
            thrustx = vectorx(thrustmax,angle)
            thrusty = vectory(thrustmax,angle)
            thrusting = True
        End If
        If KeyDown(KEY_DOWN)
            thrustx = vectorx(-thrustmax,angle)
            thrusty = vectory(-thrustmax,angle)
            thrusting = True
        End If
        If Not thrusting 
            thrustx = 0
            thrusty = 0
        End If
        velocityx += thrustx
        velocityy += thrusty
        speed = vectordistance(velocityx,velocityy)
        heading = vectorangle(velocityx,velocityy)
        If speed > speedmax Then speed = speedmax
        If KeyDown(KEY_SPACE) Then speed = 0
        velocityx = vectorx(speed,heading)
        velocityy = vectory(speed,heading)
        x+=velocityx
        If x<0 Then x=640
        If x>640 Then x=0
        y+=velocityy
        If y<0 Then y=480
        If y>480 Then y=0
    End Method
    Method draw()
         SetColor 0,127,255
          DrawOval x-10,y-10,21,21
         SetColor 127,255,127
          DrawLine x,y,x+vectorx(10,heading),y+vectory(10,heading)
          SetColor 255,255,255
          DrawLine x,y,x+vectorx(20,angle),y+vectory(20,angle)
        SetColor 255,255,255
        DrawText "Vector example 5 - space ship method b.",0,0
          DrawText " Angle: " + angle,0,15        
          DrawText "Speed: " + Int(speed) + " Heading: " + heading,0,30
    End Method
End Class

Global players:List<player> = New List<player>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
           players.AddLast(New player(640/2,480/2))
    End Method
    Method OnUpdate()
        For Local i:=Eachin players
            i.update
        Next
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        For Local i:=Eachin players
            i.draw
        Next
    End Method
End Class

' Thanks to Blitzcoder for the functions
';= Get horizontal size of vector using distance and angle
Function vectorx:Float(distance:Float,angle:Float)
    Return Sin(angle)*distance
End Function
';= Get vertical size of vector using distance and angle
Function vectory:Float(distance:Float,angle:Float)
    Return Sin(angle-90)*distance
End Function
';= Get True length of a vector
Function vectordistance:Float(x:Float,y:Float)
    Return Sqrt(x*x+y*y)
End Function
';= Get True angle of a vector
Function vectorangle:Float(x:Float,y:Float)
    Return -ATan2(x,y)+180
End Function


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vectors code example 4 (space ship method A)


Use cursors and space to control ship. Thanks to t-c's blitz tutorials.
In this example you can control a spaceship that uses vectors.

Here a video of it : http://youtu.be/dFQNdTAatss


Code below :

Import mojo

Class player
    Field x:Float,y:Float
    Field thrustmax:Float=0.1
    Field speedmax:Float=5
    Field rotatemax:Float=4
    Field thrust:Float,angle:Int,speed:Float,heading:Int
    Field thrusting:Bool=False
    Method New(x:Float,y:Float)
        Self.x = x
        Self.y = y
    End Method
    Method update()
        local velocityx:Float,velocityy:Float        
        If KeyDown(KEY_LEFT) Then angle-=rotatemax
        If KeyDown(KEY_RIGHT) Then angle+=rotatemax
        If angle<0 Then angle=angle+360
        If angle>359 Then angle=angle-360
        thrusting=False
        If KeyDown(KEY_UP) 
            thrust = thrustmax
            thrusting = True
        End If
        If KeyDown(KEY_DOWN)
            thrust = -thrustmax
            thrusting = True
        End If
        If Not thrusting Then thrust = 0
                
        velocityx = vectorx(speed,heading) + vectorx(thrust,angle)
        velocityy = vectory(speed,heading) + vectory(thrust,angle)
        speed = vectordistance(velocityx,velocityy)
        heading = vectorangle(velocityx,velocityy)
        If speed > speedmax Then speed = speedmax
        
        If KeyDown(KEY_SPACE) Then speed = 0
        
        x+=vectorx(speed,heading)
        If x<0 Then x=640
        If x>640 Then x=0
        y+=vectory(speed,heading)
        If y<0 Then y=480
        If y>480 Then y=0
        
    End Method
    Method draw()
         SetColor 0,127,255
          DrawOval x-10,y-10,21,21
         SetColor 127,255,127
          DrawLine x,y,x+vectorx(10,heading),y+vectory(10,heading)
          SetColor 255,255,255
          DrawLine x,y,x+vectorx(20,angle),y+vectory(20,angle)
        SetColor 255,255,255
        DrawText "Vector example 4 - space ship.",0,0
          DrawText " Angle: " + angle,0,15        
          DrawText "Speed: " + Int(speed) + " Heading: " + heading,0,30
    End Method
End Class

Global players:List<player> = New List<player>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
           players.AddLast(New player(640/2,480/2))
    End Method
    Method OnUpdate()
        For Local i:=Eachin players
            i.update
        Next
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        For Local i:=Eachin players
            i.draw
        Next
    End Method
End Class

' Thanks to Blitzcoder for the functions
';= Get horizontal size of vector using distance and angle
Function vectorx:Float(distance:Float,angle:Float)
    Return Sin(angle)*distance
End Function
';= Get vertical size of vector using distance and angle
Function vectory:Float(distance:Float,angle:Float)
    Return Sin(angle-90)*distance
End Function
';= Get True length of a vector
Function vectordistance:Float(x:Float,y:Float)
    Return Sqrt(x*x+y*y)
End Function
';= Get True angle of a vector
Function vectorangle:Float(x:Float,y:Float)
    Return -ATan2(x,y)+180
End Function


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vectors code example 3


Here another example with Vectors. Thanks to Blitzcoder for the tutorial.

Code below :

Import mojo

Global numfishies:Int=100

Class fishy
    Field x:Float,y:Float,speed:Float,heading:Float,size:Float
    Field headx:Float,heady:Float
    Field color:Int
    Method New(x:Float,y:Float,speed:Float,heading:Float,size:Float,color:Int)
        Self.x = x
        Self.y = y
        Self.speed = speed
        Self.heading = heading
        Self.size = size
        Self.color = color
    End Method
    Method update()
        heading+=Rnd(-10,10)
        If heading<0 Then heading=heading + 360
        If heading>359 Then heading=heading - 360
        x += vectorx(speed,heading)
        If x<0 Then x=640
        If x>640 Then x=0
        y += vectory(speed,heading)
        If y<0 Then y=480
        If y>480 Then y=0
        headx=x+vectorx(size,heading)
        heady=y+vectory(size,heading)
    End Method
    Method draw()
        SetColor color,color,color
        DrawLine x,y,headx,heady
    End Method
End Class

Global fishies:List<fishy> = New List<fishy>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        For Local i=0 Until numfishies
            fishies.AddLast(New fishy(Rnd(0,640),Rnd(0,480),Rnd(2,5),Rnd(0,360),Rnd(5,10),Rnd(32,200)))
        Next
    End Method
    Method OnUpdate()
        For Local i:=Eachin fishies
            i.update
        Next
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        For Local i:=Eachin fishies
            i.draw
        Next
    End Method
End Class

' Thanks to Blitzcoder for the functions
';= Get horizontal size of vector using distance and angle
Function vectorx:Float(distance:Float,angle:Float)
    Return Sin(angle)*distance
End Function
';= Get vertical size of vector using distance and angle
Function vectory:Float(distance:Float,angle:Float)
    Return Sin(angle-90)*distance
End Function
';= Get True length of a vector
Function vectordistance:Float(x:Float,y:Float)
    Return Sqrt(x*x+y*y)
End Function
';= Get True angle of a vector
Function vectorangle:Float(x:Float,y:Float)
    Return -ATan2(x,y)+180
End Function


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vectors code example 2.


Here another Vector code example. Thanks to Bettiesart for this.

Code below :

Import mojo

Global numdots:Int=100

Class dots
    Field x:Float,y:Float
    Field lastangle:Float
    Field color:Int
    Method New(x:Float,y:Float,lastangle:Float,color:Int)
        Self.x = x
        Self.y = y
        Self.lastangle = lastangle
        Self.color = color
    End Method
    Method update()
        Local length:Float,angle:Float,incx:Float,incy:Float
        Repeat
          incx = Rnd(-5,5)
          incy = Rnd(-5,5)
          length = vectordistance(incx,incy)
          angle = vectorangle(incx,incy)
        Until length >= 2 And length <= 5 And Abs(lastangle-angle)<=10
        lastangle=angle
        x+=incx
        If x<0 Then x=640
        If x>640 Then x=0
        y+=incy
        If y<0 Then y=480
        If y>480 Then y=0 
    End Method
    Method draw()
        SetColor color,color,color
        DrawOval x,y,6,6
    End Method
End Class

Global dot:List<dots> = New List<dots>

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        For Local i=0 Until numdots
            dot.AddLast(New dots(Rnd(0,640),Rnd(0,480),Rnd(0,360),Rnd(32,200)))
        Next
    End Method
    Method OnUpdate()
        For Local i:=Eachin dot
            i.update
        Next
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        For Local i:=Eachin dot
            i.draw
        Next
    End Method
End Class

' Thanks to Blitzcoder for the functions
';= Get horizontal size of vector using distance and angle
Function vectorx:Float(distance:Float,angle:Float)
    Return Sin(angle)*distance
End Function
';= Get vertical size of vector using distance and angle
Function vectory:Float(distance:Float,angle:Float)
    Return Sin(angle-90)*distance
End Function
';= Get True length of a vector
Function vectordistance:Float(x:Float,y:Float)
    Return Sqrt(x*x+y*y)
End Function
';= Get True angle of a vector
Function vectorangle:Float(x:Float,y:Float)
    Return -ATan2(x,y)+180
End Function


Function Main()
    New MyGame()
End Function

Monkey-X - Beginners - Vectors code example 1.


I am trying to learn about Vectors. I found a archived blitzcoder article about it for Blitz basic. I am making my own examples of them for Monkey here.

Code below :

Import mojo

Global x1:Float,y1:Float,x2:Float,y2:Float
Global length:Float,angle:Float

Class MyGame Extends App

    Method OnCreate()
        SetUpdateRate(60)
        x1=640/2
        y1=480/2
    End Method
    Method OnUpdate()
        x2 = MouseX()
        y2 = MouseY()
        length=vectordistance(x2-x1,y2-y1)
        angle=vectorangle(x2-x1,y2-y1)
        x2 = x1+vectorx(length,angle)
        y2 = y1+vectory(length,angle)
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        DrawLine x1,y1,x2,y2
    End Method
End Class

' Thanks to Blitzcoder for the functions
';= Get horizontal size of vector using distance and angle
Function vectorx:Float(distance:Float,angle:Float)
    Return Sin(angle)*distance
End Function
';= Get vertical size of vector using distance and angle
Function vectory:Float(distance:Float,angle:Float)
    Return Sin(angle-90)*distance
End Function
';= Get True length of a vector
Function vectordistance:Float(x:Float,y:Float)
    Return Sqrt(x*x+y*y)
End Function
';= Get True angle of a vector
Function vectorangle:Float(x:Float,y:Float)
    Return -ATan2(x,y)+180
End Function


Function Main()
    New MyGame()
End Function

Tuesday, February 17, 2015

Monkey-X - Random obstacle avoidance - code example


Here a short example of random obstacle avoidance. When the next position in the path is occupied then step into a random direction and continue path.

Code below :

Import mojo

Global mapwidth:Int=20
Global mapheight:Int=20
Global tilewidth:Int=640/mapwidth
Global tileheight:Int=480/mapheight
Global map:Int[mapwidth][]

Class agents
    Field x:Int,y:Int,dx:Int,dy:Int
    Field delete:Bool=False
    Method New(x:Int,y:Int,dx:Int,dy:Int)
        Self.x = x
        Self.y = y
        Self.dx = dx
        Self.dy = dy
    End Method
    Method update()
        Local tx:Int=x,ty:Int=y
        If x<dx Then tx+=1
        If x>dx Then tx-=1
        If y<dy Then ty+=1
        If y>dy Then ty-=1
        If map[tx][ty] = 1 'if occupied step into random direction
            Local exitloop = False
            While exitloop = False
                tx=x+Rnd(-1,2)
                ty=y+Rnd(-1,2)
                If map[tx][ty] = 0 Then exitloop = True
            Wend
        End If
        'update the position
        x = tx
        y = ty
        'if arived then delete agent from list
        If x=dx And y=dy Then delete = True
        If delete = True Then
            For Local i:=Eachin agent
                If i.delete=True Then agent.Remove i
            Next
        End If
    End Method
    Method draw()
        SetColor 255,0,0
        DrawOval x*tilewidth,y*tileheight,tilewidth,tileheight
    End Method
End Class

Global agent:List<agents> = New List<agents>

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(10)
        'setup the array
        For Local i = 0 Until mapwidth
            map[i] = New Int[mapheight]
        Next
        'make the obstacles
        For Local i=0 Until 30
            Local x1:Int=Rnd(2,18)
            Local y1:Int=Rnd(2,18)
            map[x1][y1] = 1
        Next
    End Method
    Method OnUpdate()        
        If Rnd(50)<5 Then addagent
        For Local i:=Eachin agent
            i.update
        Next
    End Method
    Method OnRender()
        Cls 0,0,0 
        SetColor 255,255,255
        drawmap
        For Local i:=Eachin agent
            i.draw
        Next
        SetColor 255,255,255
        DrawText "Random obstacle avoidance example.",0,0
    End Method
End Class

Function addagent:Void()
    Local x:Int = 0
    Local y:Int = Rnd(mapheight-1)
    Local dx:Int = mapwidth-1
    Local dy:Int=Rnd(mapheight-1)
    agent.AddLast(New agents(x,y,dx,dy))
End Function

Function drawmap:Void()
    SetColor 0,255,0
    For Local y=0 Until mapheight
    For Local x=0 Until mapwidth
        If map[x][y] = 1
            DrawOval x*tilewidth,y*tileheight,tilewidth,tileheight
        End If
    Next
    Next
End Function

Function Main()
    New MyGame()
End Function