Realm of Racket: Learn to Program, One Game at a Time!
Matthias Felleisen, Eric Peterson, Conrad Barski, David Van Horn, Forrest Bice, Rose DeMaio, Spencer Florence, Mimi Lin, Scott L
Format: PDF / Kindle (mobi) / ePub
Racket is a descendant of Lisp, a programming language renowned for its elegance, power, and challenging learning curve. But while Racket retains the functional goodness of Lisp, it was designed with beginning programmers in mind. Realm of Racket is your introduction to the Racket language.
In Realm of Racket, you'll learn to program by creating increasingly complex games. Your journey begins with the Guess My Number game and coverage of some basic Racket etiquette. Next you'll dig into syntax and semantics, lists, structures, and conditionals, and learn to work with recursion and the GUI as you build the Robot Snake game. After that it's on to lambda and mutant structs (and an Orc Battle), and fancy loops and the Dice of Doom. Finally, you'll explore laziness, AI, distributed games, and the Hungry Henry game.
As you progress through the games, chapter checkpoints and challenges help reinforce what you've learned. Offbeat comics keep things fun along the way.
As you travel through the Racket realm, you'll:
• Master the quirks of Racket's syntax and semantics;
• Learn to write concise and elegant functional programs;
• Create a graphical user interface using the 2htdp/image library;
• Create a server to handle true multiplayer games.
Realm of Racket is a lighthearted guide to some serious programming. Read it to see why Racketeers have so much fun!
Such local definitions are useful if you don’t want to compute a value multiple times or if you don’t want to nest expressions too deeply. These definitions exist only within the pair of parentheses in which they are defined and cannot be accessed anywhere else. We call this the scope of the definition. For instance, consider this program fragment: (struct posn (x y)) (struct rectangle (width height)) (define (inside-of-rectangle? r p) (define x (posn-x p)) (define y (posn-y p)) (define width
expire field represents the number of clock ticks left before this goo expires. Now we have introduced all of the structs needed to represent a world, and we can experiment with some simple worlds. Here is one: > (define snake-example (snake "up" (list (posn 1 1) (posn 1 2) (posn 1 3)))) > (define goo-example (list (goo (posn 1 0) 3) (goo (posn 5 8) 15))) > (define pit-example (pit snake-example goo-example)) > pit-example (pit (snake "up" (list (posn 1 1) (posn 1 2) (posn 1 3))) (list (goo
that consumes or produces another function is called a higher-order function, and it can compute all kinds of nifty things. Here is a simple example called my-map: (define (my-map func lst) (cond [(empty? lst) empty] [else (cons (func (first lst)) (my-map func (rest lst)))])) This function takes two arguments: a function of one argument, called func, and a list. It applies func to each element of the list and then collects the results in a list. Let’s see how we can apply this function: >
a call to render-id+image. It then adds a progress bar on top of that image with a call to add-progress-bar. The render-id+image function takes the app structure and generates an image from the latest message the client received: client.rkt (define (render-id+image app) (define id (app-id app)) (define base-image (app-img app)) (overlay (cond [(boolean? id) base-image] [else (define s (string-append LOADING-OPEN-TEXT id)) (above base-image (text s TEXT-SIZE TEXT-COLOR))]) BASE)) This
are the only ones that the server deals with: server.rkt (define (goto p iw msg) (define c (make-rectangular (second msg) (third msg))) (set-play-players! p (add-waypoint (play-players p) c iw)) (broadcast-universe p)) First, goto creates a complex number representing the new waypoint. Second, it modifies the current list of players with that waypoint added to the client who sent the message. Finally, it broadcasts the new state of the universe. This broadcast allows the client to see