Haskell 中的典型游戏框架是什么 [英] What would be a typical game skeleton in Haskell

查看:26
本文介绍了Haskell 中的典型游戏框架是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Haskell 游戏的典型游戏框架是什么?例如,让我们简单地射击它们?关于不变性问题,我对数据结构以及如何管理世界上所有元素的更新特别感兴趣.就这么说

What would be the typical game skeleton for a Haskell game, let's say a simple shoot them up for instance? I am particularly interested on the data structure, and how to manage the update of all the elements in the world, in regards of immutability issue. Just saying that

  new_world=update_world(world)

有点简单.例如,如何处理世界元素之间可能发生的多重交互(碰撞、对玩家的反应等......),尤其是当你有很多独立"元素受到影响时.

is a little bit simplistic. For instance, how to deal with multiple interaction that can occurs between element of the world (collisions, reaction to player, etc....) especially when you have a lot of 'independent' elements impacted.

主要关注的是世界的不变性,这使得基于世界的另一个子集更新世界的小"部分变得非常困难.

The main concern is about immutability of the world, which makes very difficult to update a "small" part of the world based on another subset of the world.

推荐答案

I love gloss (gloss 2D 库.它非常符合您的需求(我认为)

I love gloss (gloss 2D library. it's very closed to your needs (I think)

一个非常简单的例子

import Graphics.Gloss
import Graphics.Gloss.Interface.Pure.Game

-- Your app data
data App = App { mouseX :: Float
               , mouseY :: Float
               }

-- Draw world using your app data
      -- Here thing about *draw* your world (e.g. if radius > MAX then red else blue)
drawWorld (App mousex mousey) = color white $ circle mousex

-- Handle input events and update your app data
      -- Here thing about user interaction (e.g. when press button start jump!)
handleEvent
    (EventMotion (x, y)) -- current viewport mouse coordinates
    (App _ _) = App x y
handleEvent e w = w


-- Without input events you can update your world by time
     -- Here thing about time (e.g. if jumping use `t` to compute new position)
handleTime t w = w

runApp =
    play
        ( InWindow "Test" (300, 300) (0, 0) ) -- full screen or in window
        black                                 -- background color
        20                                    -- frames per second
        ( App 0 0 )                           -- your initial world
        drawWorld                             -- how to draw the world?
        handleEvent                           -- how app data is updated when IO events?
        handleTime                            -- how app data is updated along the time?

-- enjoy!
main = runApp

一个简单的例子,沿三个事件处理程序(绘制、输入和时间)修改一些数据结构(圆半径列表)

One simple example modifying some data structure (a list of circle radius) along the three event handlers (draw, input and time)

import Graphics.Gloss
import Graphics.Gloss.Interface.Pure.Game
import System.IO.Unsafe

data App = App { mouseX :: Float
               , mouseY :: Float
               , circleList :: [Float]
               , lastTime :: Float
               , currTime :: Float
               }

drawWorld app =
    color white $ pictures $ map circle $ circleList app

handleEvent
    (EventMotion (x, y)) -- current viewport mouse coordinates
    app = app { mouseX = x, mouseY = y,
                -- drop circles with radius > mouse **MOVED** position
                circleList = filter (<(abs x)) $ circleList app }
handleEvent e app = app

handleTime t app =
    app { currTime = currTime', lastTime = lastTime', circleList = circleList' }
    where currTime' = currTime app + t
          -- create new circle each 1 second
          createNew = currTime' - lastTime app > 1
          lastTime' = if createNew then currTime' else lastTime app
          -- each step, increase all circle radius
          circleList' = (if createNew then [0] else []) ++ map (+0.5) (circleList app)

runApp =
    play
        ( InWindow "Test" (300, 300) (0, 0) )
        black
        20
        ( App 0 0 [] 0 0 )
        drawWorld
        handleEvent
        handleTime

main = runApp

结果

这篇关于Haskell 中的典型游戏框架是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆