Last active
April 18, 2019 17:43
-
-
Save codebykeoma/50f2e22b7103cb2b956bd10274ae8ce2 to your computer and use it in GitHub Desktop.
This code shows the interaction between a character (sprite) and a sword (image). I created this for a student of mine to reference.
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
system('clear') | |
require 'ruby2d' | |
set title: 'Sprite animation' | |
set background: 'blue' | |
Zero = 0 | |
# hero parameters | |
@hero_width = 78 | |
@hero_height = 99 | |
@hero_time = 150 | |
@hero_direction = '' | |
@move_by = 4 | |
@sprint_speed = 2 | |
# sword parameters | |
@sword_height = 64 | |
@sword_width = 64 | |
# @sword_x = 150 | |
# @sword_y = 390 | |
@sword_x = Window.width/2 - @sword_width/2 | |
@sword_y = Window.height/2 - @sword_height/2 | |
# load assests | |
# images | |
@sword = Image.new( | |
'./assets/sword.png', | |
height: @sword_height, | |
width: @sword_width, | |
rotate: 180, | |
x: @sword_x, | |
y: @sword_y | |
) | |
# sprites | |
@hero = Sprite.new( | |
'./assets/hero.png', | |
width: @hero_width, | |
height: @hero_height, | |
clip_width: @hero_width, | |
clip_height: @hero_height, | |
x: Zero, | |
y: Window.height - @hero_height, | |
time: @hero_time, | |
animations: { | |
walk: 1..2, | |
climb: 3..4, | |
cheer: 5..6 | |
} | |
) | |
# methods | |
# this method ensures that objects stay within the game window | |
# e.g. call set_boundaries(@hero) to keep @hero within the limits | |
def set_boundaries(obj) | |
# x-axis boundaries | |
if obj.x >= Window.width - obj.width | |
obj.x = Window.width - obj.width | |
end | |
if obj.x <= Zero | |
obj.x = Zero | |
end | |
#y-axis boundaries | |
if obj.y >= Window.height - obj.height | |
obj.y = Window.height - obj.height | |
end | |
if obj.y <= Zero | |
obj.y = Zero | |
end | |
end | |
# this method determines if two objects have collided | |
def is_collided(obj_one, obj_two) | |
# if first_object.x <= second_object.x + second_object.width && first_object.x + first_object.width >= second_object.x && first_object.y <= second_object.y + second_object.height && first_object.y + first_object.height >= second_object.y | |
if obj_one.x <= obj_two.x + obj_two.width && obj_one.x + obj_one.width >= obj_two.x && obj_one.y <= obj_two.y + obj_two.height && obj_one.y + obj_one.height >= obj_two.y | |
puts 'object one is colliding with object two :)' | |
else | |
puts 'object one is not colliding with object two :)' | |
end | |
end | |
# handle user input | |
on :key_held do |event| | |
case event.key | |
when 'left' | |
# animation | |
@hero.play animation: :walk, loop: false, flip: :horizontal | |
# movement | |
@hero.x = @hero.x - @move_by | |
@hero_direction = 'left' | |
when 'right' | |
# animation | |
@hero.play animation: :walk, loop: false | |
# movement | |
@hero.x = @hero.x + @move_by | |
@hero_direction = 'right' | |
when 'up' | |
# animation | |
@hero.play animation: :climb, loop: false | |
# movement | |
@hero.y = @hero.y - @move_by | |
@hero_direction = 'up' | |
when 'down' | |
# animation | |
@hero.play animation: :climb, loop: false | |
# movement | |
@hero.y = @hero.y + @move_by | |
@hero_direction = 'down' | |
when 'c' | |
@hero.play animation: :cheer, loop: false | |
when 's' | |
# update so character stops moving when hero_direction key is released | |
if @hero_direction == 'left' | |
@hero.x = @hero.x - @move_by - @sprint_speed | |
end | |
if @hero_direction == 'right' | |
@hero.x = @hero.x + @move_by + @sprint_speed | |
end | |
if @hero_direction == 'up' | |
@hero.y = @hero.y - @move_by - @sprint_speed | |
end | |
if @hero_direction == 'down' | |
@hero.y = @hero.y + @move_by + @sprint_speed | |
end | |
end | |
# pass hero to the method responsible for keeping objects within the game screen boundaries | |
set_boundaries(@hero) | |
is_collided(@hero, @sword) | |
end | |
on :key_up do |event| | |
case event.key | |
when 'left' | |
@hero_direction = '' | |
when 'right' | |
@hero_direction = '' | |
when 'up' | |
@hero_direction = '' | |
when 'down' | |
@hero_direction = '' | |
end | |
end | |
show |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment