PasteRack.org
Paste # 38460
2025-04-17 18:34:02

Fork as a new paste.

Paste viewed 133 times.


Embed:

  1. #lang racket
  2. ;; The first three lines of this file were inserted by DrRacket. They record metadata
  3. ;; about the language level of this file in a form that our tools can easily process.
  4. #reader(lib "htdp-intermediate-lambda-reader.ss" "lang")((modname WIPPROJECT05) (read-case-sensitive #t) (teachpacks ((lib "image.rkt" "teachpack" "2htdp"))) (htdp-settings #(#t constructor repeating-decimal #f #t none #f ((lib "image.rkt" "teachpack" "2htdp")) #f)))
  5. (require 2htdp/image)
  6. (require 2htdp/universe)
  7.  
  8.  
  9. (define-struct tile [x num])
  10. (: make-tile (Integer Integer -> Tile))
  11.  
  12. (define-struct world [tiles time])
  13. (: make-world ((ListOf Tile) Integer -> World))
  14.  
  15. (: random-tiles (Number -> (ListOf Tile)))
  16. (define (random-tiles c)
  17.   (cond [(= 0 c) empty]
  18.         [else
  19.          (let*
  20.               ([x (random 4)]
  21.               [num c]
  22.               [a (make-tile x num)])
  23.               (cons a (random-tiles (- c 1))))]))
  24.  
  25.  
  26.  
  27. (define BUILD (make-world (random-tiles 25) 0))
  28.  
  29.  
  30.  
  31. (define Image (signature (predicate image?)))
  32.  
  33.  
  34.  
  35. (define (draw-tiles tiles w)
  36.   (cond
  37.     [(empty? tiles) (empty-scene 240 360)]
  38.     [(cons? tiles) (place-image
  39.                     (overlay (rectangle 60 100 "outline" "white")
  40.                              (rectangle 60 100 "solid" "black"))
  41.                             (+ 30 (* (tile-x (first tiles)) 60))
  42.                             (+ (+ (* (tile-num (first tiles)) -100) (world-time w)) 200)
  43.                 (draw-tiles (rest tiles) w))]))
  44.  
  45.  
  46. (: draw-world (World -> Image))
  47. (define (draw-world w)
  48. (draw-tiles (world-tiles w) w))
  49.  
  50. (: tick-world (World -> World))
  51. (define (tick-world w)
  52.   (make-world (world-tiles w) (+ 5 (world-time w))))
  53.  
  54. (big-bang BUILD
  55.   (to-draw draw-world)
  56.   (on-tick tick-world 0.03))

=>