1 USING: accessors alien.c-types alien.data arrays calendar colors
2 combinators combinators.short-circuit flatland generalizations
3 grouping kernel locals math math.intervals math.order
4 math.rectangles math.vectors namespaces opengl opengl.gl
5 opengl.glu processing.shapes sequences sequences.generalizations
6 shuffle threads ui ui.gadgets ui.gestures ui.render ;
7 FROM: multi-methods => GENERIC: METHOD: ;
11 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
13 ! Inspired by this Ruby/Shoes version by why: http://gist.github.com/26431
15 ! Which was based on this Nodebox version: http://billmill.org/pong.html
18 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
20 : clamp-to-interval ( x interval -- x )
21 [ from>> first max ] [ to>> first min ] bi ;
23 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
25 TUPLE: <play-field> < <rectangle> ;
26 TUPLE: <paddle> < <rectangle> ;
28 TUPLE: <computer> < <paddle> { speed initial: 10 } ;
30 : computer-move-left ( computer -- ) dup speed>> move-left-by ;
31 : computer-move-right ( computer -- ) dup speed>> move-right-by ;
33 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
36 { diameter initial: 20 }
37 { bounciness initial: 1.2 }
38 { max-speed initial: 10 } ;
40 : above-lower-bound? ( ball field -- ? ) bottom 50 - above? ;
41 : below-upper-bound? ( ball field -- ? ) top 50 + below? ;
43 : in-bounds? ( ball field -- ? )
45 [ above-lower-bound? ]
46 [ below-upper-bound? ]
49 :: bounce-change-vertical-velocity ( BALL -- )
58 :: bounce-off-paddle ( BALL PADDLE -- )
60 BALL bounce-change-vertical-velocity
62 BALL x PADDLE center x - 0.25 * BALL vel>> (x!)
64 PADDLE top BALL pos>> (y!) ;
66 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
68 : mouse-x ( -- x ) hand-loc get first ;
70 :: valid-paddle-interval ( PADDLE PLAY-FIELD -- interval )
72 PLAY-FIELD [ left ] [ right ] bi PADDLE width - [a,b] ;
74 :: align-paddle-with-mouse ( PADDLE PLAY-FIELD -- )
78 PADDLE PLAY-FIELD valid-paddle-interval
84 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
86 ! Protocol for drawing PONG objects
88 GENERIC: draw ( obj -- )
90 METHOD: draw { <paddle> } [ bottom-left ] [ dim>> ] bi rectangle ;
91 METHOD: draw { <ball> } [ pos>> ] [ diameter>> 2 / ] bi circle ;
93 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
95 TUPLE: <pong> < gadget paused field ball player computer ;
99 T{ <play-field> { pos { 0 0 } } { dim { 400 400 } } } clone >>field
100 T{ <ball> { pos { 50 50 } } { vel { 3 4 } } } clone >>ball
101 T{ <paddle> { pos { 200 396 } } { dim { 75 4 } } } clone >>player
102 T{ <computer> { pos { 200 0 } } { dim { 75 4 } } } clone >>computer ;
104 M: <pong> pref-dim* ( <pong> -- dim ) drop { 400 400 } ;
105 M: <pong> ungraft* ( <pong> -- ) t >>paused drop ;
107 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
109 M:: <pong> draw-gadget* ( PONG -- )
115 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
117 :: iterate-system ( GADGET -- )
119 GADGET field>> :> FIELD
120 GADGET ball>> :> BALL
121 GADGET player>> :> PLAYER
122 GADGET computer>> :> COMPUTER
124 BALL FIELD in-bounds? [
126 PLAYER FIELD align-paddle-with-mouse
132 BALL COMPUTER to-the-left-of? [ COMPUTER computer-move-left ] when
133 BALL COMPUTER to-the-right-of? [ COMPUTER computer-move-right ] when
135 ! check if ball bounced off something
137 ! player-blocked-ball?
138 BALL PLAYER { [ above? ] [ in-between-horizontally? ] } 2&&
139 [ BALL PLAYER bounce-off-paddle ] when
141 ! computer-blocked-ball?
142 BALL COMPUTER { [ below? ] [ in-between-horizontally? ] } 2&&
143 [ BALL COMPUTER bounce-off-paddle ] when
146 BALL FIELD in-between-horizontally? not
147 [ BALL reverse-horizontal-velocity ] when
149 ] [ t GADGET paused<< ] if ;
151 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
153 :: start-pong-thread ( GADGET -- )
159 [ GADGET iterate-system GADGET relayout-1 25 milliseconds sleep t ]
166 ! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
168 : pong-window ( -- ) pong [ "PONG" open-window ] [ start-pong-thread ] bi ;
170 : pong-main ( -- ) [ pong-window ] with-ui ;