Created
July 15, 2018 16:06
-
-
Save jwoertink/77795fbc89a76dc2bd0ffac797deb738 to your computer and use it in GitHub Desktop.
Trying to get character movement similar to FinalFantasy V or VI. The character should move smoothly but always stop moving directly over a tile. Tiles are 64x64.
This file contains hidden or 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
def initialize | |
@tile_x = 9 | |
@tile_y = 7 | |
@fine_x = 0 | |
@fine_y = 0 | |
end | |
def draw(renderer) | |
renderer.copy(sprite, dstrect: SDL::Rect[ | |
@tile_x * 64 + @fine_x, | |
@tile_y * 64 + @fine_y, | |
64, | |
64]) | |
end | |
def move(x, y) | |
@fine_x += x | |
@fine_y += y | |
if @fine_x <= -64 | |
@tile_x -= 1 | |
@fine_x = 0 | |
end | |
if @fine_y <= -64 | |
@tile_y -= 1 | |
@fine_y = 0 | |
end | |
if @fine_x >= 64 | |
@tile_x += 1 | |
@fine_x = 0 | |
end | |
if @fine_y >= 64 | |
@tile_y += 1 | |
@fine_y = 0 | |
end | |
return @fine_x.zero? && @fine_y.zero? | |
end |
This file contains hidden or 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
character = Character.new | |
loop do | |
case event = SDL::Event.poll | |
when SDL::Event::Quit | |
break | |
when SDL::Event::Keyboard | |
case event.sym | |
when .escape? | |
@game_running = false | |
when .right? | |
character.move(1, 0) | |
end if event.keydown? | |
end | |
renderer.draw_color = SDL::Color[0, 0, 0, 255] | |
renderer.clear | |
character.draw(renderer) | |
renderer.present | |
break unless @game_running | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This current setup omits all of the SDL setup code. Running this code makes the character move 1px at a time but only while holding the arrow key down. I'm leaving off the other directions for now because if I can get moving to the right, then I can duplicate that for the other directions.