;; The first three lines of this file were inserted by DrScheme. They record metadata ;; about the language level of this file in a form that our tools can easily process. #reader(lib "htdp-advanced-reader.ss" "lang")((modname |animation slowdown|) (read-case-sensitive #t) (teachpacks ()) (htdp-settings #(#t constructor repeating-decimal #t #t none #f ()))) (require 2htdp/image) (require 2htdp/universe) ; INSERT HI-RES IMAGE HERE ******** ;; I used a 1200x900 cut-n-paste of a digicam photo via MS Paint (define HI-RES {text "Insert large bitmap here" 20 "black"}) (define SIZE 800) ;; The WorldState is a (make-world Posn List-of-Posn} (define-struct world (ball bricks)) (define init-world (make-world (make-posn 100 200) (build-list 3000 (lambda (n) (make-posn (random SIZE) (random SIZE)))))) (define BACKGROUND (rectangle SIZE SIZE "solid" "white")) (define BRICK (rectangle 5 2 "solid" "blue")) (define BALL (circle 5 "solid" "red")) (define speed 5) ;; place-brick : Posn Image -> Image ;; Adds a brick image at the given point into the given image (define (place-brick pos background) (place-image BRICK (posn-x pos) (posn-y pos) background)) ;; An image constant containing all the brick images overlaid on the blank background (define BRICKS (foldr place-brick BACKGROUND (world-bricks init-world))) ;; Tick-handler simply moves the ball in a world-wrapping straight line (define (tick-handler ws) (local [(define (move-ball pos) (make-posn (modulo (+ speed (posn-x pos)) SIZE) (modulo (+ speed (posn-y pos)) SIZE)))] (make-world (move-ball (world-ball ws)) (world-bricks ws)))) ;; Render draws the ball and one version of the background (define (render ws) (place-image BALL (posn-x (world-ball ws)) (posn-y (world-ball ws)) ;;; Four ways to draw the ball's background. Uncomment one at a time. ;; 1. Blank background to see the ball move at full speed BACKGROUND ;; 2. Use place-image to build a new background image depending on current state ;(foldr place-brick BACKGROUND (world-bricks ws)) ;; 3. Use a constant image, full of rectangles, as the background ;BRICKS ;; 4. Use a constant high-resolution "flat?" image for the background ;(place-image HI-RES (/ SIZE 2) (/ SIZE 2) BACKGROUND) ) ) (big-bang init-world (on-tick tick-handler) (on-draw render))