Saturday, March 26, 2016

MonkeyX - Space Game Ship/Shooting/rotation - code example


Import mojo

Global ang:Float=0
' poly shape
Global ship:Float[]=[    -5.0,-5.0,
                         5.0,0.0,
                         -5.0,5.0]  
Class bullet
    Field x:Float
    Field y:Float
    Field angle:Float
    Field speed:Float
    Field timeout:Int
    Field deleteme:Bool=False
    Field tp:Int 'type of bullet/laser
    Method New(    x:Int,y:Int,
                angle:Float,
                speed:Float,
                tp:Int)
        Self.x = x
        Self.y = y
        Self.angle = angle
        Self.speed = speed
        Self.tp = tp
        timeout = 200
    End Method
    Method update()
        x+=Cos(angle)*speed
        y+=Sin(angle)*speed
        timeout-=1
        If timeout<0 Then
            deleteme = True
        End If
    End Method
    Method draw()
        SetColor 255,255,0
        Select tp
            Case 1
            DrawOval x-3,y-3,6,6
            Case 2
            DrawOval x-6,y-6,12,12
        End Select
    End Method
End Class

Global mybullet:List<bullet> = New List<bullet>

Class MyGame Extends App
    Field bulletdelay:Int=30
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate() 
        ang+=1
        If ang>360 Then ang=0        
        ' here we shoot when bulletdelay
        ' is <0
        bulletdelay-=1
        If bulletdelay<0
            shoot(Rnd(1,4))
            bulletdelay=30
        Endif
        '
        For Local i:=Eachin mybullet
            i.update
            If i.deleteme = True
                mybullet.Remove(i)
            End If
        Next
    End Method
    Method OnRender()
        Cls 0,0,0  
        SetColor 255,255,255
        PushMatrix()
        Translate DeviceWidth()/2,DeviceHeight()/2
        Rotate(-ang)
        Scale(4,4)
        DrawPoly(ship)
        PopMatrix()  
        Translate 0,0
        SetColor 255,0,0
        DrawLine    DeviceWidth()/2,
                      DeviceHeight()/2,
                    DeviceWidth()/2+Cos(ang)*64,
                    DeviceHeight()/2+Sin(ang)*64
        For Local i:=Eachin mybullet
            i.draw
        Next
        SetColor 255,255,255
           DrawText "MonkeyX - Space game ship/shooting/rotating example.",0,0
    End Method
End Class

Function shoot(tp:Int)
    Select tp
        Case 1
        'forwards shot
        mybullet.AddLast(New bullet(    DeviceWidth/2,
                                        DeviceHeight/2,
                                        ang,
                                        3,
                                        1))
        'backward shot
        mybullet.AddLast(New bullet(    DeviceWidth/2,
                                        DeviceHeight/2,
                                        ang-180,
                                        3,
                                        1))
        Case 2        
        ' three forwards spread shot
        For Local a=-16 To 16 Step 16
            mybullet.AddLast(New bullet(    DeviceWidth/2,                
                                            DeviceHeight/2,
                                            ang+a,
                                            2,
                                            2))
        Next
        Case 3
        ' circle shot
        For Local a=0 Until 360 Step 16
            mybullet.AddLast(New bullet(    DeviceWidth/2,
                                            DeviceHeight/2,
                                            ang+a,
                                            4,
                                            1))            
        Next
    End Select

End Function

Function Main()
    New MyGame()
End Function

MonkeyX - Space game Ship/Map/Scrolling - code example


If you need a starting point for a space game look at this. Ship and rotation. Map 
Scrolls based on the angle/direction of the ship.


Import mojo

Global tilewidth:Int=32
Global tileheight:Int=32
Global ang:Float=0
Global thrust:Float=2
' poly shape
Global ship:Float[]=[    -5.0,-5.0,
                         5.0,0.0,
                         -5.0,5.0]  

Class map
    Field mapx:Float=0
    Field mapy:Float=0
    Field map:Int[][] = [    [0,0,0,1,0,1,0,1,0,1,0,1,0,0,0],
                            [0,0,0,0,1,0,0,0,0,0,1,0,0,0,0],
                            [0,1,0,1,0,1,0,0,0,1,0,1,0,1,0],
                            [0,1,0,0,0,0,1,1,1,0,0,0,0,1,0],
                            [0,1,0,1,0,0,1,0,1,0,0,1,0,1,0],
                            [0,1,0,0,0,0,1,1,1,0,0,0,0,1,0],
                            [0,1,0,1,0,1,0,0,0,1,0,1,0,1,0],
                            [0,1,0,0,1,0,0,0,0,0,1,0,0,1,0],
                            [0,0,0,1,0,1,0,1,0,1,0,1,0,0,0],
                            [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]                                                                                                                                                                                                    
    Method New()        
    End Method
    Method update()
           mymap.mapx -= Cos(ang)*thrust
           mymap.mapy -= Sin(ang)*thrust        
    End Method
    Method draw()
        SetColor 155,155,155
        For Local y=0 Until 10
        For Local x=0 Until 15
            If map[y][x] = 1
                DrawRect     x*tilewidth+mapx,
                            y*tileheight+mapy,
                            tilewidth,
                            tileheight
            End If
        Next
        Next
    End Method
End Class

Global mymap:map = New map()

Class MyGame Extends App
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate() 
        ang+=1
        If ang>360 Then ang=0
        mymap.update
    End Method
    Method OnRender()
        Cls 0,0,0  
        mymap.draw                
        SetColor 255,255,255
        PushMatrix()
        Translate DeviceWidth()/2,DeviceHeight()/2
        Rotate(-ang)
        Scale(4,4)
        DrawPoly(ship)
        PopMatrix()  
        Translate 0,0
        SetColor 255,0,0
        DrawLine    DeviceWidth()/2,
                      DeviceHeight()/2,
                    DeviceWidth()/2+Cos(ang)*64,
                    DeviceHeight()/2+Sin(ang)*64
                    SetColor 255,255,255
           DrawText "MonkeyX - Space game ship/map/scrolling example",0,0
    End Method
End Class


Function Main()
    New MyGame()
End Function

MonkeyX - Space Game Make/Rotate Poly and Cos Sin - code example



If you need to know how to start a top down space game then take a look at this.

'angle 0 is facing right
'
' 000
' 0-- - = angle 0
' 000

Import mojo

Class MyGame Extends App
    Global ang:Float=0
    Method OnCreate()
        SetUpdateRate(60)
    End Method
    Method OnUpdate() 
        ang+=1
        If ang>360 Then ang=0
    End Method
    Method OnRender()
        Cls 0,0,0 
        'center of the image is 0,0 on screen
        'here we create the poly shape_coords
        Local vorm1:Float[]=[    -5.0,-5.0,
                                5.0,0.0,
                                -5.0,5.0]                                
        SetColor 255,255,255
        PushMatrix()
        ' we draw the poly to translated coords
        Translate DeviceWidth()/2,DeviceHeight()/2
        ' rotate in reverse
        Rotate(-ang)
        ' enlarge times
        Scale(4,4)
        ' draw the poly
           DrawPoly(vorm1)
           PopMatrix()
           'translate back to 0,0           
           Translate 0,0
           SetColor 255,0,0
           DrawLine     DeviceWidth()/2,
                       DeviceHeight()/2,
                       DeviceWidth()/2+Cos(ang)*64,
                       DeviceHeight()/2+Sin(ang)*64
                       SetColor 255,255,255
           DrawText "MonkeyX - Space game Rotate Poly and Cos Sin example",0,0
           DrawText "angle:"+ang,0,20
    End Method
End Class


Function Main()
    New MyGame()
End Function

Tuesday, March 22, 2016

MonkeyX - 2d Line Explosion Effect - code example


Import mojo

Global numrings:Int=20

Class ring
    Field x1:Int,y1:Int
    Field x2:Float[]
    Field y2:Float[]
    Field spd:Float[]
    Field ang:Int[]
    Field n:Int
    Field thetime:Int
    Field timeout:Int
    Field deleteme:Bool
    Method New(x:Int,y:Int,num:Int,tmo:Int)
        timeout = tmo
        x1 = x
        y1 = y
        x2 = New Float[num]
        y2 = New Float[num]
        spd = New Float[num]
        ang = New Int[num]
        n = num
        For Local i=0 Until num
            ang[i] = (i*(360/num))
            x2[i] = x1+Cos(ang[i])'*Rnd(1,7)
            y2[i] = y1+Sin(ang[i])'*Rnd(1,7)
            spd[i] = 1'Rnd(0.2,0.8)
        Next
    End Method
    Method update()
        For Local i=0 Until n
            x2[i]+=Cos(ang[i])*spd[i]
            y2[i]+=Sin(ang[i])*spd[i]
        Next
        thetime+=1
        If thetime > timeout
            deleteme = True
        End If
    End Method
    Method draw()
        SetColor 0,0,0
        For Local i=1 Until n
            DrawLine x2[i-1],y2[i-1],x2[i],y2[i]
        Next
        DrawLine x2[0],y2[0],x2[n-1],y2[n-1]
    End Method
End Class

Global r:List<ring> = New List<ring>

Class MyGame Extends App
    ' time containers
    Field newring:Int[numrings]
    'locations on screen
    Field lx:Int[numrings]
    Field ly:Int[numrings]
    Method OnCreate()
        SetUpdateRate(60)
        ' set random positions
        For Local i=0 Until numrings
            lx[i] = Rnd(0,DeviceWidth)
            ly[i] = Rnd(0,DeviceHeight)
        Next
    End Method
    Method OnUpdate()
        For Local i=0 Until numrings
            newring[i]-=1
            ' if time
            If newring[i]<0
                ' chance for new location
                If Rnd(100)<10
                    lx[i] = Rnd(0,DeviceWidth)
                    ly[i] = Rnd(0,DeviceHeight)
                End If
                'add new ring
                r.AddLast(New ring(    lx[i],
                                    ly[i],
                                    Rnd(3,20),
                                    Rnd(10,60)))
                ' set new time
                newring[i] = Rnd(10,40)                                    
            End If
        Next
        ' update the rings
        For Local i:=Eachin r
            i.update
            If i.deleteme = True
                r.Remove(i)
            End If
        Next
    End Method
    Method OnRender()
        Cls 255,255,255 
        SetColor 0,0,0
        'draw the rings
        For Local i:=Eachin r
            i.draw
        Next
    End Method
End Class


Function Main()
    New MyGame()
End Function

Monday, March 21, 2016

MonkeyX - Sideview Mining Map Generator - code example


Import mojo

Global mapwidth:Int=200
Global mapheight:Int=100

Class maptest
    Field tw:Float,th:Float
    Field w:Int,h:Int
    'bottom x and y contain the coords of the next
    'shaft to be created. center of room last pass
    Field bottomy:Int
    Field bottomx:Int
    Field map:Int[][]
    Method New(w:Int,h:Int)
        Self.w = w
        Self.h = h
        tw = DeviceWidth()/w
        th = DeviceHeight()/h
        map = New Int[w][]
        For Local i=0 Until w
            map[i] = New Int[h]
        Next
        drawmaprect(0,0,w-1,15)
        For Local i=0 Until h
            map[1][i] = 0
            map[w-2][i] = 0
        Next
        ' x,y,number of tunnels>>
        makemine(w/2,15,Rnd(1,3))
        makemine(bottomx,bottomy,Rnd(1,3))
        If bottomy<(Float(mapheight)/2)
            makemine(bottomx,bottomy,Rnd(1,3))
        End If
    End Method
    Method makemine(x:Int,y:Int,depth:Int)
        Local vy:Int=y
        For Local mydepth=0 Until depth
            Local d1:Int=Rnd(8,16)'depth
            tunneldown(x,y,d1)
            y+=d1
            Local d2:Int=Rnd(1,4)'direction
            If d2=1 Then 
                sidetunnel(x,y,"left")
            End If
            If d2=2 Then 
                sidetunnel(x,y,"right")
            End If
            If d2=3 Then 
                sidetunnel(x,y,"left")
                sidetunnel(x,y,"right")
            End If
        Next
        For Local y1=vy Until bottomy+2
            map[x][y1] = 2
        Next
    End Method
    Method sidetunnel(x:Int,y:Int,d:String)
        If d="left"
            Local width:Int=Rnd(5,15)
            drawmaprect(x-width+2,y,width,3)
            Local roomw:Int=Rnd(5,15)
            drawmaprect(x-width+2-roomw,y-1,roomw,5)
            For Local x1=0 Until roomw/3
                map[(x-width+2-roomw)+x1][y+4] = 3
            Next
            bottomx = x-width-(roomw/2)
            bottomy = y
        End If
        If d="right"
            Local width:Int=Rnd(5,15)
            drawmaprect(x-1,y,width,3)
            Local roomw:Int=Rnd(5,15)
            drawmaprect(x+width,y-1,roomw,5)        
            For Local x1=roomw Until roomw/1.5 Step -1
                map[(x+width)+x1][y+4] = 3
            Next
            bottomx = x+width+(roomw/2)            
            bottomy = y
        End If
    End Method
    Method tunneldown(x:Int,y:Int,d:Int)
        drawmaprect(x-2,y,4,d)
    End Method
    Method drawmaprect(x:Int,y:Int,w:Int,h:Int)
        For Local y1=y To y+h
        For Local x1=x To x+w
            map[x1][y1] = 1
        Next
        Next        
    End Method
    Method draw()
        For Local y=0 Until h
        For Local x=0 Until w
            Local x1:Float=DeviceWidth()/Float(mapwidth)*Float(x)
            Local y1:Float=DeviceHeight/Float(mapheight)*Float(y)
            If map[x][y] = 1
                SetColor 255,255,255                
                DrawRect x1,y1,tw,th+1
            End If
            If map[x][y] = 3
                SetColor 200,200,10
                DrawOval x1,y1,tw,th+1
            End If            
        Next
        Next
    End Method
End Class

' -----------------------------------------------------------------------------------------------

Global mymaptest:maptest

Class MyGame Extends App
    Field nmap:Int=0
    Method OnCreate()
        Local date := GetDate()
        Seed = date[5]
        SetUpdateRate(60)
        restartgame
    End Method
    Method OnUpdate()    
        nmap+=1
        If KeyDown(KEY_SPACE)=False And nmap>60
            restartgame
            nmap=0
        End If
    End Method
    Method OnRender()
        Cls 0,0,0 
        mymaptest.draw
        SetColor 255,255,0
        DrawText "Hold Spacebar to pauze",20,DeviceHeight()-15
        DrawText "MonkeyX Sideview - Mining map Example",20,0
    End Method
End Class



Function Main()
    New MyGame()
End Function


Function restartgame()
    mymaptest = New maptest(mapwidth,mapheight)
End Function

Function distance:Int(x1:Int,y1:Int,x2:Int,y2:Int)
    Return Abs(x2-x1)+Abs(y2-y1)
End Function 
    
Function rectsoverlap:Bool(x1:Int, y1:Int, w1:Int, h1:Int, x2:Int, y2:Int, w2:Int, h2:Int)
    If x1 >= (x2 + w2) Or (x1 + w1) <= x2 Then Return False
    If y1 >= (y2 + h2) Or (y1 + h1) <= y2 Then Return False
    Return True
End Function