[racket] get-pixel

From: Stephen Bloch (sbloch at adelphi.edu)
Date: Tue Nov 30 17:19:17 EST 2010

Matthias Felleisen <matthias at ccs.neu.edu> wrote:

> Your programmer suffers from a major design flaw: you are conflating the 'model' (what data is needed to keep track of the events in my game) with its 'view' (how should the model be displayed on screen). Separate those two aspects and your program will run like a bat out of hell fire. --

I'm teaching a course on GUI programming (in Java) this semester, and my students have been doing the same thing: trying to store information in the view.  Your program will be much cleaner (and possibly much more efficient) if you think of the graphical display as write-only memory.  Maintain the current state of the Space Invaders game in internal variables that have little or nothing to do with graphics.  Whenever you change these internal variables, it should trigger a screen update, which will be computed from the internal variables.

If you write the program using the universe teachpack, rather than the more general graphics libraries, you'll be FORCED to structure it correctly: you design a "model" type, write a function from model-> model to use as a clock-tick handler, write a function from model -> image to use as a refresh handler, etc.  Once you've gotten used to model/view separation, you'll never want to give it up even when working with a library that allows you to.

Stephen Bloch
sbloch at adelphi.edu

Posted on the users mailing list.