Skip to content

Instantly share code, notes, and snippets.

@Shinpeim
Last active December 12, 2015 08:39
Show Gist options
  • Save Shinpeim/4745444 to your computer and use it in GitHub Desktop.
Save Shinpeim/4745444 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
class Player
attr_reader :position
# Cで言うenum的なやつ
module MOVING_MODE
NORMAL = 1
FAST = 2
KANI = 3 #蟹モードが増えた
end
def initialize(position)
@position = position
@moving_mode = MOVING_MODE::NORMAL
end
def to_fast_mode
@moving_mode = MOVING_MODE::FAST
end
def to_kani_mode
@moving_mode = MOVING_MODE::KANI
end
def move(direction)
case @moving_mode
when MOVING_MODE::NORMAL
x_speed = 10 # かにモードとの整合性のために
y_speed = 10 # 無駄な変更が加えられている
when MOVING_MODE::FAST
x_speed = 20 # FASTモードにも!
y_speed = 20
when MOVING_MODE::KANI
x_speed = 40
y_speed = 5
end
case direction
when :up
@position[:y] -= y_speed #ここも書き換えないといけない
when :down
@position[:y] += y_speed # ここも
when :left
@position[:x] -= x_speed # こk
when :right
@position[:x] += x_speed # k
end
end
end
player = Player.new(:x => 100, :y => 100)
player.move(:up)
p player.position # => {:x => 100, :y => 90}
player.to_fast_mode #ここで倍速モードに
player.move(:down)
p player.position # => {:x => 100, :y => 110}
player.to_kani_mode
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