;; world_template.scm Daniel Azus 12-4-2006 ;; This program uses the world teachpack ;; This is a template for a program that uses the world teachpack that can use the mouse and keyboard ;; All default values are defined at the top of the program ;; Constants (define WindowWidth 800) ;; The width of the window in pixels (define WindowHeight 640) ;; The height of the window in pixels (define FrameRate 30) ;; The target rate the window is updated per second (frames per second) (define FrameDelay (/ 1 FrameRate)) ;; The delay between frames, based on the frame rate ;; Constants ;; Default values for variables (define InitFrame 0) (define InitGameOver false) (define InitPaused false) ;; =====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====* ;; Defining and setting the default world ;; Defining the world ;; World variables ;; Frame = Frames since the program started ;; GameOver = True if the player has lost the game ;; Paused = If the world is paused (define-struct World [Frame GameOver Paused]) ;; The first world that starts with the default values (define InitialWorld (make-World InitFrame InitGameOver InitPaused)) ;; =====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====* ;; Drawing helper functions ;; Used when drawing images in a world (define (PutImage image x y image2) (place-image image (+ x (/ (image-width image) 2)) (+ y (/ (image-height image) 2)) image2) ) ;; Used when drawing images in a world with out offset (define (PutImageNorm image x y image2) (place-image image x y image2) ) ;; Used when drawing text in a world (define (PutText Text TextColor x y image2) (PutImageNorm (text Text 12 TextColor) x y image2) ) ;; Used when drawing text in a world with a font size (define (PutTextSized Text TextColor fontsize x y image2) (PutImageNorm (text Text fontsize TextColor) x y image2) ) ;; =====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====* ;; Everything that has to do with drawing ;; Constant drawing colors (define ColorWhite (make-color 0 0 0)) (define ColorBlack (make-color 0 0 0)) (define ColorRed (make-color 255 0 0)) (define ColorGreen (make-color 0 255 0)) (define ColorBlue (make-color 0 0 255)) (define ColorDarkGrey (make-color 100 100 100)) ;; Constants for drawing (define Background (rectangle WindowWidth WindowHeight 'solid ColorDarkGrey)) ;; This draws the game in action (define (DrawGame aworld) (PutTextSized (number->string (World-Frame aworld)) ColorWhite 18 (/ WindowWidth 2) 0 (PutImage Background 0 0 (empty-scene WindowWidth WindowHeight) )) ) ;; This takes care of making sure everything gets drawn (define (Draw-Window aworld) (cond [(World-GameOver aworld) (PutTextSized "GAME OVER" ColorBlue 50 (/ WindowWidth 4) (/ WindowHeight 4) (PutTextSized "Press 'n' to restart" ColorBlue 16 (+ (/ WindowWidth 4) 100) (- (/ WindowHeight 2) 50) (DrawGame aworld))) ] [(World-Paused aworld) (PutTextSized "PAUSED" ColorBlue 50 (+ (/ WindowWidth 4) 50) (/ WindowHeight 4) (DrawGame aworld)) ] [else (DrawGame aworld)] ) ) ;; =====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====* ;; Everything that has to do with updating ;; Takes care of updating all the world variables of the current world (define (UpdateWorld aworld) (cond [(World-Paused aworld) aworld] ;; Check if this world is paused [else (make-World (+ (World-Frame aworld) 1) ;; Increment the frame count (World-GameOver aworld) ;; Game over false ;; This code will not be reached if the world is paused, so it is ok to use a constant value. ) ] ) ) ;; This takes care of making sure everything gets updated (define (Update aworld) (cond [(World-Paused aworld) aworld] ; If world is paused nothing changes [else (UpdateWorld aworld)] ) ) ;; =====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====* ;; Everything that has to do with mouse input ;; This is called when a mouse button is pressed. ;; It is unable to tell which button. (define (Mouse-Button-Down x y aworld) aworld ) ;; This is called when a mouse button is released. ;; It is unable to tell which button. (define (Mouse-Button-Released x y aworld) aworld ) ;; This is called when the mouse is moved. ;; It is unable to tell which button. (define (Mouse-Move x y aworld) aworld ) ;; This is called when the mouse is dragged. ;; It is unable to tell which button. (define (Mouse-Drag x y aworld) aworld ) ;; This is called anytime the mouse changes. ;; It is unable to tell which button is pressed or released. (define (Handle-Mouse a b c d) (cond [(eqv? d 'button-down) (Mouse-Button-Down b c a)] [(eqv? d 'button-up) (Mouse-Button-Released b c a)] [(eqv? d 'drag) (Mouse-Drag b c a)] [(eqv? d 'move) (Mouse-Move b c a)] [else a] ) ) ;; =====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====* ;; Everything that has to do with keyboard input ;; The up arrow key was pressed (define (Key-UpArrow aworld) aworld ) ;; The down arrow key was pressed (define (Key-DownArrow aworld) aworld ) ;; The W key was pressed (define (Key-w aworld) aworld ) ;; The S key was pressed (define (Key-s aworld) aworld ) ;; The S key was pressed (define (Key-n aworld) (if (World-GameOver aworld) InitialWorld aworld ) ) ;; This is called anytime a key to pressed or released. ;; It is unable to tell if a key is still pressed or which key was released. ;; If a key is held down then the function will get called multiple times. (define (Handle-Keys a b) (cond [(eqv? b 'up) (Key-UpArrow a)] [(eqv? b 'down) (Key-DownArrow a)] [(eqv? b #\w) (Key-w a)] [(eqv? b #\s) (Key-s a)] [(eqv? b #\n) (Key-n a)] [else a] ) ) ;; =====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====*=====* ;; Sets up the window and what functions to call and when they should be called ;; Creates the window (big-bang WindowWidth WindowHeight FrameDelay InitialWorld) ;; Set the drawing function (on-redraw Draw-Window) ;; Set the updating function (on-tick-event Update) ;; Set the key press function (on-key-event Handle-Keys) ;; Set the mouse handler function (on-mouse-event Handle-Mouse)