<RGSS>
class Stg_player < Sprite #-------------------------------------------------------------------------- # ● 公開インスタンス変数 #-------------------------------------------------------------------------- attr_reader :dx #横方向の当たり判定 attr_reader :dy #縦方向の当たり判定 #-------------------------------------------------------------------------- # ● オブジェクト初期化 ■更新■ #-------------------------------------------------------------------------- def initialize super() #自機表示 self.bitmap = RPG::Cache.picture("player") #原点を画像の中心に設定 self.ox = self.bitmap.width / 2 self.oy = self.bitmap.height / 2 @dx = 4 @dy = 4 @px = self.x @py = self.y #小数点座標の確保 end #-------------------------------------------------------------------------- # ● 自機の移動処理 ■更新■ #-------------------------------------------------------------------------- def move move_x = 0 #横方向の移動量 move_y = 0 #縦方向の移動量 #入力されたキーによって画像の移動量を決める move_y -= 6 if Input.press?(Input::UP) move_y += 6 if Input.press?(Input::DOWN) move_x += 6 if Input.press?(Input::RIGHT) move_x -= 6 if Input.press?(Input::LEFT) #斜め入力のときは速度を落とす if move_x * move_y != 0 move_x *= 0.7 move_y *= 0.7 end #Bボタンを押した場合は、低速にする。 if Input.press?(Input::B) move_x *= 0.5 move_y *= 0.5 end #移動量を座標に加算する ■更新■ @px += move_x @py += move_y self.x = @px self.y = @py #画面外に出たら座標を画面内に強制する $x_lim = 640 + 3 - self.bitmap.width / 2 #右 self.x = $x_lim if self.x > $x_lim #右のめり込みを3ドットに $x_min = self.bitmap.width / 2 - 3 #左 self.x = $x_min if self.x < $x_min #左のめり込みを3ドットに $y_lim = 480 + 3 - self.bitmap.height / 2 #下 self.y = $y_lim if self.y > $y_lim #下のめり込みを3ドットに $y_min = self.bitmap.height / 2 - 3 #上 self.y = $y_min if self.y < $y_min #上のめり込みを3ドットに end #-------------------------------------------------------------------------- # ● 自機のグラフィック変更 #-------------------------------------------------------------------------- def yukkuri self.bitmap = RPG::Cache.picture("player_slow") end #-------------------------------------------------------------------------- # ● 自機のグラフィックを戻す #-------------------------------------------------------------------------- def normal self.bitmap = RPG::Cache.picture("player") end end
<RGSS2>