-
Notifications
You must be signed in to change notification settings - Fork 6
/
ex-156.rkt
81 lines (62 loc) · 1.49 KB
/
ex-156.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#lang htdp/bsl
(require 2htdp/image)
(require 2htdp/universe)
(require test-engine/racket-tests)
; ### CONSTANTS
(define HEIGHT 400)
(define WIDTH 600)
(define XSHOTS (/ WIDTH 2))
(define YSPEED -5)
(define BACKGROUND (empty-scene WIDTH HEIGHT))
(define SHOT (triangle 5 "solid" "red"))
; ### DATA DEFINITIONS
; A Shot is a integer Number
; interpretation: 10 is a shot at 10px from top
; A List-of-shots is one of:
; - '()
; - (cons Shot List-of-shots)
;
; interpretation: list of fired shots
; A WorldState is a List-of-shots
; ### FUNCTIONS
; WorldState -> Image
(define (render-world ws)
(cond
[(empty? ws) BACKGROUND]
[else
(place-image
SHOT
XSHOTS
(first ws)
(render-world (rest ws))
)]))
; WorldState KeyEvent -> WorldState
; handles the key events
(check-expect (on-key-press '() " ") (cons HEIGHT '()))
(check-expect (on-key-press (cons 50 '()) " ") (cons HEIGHT (cons 50 '())))
(define (on-key-press ws ke)
(cond
[(key=? ke " ") (cons HEIGHT ws)]
[else ws]
))
; WorldState KeyEvent -> WorldState
; handles the ticking of the world
(check-expect (move-shots '()) '())
(check-expect (move-shots (cons 10 '())) (cons (+ 10 YSPEED) '()))
(define (move-shots ws)
(cond
[(empty? ws) '()]
[else
(cons
(+ (first ws) YSPEED)
(move-shots (rest ws))
)]))
(define (main ws)
(big-bang
ws
[to-draw render-world]
[on-key on-key-press]
[on-tick move-shots]
))
(test)
(main '())