0

我希望我的形状不要超出窗口区域。例如,当我按下左键时,形状会移出窗口区域,但我希望它撞到一边并留在那里。

 def update

    if button_down?(Gosu::KbRight)
      if @shape_x != (WIDTH - SHAPE_DIM)
        @shape_x += 3
      end
    end
    if button_down?(Gosu::KbLeft) 
      if @shape_x != (WIDTH - SHAPE_DIM)
        @shape_x -= 3
      end
    end
    if button_down?(Gosu::KbUp) 
      @shape_y -= 3
  end
  if button_down?(Gosu::KbDown) 
    @shape_y += 3
end

  end
4

1 回答 1

1

假设 WIDTH 是窗口宽度,您必须使用<and >,而不是!=

  def update

    if button_down?(Gosu::KbRight)
      if @shape_x < (WIDTH - SHAPE_DIM)
        @shape_x += 3
      end
    end
    if button_down?(Gosu::KbLeft) 
      if @shape_x > (WIDTH - SHAPE_DIM)
        @shape_x -= 3
      end
    end
    if button_down?(Gosu::KbUp) 
      @shape_y -= 3
    end
    if button_down?(Gosu::KbDown) 
      @shape_y += 3
    end

  end
于 2019-10-01T14:49:27.347 回答