Skip to content

Instantly share code, notes, and snippets.

@ledsun
Forked from Shinpeim/player_01.rb
Last active December 12, 2015 08:59
Show Gist options
  • Save ledsun/4748362 to your computer and use it in GitHub Desktop.
Save ledsun/4748362 to your computer and use it in GitHub Desktop.
# -*- 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]
@ledsun
Copy link
Author

ledsun commented Feb 10, 2013

速度を状態に持つバージョン

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