---
# System prepended metadata

title: Rac-man Function Map

---

# Rac-man Function Map

### Libraries
```
(require 2htdp/image)
(require 2htdp/universe)
(require 2htdp/iTunes)
(require 2htdp/abstraction)
```


### Constants
```racket
; Width of World
(define DIAM 40)

; Create World 
(define playArea
  (rectangle (* DIAM 20) (* DIAM 20) "solid" "black"))

; Dot Image
(define dotPic
  (circle (/ DIAM 10) "solid" "pink"))
  
; Cookie Image
(define POWERUP
  (circle (/ DIAM 20) "solid" "white"))
  
; Ghost1 image
(define ghost1
    (rectangle 30 40 "solid" "blue"))

; Ghost2 image
(define ghost2
    (rectangle 30 40 "solid" "red"))

;Ghost3 image
(define ghost3
    (rectangle 30 40 "solid" "pink"))
    
;Ghost4 image
(define ghost4
    (rectangle 30 40 "solid" "orange"))

; Rac-man Image
(define racman-pic
  (circle (/ DIAM 2.5) "solid" (color 255 223 0 255)))
```
### Structures
```racket
(define-struct racman [x y curdir nexdir])
; A racman is a structure 
; Interpretation represents current state of racman. x and y position, the directoin he currently is heading and the intended next direction of the user
; --- (make-racman 55 100 "up" "right")

; A **Speed** is a Number
; Interprtation represents the velocity of racman and ghosts at current state

; A **Dots** is a list of posns
; Represents the pellets that racman eats for points 


(define-struct gamestate [racman dots speed])
; a gamestate is a structure
; Interpretion represents the current state of the entire game with racman dots on screen, and the current game speed
--- (make-gamestate (make-racman 55 100 "up" "right") (define dotslist (list (make-posn 200 100) (make-posn 220 100))) 5)
```
### Functions
#### Wishlist
Gamestate
```racket

; Wishlist gamestate structure 
; A power-up is a an boolean
; Interpretation represents whether racman is in powerup state which allows him to eat ghosts

; A lives is a number
; Interpretation represents the remainng lives of racman 
; --- initialzed to 3

; Points is a number
; Interpretation represents that current points of racman player
;(define-struct gamestate [ rac-man ghosts maze dots speed power-up lives points])

; Maze is a list? array?
; Interpretation represents the possible paths and boundries of where racman and
; ghosts are allowed to go

(define (dotListGenerator DIAM) (list (make-posn 400 400)))
```

Menu
```racket
; Menu struct
; A highscores is a list-of numbers
; (define-struct menustate [highscores])

; Menu function
; a gs is a menustate
; menustate -> menustate
; Displays high scores, starts game, quits
(define (menu gs)
  (big-bang gs
    [on-tick checkClick]
    [to-draw renderMenu]
    [on-key menuKeys]
    [on-mouse menuClicks]))

; renderMenu
; a scores is a highschores
; scores -> image
; Renders the menu screen with a list of high scores
(define (renderMenu scores)
    (overlay buttons (renderScores scores)))
    
; renderScores
; scores -> image
; Renders the list of high scores
(define (renderScores scores)
    (for/list ([i (in-range scores)])
        (text "1. ")))
        
; menuKeys
; An ms is a menustate, a key is a keyboard input
; menustate -> menustate
; calls the functions specified by key presses
(define (menuSelections ms key)
    (cond
        [(string=? key " ") (main start)]
        [(string=? key "ESC") (quit)]
        [else ms]))
        
; menuClicks
; An ms is a menustate
; menustate -> menustate
; calls the functions specified by the buttons
(define (menuSelections ms)
    (cond
        [(= (mouse-x)) (main start)]
        [(string=? key "ESC") (quit)]
        [else ms]))
```

### Functional Functions
Render
```racket
; gamestate -> image
; renders the current state of the game

(define (render gs)
  (place-image racman-pic (racman-x (gamestate-racman gs)) (racman-y (gamestate-racman gs))
               (renderDots (gamestate-dots gs) playArea)))

; [list-of dots] image -> image
; renders the current dots on background
(define (renderDots lod im)
  (cond
    [(empty? lod) im]
    [else (place-image dotPic (posn-x (first lod)) (posn-y (first lod))
                      (renderDots (rest lod) im))]))
```
Move Functions
```racket
; gamestate -> gamestate
; moves Rac-Man
(define (move gs)
  (make-gamestate (cond
                     [(string=? "up" (racman-nexdir (gamestate-racman gs)))
                      (moveWormUp (gamestate-racman gs))]
                     [(string=? "down" (racman-nexdir (gamestate-racman gs)))
                      (moveWormDown (gamestate-racman gs))]
                     [(string=? "left" (racman-nexdir (gamestate-racman gs)))
                      (moveWormLeft (gamestate-racman gs))]
                     [(string=? "right" (racman-nexdir (gamestate-racman gs)))
                      (moveWormRight (gamestate-racman gs))]
                     [else (gamestate-racman gs)])
(eatDots2 (gamestate-dots gs) (gamestate-racman gs)) (gamestate-speed gs)))
```

```racket
; NEW eatDots
; lod is a list-of dots, rman is a racman
; [list-of dots] rman -> [list-of dots]
; removes dot from dots if collision with racman
(define (eatDots2 lod rman)
  (for/list ([i lod])
    (if (and
         (< (abs (- (racman-x rman) (posn-x i))) 10)
         (< (abs (- (racman-y rman) (posn-y i))) 10))
       (make-posn (random 800) (random 800))
       i)))
```

```racket
; rman is a racman
; racman -> racman
; moves rac-man up
(define (moveWormUp rman)
  (make-racman (racman-x rman) (- (racman-y rman) 5) "up" (racman-nexdir rman)))

; rman is a racman
; racman -> racman
; moves rac-man down
(define (moveWormDown rman)
  (make-racman (racman-x rman) (+ (racman-y rman) 5) "down" (racman-nexdir rman)))

; rman is a racman
; racman -> racman
; moves rac-man left
(define (moveWormLeft rman)
  (make-racman (- (racman-x rman) 5) (racman-y rman) "left" (racman-nexdir rman)))

; rman is a racman
; racman -> racman
; moves rac-man right
(define (moveWormRight rman)
  (make-racman (+ (racman-x rman) 5) (racman-y rman) "right" (racman-nexdir rman)))
```
Change Direction (on-key function)
```racket
; gs is a gamestate, key is a keyboard input
; gamestate, key -> gamestate
; Changes the next direction of racman
(define (changeDir gs key)
  (make-gamestate (make-racman (racman-x (gamestate-racman gs)) (racman-y (gamestate-racman gs)) (racman-curdir (gamestate-racman gs))
                    (cond
                      [(string=? key "left") "left"]
                      [(string=? key "right") "right"]
                      [(string=? key "up") "up"]
                      [(string=? key "down") "down"]
                      [else (racman-nexdir (gamestate-racman gs))]))
                  (gamestate-dots gs) (gamestate-speed gs)))
```

### End of Game Check
This function needs to be modified to check for 0 lives left.
```racket
; gs is a gamestate
; gamestate -> boolean
; Checks for collision with the outside walls
(define (gameOver? gs)
  (cond
    [(<= (racman-y (gamestate-racman gs)) 0) #true]
    [(>= (racman-y (gamestate-racman gs)) (image-height playArea)) #true]
    [(<= (racman-x (gamestate-racman gs)) 0) #true]
    [(>= (racman-x (gamestate-racman gs)) (image-width playArea)) #true]
    [else #false]))
(define (stopScreen gs)
  (overlay/align "middle" "middle" (text "GAME OVER" 48 "black") (render gs)))
```

```racket
; gs is a gamestate
; gamestate -> gamestate
; Runs the game
(define (main gs)
  (big-bang gs
    [on-tick move]
    [to-draw render]
    [on-key changeDir]
    [stop-when gameOver? stopScreen]))
```

Running the program
```racket
(define dotList (list (make-posn 200 100) (make-posn 220 100) (make-posn 240 100) (make-posn 260 100)
                     (make-posn 200 120)
                     (make-posn 200 140)
                     (make-posn 200 160)
                     (make-posn 200 180)
                     (make-posn 200 200) (make-posn 220 200) (make-posn 240 200) (make-posn 260 200)))
(define start  (make-gamestate (make-racman 20 20 "right" "right") dotList 20))
(main start)

; (eatDots2 dotList (make-racman 20 20 "right" "right"))
```
