haskell - 在 Haskell 中绘制游戏板 - 问题
问题描述
我正在尝试为 Peg Solitaire 游戏构建棋盘,但我被卡住了。我希望你能帮助我。以下代码运行并在 3 个圆上生成一个 3 的正方形。我怎么能再制作 3 个这样的方格,但把它们放在其他位置?我使用光泽库
module Main(main) where
import Graphics.Gloss
import Graphics.Gloss.Data.ViewPort
import Graphics.Gloss.Interface.Pure.Game
import Data.List
width, height, offset :: Int
width = 400
height = 400
offset = 100
window :: Display
window = InWindow "Peg Solitaire" (width, height) (offset, offset)
background :: Color
background = white
drawing :: Picture
drawing = Pictures [ (translate (x * 40) (y * 40) $ circleSolid 12)| x<-[-1..1], y<-[2..4] ]
main = display window background drawing
解决方案
我不熟悉您的图形库,但显然您可以使用列表推导并将其传递给Pictures
构造函数。
因此,只需编写适当的列表理解表达式即可。
您给出的表达式可以重写为:
drawing1 = let circles1 = [ (translate (x1 * 40) (y1 * 40) $ circleSolid 12) |
x1 <- [-1..1], y1 <- [2..4] ] in Pictures circles1
如果您想将 3+1=4 圆组排列成规则网格,您可以引入额外的循环级别,例如使用变量 x0 和 y0,如下所示:
drawing2 = let circles2 = [ (translate (x0*200 + x1*40) (y0*200 + y1*40)
$ circleSolid 12) |
x0 <- [0,1] , y0 <- [0,1],
x1 <- [-1..1], y1 <- [2..4] ]
in Pictures circles2
如果您喜欢以任意方式排列圆组,您可以引入一个额外的循环变量,例如cg
遍历圆组的主坐标:
circleGroups = [ (0,0), (0,200), (200,0), (200,200) ] -- list of (x,y) pairs
drawing3 = let circles3 = [
translate ((fst cg)*200 + x1*40) ((snd cg)*200 + y1*40)
$ circleSolid 12 |
cg <- circleGroups,
x1 <- [-1..1], y1 <- [2..4] ]
in Pictures circles3
注意:请将您的源代码限制为每行大约 80 个字符,因此我们不必使用水平滑块。这确实是代码可读性的巨大障碍。谢谢。
推荐阅读
- image - 如何显示来自 url 的图像以与 array.map 反应本机?
- reactjs - 更改我单击的按钮的文本
- java - 如何从列表转换
列出 在爪哇 - html - ngModel 默认值设置不接收来自模型的信息
- angular - 如何在 Angular8 库中包含图标等资产?
- cassandra - 在 spring 数据中连接到 2 个 cassandra 集群
- odoo - 路径 /shop/get_suggest 是什么意思?
- python - fstrings 如何/在哪里实现?
- sql - 在触发器中使用更新
- dynamics-crm - 如何取消合并从 D365 OOB 功能合并的记录?