-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example and restructured files (#4)
- Loading branch information
1 parent
30bce07
commit 0c5ae1c
Showing
6 changed files
with
127 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |