Skip to content

Commit

Permalink
Added an example and restructured files (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobias-wilfert authored and blacktm committed Mar 14, 2019
1 parent 30bce07 commit 0c5ae1c
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 3 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# Ruby 2D Examples

## Color Dodge Game
Try to survive the endless wave of colored squares!
Stay alive as long as you can and your score will go up!
Touch sufficient colored squares to empty your life bar and you'll have to start again!

## KeyEvent
An example using the `on` method to capture `KeyEvents`.
It displays the keys you type, like stamps on the screen.

Press **space** to clear, and **backspace** to quit.
## Toggle Switch
An example of a simple switch that when toggled
will change its appearance to reflect its state as well
as change the background color of the application.

## Triangle Game
A simple "Triangle Game" that allows you to move a Roguelike '@' around the window (and off of it).
11 changes: 11 additions & 0 deletions keyevent-example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# KeyEvent Example

Made by [slime-man](https://github.com/slime-man)

**Ruby2D Version:** 0.8.1

An example using the `on` method to capture `KeyEvents`.
It displays the keys you type, like stamps on the screen.

## Controls:
Press 'space' to clear, and 'backspace' to quit.
4 changes: 2 additions & 2 deletions keyevent-example.rb → keyevent-example/keyevent-example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

set title: "KeyEvent Example"
text_size = 20
font_path = "fonts/Ubuntu.ttf"
font_path = "../fonts/Ubuntu.ttf"

on :key_down do |k|
key = k['key']
Expand All @@ -18,9 +18,9 @@
close
else
Text.new(
key,
x: rand((get :width) - text_size),
y: rand((get :height) - text_size),
text: key,
size: text_size,
font: font_path,
z: 0,
Expand Down
15 changes: 15 additions & 0 deletions toggle-switch/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Toggle Switch

Made by [Tobias Wilfert](https://github.com/tobias-wilfert)

**Ruby2D Version:** 0.8.1

Feel free to use and modify as you wish!

## Controls:
Mouse to toggle the switch

## How to use:
This is an example of a simple switch that when toggled
will change its appearance to reflect its state as well
as change the background color of the application.
66 changes: 66 additions & 0 deletions toggle-switch/toggle-switch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
require 'ruby2d'
require_relative 'union'

light_is_on = true

set background: 'aqua'
set title: "Simple Switch"

middle_bar = Square.new(
x:295, y:215,
size:50,
color: 'green',
z:0
)

left_circle = Circle.new(
x: 300, y: 240,
radius: 25,
sectors: 32,
color: 'white',
z: 1
)

right_circle = Circle.new(
x: 340, y: 240,
radius: 25,
sectors: 32,
color: 'green',
z: 0
)

# Create a union of all shapes that are a part of the switch
switch = Union.new([left_circle,middle_bar,right_circle])

on :mouse_down do |event|
# If clicked while on top of the switch
if switch.contains?(event.x, event.y)
if light_is_on # same as 'if light_is_on == true'

light_is_on = false
set background: 'blue'

left_circle.z = 0
right_circle.z = 1

middle_bar.color = 'silver'
left_circle.color = 'silver'
right_circle.color = 'white'

else

light_is_on = true
set background: 'aqua'

left_circle.z = 1
right_circle.z = 0

middle_bar.color = 'green'
left_circle.color = 'white'
right_circle.color = 'green'

end
end
end

show
21 changes: 21 additions & 0 deletions toggle-switch/union.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Union
# A class that behaves like the mathematical union

# @param sets [array] An array of shapes
def initialize(sets)
@sets = sets
end

# @param x_position [int] An x coordinate on the screen
# @param y_position [int] An y coordinate on the screen
# @return [bool] True if one of the shapes in sets contains the position
def contains?(x_position,y_position)
@sets.each do |set|
if set.contains?(x_position,y_position)
return true
end
end
return false
end

end

0 comments on commit 0c5ae1c

Please sign in to comment.