Class: EduDraw::AnimationPen

Inherits:
Pen
  • Object
show all
Defined in:
lib/edu_draw/animation_pen.rb

Overview

An AnimatedPen is a special Pen that is used for drawing animated shapes

Instance Attribute Summary

Attributes inherited from Pen

#angle, #color, #shapes, #x, #y

Instance Method Summary collapse

Methods inherited from Pen

#angle_in_rad, #down!, #down?, #fill, #move, #turn_left, #turn_right, #up!, #up?

Constructor Details

#initialize(x, y, angle, color) ⇒ AnimationPen

Creates a new AnimationPen

Parameters:

  • x (Fixnum)

    Initial value of Pen#x

  • y (Fixnum)

    Initial value of Pen#y

  • angle (Fixnum)

    Initial value of Pen#angle

  • color (Gosu::Color)

    Initial value of Pen#color



9
10
11
12
13
# File 'lib/edu_draw/animation_pen.rb', line 9

def initialize(*args)
  super
  @update_block = -> {}
  @draw_block = -> {}
end

Instance Method Details

#draw_frame(&block) ⇒ Object

Note:

Every Pen#move outside this block will draw only in the first frame

Note:

Remember to set the state of the pen so that it can start drawing the next frame. In the given example the last ‘pen.turn_right 90` is not neccessary for the shape, but without it, the pen would draw the rectangle in a different direction on the next frame. Maybe, this is what you need, but it is still recommended to do these changes in the #each_frame-block.

Defines what the pen needs to draw every frame

Examples:

# Draw a rectangle
pen.draw_frame do
  pen.move 20
  pen.turn_right 90
  pen.move 10
  pen.turn_right 90
  pen.move 20
  pen.turn_right 90
  pen.move 10
  pen.turn_right 90
end


48
49
50
# File 'lib/edu_draw/animation_pen.rb', line 48

def draw_frame &block
  @draw_block = block
end

#each_frame(&block) ⇒ Object

Defines what needs to be done every frame before drawing

Examples:

# This makes the pen turn around slowly
pen.each_frame do
  pen.turn_right 1
end


22
23
24
# File 'lib/edu_draw/animation_pen.rb', line 22

def each_frame &block
  @update_block = block
end

#updateObject



53
54
55
56
57
# File 'lib/edu_draw/animation_pen.rb', line 53

def update
  empty_shapes
  @update_block.call
  @draw_block.call
end