Tutorial:Snake Trail
From Plasmaworks
This beginner tutorial draws a snake tail of spheres as you move the mouse.
- Set up a Plasmacore project as described in Plasmacore Project Management. Call it "Snake Trail".
- Download this image and save it in your project as images/sphere.png:
- Replace src/snake_trail.slag with the following:
Version 1: Simple Trail
class SnakeTrail : Screen
PROPERTIES
trail() : Vector2[]
img_sphere : Image
METHODS
method init
img_sphere = Image("sphere")
img_sphere.handle = Handle.center
img_sphere.color = Color.red
img_sphere.scale = 0.5
method update
trail.add( Input.mouse_position )
if (trail.count > 10) trail.remove_first
method draw
forEach (pos in trail) img_sphere.draw(pos)
endClass
Version 2: Object-Based Trail
- This variation uses custom classes for the spheres and the trail.
class SnakeTrail : Screen
PROPERTIES
trail(10) : Trail
METHODS
method init
Images.load
method update
trail.update( Input.mouse_position )
method draw
trail.draw
endClass
singleton class Images
PROPERTIES
img_sphere : Image
METHODS
method load
img_sphere = Image("sphere.png")
img_sphere.handle = Handle.center
img_sphere.scale = 0.5
endClass
class Sphere( Vector2 position, Color color )
METHODS
method draw
local var img = Images.img_sphere
img.color = color
img.draw(position)
endClass
class Trail( Int32 max_count )
PROPERTIES
spheres() : Sphere[]
METHODS
method update( Vector2 position )
if (spheres.count? and spheres.last.position == position) return
spheres.add( Sphere(position,random_Color) )
if (spheres.count > max_count) spheres.remove_first
method draw
forEach (sphere in spheres) sphere.draw
endClass
