gpt4 book ai didi

Canvas %在帧%没有帧%大小

转载 作者:行者123 更新时间:2023-12-04 21:34:35 25 4
gpt4 key购买 nike

我在 Racket 中创建了一个小蛇游戏,并尝试显示边界。
我定义了一些常量:

(define WIDTH 500)

(define HEIGHT 500)

(define BLK_SIZE 10)
所以,所有块都是一个 50px 的正方形。
我创建了一个框架%
(define frame 
(new (class frame%
(define/augment (on-close)
(send timer stop)
(printf "ending...\n"))
(super-new))
[label "Snake"]
[width WIDTH]
[height HEIGHT]))
和 Canvas %
(define game-canvas%
(class canvas%
(define/override (on-char e)
(case (send e get-key-code)
[(left) (unless (eq? last-dir 'right)
(set! dir 'left))]
[(right) (unless (eq? last-dir 'left)
(set! dir 'right))]
[(up) (unless (eq? last-dir 'down)
(set! dir 'up))]
[(down) (unless (eq? last-dir 'up)
(set! dir 'down))]
[else (void)]))

(super-new)))
;...
(define game-canvas (new game-canvas% (parent frame)))
我定义了边界:
(define (create-border)
(let (
[nbr-block-x (/ WIDTH BLK_SIZE)]
[nbr-block-y (/ HEIGHT BLK_SIZE)])
(append
(for/list ([x (in-range nbr-block-x)])
(make-cell x 0))
(for/list ([x (in-range nbr-block-x)])
(make-cell x (sub1 nbr-block-x)))
(for/list ([y (in-range nbr-block-y)])
(make-cell 0 y))
(for/list ([y (in-range nbr-block-y)])
(make-cell (sub1 nbr-block-y) y)))))

(define borders (create-border))
最后我画了边界:
(define-struct cell (x y))

(define (real-world-coordinate elem)
(make-cell
(* BLK_SIZE (cell-x elem))
(* BLK_SIZE (cell-y elem))))

(define (draw-list-of-square! dc square-list)
(for-each (lambda (elem)
(send dc draw-rectangle
(cell-x (real-world-coordinate elem))
(cell-y (real-world-coordinate elem))
BLK_SIZE
BLK_SIZE))
square-list))
使用该代码,我只能在窗口中看到边框。
这是代码的结果(不调整框架大小):
render
如您所见,右边框和下边框在我的框架之外。
因为我从 0 到 (sub1 nbr-block-x) 绘制块(49)(像素 490)边框应该出现在我的框架中。
我已经检查了 frame% 和 canvas% 的文档。 Canvas% 定义设置为 0 的边距,frame% 定义 borderspacing也设置为
0.

最佳答案

现在可能为时已晚,但它可能对其他人仍然有用。

您应该让框架自动调整到 Canvas 而不是相反:

#lang racket/gui

(define WIDTH 500)
(define HEIGHT 500)

(define frame (new frame% [label "Snake"])) ; no size is specified

(define game-canvas
(new canvas% [parent frame]
[min-width WIDTH]
[min-height HEIGHT]
[stretchable-width #f] ; To make sure the canvas' sizes stay constant even if the frame is resized with the mouse
[stretchable-height #f]
[paint-callback
(λ(cv dc)
(send dc set-background "yellow")
(send dc clear)
)]))

; Before being shown, the sizes are automatically adjusted.
(send frame show #t)

如果需要,也可以强制重新计算大小
(send frame reflow-container)

如果在显示框架后有任何尺寸变化。

关于 Canvas %在帧%没有帧%大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41968862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com