Paste: PONG: stop thread when window closes

Author: dharmatech
Mode: factor
Date: Sat, 22 Nov 2008 11:49:12
Plain Text |
: mouse-x ( -- x ) hand-loc get first ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

TUPLE: <pong> < gadget draw closed ;

M: <pong> pref-dim*    ( <pong> -- dim ) drop { 400 400 } ;
M: <pong> draw-gadget* ( <pong> --     ) draw>> call ;
M: <pong> ungraft*     ( <pong> --     ) t >>closed drop ;

! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

: play-pong ( -- )

[let | PONG [ <pong> new-gadget ] |

  PONG

  [let | WIDTH  [ 400 ]
         HEIGHT [ 400 ] |

  [let | BALL-DIAMETER [ 20   ]
         PADDLE-SIZE   [ 75   ]
         VX!           [  3   ]
         VY!           [  4   ]
         PX!           [ 50   ]
         PY!           [ 50   ]
         BOUNCE        [  1.2 ]
         COMPUTER!     [ WIDTH 2 / ]
         COMPSPEED!    [ 10 ]        |
    
    [
      -50 PY < PY HEIGHT 50 + < and
      [

        [let | PADDLE-LEFT [ mouse-x 0 max   WIDTH PADDLE-SIZE -   min ]
               NY! [ PY VY + ]
               NX! [ PX VX + ] |
  
          COMPUTER PADDLE-SIZE + NX < [ COMPUTER COMPSPEED + COMPUTER! ] when
          NX COMPUTER               < [ COMPUTER COMPSPEED - COMPUTER! ] when
  
          { COMPUTER 0 } { PADDLE-SIZE 4 } rectangle
  
          HEIGHT NY <
          0 VY <
          PADDLE-LEFT NX <
          NX PADDLE-LEFT PADDLE-SIZE + <
          and and and
          [
            VY neg BOUNCE * VY!
            NX PADDLE-LEFT PADDLE-SIZE 2 / + - 0.25 * VX!
            HEIGHT NY!
          ]
          when
  
          NY 0 <
          VY 0 <
          COMPUTER NX <
          NX   COMPUTER PADDLE-SIZE + <
          and and and
          [
            VY neg BOUNCE * VY!
            NX COMPUTER PADDLE-SIZE 2 / + - 0.25 * VX!
            0 NY!
          ]
          when
  
          NX 0 < WIDTH NX < or [ VX neg VX! ] when
  
          [let | MX [ mouse-x WIDTH PADDLE-SIZE - min 0 max ]
                 MY [ HEIGHT 4 - ] |

            { MX MY } { PADDLE-SIZE 4 } rectangle ]
  
          NX PX!
          NY PY!
  
          { PX PY } BALL-DIAMETER circle
        ]
      ]
      when
    ]

  ] ]

  >>draw drop

  PONG "test" open-window

  [ [ PONG closed>> [ f ] [ PONG relayout-1 25 sleep t ] if ] loop ] in-thread

  ] ;

Annotation: 'USING:' line needed for the above

Author: dharmatech
Mode: factor
Date: Sat, 22 Nov 2008 13:08:04
Plain Text |
USING: accessors kernel locals math math.order namespaces
       processing.shapes sequences threads ui ui.gadgets ui.gestures
       ui.render ;

New Annotation

Summary:
Author:
Mode:
Body: