球拍:使用大爆炸和点击
Racket: using big bang and on-click
我不知何故能够在球拍(房子)中画出一个小的基本图像。现在,我坚持的是,使用 big-bang 来绘制图像——然后能够单击房屋的屋顶并将其更改为另一种颜色。这是我目前所拥有的:
#lang racket
(require 2htdp/image)
(require 2htdp/universe)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" "red")
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view
我已经尝试了一百万种不同的方法来让它与 big-bang
一起工作,但没有任何效果。对 big-bang
有什么帮助吗?点击它会改变屋顶的颜色吗?
big-bang
表单通过在整个处理程序中传递 "state" 来工作,然后将其传递给 to-draw
处理程序以绘制将刷新到屏幕的图片。
对于初学者,您应该将绘图代码提升到 big-bang
的 to-draw
子句中:
(big-bang
null
(to-draw
(λ (state)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" "red")
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view)))
这将显示您的房子作为每次抽奖调用的结果。您还需要一些方法来管理您的世界状态。执行此操作的常规方法是使用结构。
(struct world (roof-color))
您提供给 big-bang
的第一个参数是初始状态,因此我们应该初始化状态以使用 "red"
作为屋顶颜色。然后我们可以在 to-draw
处理程序中使用当前状态的颜色:
(big-bang
(world "red")
(to-draw
(λ (state)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" (world-roof-color state))
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view)))
最后,您可以实现一个 on-mouse
子句来处理鼠标点击。这是一个简单的实现,可以在用户单击时使屋顶变绿。
(big-bang
(world "red")
(on-mouse
(λ (state x y event)
(case event
[("button-up")
(world "green")]
[else state])))
(to-draw
(λ (state)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" (world-roof-color state))
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view)))
有关详细信息,see the relevant documentation in the HtDP 2e section of the Racket documentation。您应该能够弄清楚如何根据您的需要进行调整。
我不知何故能够在球拍(房子)中画出一个小的基本图像。现在,我坚持的是,使用 big-bang 来绘制图像——然后能够单击房屋的屋顶并将其更改为另一种颜色。这是我目前所拥有的:
#lang racket
(require 2htdp/image)
(require 2htdp/universe)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" "red")
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view
我已经尝试了一百万种不同的方法来让它与 big-bang
一起工作,但没有任何效果。对 big-bang
有什么帮助吗?点击它会改变屋顶的颜色吗?
big-bang
表单通过在整个处理程序中传递 "state" 来工作,然后将其传递给 to-draw
处理程序以绘制将刷新到屏幕的图片。
对于初学者,您应该将绘图代码提升到 big-bang
的 to-draw
子句中:
(big-bang
null
(to-draw
(λ (state)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" "red")
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view)))
这将显示您的房子作为每次抽奖调用的结果。您还需要一些方法来管理您的世界状态。执行此操作的常规方法是使用结构。
(struct world (roof-color))
您提供给 big-bang
的第一个参数是初始状态,因此我们应该初始化状态以使用 "red"
作为屋顶颜色。然后我们可以在 to-draw
处理程序中使用当前状态的颜色:
(big-bang
(world "red")
(to-draw
(λ (state)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" (world-roof-color state))
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view)))
最后,您可以实现一个 on-mouse
子句来处理鼠标点击。这是一个简单的实现,可以在用户单击时使屋顶变绿。
(big-bang
(world "red")
(on-mouse
(λ (state x y event)
(case event
[("button-up")
(world "green")]
[else state])))
(to-draw
(λ (state)
(text "---Small Shack---" 18 "brown")
(define door (rectangle 30 50 "solid" "white"))
(define shack (above (triangle 100 "solid" (world-roof-color state))
(rectangle 120 80 "solid" "grey")))
(define doorhandle (overlay/align "right" "center" (circle 5 "solid" "blue") door))
(define house-view (overlay/align "center" "bottom" doorhandle shack))
house-view)))
有关详细信息,see the relevant documentation in the HtDP 2e section of the Racket documentation。您应该能够弄清楚如何根据您的需要进行调整。