Skip to content

Instantly share code, notes, and snippets.

@makevoid
Last active Jun 11, 2020
Embed
What would you like to do?
DragonRuby game starter - Tile board (square tiles) - simple base for a boardgame
class Color
RED = [200, 128, 128]
BLUE = [128, 128, 200]
GREEN = [128, 200, 128]
WHITE = [200, 200, 200]
GREY = [128, 128, 128]
BLACK = [20, 20, 20 ]
end
class World
PADDING = 46
PADDING_BOTTOM = 20
end
class Tile
DIMENSION = 92
attr_accessor :x, :y, :width, :height, :color
def initialize(x:, y:, width:, height:, color:)
@x = x
@y = y
@width = width
@height = height
@color = color
end
def self.gen(x:, y:, width: DIMENSION, height: DIMENSION, color: Color::RED)
padding = World::PADDING
padding_x, padding_y = padding, padding+World::PADDING_BOTTOM
x += padding_x
y += padding_y
new(x: x, y: y, width: width, height: height, color: color)
end
def to_a
[x, y, width, height] << color
end
alias :rect :to_a
def serialize
{ x: x, y: y, width: width, height: height, color: color }
end
alias :to_h :serialize
def inspect
serialize.to_s
end
def to_s
serialize.to_s
end
end
class SquareTileGame
attr_gtk
def generate_tiles
tiles = []
rows = 6
cols = 12
size = 100
rows.times do |row|
cols.times do |col|
x = col * size
y = row * size
tiles << Tile.gen(x: x, y: y)
end
end
tiles
end
def defaults
state.tiles ||= generate_tiles
end
def input
return unless inputs.click
tile_clicked = state.tiles.find do |tile|
inputs.click.inside_rect? tile.rect
end
return unless tile_clicked
if tile_clicked.color == Color::BLUE
tile_clicked.color = Color::RED
else
tile_clicked.color = Color::BLUE
end
end
def tick
defaults
input
render
end
def render
args.outputs.solids << state.tiles.map(&:to_a)
end
end
def main!
GAME = SquareTileGame.new
def tick args
GAME.args = args
GAME.tick
end
$gtk.reset
end
main!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment