-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathstars2.rkt
79 lines (58 loc) · 1.97 KB
/
stars2.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
#lang racket
;; Version 2, in 3D
(require 2htdp/universe 2htdp/image)
(require "util.rkt")
;; Screen size
(define WIDTH 600)
(define HEIGHT 600)
;; - to + of this value for new stars
(define MAX-STAR-XY 25000)
(define MAX-STARS 100)
(define TICK-RATE 1/25)
(define ACCEL 1)
(define START-Z 100)
;; -----------------------------------------------------------
(struct starfield (stars) #:transparent)
(struct astar (x y z) #:transparent)
(define (start-space)
(big-bang (starfield (times-repeat MAX-STARS (new-star)))
(on-tick fly TICK-RATE)
(to-draw render-space)
(stop-when end-flight)))
(define (screen-x s) (+ (/ (astar-x s) (astar-z s)) (/ WIDTH 2)))
(define (screen-y s) (+ (/ (astar-y s) (astar-z s)) (/ HEIGHT 2)))
(define (random-star-xy) (- (random MAX-STAR-XY) (/ MAX-STAR-XY 2)))
(define (new-star)
(astar (random-star-xy)
(random-star-xy)
(+ (random START-Z) 10)))
(define (move-star s)
(astar (astar-x s) (astar-y s) (- (astar-z s) ACCEL)))
(define (stars-in-view stars)
(define (replace-star s)
(if (star-out-of-view? s) (new-star) s))
(map replace-star stars))
(define (star-out-of-view? s)
(<= (astar-z s) 1))
(define (fly w)
(starfield (map move-star (stars-in-view (starfield-stars w)))))
;; -----------------------------------------------------------
(define (render-space w)
(stars+scene (starfield-stars w) (empty-scene WIDTH HEIGHT "black")))
(define (stars+scene stars scene)
(foldl (λ (s scene)
(place-image (circle (star-size s) "solid" (star-colour s))
(screen-x s)
(screen-y s)
scene))
scene stars))
(define (star-size s)
(define z (astar-z s))
(cond [(> z 75) 1]
[else (+ 1 (/ (- 75 z) 20)) ]))
(define (star-colour s)
(define z (astar-z s))
(cond [(> z 90) (color 255 255 255 20)]
[else (color 255 255 255 (+ 20 (* 2 (- 90 z))))]))
(define (end-flight w) #f)
;;(start-space)