UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on January 28, 2025, 09:23:05 AM

Title: Storing Objects In Linked List - Moving Objects Example - PlayBASIC 2D
Post by: kevin on January 28, 2025, 09:23:05 AM
This PlayBASIC code demonstrates the use of **LinkedList** features for managing dynamic objects in a simple animation. Here's what the code does step-by-step:

1. Setup and Initialization:
  - The program sets the frame rate to 60 frames per second (`setfps 60`).
  - A type, `tAnimatedObject`, is defined to represent individual animated objects with attributes for position (`X#, Y#`), angle (`Angle#`), speed (`Speed#`), and color (`Colour`).

2. Dynamic Object Creation:
  - When the left mouse button is pressed (`if mb=1`), a new `tAnimatedObject` is created and added to the `LinkedList`:
    - The angle starts at `0`, representing the initial position.
    - The speed of movement is set to `4.45`.
    - A random color is assigned to the object (`rndrgb()`).

3. Object Animation:
  - The objects are animated in a circular motion around the mouse pointer:
    - The position of each object (`X#, Y#`) is calculated using trigonometric functions (`cos` and `sin`) based on its angle, spanning a radius of 100 pixels around the mouse.
    - Each object's angle is incremented by its speed, causing it to move along the circular path.

4. Object Removal:
  - If an object's angle exceeds 180 degrees, it is removed from the `LinkedList` (`if (angle# > 180) then Object = null`), simulating objects "disappearing" after completing their path on the left-hand side of the mouse pointer.

5. Visual Feedback:
  - A circle is drawn around the mouse pointer to indicate the reference position.
  - Each animated object is rendered as a circle with its assigned color at its calculated position (`circlec`).
  - The angle of each object is displayed for debugging purposes.

6. Loop:
  - The program continuously runs this logic in a loop, redrawing the screen (`cls` and `sync`) and updating all active objects.

Summary:
This demo creates and animates objects dynamically using PlayBASIC's **LinkedList** features. Objects are created when the mouse is clicked and move in a 180-degree arc around the mouse pointer, from the right-hand side to the left. They are removed from the list once they complete their arc. The code demonstrates dynamic object creation, animation, and memory management using `LinkedList` operations.


[pbcode]

    setfps 60


    type tAnimatedObject
            X#,Y#
            Angle#
            Speed#
            Colour
    endtype

    Dim object as tAnimatedobject list

    do
        cls
       
        mx=mousex()
        my=mousey()
        mb=mousebutton()

        if mb=1
            Object = new tAnimatedobject

            Object.Angle    =  0
            Object.Speed    =  4.45
            Object.Colour    = rndrgb()
            flushmouse
        endif


        print "press mouse to create dynamic object"

        // draw a circle at mouse position       
        circle mx,my,50,true


        for each object()
           
            angle#=Object.Angle
            // Animate object
            object.X#=mx+cos(angle#)*100
            object.Y#=my+sin(angle#)*100
            circlec object.x#,object.y#,50,true,object.colour
            angle#+=object.speed#
            Object.Angle=angle#
            print angle#
            if (angle#>180) then Object = null
        next   
        Sync
    loop

[/pbcode]