Created
July 9, 2021 22:59
-
-
Save SealtielFreak/8cb633f85f4c2edd6febde0babd3dab5 to your computer and use it in GitHub Desktop.
Collision demo with Bomp library for Ruby2D
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
require 'ruby2d' | |
require 'bomp' | |
class Box < Rectangle | |
attr_reader :x, :y, :width, :height | |
def initialize | |
super() | |
self.width = rand 50..150 | |
self.height = rand 50..150 | |
self.x = rand(0..Window.width - @width) | |
self.y = rand(0..Window.height - @height) | |
self.color = 'random' | |
end | |
end | |
class Player < Square | |
def initialize | |
super() | |
self.size = 25 | |
self.color = 'red' | |
end | |
end | |
@world = Bomp::World.new | |
@boxes = Array.new(15) { Box.new } | |
@player = Player.new | |
@boxes.each { |box| @world.add box } | |
@world.add @player | |
on :key_held do |event| | |
key = event.key | |
speed = 2.5 | |
x, y = [0, 0] | |
case key | |
when 'up' | |
y -= 1 | |
when 'down' | |
y += 1 | |
when 'left' | |
x -= 1 | |
when 'right' | |
x += 1 | |
end | |
filter = lambda do |item, other| | |
item.color = 'random' | |
other.color = 'random' | |
return 'slide' | |
end | |
puts @world.move(@player, x: x * speed, y: y * speed, filter: filter) | |
end | |
show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment