2 USING: kernel accessors locals math math.intervals math.order
3 namespaces sequences threads
11 combinators.short-circuit.smart
12 combinators.cleave.enhanced
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 ! Inspired by this Ruby/Shoes version by why: http://gist.github.com/26431
22 ! Which was based on this Nodebox version: http://billmill.org/pong.html
25 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
27 : clamp-to-interval ( x interval -- x )
28 [ from>> first max ] [ to>> first min ] bi ;
30 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
32 TUPLE: <play-field> < <rectangle> ;
33 TUPLE: <paddle> < <rectangle> ;
35 TUPLE: <computer> < <paddle> { speed initial: 10 } ;
37 : computer-move-left ( computer -- ) dup speed>> move-left-by ;
38 : computer-move-right ( computer -- ) dup speed>> move-right-by ;
40 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
43 { diameter initial: 20 }
44 { bounciness initial: 1.2 }
45 { max-speed initial: 10 } ;
47 : above-lower-bound? ( ball field -- ? ) bottom 50 - above? ;
48 : below-upper-bound? ( ball field -- ? ) top 50 + below? ;
50 : in-bounds? ( ball field -- ? )
52 [ above-lower-bound? ]
53 [ below-upper-bound? ]
56 :: bounce-change-vertical-velocity ( BALL -- )
65 :: bounce-off-paddle ( BALL PADDLE -- )
67 BALL bounce-change-vertical-velocity
69 BALL x PADDLE center x - 0.25 * BALL vel>> (x!)
71 PADDLE top BALL pos>> (y!) ;
73 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
75 : mouse-x ( -- x ) hand-loc get first ;
77 :: valid-paddle-interval ( PADDLE PLAY-FIELD -- interval )
79 PLAY-FIELD [ left ] [ right ] bi PADDLE width - [a,b] ;
81 :: align-paddle-with-mouse ( PADDLE PLAY-FIELD -- )
85 PADDLE PLAY-FIELD valid-paddle-interval
91 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
93 ! Protocol for drawing PONG objects
95 GENERIC: draw ( obj -- )
97 METHOD: draw ( <paddle> -- ) [ bottom-left ] [ dim>> ] bi rectangle ;
98 METHOD: draw ( <ball> -- ) [ pos>> ] [ diameter>> 2 / ] bi circle ;
100 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
102 USE: syntax ! Switch back to core 'TUPLE:' instead of the one provided
105 TUPLE: <pong> < gadget paused field ball player computer ;
109 T{ <play-field> { pos { 0 0 } } { dim { 400 400 } } } clone >>field
110 T{ <ball> { pos { 50 50 } } { vel { 3 4 } } } clone >>ball
111 T{ <paddle> { pos { 200 396 } } { dim { 75 4 } } } clone >>player
112 T{ <computer> { pos { 200 0 } } { dim { 75 4 } } } clone >>computer ;
114 M: <pong> pref-dim* ( <pong> -- dim ) drop { 400 400 } ;
115 M: <pong> ungraft* ( <pong> -- ) t >>paused drop ;
117 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
119 M:: <pong> draw-gadget* ( PONG -- )
125 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
127 :: iterate-system ( GADGET -- )
129 [let | FIELD [ GADGET field>> ]
130 BALL [ GADGET ball>> ]
131 PLAYER [ GADGET player>> ]
132 COMPUTER [ GADGET computer>> ] |
134 [wlet | align-player-with-mouse [ ( -- )
135 PLAYER FIELD align-paddle-with-mouse ]
137 move-ball [ ( -- ) BALL 1 move-for ]
139 player-blocked-ball? [ ( -- ? )
140 BALL PLAYER { [ above? ] [ in-between-horizontally? ] } && ]
142 computer-blocked-ball? [ ( -- ? )
143 BALL COMPUTER { [ below? ] [ in-between-horizontally? ] } && ]
145 bounce-off-wall? [ ( -- ? )
146 BALL FIELD in-between-horizontally? not ]
148 stop-game [ ( -- ) t GADGET (>>paused) ] |
150 BALL FIELD in-bounds?
153 align-player-with-mouse
159 BALL COMPUTER to-the-left-of? [ COMPUTER computer-move-left ] when
160 BALL COMPUTER to-the-right-of? [ COMPUTER computer-move-right ] when
162 ! check if ball bounced off something
164 player-blocked-ball? [ BALL PLAYER bounce-off-paddle ] when
165 computer-blocked-ball? [ BALL COMPUTER bounce-off-paddle ] when
166 bounce-off-wall? [ BALL reverse-horizontal-velocity ] when
173 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
175 :: start-pong-thread ( GADGET -- )
181 [ GADGET iterate-system GADGET relayout-1 25 milliseconds sleep t ]
188 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
190 : pong-window ( -- ) pong [ "PONG" open-window ] [ start-pong-thread ] bi ;
192 : pong-main ( -- ) [ pong-window ] with-ui ;