Skip to content

Instantly share code, notes, and snippets.

@skinnyjames
Created April 8, 2026 22:13
Show Gist options
  • Select an option

  • Save skinnyjames/79655d57426351609a1337f06c9d0057 to your computer and use it in GitHub Desktop.

Select an option

Save skinnyjames/79655d57426351609a1337f06c9d0057 to your computer and use it in GitHub Desktop.
Just the code to slice a sprite sheet up and move the character. It needs the spritesheet to work, I'll publish a repo if you want to try it out.
class Char < Hokusai::Block
template do
child(Hokusai::Blocks::Empty) do
on(:keydown) do |event|
move(event)
end
end
end
# 68x68 blocks
def initialize(**args)
@x = 0.0
@y = 0.0
@sprite_index = 0
super
end
def sprite(index)
@rects ||= begin
arr = []
row = 0.0
col = 0.0
while row < 1024
while col < 1024
arr << Hokusai::Rect.new(col, row, 64.0, 64.0)
col += 64
end
col = 0.0
row += 64
end
arr
end
@rects[index]
end
def move(event)
case event.key
when :left
@x -= 5.0
when :up
@y -= 5.0
when :down
@y += 5.0
case @sprite_index
when 48
@sprite_index = 49
when 49
@sprite_index = 50
else
@sprite_index = 48
end
when :right
@x += 5.0
case @sprite_index
when 64
@sprite_index = 65
when 65
@sprite_index = 66
when 66
@sprite_index = 67
when 67
@sprite_index = 68
when 68
@sprite_index = 69
else
@sprite_index = 64
end
end
end
def render(canvas)
canvas.x = @x
canvas.y = @y
draw do
image(Hokusai.images.get("body"), @x, @y, 64.0, 64.0) do |command|
command.slice = sprite(@sprite_index)
end
end
yield canvas
end
end
class Map < Hokusai::Block
template do
child(Char) do
prop :width, 64.0
prop :height, 64.0
on(:click) do
puts "hello"
end
end
end
end
class Game < Hokusai::Block
template <<~EOF
[template]
map
EOF
uses(map: Map)
end
Hokusai::Backend.run(Game) do |config|
config.title = "Okay"
config.fps = 60
config.after_load do
Hokusai.images.register "body", Hokusai::Image.from_file("assets/body.png")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment