-
-
Save ledsun/4748362 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_writer :speed | |
def initialize(param) | |
@position_x = param[:x] | |
@position_y = param[:y] | |
@speed = param[:speed] | |
end | |
def move(direction) | |
case direction | |
when :up | |
@position_y -= @speed | |
when :down | |
@position_y += @speed | |
when :left | |
@position_x -= @speed | |
when :right | |
@position_x += @speed | |
end | |
end | |
def position | |
return @position_x, @position_y | |
end | |
end | |
player = Player.new(:x => 100, :y => 100, :speed => 10) | |
player.move(:up) | |
p player.position # => [100, 90] | |
player.speed = 20 | |
player.move(:down) | |
p player.position #=> [100, 110] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
速度を状態に持つバージョン