Tiny Thor (made with Monkey X)
Develop on Windows or OSX and deploy easily
Crypt of the Necrodancer (made with Monkey X)
New Star Soccer - BAFTA Winner 2013!!! (made with Monkey X )
Ted - The code editor of Cerberus X
Avalon Legends (made with Monkey X)
Race Time (made with Monkey X)

Unlimited Bobs - mojo2

Rich Sep 19, 2018

  1. Rich

    Rich Member 3rd Party Module Dev

    Messages:
    82
    Likes Received:
    60
    Trophy Points:
    18
    Hi
    Not sure if this has been posted before. Thought I'd play last night.
    Takes me back to the Amiga days

    Code (Cerberus X):
    1.  
    2. Strict
    3. ' credit https://www.pygame.org/pcr/unlimited_sprites/index.php
    4.  
    5. Import mojo2
    6.  
    7. Class game Extends App
    8.     Field canvas:Canvas
    9.     Field images:Image[25]
    10.     Field frame:Int
    11.     Field cx:Int,cy:Int
    12.     Field xa:Float,ya:Float
    13.     Field icanvas:Canvas
    14.  
    15.     ' increase number of frames to make a largger gap between "new balls"
    16.     Const frames:=10
    17.  
    18.     Method OnCreate:Int()
    19.         Local i:Int
    20.      
    21.         cx=DeviceWidth()/2
    22.         cy=DeviceHeight()/2
    23.      
    24.         canvas = New Canvas
    25.         icanvas = New Canvas
    26.      
    27.         SetUpdateRate(60)
    28.         i=0
    29.         While i<frames  
    30.             images[i] = New Image(cx*2,cy*2)
    31.             i=i+1
    32.         Wend
    33.  
    34.         xa=0
    35.         ya=0
    36.  
    37.         frame=0
    38.         Return 0
    39.     End
    40.  
    41.     Method OnUpdate:Int()
    42.         Return True
    43.     End
    44.  
    45.     Method OnRender:Int()
    46.         Local x:Float,y:Float
    47.  
    48.         ' MATHS stuff, play with these numbers to produce different routes for the balls
    49.         x = cx*Sin(xa*0.74)
    50.         y = cy*Cos(ya*0.64)
    51.  
    52.         ' move angle
    53.         xa = xa + 1.1
    54.         ya = ya + 1.3
    55.  
    56.         ' draw into image frame only
    57.         icanvas.SetRenderTarget(images[frame])
    58.      
    59.         ' just draw the ball (dont clear old balls <<< thats the trick)
    60.         DrawBall(icanvas,cx+x,cy+y)
    61.         icanvas.Flush
    62.  
    63.         ' draw image frame to screen
    64.         canvas.Clear
    65.         canvas.DrawImage images[frame],cx,cy
    66.         canvas.Flush
    67.      
    68.         frame=(frame+1) Mod frames
    69.         Return True
    70.     End
    71.  
    72.     Method DrawBall:Void(c:Canvas,x:Float,y:Float)
    73.         ' draw simple shaded ball using DrawOval (vert Amiga like)
    74.         c.SetColor 0,0,0
    75.         c.DrawOval x-20,y-20,40,40
    76.         c.SetColor 0.4,0.5,0.5
    77.         c.DrawOval x-18,y-18,36,36
    78.         c.SetColor 0.6,0.8,0.8
    79.         c.DrawOval x-17,y-17,30,30
    80.         c.SetColor 0.8,0.9,0.9
    81.         c.DrawOval x-16,y-16,20,20
    82.         c.SetColor 1,1,1
    83.         c.DrawOval x-12,y-12,8,8  
    84.     End
    85. End
    86.  
    87. Function Main:Int()
    88.     New game      
    89.     Return 0
    90. End
    91.  

    Cant think of a way to do this in Mojo 1 as it uses multiple images to draw into before flushing it onto the screen
     
    Last edited: Sep 19, 2018
    AutmnLeaf, Ferdi and MikeHart like this.
  2. MikeHart

    MikeHart Administrator Staff Member Administrator Moderator

    Messages:
    1,231
    Likes Received:
    300
    Trophy Points:
    83
    Looking good!
     
  3. AutmnLeaf

    AutmnLeaf New Member

    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    3
    For curiosity, I made sometime last year "unlimited objects" video, too. My implementation was a bit clumsy, but did the job.

    The Monkey X Pro / Cerberus X source code, that does the job (no external graphics files):

    Code (Cerberus X):
    1. Import mojo2
    2.  
    3. Function Main()
    4.     New MyApp
    5. End
    6.  
    7. Class MyApp Extends App
    8.  
    9.     Field gfxCopy:Image[16]
    10.     Field ccanvas:Canvas[16]
    11.     Field counter:Int
    12.     Field angle:Float
    13.     Field canvas:Canvas
    14.  
    15.     Field x:Float, y:Float
    16.     Field r:Float
    17.  
    18.     Method OnCreate()
    19.      
    20.         canvas = New Canvas()
    21.         counter = 0
    22.      
    23.         For Local i:Int = 0 To 15
    24.             gfxCopy[i] = New Image(640,480,.0,.0)
    25.             ccanvas[i] = New Canvas(gfxCopy[i])
    26.         Next
    27.      
    28.         r = 100
    29.         SetUpdateRate(60)
    30.     End
    31.  
    32.     Method OnUpdate()
    33.         angle = angle + 2
    34.     End
    35.  
    36.     Method OnRender()
    37.      
    38.         ccanvas[counter].PushMatrix()
    39.      
    40.         x = Cos(angle) * r + 640 / 2
    41.         y = Sin(angle) * r + 480 / 2
    42.      
    43.         r = r + 0.1
    44.      
    45.         ccanvas[counter].SetColor(0,0,1)
    46.         ccanvas[counter].DrawCircle(x,y,32)
    47.         ccanvas[counter].SetColor(1,0,1)
    48.         ccanvas[counter].DrawCircle(x,y,30)
    49.      
    50.         ccanvas[counter].Flush()
    51.         ccanvas[counter].PopMatrix()
    52.      
    53.         canvas.PushMatrix()
    54.         canvas.Clear
    55.  
    56.         canvas.DrawImage(gfxCopy[counter],0,0)
    57.  
    58.         canvas.Flush()
    59.         canvas.PopMatrix()
    60.              
    61.         counter = counter + 1
    62.         If counter = 16 Then counter = 0
    63.      
    64.     End
    65.  
    66. End Class
    Copy/paste the code into Cerberus X editor and see the miracle -- unlimited objects! :)

    Screenshot of the program:

    unlimited objects.png

    Below is the video I made:

     
    Last edited: Sep 26, 2018
    MikeHart and Rich like this.
  4. AutmnLeaf

    AutmnLeaf New Member

    Messages:
    22
    Likes Received:
    11
    Trophy Points:
    3
    I tried it this morning by using only two canvases, too. The effect is same.

    Code (Cerberus X):
    1. Import mojo2
    2.  
    3. Function Main()
    4.     New MyApp
    5. End
    6.  
    7. Class MyApp Extends App
    8.  
    9.     Field gfxCopy:Image[16]
    10.     Field ccanvas:Canvas
    11.     Field counter:Int
    12.     Field angle:Float  
    13.     Field canvas:Canvas
    14.  
    15.     Field scaleX:Float, scaleY:Float
    16.      
    17.     Field x:Float, y:Float
    18.     Field r:Float
    19.  
    20.     Method OnCreate()
    21.          
    22.         canvas = New Canvas()
    23.         counter = 0
    24.      
    25.         For Local i:Int = 0 To 15
    26.             gfxCopy[i] = New Image(640,480,.0,.0)
    27.         Next
    28.      
    29.         ccanvas = New Canvas(gfxCopy[0])
    30.      
    31.         scaleX = DeviceWidth / 640
    32.         scaleY = DeviceHeight / 480
    33.      
    34.         r = 100
    35.         SetUpdateRate(60)
    36.     End
    37.  
    38.     Method OnUpdate()
    39.  
    40.     End
    41.  
    42.     Method OnRender()
    43.      
    44.         angle = angle + 2
    45.  
    46.         x = Cos(angle) * r + 640 / 2
    47.         y = Sin(angle) * r + 480 / 2
    48.      
    49.         r = r + 0.1
    50.  
    51.         counter = counter + 1
    52.         If counter = 16 Then counter = 0
    53.              
    54.         ccanvas.SetRenderTarget(gfxCopy[counter])
    55.         ccanvas.SetColor(1,1,0)
    56.         ccanvas.DrawCircle(x,y,32)
    57.         ccanvas.SetColor(1,0,1)
    58.         ccanvas.DrawCircle(x,y,30)
    59.      
    60.         ccanvas.Flush()
    61.      
    62.         canvas.PushMatrix()
    63.         canvas.Scale(scaleX,scaleY)
    64.      
    65.         canvas.Clear  
    66.  
    67.         canvas.DrawImage(gfxCopy[counter],0,0)
    68.  
    69.         canvas.Flush()
    70.         canvas.PopMatrix()
    71.                      
    72.     End
    73.  
    74. End Class
    I edited the code so that, it gives same output on HTML5 target as in other targets. I noticed, that if calculations are done in OnUpdate() on HTML5 target, the effect is not the same as in Desktop target.
     
    Last edited: Sep 26, 2018
    Rich and MikeHart like this.