-
-
Save ledsun/4761880 to your computer and use it in GitHub Desktop.
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
# -*- 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} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
移動倍率を持たせてみた