1.18.7 Drawing the World

► |  | Games: Fun Examples

1 | Bundled Games
2 | Implementing New Games
3 | Showing Scribbled Help
4 | Showing Text Help

► | 1 | Bundled Games

1.1 | Aces — Solitaire Card Game
1.2 | Go Fish — Kid’s Card Game
1.3 | Crazy 8s — Card Game
1.4 | Blackjack — 21 Card Game
1.5 | Rummy — Card Game
1.6 | Spider — Solitaire Card Game
1.7 | Memory — Kid’s Game
1.8 | Slidey — Picture Puzzle
1.9 | Same — Dot-Removing Game
1.10 | Minesweeper — Logic Game
1.11 | Paint By Numbers — Logic Game
1.12 | Lights Out — Logic Game
1.13 | Pousse — Tic-Tac-Toe-like Game
1.14 | Gobblet — Strategy Game
1.15 | Jewel — 3-D Skill Game
1.16 | Parcheesi — Board Game
1.17 | Checkers — Board Game
1.18 | Chat Noir — Puzzle Game
1.19 | Tally Maze — Maze Enumeration Game
1.20 | GCalc — Visual λ-Calculus

▼ | 1.18 | Chat Noir — Puzzle Game

1.18.1 | Overview
1.18.2 | The World
1.18.3 | Breadth-first Search
1.18.4 | Board to Graph
1.18.5 | The Cat’s Path
1.18.6 | Drawing the Cat
1.18.7 | Drawing the World
1.18.8 | Handling Input
1.18.9 | Tests
1.18.10 | Run, program, run

On this page:

<drawing>
<drawing-tests>
<constants>
<constants>2
<render-world>
<render-board>
<render-cell>
<crop-whiskers>
<world-width>
<world-height>
<cell-center-x-tests>
<cell-center-x-tests>2
<cell-center-x>
<cell-center-y-tests>
<cell-center-y>

top contents ← prev up next →

##### 1.18.7`` Drawing the World🔗ℹ

<drawing> ::=

> <constants>
> <render-world>
> <render-board>
> <render-cell>
> <crop-whiskers>
> <world-width>
> <world-height>
> <cell-center-x>
> <cell-center-y>

<drawing-tests> ::=

> <cell-center-x-tests>
> <cell-center-y-tests>
> <world-size-tests>

There are a number of constants that are given names to make the code more readable.

These first two constants give the radius of the circles that are drawn on the board, plus the radius of an invisible circle that, if they were drawn on top of the circles, would touch each other. Accordingly, circle-spacing is used when computing the positions of the circles, but the circles are drawn using circle-radius.

<constants> ::=

> (define circle-radius 20)
> (define circle-spacing 22)

The other four constants specify the colors of the circles.

<constants> ::=

> (define normal-color 'lightskyblue)
> (define on-shortest-path-color 'white)
> (define blocked-color 'black)
> (define under-mouse-color 'black)

The main function for drawing a world is render-world. It is a fairly straightforward composition of helper functions. First, it builds the image of a board, and then puts the cat on it. Lastly, since the whiskers of the cat might now hang off of the edge of the board (if the cat is on a leftmost or rightmost cell), it trims them. This ensures that the image is always the same size and that the pinhole is always in the upper-left corner of the window.

<render-world> ::=

> (define/contract (render-world w)
> (-> world? image?)
> (define the-cat
> (cond
> [(equal? (world-state w) 'cat-won) happy-cat]
> [(equal? (world-state w) 'cat-lost) mad-cat]
> [else thinking-cat]))
> (crop-whiskers
> w
> (underlay/xy (render-board (world-board w)
> (world-size w)
> (on-cats-path? w)
> (world-mouse-posn w))
> (- (cell-center-x (world-cat w))
> (/ (image-width the-cat) 2))
> (- (cell-center-y (world-cat w))
> (/ (image-height the-cat) 2))
> the-cat)))

The render-board function uses for/fold to iterate over all of the cells in cs. It starts with an empty rectangle and, one by one, puts the cells on image.

<render-board> ::=

> (define/contract (render-board cs world-size on-cat-path? mouse)
> (-> (listof cell?)
> natural-number/c
> (-> posn? boolean?)
> (or/c #f posn?)
> image?)
> (for/fold ([image (rectangle (world-width world-size)
> (world-height world-size)
> 'solid
> 'white)])
> ([c (in-list cs)])
> (underlay/xy image
> (- (cell-center-x (cell-p c)) circle-radius)
> (- (cell-center-y (cell-p c)) circle-radius)
> (render-cell c
> (on-cat-path? (cell-p c))
> (and (posn? mouse)
> (equal? mouse (cell-p c)))))))

The render-cell function accepts a cell, a boolean indicating if the cell is on the shortest path between the cat and the boundary, and a second boolean indicating if the cell is underneath the mouse. It returns an image of the cell, with the pinhole placed in such a way that overlaying the image on an empty image with pinhole in the upper-left corner results in the cell being placed in the right place.

<render-cell> ::=

> (define/contract (render-cell c on-short-path? under-mouse?)
> (-> cell? boolean? boolean? image?)
> (define x (cell-center-x (cell-p c)))
> (define y (cell-center-y (cell-p c)))
> (define main-circle
> (cond
> [(cell-blocked? c)
> (circle circle-radius 'solid blocked-color)]
> [else
> (circle circle-radius 'solid normal-color)]))
> (cond
> [under-mouse?
> (underlay main-circle
> (circle (quotient circle-radius 2) 'solid under-mouse-color))]
> [on-short-path?
> (underlay main-circle
> (circle (quotient circle-radius 2) 'solid
> on-shortest-path-color))]
> [else
> main-circle]))

The chop-whiskers function ensures that when the cat is near the edge of the board (and its whiskers would hang off) that the image that the board produces still has the same size (by clipping away the cat’s whiskers).

<crop-whiskers> ::=

> (define/contract (crop-whiskers w img)
> (-> world? image? image?)
> (define cat-posn (world-cat w))
> (define cat-x (posn-x cat-posn))
> (define cat-y (posn-y cat-posn))
> (define left-edge? (and (even? cat-y) (= cat-x 0)))
> (define right-edge? (and (odd? cat-y) (= cat-x (- board-size 1))))
> (define width (world-width (world-size w)))
> (define height (world-height (world-size w)))
> (cond
> [left-edge?
> (crop (- (/ (image-width happy-cat) 2) circle-radius)
> 0
> width height img)]
> [right-edge? (crop 0 0 width height img)]
> [else img]))

The world-width function computes the width of the rendered world, given the world’s size by finding the center of the rightmost posn, and then adding an additional radius.

<world-width> ::=

> (define/contract (world-width board-size)
> (-> natural-number/c number?)
> (define rightmost-posn
> (make-posn (- board-size 1) (- board-size 2)))
> (+ (cell-center-x rightmost-posn) circle-radius))

Similarly, the world-height function computest the height of the rendered world, given the world’s size.

<world-height> ::=

> (define/contract (world-height board-size)
> (-> natural-number/c number?)
> (define bottommost-posn
> (make-posn (- board-size 1) (- board-size 1)))
> (ceiling (+ (cell-center-y bottommost-posn)
> circle-radius)))

The cell-center-x function returns the x coordinate of the center of the cell specified by p.

For example, the first cell in the third row (counting from 0) is flush with the edge of the screen, so its center is just the radius of the circle that is drawn.

<cell-center-x-tests> ::=

> (test (cell-center-x (make-posn 0 2))
> circle-radius)

The first cell in the second row, in contrast is offset from the third row by circle-spacing.

<cell-center-x-tests> ::=

> (test (cell-center-x (make-posn 0 1))
> (+ circle-spacing circle-radius))

The definition of cell-center-x multiplies the x coordinate of p by twice circle-spacing and then adds circle-radius to move over for the first circle. In addition if the y coordinate is odd, then it adds circle-spacing, shifting the entire line over.

<cell-center-x> ::=

> (define/contract (cell-center-x p)
> (-> posn? number?)
> (define x (posn-x p))
> (define y (posn-y p))
> (+ circle-radius
> (* x circle-spacing 2)
> (if (odd? y)
> circle-spacing
> 0)))

The cell-center-y function computes the y coordinate of a cell’s location on the screen. For example, the y coordinate of the first row is the radius of a circle, ensuring that the first row is flush against the top of the screen.

<cell-center-y-tests> ::=

> (test (cell-center-y (make-posn 1 0))
> circle-radius)

Because the grid is hexagonal, the y coordinates of the rows do not have the same spacing as the x coordinates. In particular, they are off by sin(pi/3). We approximate that by 433/500 in order to keep the computations and test cases simple and using exact numbers. A more precise approximation would be 0.8660254037844386, but it is not necessary at the screen resolution.

<cell-center-y> ::=

> (define/contract (cell-center-y p)
> (-> posn? number?)
> (+ circle-radius
> (* (posn-y p)
> circle-spacing 2
> 433/500)))

top contents ← prev up next →