Skip to content

Instantly share code, notes, and snippets.

@codebykeoma
Last active April 18, 2019 17:43
Show Gist options
  • Save codebykeoma/50f2e22b7103cb2b956bd10274ae8ce2 to your computer and use it in GitHub Desktop.
Save codebykeoma/50f2e22b7103cb2b956bd10274ae8ce2 to your computer and use it in GitHub Desktop.

Revisions

  1. code by keoma renamed this gist Apr 18, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. code by keoma renamed this gist Apr 18, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. code by keoma renamed this gist Apr 18, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. codebykeoma revised this gist Apr 15, 2019. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions cxk_hero.rb
    Original file line number Diff line number Diff line change
    @@ -58,23 +58,23 @@

    # 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(object)
    def set_boundaries(obj)
    # x-axis boundaries
    if object.x >= Window.width - object.width
    object.x = Window.width - object.width
    if obj.x >= Window.width - obj.width
    obj.x = Window.width - obj.width
    end

    if object.x <= Zero
    object.x = Zero
    if obj.x <= Zero
    obj.x = Zero
    end

    #y-axis boundaries
    if object.y >= Window.height - object.height
    object.y = Window.height - object.height
    if obj.y >= Window.height - obj.height
    obj.y = Window.height - obj.height
    end

    if object.y <= Zero
    object.y = Zero
    if obj.y <= Zero
    obj.y = Zero
    end
    end

  5. codebykeoma revised this gist Apr 14, 2019. No changes.
  6. codebykeoma revised this gist Apr 14, 2019. No changes.
  7. codebykeoma revised this gist Apr 14, 2019. 1 changed file with 19 additions and 3 deletions.
    22 changes: 19 additions & 3 deletions cxk_hero.rb
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,11 @@
    # 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

    @@ -28,8 +33,8 @@
    height: @sword_height,
    width: @sword_width,
    rotate: 180,
    x: 150,
    y: 390
    x: @sword_x,
    y: @sword_y
    )

    # sprites
    @@ -73,6 +78,16 @@ def set_boundaries(object)
    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
    @@ -128,6 +143,7 @@ def set_boundaries(object)

    # 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|
    @@ -143,4 +159,4 @@ def set_boundaries(object)
    end
    end

    show
    show
  8. codebykeoma revised this gist Apr 14, 2019. 1 changed file with 6 additions and 2 deletions.
    8 changes: 6 additions & 2 deletions cxk_hero.rb
    Original file line number Diff line number Diff line change
    @@ -49,7 +49,10 @@
    }
    )

    # define methods
    # 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(object)
    # x-axis boundaries
    if object.x >= Window.width - object.width
    @@ -123,6 +126,7 @@ def set_boundaries(object)
    end
    end

    # pass hero to the method responsible for keeping objects within the game screen boundaries
    set_boundaries(@hero)
    end

    @@ -139,4 +143,4 @@ def set_boundaries(object)
    end
    end

    show
    show
  9. codebykeoma renamed this gist Apr 14, 2019. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  10. codebykeoma created this gist Apr 14, 2019.
    142 changes: 142 additions & 0 deletions keoma_hero.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,142 @@
    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

    # load assests

    # images
    @sword = Image.new(
    './assets/sword.png',
    height: @sword_height,
    width: @sword_width,
    rotate: 180,
    x: 150,
    y: 390
    )

    # 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
    }
    )

    # define methods
    def set_boundaries(object)
    # x-axis boundaries
    if object.x >= Window.width - object.width
    object.x = Window.width - object.width
    end

    if object.x <= Zero
    object.x = Zero
    end

    #y-axis boundaries
    if object.y >= Window.height - object.height
    object.y = Window.height - object.height
    end

    if object.y <= Zero
    object.y = Zero
    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

    set_boundaries(@hero)
    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