2008-10-01 21:12:46 -04:00
|
|
|
! Copyright (C) 2006, 2007, 2008 Alex Chapman
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2009-02-05 04:28:41 -05:00
|
|
|
USING: accessors alarms arrays calendar kernel make math math.rectangles math.parser namespaces sequences system tetris.game tetris.gl ui.gadgets ui.gadgets.labels ui.gadgets.worlds ui.gadgets.status-bar ui.gestures ui.render ui ;
|
2009-05-15 00:23:06 -04:00
|
|
|
FROM: tetris.game => level>> ;
|
2008-10-01 21:12:46 -04:00
|
|
|
IN: tetris
|
|
|
|
|
|
|
|
TUPLE: tetris-gadget < gadget { tetris tetris } { alarm } ;
|
|
|
|
|
|
|
|
: <tetris-gadget> ( tetris -- gadget )
|
2009-02-16 05:04:32 -05:00
|
|
|
tetris-gadget new swap >>tetris ;
|
2008-10-01 21:12:46 -04:00
|
|
|
|
|
|
|
M: tetris-gadget pref-dim* drop { 200 400 } ;
|
|
|
|
|
|
|
|
: update-status ( gadget -- )
|
|
|
|
dup tetris>> [
|
|
|
|
"Level: " % dup level>> #
|
|
|
|
" Score: " % score>> #
|
|
|
|
] "" make swap show-status ;
|
|
|
|
|
|
|
|
M: tetris-gadget draw-gadget* ( gadget -- )
|
|
|
|
[
|
2009-02-05 04:28:41 -05:00
|
|
|
[ dim>> first2 ] [ tetris>> ] bi draw-tetris
|
2008-10-01 21:12:46 -04:00
|
|
|
] keep update-status ;
|
|
|
|
|
|
|
|
: new-tetris ( gadget -- gadget )
|
|
|
|
[ <new-tetris> ] change-tetris ;
|
|
|
|
|
|
|
|
tetris-gadget H{
|
2008-12-15 21:31:55 -05:00
|
|
|
{ T{ button-down f f 1 } [ request-focus ] }
|
2008-10-01 21:12:46 -04:00
|
|
|
{ T{ key-down f f "UP" } [ tetris>> rotate-right ] }
|
|
|
|
{ T{ key-down f f "d" } [ tetris>> rotate-left ] }
|
|
|
|
{ T{ key-down f f "f" } [ tetris>> rotate-right ] }
|
|
|
|
{ T{ key-down f f "e" } [ tetris>> rotate-left ] } ! dvorak d
|
|
|
|
{ T{ key-down f f "u" } [ tetris>> rotate-right ] } ! dvorak f
|
|
|
|
{ T{ key-down f f "LEFT" } [ tetris>> move-left ] }
|
|
|
|
{ T{ key-down f f "RIGHT" } [ tetris>> move-right ] }
|
|
|
|
{ T{ key-down f f "DOWN" } [ tetris>> move-down ] }
|
|
|
|
{ T{ key-down f f " " } [ tetris>> move-drop ] }
|
|
|
|
{ T{ key-down f f "p" } [ tetris>> toggle-pause ] }
|
|
|
|
{ T{ key-down f f "n" } [ new-tetris drop ] }
|
|
|
|
} set-gestures
|
|
|
|
|
|
|
|
: tick ( gadget -- )
|
|
|
|
[ tetris>> ?update ] [ relayout-1 ] bi ;
|
|
|
|
|
|
|
|
M: tetris-gadget graft* ( gadget -- )
|
|
|
|
[ [ tick ] curry 100 milliseconds every ] keep (>>alarm) ;
|
|
|
|
|
|
|
|
M: tetris-gadget ungraft* ( gadget -- )
|
|
|
|
[ cancel-alarm f ] change-alarm drop ;
|
|
|
|
|
|
|
|
: tetris-window ( -- )
|
|
|
|
[
|
|
|
|
<default-tetris> <tetris-gadget>
|
|
|
|
"Tetris" open-status-window
|
|
|
|
] with-ui ;
|
|
|
|
|
|
|
|
MAIN: tetris-window
|