Advertisement
Guest User

Untitled

a guest
Apr 13th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Racket 5.32 KB | None | 0 0
  1. #lang racket
  2. (provide (all-defined-out))
  3. (require "gameobject.rkt")
  4.  
  5. ;; Constants
  6. (define block-size 20)
  7.  
  8. ;; Snake object
  9. (define snake%
  10.   (class gameobject%
  11.     (init-field gamemodel)
  12.     (field (velocityX 1)
  13.            (velocityY 0))
  14.  
  15.     ;; Defines the list that handles the snake coordinates
  16.     (define initial-state (list (list 300 300) (list 300 300) (list 300 300)))
  17.     (define snake-list (list (list 300 300) (list 300 300) (list 300 300)))
  18.  
  19.     ;; Getter for snake list
  20.     (define/public (get-snake-list)
  21.       snake-list)
  22.  
  23.     ;; Updates snake by making a new list of coordinates and removing
  24.     ;; the end of the snake
  25.     (define/public (update-snake x-cord y-cord)
  26.       (define temp (list x-cord y-cord))
  27.       (define temp2 (remove (last snake-list) snake-list))
  28.       (set! snake-list (append (list temp) temp2)))
  29.  
  30.     ;; Increases the size of the snake by adding a new list to the existing snake
  31.     (define/public (grow-snake val1 val2)
  32.       (define temp (list val1 val2))
  33.       (set! snake-list (append (list temp) snake-list)))
  34.  
  35.     ;; Getters and setters for velocity
  36.     (define/public (getVelX)
  37.       velocityX)
  38.     (define/public (getVelY)
  39.       velocityY)
  40.     (define/public (setVelX velX)
  41.       (set! velocityX velX))
  42.     (define/public (setVelY velY)
  43.       (set! velocityY velY))
  44.  
  45.     ;; Move functions for Snake
  46.     ;; The unless clauses makes sure you cannot move backwards through yourself
  47.     (define/public (moveUp)
  48.       (set! velocityX 0)
  49.       (unless (eq? (send this getVelY) 1)
  50.         (set! velocityY -1)))
  51.     (define/public (moveDown)
  52.       (set! velocityX 0)
  53.       (unless (eq? (send this getVelY) -1)
  54.         (set! velocityY 1)))
  55.     (define/public (moveRight)
  56.       (set! velocityY 0)
  57.       (unless (eq? (send this getVelX) -1)
  58.         (set! velocityX 1)))      
  59.     (define/public (moveLeft)
  60.       (set! velocityY 0)
  61.       (unless (eq? (send this getVelX) 1)
  62.         (set! velocityX -1)))
  63.  
  64.     ;; Help function that limits a value between a max and minimum
  65.     (define/private (clamp var min max)
  66.       (cond
  67.         [ (>= var max) max]
  68.         [ (<= var min) min]
  69.         [ else var]))
  70.  
  71.     ;; Called when the player loses.
  72.     ;; Resets the snake to its initial state
  73.     (define/private (game-reset)
  74.       (define initialx 300)
  75.       (define initialy 300)
  76.       (send this setX initialx)
  77.       (send this setY initialy)
  78.       (send this setVelX 1)
  79.       (send this setVelY 0)
  80.       (set! snake-list initial-state))
  81.  
  82.     ;; Checks if the snake overlaps with a piece of food by comparing distances
  83.     (define/private (ate-food? x x1 y y1)
  84.       (define xdis (- x x1))
  85.       (define ydis (- y y1))
  86.       (if (and (< (abs xdis) 10) (< (abs ydis) 10))
  87.           #t
  88.           #f))
  89.  
  90.     ;; Checks if the snake has collided with itself
  91.     ;; Self-collision also happens upon colliding with a wall
  92.     (define/private (ate-self?)
  93.       (define all-but-head (cdr snake-list))
  94.       (define snake-head (car snake-list))
  95.       ;; Checks if the coordinates of the snakes' head is the same as any of the coordinates
  96.       ;; in the tail
  97.       (unless (null? all-but-head)
  98.         (if (pair? (member snake-head all-but-head))
  99.             (game-reset)
  100.             1)))
  101.  
  102.     ;; Help function that calls the other collision functions every tick
  103.     ;; Upon collision with food it calls the game model to remove the food
  104.     ;; This causes the spawner class to spawn a new piece of food
  105.     (define/private (collision)
  106.       (ate-self?)
  107.       (for ( [obj (send gamemodel get-objects) ] )
  108.         (if (ate-food? (send this getX)
  109.                        (send obj getX)
  110.                        (send this getY)
  111.                        (send obj getY))
  112.             (begin
  113.               (grow-snake (send this getX) (send this getY))
  114.               (send gamemodel remove-object obj))
  115.             1)))
  116.  
  117.    
  118.     ;; Render function of Snake
  119.     (define/public (render dc)
  120.       ;; Gets current coordinates and size
  121.       (let ((xpos (send this getX))
  122.             (ypos (send this getY))
  123.             (size (send this getSize)))  
  124.         (send dc set-brush "red" 'solid)
  125.  
  126.         ;; Draws the snake as a rectangle by fetching the coordinates from the snake list
  127.         (define temp1 (send this get-snake-list))
  128.         (for ( [elem temp1] )
  129.           (send dc draw-rectangle (car elem) (car (cdr elem)) size size))))
  130.  
  131.    
  132.     ;; Tick function of Snake
  133.     (define/public (tick)
  134.       ;; Gets current coordinates and velocities
  135.       (let ((xpos (send this getX))
  136.             (velx (send this getVelX))
  137.             (ypos (send this getY))
  138.             (vely (send this getVelY)))
  139.  
  140.         ;; Moves the snake by setting its coordinates to one block-size in the
  141.         ;; direction specified by the velocity
  142.         (send this setY (+ ypos (* vely block-size)))
  143.         (send this setX (+ xpos (* velx block-size)))
  144.  
  145.         ;; Sends the updated coordinates to be put in the list
  146.         (send this update-snake (send this getX) (send this getY))
  147.  
  148.         ;; Limits the coordinates to the game area, adjusted by block-size
  149.         (send this setX (clamp (send this getX) 0 (- 1200 block-size)))
  150.         (send this setY (clamp (send this getY) 0 (- 800 block-size)))
  151.        
  152.         ;; Checks for collision
  153.         (collision)))
  154.     (super-new [size block-size])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement