Skip to content

Instantly share code, notes, and snippets.

@ledsun
Forked from Shinpeim/player_02.rb
Created February 12, 2013 12:02
Show Gist options
  • Save ledsun/4761880 to your computer and use it in GitHub Desktop.
Save ledsun/4761880 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Player
attr_reader :position
def initialize(position)
@position = position
@x_ratio = 1
@y_ratio = 1
end
def move(direction)
case direction
when :up
@position[:y] -= 10 * @y_ratio
when :down
@position[:y] += 10 * @y_ratio
when :left
@position[:x] -= 10 * @x_ratio
when :right
@position[:x] += 10 * @x_ratio
end
end
end
class FastPlayer < Player
def initialize(position)
super
@x_ratio = @y_ratio = 2
end
end
class KaniPlayer < Player
def initialize(position)
super
@x_ratio = 4
@y_ratio = 0.5
end
end
player = Player.new(:x => 100, :y => 100)
player.move(:up)
p player.position # => {:x => 100, :y => 90}
player = FastPlayer.new player.position
player.move(:down)
p player.position #=> {:x => 100, :y => 110}
player = KaniPlayer.new player.position
player.move(:up)
player.move(:left)
p player.position #=> {:x => 60, :y => 105}
@ledsun
Copy link
Author

ledsun commented Feb 12, 2013

移動倍率を持たせてみた

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment