CSci 1301: Lab 3

Friday, Sept 26th. Due: Wednesday, October 1 at 11:59pm by e-mail

What to submit

The lab is done in groups of 2. In the beginning of each file please write (in comments) the names of all group members.

At the end of the lab please send me and your group partner(s) all your Racket files as e-mail attachments. My e-mail is elenam at The subject of your e-mail must be "1301 Lab 3" followed by "Final" or "Not final", depending on whether this is a final submission or you are still working on it. If you need to finish it, make sure to set up a time with your group partner(s) to finish the lab.

The lab general description

The lab asks you to build upon examples of Racket "worlds". Racket "worlds" allow you to create programs similar to animations, but worlds are interactive, i.e. they can react to mouse movements, pressed keys, and the like. Functions for worlds are given in the universe.rkt teachpack.

Examples we have considered in class: binary world, moving dot

Task 1 (15 points): Improving the moving dot example

Start from the moving dot example that we did in class. Your tasks are as follows:

Task 2 (10 points): Adding handling clicks to the dot example

Now we would like to be able to click on a spot on the canvas and make the dot jump to the place that was clicked. For this we need to add another event handling function to the big-bang to handle mouse events, such as clicks:

(define (main duration)
  (big-bang (make-posn half-w half-h)
            [to-draw show] 
            [on-tick do-nothing 1 duration] 
            [on-key move]
            [on-mouse handle-mouse-event]

The function that handles a mouse event takes four parameters:

(define (handle-mouse-event ws x y type)

Here ws is the world state (the position of the dot in our example), x,y are the coordinates of the event (such as the coordinates of where the user clicks), and type is an item of the enumeration of mouse events which includes "button-down", "button-up", etc. (see documentation for details). In this case you will be handling the "button-up" event (the end of the click).

Your function should check that the type of event is "button-up" using a mouse=? function, and if it is, it should return the state of the dot as the the x and y cooridnates of the click. Don't forget to include the else part of the condition to leave the dot position unchanged.

As always, pay attention to code quality: use of functions, well-chosen names, function documentation, comments, etc.

Extra credit, 10 points: add key handling to change the color of the dot

Copy your current moving dot solution into a separate file: you will be making changes in multiple places.

The only way to keep track of the color of the dot is to incorporate it into the world state. This means expanding the structure that you currently have. You can do it in two different ways: use a nested structure that contains the posn structure and the color, or define your own structure that contains x, y, and a color. Discuss which of these approaches you would like to take and change the world state accordngly. This means changing all the current functions that consume the state (you don't have to copy over all the functionality that you have implemented earlier, just a couple of key events and the mouse event). Use the color part of the structure in show (instead of the current magenta) to display the color.

Test your changes carefully, use a color different from magenta when you provide the initial world state to the big bang.

Pick some letter keys to denote color, for instance "y" for yellow, "b" for blue, etc. Add these cases to your key event handling functions to change the world state to the color determined by the key. If your show function uses the color in the world state then this should be sufficient to see the color changes. Test your world to see if all events are working as expected.

CSci 1301 course web site.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.