Tutorial:Explosion

From Plasmaworks

Jump to: navigation, search

This beginner tutorial draws an explosion of spheres wherever you click the mouse.

  • Set up a Plasmacore project as described in Plasmacore Project Management. Call it "Explosion".
  • Download this image and save it in your project as images/sphere.png:
File:Sphere.png
  • Replace src/explosion.slag with the following:
 class Explosion : Screen
  PROPERTIES
    spheres() : Sphere[]
 
  METHODS
    method init:
      Images.load
 
    method update:
      forEach (sphere in spheres)
        sphere.update
        if (not sphere.in_bounds) removeCurrent sphere
      endForEach
 
    method draw:
      forEach (sphere in spheres) sphere.draw
 
    method on( MouseEvent e ):
      which (e.type)
        case MouseEvent.button_press:
          forEach (1..100)
            spheres.add( Sphere(e.position) )
          endForEach
      endWhich
 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.25
 endClass
 
 
 class Sphere
   PROPERTIES
     position, velocity : Vector2
     color=random_Color : Color
 
   METHODS
     method init( position ):
       velocity = Vector2( random_Real64*9+1, random_Radians )
 
     method update:
       position += velocity
 
     method in_bounds.Logical:
       return Display.bounding_box.contains(position)
 
     method draw:
       local var img = Images.img_sphere
       img.color = color
       img.draw(position)
 endClass