F#游戏开发:修改状态变量

时间:2014-02-27 15:13:33

标签: c# .net f#

open SFML.Graphics
open SFML.Window

let window = new RenderWindow(new VideoMode(200u, 200u), "SFML works!")
let shape = new CircleShape(10.0f, FillColor=Color.Green)
let mutable pressedKey = Keyboard.Key.Unknown

let moveKeys = [ Keyboard.Key.Up; Keyboard.Key.Left;
                 Keyboard.Key.Down; Keyboard.Key.Right ]

let keyPress (e : KeyEventArgs) =
    match e.Code with
    | moveKeys -> pressedKey <- e.Code
    | _ -> pressedKey <- Keyboard.Key.Unknown

let keyRelease (e : KeyEventArgs) =
    let pressedKeys = List.filter (fun key -> Keyboard.IsKeyPressed(key)) moveKeys
    if pressedKeys.IsEmpty then pressedKey <- Keyboard.Key.Unknown
    else pressedKey <- pressedKeys.Head

window.Closed.Add(fun evArgs -> window.Close())
window.KeyPressed.Add(keyPress)
window.KeyReleased.Add(keyRelease)

while window.IsOpen() do
    match pressedKey with
    | Keyboard.Key.Up    -> shape.Position <- new Vector2f(shape.Position.X, shape.Position.Y - 0.1f)
    | Keyboard.Key.Left  -> shape.Position <- new Vector2f(shape.Position.X - 0.1f, shape.Position.Y)
    | Keyboard.Key.Down  -> shape.Position <- new Vector2f(shape.Position.X, shape.Position.Y + 0.1f)
    | Keyboard.Key.Right -> shape.Position <- new Vector2f(shape.Position.X + 0.1f, shape.Position.Y)
    | _ -> ()

    window.DispatchEvents()
    window.Clear()
    window.Draw(shape)
    window.Display()

在上面的代码示例中,我创建了一个圆圈,然后按箭头键移动它。有问题的状态变量是圆的位置,由Vector2f对象(SFML库的一部分)表示

我的问题与代码段的结尾有关,在那里我找到了按下的键,然后移动了圆圈。从C#背景开始,这部分代码看起来很糟糕。

在C#中我会简单地执行以下操作:

switch (pressedKey) {
    case Keyboard.Key.Up:
         shape.Position.Y -= 0.1f;
    // etc, etc
}
  1. 创建这些新的Vector2f对象是否会导致不必要的 与我修改状态变量的方式相比 C#?

  2. 有更好的方法吗?

2 个答案:

答案 0 :(得分:4)

1)这是一个始终具体案例的表现问题。在这种情况下答案是否定的。如果您在循环中对10,000个对象执行此操作,则使用可变数据。一般来说,使事物不可变,可以让你对他们的行为做出更容易的假设,这是非常重要的。

2)以下是您可以使用此代码的方向。

open SFML.Graphics
open SFML.Window
open System

type HandleKeyboard(window : RenderWindow) =
    let mutable keyState = Set.empty

    let keyPressedHandle = 
        window.KeyPressed.Subscribe(fun key -> 
            keyState <- keyState.Add key.Code)

    let keyReleasedHandle = 
        window.KeyReleased.Subscribe(fun key -> 
            keyState <- keyState.Remove key.Code)

    let validMovementKey (keyPress : Keyboard.Key) =
        match keyPress with
        | Keyboard.Key.Up
        | Keyboard.Key.Left
        | Keyboard.Key.Down
        | Keyboard.Key.Right -> true
        | _ -> false

    let keyToMovement (keyPress : Keyboard.Key) =
        match keyPress with
        | Keyboard.Key.Up    -> Vector2f( 0.0f, -0.1f)
        | Keyboard.Key.Left  -> Vector2f(-0.1f,  0.0f)
        | Keyboard.Key.Down  -> Vector2f( 0.0f,  0.1f)
        | Keyboard.Key.Right -> Vector2f( 0.1f,  0.0f)
        | _ -> Vector2f(0.0f, 0.0f)

    member this.IsKeyPressed (key : Keyboard.Key) = 
        keyState |> Set.contains key

    member this.GetMovement () =
        keyState
        |> Set.filter validMovementKey
        |> Seq.map keyToMovement
        |> Seq.fold (+) (Vector2f(0.0f, 0.0f))

    interface IDisposable with
        member this.Dispose() =
            keyPressedHandle.Dispose()
            keyReleasedHandle.Dispose()

type SomeState = { 
    position : Vector2f;
    }

let startGame() =
    use window = new RenderWindow(VideoMode(200u, 200u), "SFML works!")
    use shape = new CircleShape(10.0f, FillColor = Color.Green)
    use keyboard = new HandleKeyboard(window)

    window.Closed.Add(fun evArgs -> window.Close())

    let rec mainLoop state =
        window.DispatchEvents()

        if keyboard.IsKeyPressed Keyboard.Key.Escape then
            window.Close()

        let newPosition = state.position + keyboard.GetMovement()
        shape.Position <- newPosition

        if window.IsOpen() then
            window.Clear()
            window.Draw(shape)
            window.Display()

            mainLoop {position = newPosition}

    mainLoop {position = Vector2f(0.0f, 0.0f)}

startGame()

答案 1 :(得分:2)

我认为如果你担心创建一个与按键相对应的小对象的开销,你就会担心错误的事情。按键的数量相对较少。

通过更改vector2类型,您可以编写类似于C#代码的代码(实质上,您可以使后备存储变量可变)。