I've create a little snake game in racket and I try to show the borders.
I defined some constantes :
(define WIDTH 500)
(define HEIGHT 500)
(define BLK_SIZE 10)
so, all block is a 50px square.
I've created a frame%
(define frame
(new (class frame%
(define/augment (on-close)
(send timer stop)
(printf "ending...\n"))
(super-new))
[label "Snake"]
[width WIDTH]
[height HEIGHT]))
and a 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)))
I defined the border :
(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))
And finally I draw the borders :
(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))
With that code, I except to see borders in my window. That is the result of the code (without resizing the frame) :
As you can see, the right and down border are outside my frame.
Since I draw block from 0 to (sub1 nbr-block-x)
(49) (in pixel 490) the borders should appear in my frame.
I've checked the doc of frame% and canvas%. Canvas% define margins which are set to 0 and frame% define border
and spacing
which are also set to
0.
That's probably too late by now, but it may still be useful to others.
You should let the frame adjust automatically to the canvas rather than the contrary:
#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)
If needed, recalculation of the size can also be forced with
(send frame reflow-container)
if any size changes after the frame has been shown.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With