Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #lang racket
- (provide (all-defined-out))
- (require "gameobject.rkt")
- ;; Constants
- (define block-size 20)
- ;; Snake object
- (define snake%
- (class gameobject%
- (init-field gamemodel)
- (field (velocityX 1)
- (velocityY 0))
- ;; Defines the list that handles the snake coordinates
- (define initial-state (list (list 300 300) (list 300 300) (list 300 300)))
- (define snake-list (list (list 300 300) (list 300 300) (list 300 300)))
- ;; Getter for snake list
- (define/public (get-snake-list)
- snake-list)
- ;; Updates snake by making a new list of coordinates and removing
- ;; the end of the snake
- (define/public (update-snake x-cord y-cord)
- (define temp (list x-cord y-cord))
- (define temp2 (remove (last snake-list) snake-list))
- (set! snake-list (append (list temp) temp2)))
- ;; Increases the size of the snake by adding a new list to the existing snake
- (define/public (grow-snake val1 val2)
- (define temp (list val1 val2))
- (set! snake-list (append (list temp) snake-list)))
- ;; Getters and setters for velocity
- (define/public (getVelX)
- velocityX)
- (define/public (getVelY)
- velocityY)
- (define/public (setVelX velX)
- (set! velocityX velX))
- (define/public (setVelY velY)
- (set! velocityY velY))
- ;; Move functions for Snake
- ;; The unless clauses makes sure you cannot move backwards through yourself
- (define/public (moveUp)
- (set! velocityX 0)
- (unless (eq? (send this getVelY) 1)
- (set! velocityY -1)))
- (define/public (moveDown)
- (set! velocityX 0)
- (unless (eq? (send this getVelY) -1)
- (set! velocityY 1)))
- (define/public (moveRight)
- (set! velocityY 0)
- (unless (eq? (send this getVelX) -1)
- (set! velocityX 1)))
- (define/public (moveLeft)
- (set! velocityY 0)
- (unless (eq? (send this getVelX) 1)
- (set! velocityX -1)))
- ;; Help function that limits a value between a max and minimum
- (define/private (clamp var min max)
- (cond
- [ (>= var max) max]
- [ (<= var min) min]
- [ else var]))
- ;; Called when the player loses.
- ;; Resets the snake to its initial state
- (define/private (game-reset)
- (define initialx 300)
- (define initialy 300)
- (send this setX initialx)
- (send this setY initialy)
- (send this setVelX 1)
- (send this setVelY 0)
- (set! snake-list initial-state))
- ;; Checks if the snake overlaps with a piece of food by comparing distances
- (define/private (ate-food? x x1 y y1)
- (define xdis (- x x1))
- (define ydis (- y y1))
- (if (and (< (abs xdis) 10) (< (abs ydis) 10))
- #t
- #f))
- ;; Checks if the snake has collided with itself
- ;; Self-collision also happens upon colliding with a wall
- (define/private (ate-self?)
- (define all-but-head (cdr snake-list))
- (define snake-head (car snake-list))
- ;; Checks if the coordinates of the snakes' head is the same as any of the coordinates
- ;; in the tail
- (unless (null? all-but-head)
- (if (pair? (member snake-head all-but-head))
- (game-reset)
- 1)))
- ;; Help function that calls the other collision functions every tick
- ;; Upon collision with food it calls the game model to remove the food
- ;; This causes the spawner class to spawn a new piece of food
- (define/private (collision)
- (ate-self?)
- (for ( [obj (send gamemodel get-objects) ] )
- (if (ate-food? (send this getX)
- (send obj getX)
- (send this getY)
- (send obj getY))
- (begin
- (grow-snake (send this getX) (send this getY))
- (send gamemodel remove-object obj))
- 1)))
- ;; Render function of Snake
- (define/public (render dc)
- ;; Gets current coordinates and size
- (let ((xpos (send this getX))
- (ypos (send this getY))
- (size (send this getSize)))
- (send dc set-brush "red" 'solid)
- ;; Draws the snake as a rectangle by fetching the coordinates from the snake list
- (define temp1 (send this get-snake-list))
- (for ( [elem temp1] )
- (send dc draw-rectangle (car elem) (car (cdr elem)) size size))))
- ;; Tick function of Snake
- (define/public (tick)
- ;; Gets current coordinates and velocities
- (let ((xpos (send this getX))
- (velx (send this getVelX))
- (ypos (send this getY))
- (vely (send this getVelY)))
- ;; Moves the snake by setting its coordinates to one block-size in the
- ;; direction specified by the velocity
- (send this setY (+ ypos (* vely block-size)))
- (send this setX (+ xpos (* velx block-size)))
- ;; Sends the updated coordinates to be put in the list
- (send this update-snake (send this getX) (send this getY))
- ;; Limits the coordinates to the game area, adjusted by block-size
- (send this setX (clamp (send this getX) 0 (- 1200 block-size)))
- (send this setY (clamp (send this getY) 0 (- 800 block-size)))
- ;; Checks for collision
- (collision)))
- (super-new [size block-size])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement