├── README.md ├── color-dodge-game ├── README.md └── color-dodge.rb ├── fonts └── Ubuntu.ttf ├── keyevent-example ├── README.md └── keyevent-example.rb ├── toggle-switch ├── README.md ├── toggle-switch.rb └── union.rb └── triangle-game └── triangle-game.rb /README.md: -------------------------------------------------------------------------------- 1 | # Ruby 2D Examples 2 | 3 | ## Color Dodge Game 4 | Try to survive the endless wave of colored squares! 5 | Stay alive as long as you can and your score will go up! 6 | Touch sufficient colored squares to empty your life bar and you'll have to start again! 7 | 8 | ## KeyEvent 9 | An example using the `on` method to capture `KeyEvents`. 10 | It displays the keys you type, like stamps on the screen. 11 | 12 | ## Toggle Switch 13 | An example of a simple switch that when toggled 14 | will change its appearance to reflect its state as well 15 | as change the background color of the application. 16 | 17 | ## Triangle Game 18 | A simple "Triangle Game" that allows you to move a Roguelike '@' around the window (and off of it). 19 | -------------------------------------------------------------------------------- /color-dodge-game/README.md: -------------------------------------------------------------------------------- 1 | # Color Dodge Game 2 | 3 | Made by [Heitorado](https://github.com/heitorado) 4 | 5 | **Ruby2D Version:** 0.6.0 6 | 7 | Feel free to use and modify as you wish! 8 | 9 | ## Controls: 10 | W, A, S, D to move the little yellow ball around 11 | 12 | ## How to play: 13 | Try to survive the endless wave of colored squares! 14 | Stay alive as long as you can and your score will go up! 15 | Touch sufficient colored squares to empty your life bar and you'll have to start again! 16 | 17 | Maybe not the best way to implement it, but it shows what can be achieved with the ruby2dgem. 18 | Help me improve, send suggestions about the code! -------------------------------------------------------------------------------- /color-dodge-game/color-dodge.rb: -------------------------------------------------------------------------------- 1 | require 'ruby2d' 2 | 3 | set title: "Color Dodge Game" 4 | set background: "black" 5 | 6 | 7 | class Enemy 8 | attr_reader :collision, :body 9 | attr_accessor :collision_opacity, :body_color 10 | attr_accessor :x, :y, :size 11 | 12 | def initialize(opts = {}) 13 | 14 | puts opts 15 | 16 | @x = opts[:x] 17 | @y = opts[:y] 18 | @size = opts[:size] 19 | @body_color = opts[:color] 20 | 21 | 22 | @body = Square.new( x: @x, y: @y, size: @size, color: @body_color ) 23 | 24 | end 25 | 26 | def body_color=(color) 27 | @body.color = color 28 | end 29 | 30 | def moveLeft(quantity) 31 | @body.x -= quantity 32 | end 33 | 34 | def getBodyPosition 35 | return @body.x, @body.y 36 | end 37 | 38 | def size 39 | return @body.size 40 | end 41 | 42 | def radius 43 | return @body.size*Math.sqrt(2)/2 44 | end 45 | end 46 | 47 | 48 | # Instantiating the player 49 | player = Circle.new(x: 10, y: 50, radius: 10, color: 'yellow') 50 | 51 | player_life = 300 52 | 53 | # Instantiating the score text and variable 54 | score = 0 55 | score_text = Text.new(x: (get :width)-150, y: 10, text: "Score: #{score}", font: "../fonts/Ubuntu.ttf", color: 'white') 56 | 57 | # Creating a lifebar 58 | lifebar = Rectangle.new(x: 10, y: 10, z: 1, width: player_life, height: 25, color: 'red') 59 | 60 | 61 | # Instantiating the enemies array and a tick counter 62 | enemies = [] 63 | 64 | tick = 0 65 | 66 | # Event of pressing a key on the keyboard 67 | on :key do |e| 68 | if e.type == :held 69 | # Here we check the bounds of the screen so the player can't trespass them. 70 | case e.key 71 | when 's' 72 | player.y += player.radius/2 if player.y < (get :height)-player.radius 73 | when 'w' 74 | player.y -= player.radius/2 if player.y > 0 75 | when 'a' 76 | player.x -= player.radius/2 if player.x > 0 77 | when 'd' 78 | player.x += player.radius/2 if player.x < (get :width)-player.radius 79 | end 80 | end 81 | end 82 | 83 | update do 84 | 85 | # Game over condition. Resets the game and the score instantly. 86 | if(lifebar.width == 0) 87 | enemies.each do |enemy| 88 | enemy.body_color = 'black' 89 | end 90 | enemies.clear() 91 | score = 0 92 | lifebar.width = player_life 93 | end 94 | 95 | 96 | if(tick%5==0 && enemies.size < 25) 97 | enemies << Enemy.new(x: 620, y: (rand*460).floor, size: 25, collision_opacity: 0.3, color: 'random') #Square.new( x: 620, y: (rand*460).floor, size: 25 ) 98 | end 99 | 100 | if(tick%25 == 0) 101 | score += 1 102 | score_text.text = "Score: #{score}" 103 | end 104 | 105 | enemies.each do |enemy| 106 | # Check for collisions with the player 107 | dx = player.x - enemy.getBodyPosition().first 108 | dy = player.y - enemy.getBodyPosition().last 109 | 110 | # Euclidian distance between the two centers of the squares 111 | distance = Math.sqrt(dx * dx + dy * dy) 112 | 113 | # If this calculated distance is less than the two radius of the shapes summed, then they are overlapping. 114 | if( distance < player.radius + enemy.radius ) 115 | #puts "COLLISION!" 116 | 117 | # Reduces player life 118 | lifebar.width -= 5 119 | end 120 | 121 | # "Throw the blocks at the player at speed 8" 122 | enemy.moveLeft(8) 123 | 124 | # Enemy arrives at the left part of the screen, we "erase it" 125 | if enemy.getBodyPosition().first < 0 - enemy.size() 126 | enemy.body_color = 'black' 127 | enemies.delete(enemy) 128 | end 129 | end 130 | 131 | tick += 1 132 | end 133 | 134 | show -------------------------------------------------------------------------------- /fonts/Ubuntu.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ruby2d/examples/0c5ae1ce0135ea641cc2a1ec44f5790da70286ea/fonts/Ubuntu.ttf -------------------------------------------------------------------------------- /keyevent-example/README.md: -------------------------------------------------------------------------------- 1 | # KeyEvent Example 2 | 3 | Made by [slime-man](https://github.com/slime-man) 4 | 5 | **Ruby2D Version:** 0.8.1 6 | 7 | An example using the `on` method to capture `KeyEvents`. 8 | It displays the keys you type, like stamps on the screen. 9 | 10 | ## Controls: 11 | Press 'space' to clear, and 'backspace' to quit. 12 | -------------------------------------------------------------------------------- /keyevent-example/keyevent-example.rb: -------------------------------------------------------------------------------- 1 | # 2 | # Ruby2D Example: Key Events 3 | # 4 | # This is a simple example capturing KeyEvents. Run it and try typing! 5 | # => author: slime-man 6 | require 'ruby2d' 7 | 8 | set title: "KeyEvent Example" 9 | text_size = 20 10 | font_path = "../fonts/Ubuntu.ttf" 11 | 12 | on :key_down do |k| 13 | key = k['key'] 14 | case key 15 | when 'space' 16 | clear 17 | when 'backspace' 18 | close 19 | else 20 | Text.new( 21 | key, 22 | x: rand((get :width) - text_size), 23 | y: rand((get :height) - text_size), 24 | size: text_size, 25 | font: font_path, 26 | z: 0, 27 | color: 'random' 28 | ) 29 | end 30 | end 31 | 32 | show 33 | -------------------------------------------------------------------------------- /toggle-switch/README.md: -------------------------------------------------------------------------------- 1 | # Toggle Switch 2 | 3 | Made by [Tobias Wilfert](https://github.com/tobias-wilfert) 4 | 5 | **Ruby2D Version:** 0.8.1 6 | 7 | Feel free to use and modify as you wish! 8 | 9 | ## Controls: 10 | Mouse to toggle the switch 11 | 12 | ## How to use: 13 | This is an example of a simple switch that when toggled 14 | will change its appearance to reflect its state as well 15 | as change the background color of the application. 16 | -------------------------------------------------------------------------------- /toggle-switch/toggle-switch.rb: -------------------------------------------------------------------------------- 1 | require 'ruby2d' 2 | require_relative 'union' 3 | 4 | light_is_on = true 5 | 6 | set background: 'aqua' 7 | set title: "Simple Switch" 8 | 9 | middle_bar = Square.new( 10 | x:295, y:215, 11 | size:50, 12 | color: 'green', 13 | z:0 14 | ) 15 | 16 | left_circle = Circle.new( 17 | x: 300, y: 240, 18 | radius: 25, 19 | sectors: 32, 20 | color: 'white', 21 | z: 1 22 | ) 23 | 24 | right_circle = Circle.new( 25 | x: 340, y: 240, 26 | radius: 25, 27 | sectors: 32, 28 | color: 'green', 29 | z: 0 30 | ) 31 | 32 | # Create a union of all shapes that are a part of the switch 33 | switch = Union.new([left_circle,middle_bar,right_circle]) 34 | 35 | on :mouse_down do |event| 36 | # If clicked while on top of the switch 37 | if switch.contains?(event.x, event.y) 38 | if light_is_on # same as 'if light_is_on == true' 39 | 40 | light_is_on = false 41 | set background: 'blue' 42 | 43 | left_circle.z = 0 44 | right_circle.z = 1 45 | 46 | middle_bar.color = 'silver' 47 | left_circle.color = 'silver' 48 | right_circle.color = 'white' 49 | 50 | else 51 | 52 | light_is_on = true 53 | set background: 'aqua' 54 | 55 | left_circle.z = 1 56 | right_circle.z = 0 57 | 58 | middle_bar.color = 'green' 59 | left_circle.color = 'white' 60 | right_circle.color = 'green' 61 | 62 | end 63 | end 64 | end 65 | 66 | show 67 | -------------------------------------------------------------------------------- /toggle-switch/union.rb: -------------------------------------------------------------------------------- 1 | class Union 2 | # A class that behaves like the mathematical union 3 | 4 | # @param sets [array] An array of shapes 5 | def initialize(sets) 6 | @sets = sets 7 | end 8 | 9 | # @param x_position [int] An x coordinate on the screen 10 | # @param y_position [int] An y coordinate on the screen 11 | # @return [bool] True if one of the shapes in sets contains the position 12 | def contains?(x_position,y_position) 13 | @sets.each do |set| 14 | if set.contains?(x_position,y_position) 15 | return true 16 | end 17 | end 18 | return false 19 | end 20 | 21 | end 22 | -------------------------------------------------------------------------------- /triangle-game/triangle-game.rb: -------------------------------------------------------------------------------- 1 | # A simple "Triangle Game" that allows you to move a Roguelike '@' around the 2 | # window (and off of it). This is a working example on MacOS 10.12 as of Dec 16, 2017. 3 | # This combines some of the Ruby2D tutorial code with keypress management 4 | # that actually works. 5 | # 6 | # Keys: hjkl: movement, q: quit 7 | # 8 | # To run: ruby triangle-game.rb after installing the Simple2D library and Ruby2D Gem. 9 | # 10 | # Author: Douglas P. Fields, Jr. 11 | # E-mail: symbolics@lisp.engineer 12 | # Site: https://symbolics.lisp.engineer/ 13 | # Copyright 2017 Douglas P. Fields, Jr. 14 | # License: The MIT License 15 | 16 | require 'ruby2d' 17 | 18 | set title: "Triangle Game" 19 | 20 | Triangle.new( 21 | x1: 320, y1: 50, 22 | x2: 540, y2: 430, 23 | x3: 100, y3: 430, 24 | color: ['red', 'green', 'blue'] 25 | ) 26 | 27 | msg = Text.new(x: 320, y: 240, text: 'Hi, Tres!', size: 20, font: '../fonts/Ubuntu.ttf') 28 | 29 | player = Text.new(x: 0, y: 0, text: '@', size: 32, font: '../fonts/Ubuntu.ttf'); 30 | 31 | msg.x = 320 - msg.width / 2 32 | msg.y = 240 - msg.height / 2 33 | 34 | tick = 0 35 | t = Time.now 36 | 37 | on :key do |e| 38 | # puts e 39 | if e.type == :down 40 | case e.key 41 | when 'j' 42 | player.y += player.height 43 | when 'k' 44 | player.y -= player.height 45 | when 'h' 46 | player.x -= player.width 47 | when 'l' 48 | player.x += player.width 49 | when 'q' 50 | close 51 | end 52 | end 53 | end 54 | 55 | update do 56 | if tick % 60 == 0 57 | set background: 'random' 58 | end 59 | tick += 1 60 | # Close the window after 5 seconds 61 | #if Time.now - t > 5 then close end 62 | end 63 | 64 | show 65 | 66 | --------------------------------------------------------------------------------