Sha256: b2514a68e27a4d4f51c8f130659d0fadd1e76d3900965ddecdb340299aa3e2c4

Contents?: true

Size: 969 Bytes

Versions: 10

Compression:

Stored size: 969 Bytes

Contents

#
# Bounce. 
# 
# When the shape hits the edge of the window, it reverses its direction. 
#

RAD = 60       # Width of the shape
X_SPEED = 2.8  # Speed of the shape
Y_SPEED = 2.2  # Speed of the shape

attr_reader :xpos, :ypos, :xdirection, :ydirection

def setup 
  size(640, 360)
  no_stroke
  frame_rate(30)
  ellipse_mode(RADIUS)
  # Set the starting position of the shape
  @xpos = width/2
  @ypos = height/2
  @xdirection = 1  # Left or Right
  @ydirection = 1  # Top to Bottom
end

def draw 
  background(102)  
  # Update the position of the shape
  @xpos = xpos + ( X_SPEED * xdirection )
  @ypos = ypos + ( Y_SPEED * ydirection )  
  # Test to see if the shape exceeds the boundaries of the screen
  # If it does, reverse its direction by multiplying by -1
  unless (RAD ... width - RAD).cover? xpos 
    @xdirection *= -1
  end
  unless (RAD ... height - RAD).cover? ypos 
    @ydirection *= -1
  end  
  # Draw the shape
  ellipse(xpos, ypos, RAD, RAD)
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
ruby-processing-2.6.3 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.6.2 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.6.1 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.6.0 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.5.1 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.5.0 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.4.4 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.4.3 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.4.2 samples/processing_app/topics/motion/bounce.rb
ruby-processing-2.4.1 samples/processing_app/topics/motion/bounce.rb