• Hi folks,

    the forum is now set to read only.

    After 6 yrs under my control, I have done all the work on April 6th that was needed to be able to transfer the database, forum files and the domain to Philipp Moeller aka @Phil7. The Github repository was already transfered and under his control.

    According to GDPR/DSGVO laws, all user accounts are now deleted that didn't accept the new terms (Updated on March 7th 2024 and an email was send to your address).

    I wish you all a great future and health. As the internet is gigantic but sometimes also very small, I am sure we will bump into each other someday again. Looking forward to it. :-)

    Cheers
    Michael

  • Hi folks,
    as per verbal aggreement between the new owner of the Cerberus X community and myself, I will keep this place online till May 6th, 2024.
    After that date it will be purged and this Domain will be most likely be used for my own stuff again.
    I am not participating in the new place for various reasons.
    If someone wants to contact me you can do so via mail to mike@fantomgl.com.
    Best wishes
    Michael

Snippet Retro-style runtime generated sprite

Wingnut

Well-known member
3rd Party Module Dev
Tutorial Author
Joined
Jan 2, 2020
Messages
1,414
Here is a simple example on how to create a 2D sprites in runtime, using what I hope is a very familiar example.

Code:
' UP, UP, AND AWAY

Import mojo2

Function Main()
    New MyApp
End

Class MyApp Extends App
    
    Field canvas : Canvas
    Field spritecanvas:Canvas
    Field sprite:Image
     Field x:Int, y:Int
 
    Field def:String

    Method OnCreate()
        canvas = New Canvas ; SetUpdateRate 0
        sprite = New Image(24,21,0,0,0) ' Enable sprite
        spritecanvas = New Canvas(sprite)
        createSprite(spritecanvas)
    End

    Method OnRender()
        canvas.Clear 0,136/255.0,1
        canvas.DrawImage sprite,x,y
        x = x + 1 ; y = y + 1
        If x >= 200 Then x = 0
        If y >= 200 Then y = 0
        canvas.Flush
    End

    Method createSprite(cp:Canvas)
        cp.Clear 0,0,0,0
        cp.SetColor 1,1,1
               def = "         *******        "
         def = def + "       ***********      "
         def = def + "      *************     "
         def = def + "      *****  ******     "
         def = def + "     ***** **  *****    "
         def = def + "     ***** *********    "
         def = def + "     ***** **  *****    "
         def = def + "      *****  ******     "
         def = def + "      *************     "
         def = def + "      *************     "
         def = def + "      * ********* *     "
         def = def + "       * ******* *      "
         def = def + "       *  *****  *      "
        def = def + "        *  ***  *       "
        def = def + "        *  ***  *       "
        def = def + "         *  *  *        "
        def = def + "         *  *  *        "
        def = def + "          *****         "
        def = def + "          *****         "
        def = def + "          *****         "
        def = def + "           ***          "                                         
         Local tempx:Int = 0, tempy:Int = 0
         For Local temp:Int = 1 To Len(def)
             If Mid(def,temp,1) = "*" Then cp.DrawRect tempx,tempy,1,1
             tempx = tempx + 1 ; If tempx >= 24 Then tempx=tempx-24 ; tempy = tempy + 1
         Next   
        cp.Flush
    End
    
    Function Len:Int(s:String)
        Return s.Length
    End
    
    Function Mid:String(s:String,p:Int,n:Int)
        p=p-1 ; Return s[(p)..(p+n)]
    End

End
 
A version with additional animation.
Code:
' UP, UP, AND AWAY

#HTML5_CANVAS_WIDTH = 640
#HTML5_CANVAS_HEIGHT = 480
#HTML5_APP_TITLE = "Demo"
#HTML5_APP_FILENAME = "demo.html"
#HTML5_CANVAS_RESIZE_MODE = 1         ' 0 = locked, 1 = stretch, 2 = resize.
#HTML5_CANVAS_ALPHA = False
#HTML5_CANVAS_ANTIALIAS = True
#HTML5_CONSOLE_SHOW = False
#MOJO_AUTO_SUSPEND_ENABLED = True   

Import mojo2

Function Main()
    New MyApp
End

Class MyApp Extends App
    
    Field canvas : Canvas
    Field spritecanvas:Canvas
    Field sprite:Image
     Field x:Int, y:Int
     Field bd:Float
     Field b:Float
 
    Field def:String

    Method OnCreate()
        canvas = New Canvas
        SetSwapInterval 1 ; SetUpdateRate 0
        sprite = New Image(24,21,0,0,0) ' Enable sprite
        spritecanvas = New Canvas(sprite)
        createSprite(spritecanvas)
    End

    Method OnRender()
        canvas.Clear 0,136/255.0,1
        canvas.SetColor 1,1,1
        ' canvas.DrawImage sprite,x,y
        ' x = x + 1 ; y = y + 1
        ' If x >= 200 Then x = 0
        ' If y >= 200 Then y = 0
        
        canvas.SetColor 0,1,0
        For Local angle := 0 To 360 Step 10
            Local xx := 300 * Cos(angle-bd) + 640/2-12
            Local yy := 150 * Sin(angle-bd) + 480/2-12
            canvas.DrawImage(sprite, xx, yy)
        Next
        
        canvas.SetColor 1,1,0
        For Local angle := 0 To 360 Step 10
            Local a := 320-12 ' Semi-major axis
            Local b := 240-11 ' Semi-minor axis
            Local centerX := 320, centerY := 240
            Local xx := centerX-12+ a * Cos(angle+bd)
            Local yy := centerY-10 + b * Sin(angle+b)
            canvas.DrawImage(sprite, xx, yy)
        Next

        canvas.SetColor 1,1,1
        For Local temp:Int = 0 To 359 Step 20
            Local w:Int = 100 * Sin(temp + x)
            Local h:Int = 15 * Cos(temp + b)
            y = y + 1
            canvas.DrawImage(sprite, w * 1 * Sin(temp + w / 4) + 320, w * Cos(temp + y / 8) + 240)
        Next
        
        b = b + 4
        bd = bd + 1
        x = x + 2
 
        canvas.Flush
    End

    Method createSprite(cp:Canvas)
        cp.Clear 0,0,0,0
        cp.SetColor 1,1,1
              def = "         *******        "
         def = def + "       ***********      "
         def = def + "      *************     "
         def = def + "      *****  ******     "
         def = def + "     ***** **  *****    "
         def = def + "     ***** *********    "
         def = def + "     ***** **  *****    "
         def = def + "      *****  ******     "
         def = def + "      *************     "
         def = def + "      *************     "
         def = def + "      * ********* *     "
         def = def + "       * ******* *      "
         def = def + "       *  *****  *      "
        def = def + "        *  ***  *       "
        def = def + "        *  ***  *       "
        def = def + "         *  *  *        "
        def = def + "         *  *  *        "
        def = def + "          *****         "
        def = def + "          *****         "
        def = def + "          *****         "
        def = def + "           ***          "                                         
         Local tempx:Int = 0, tempy:Int = 0
         For Local temp:Int = 1 To Len(def)
             If Mid(def,temp,1) = "*" Then cp.DrawRect tempx,tempy,1,1
             tempx = tempx + 1 ; If tempx >= 24 Then tempx=tempx-24 ; tempy = tempy + 1
         Next   
        cp.Flush
    End
    
    Function Len:Int(s:String)
        Return s.Length
    End
    
    Function Mid:String(s:String,p:Int,n:Int)
        p=p-1 ; Return s[(p)..(p+n)]
    End

End
 

Attachments

  • Screenshot 2024-03-04 at 21.44.00.png
    Screenshot 2024-03-04 at 21.44.00.png
    164.4 KB · Views: 18
I changed your Int values to Float and it seems to make the inner animation smoother.
 
Back
Top Bottom