( flappy.b ) 1
seed ( var ) 1 seed ! ( init ) : rand
( -- n ) seed 128 seed @ * seed @ 11 >> ^ 16 seed @ * ^ <=> 0x7FFF & return ( ret )
; ( ret ) :: srand ( n -- ) seed ! ( forth ) ; ( ret ) 1
time ( var ) 0 time ! ( init ) 1
key ( var ) 0 key ! ( init ) :: drawPixel
( x y c -- ) 2 pick 2 pick 2 pick putpixel drop drop drop
; ( ret ) : drawFastHLine
( x y w c -- ) 0
! ( init ) begin
@ 2 pick <<> while ( for ) 3 pick
@ + 3 pick 2 pick drawPixel
drop repeat ( endfor ) drop drop drop drop
; ( ret ) : fillRect
( x y w h c -- ) 0
! ( init ) begin
@ 2 pick <<> while ( for ) 0 1
! ( init ) begin 1
@ 3 pick <<> while ( for ) 4 pick 1
@ + 4 pick
@ + 2 pick drawPixel 1
drop repeat ( endfor )
drop repeat ( endfor ) drop drop drop drop drop
; ( ret ) : millis
( -- n ) time @ return ( ret )
; ( ret ) : A
( -- n ) key @ return ( ret )
; ( ret ) : random
( a b -- n ) begin
rand 0x7F & <=> over <>> while ( while ) repeat ( endwhile )
@ nip nip return ( ret )
; ( ret ) : drawFastVLine
( x y h c -- ) 3 pick 128 <<> if ( if ) 0
! ( init ) begin
@ 2 pick <<> while ( for ) 3 pick 3 pick
@ + 2 pick drawPixel
drop repeat ( endfor ) then ( endif ) drop drop drop drop
; ( ret ) : setTextColor
( c -- ) drop
; ( ret ) : setCursor
( x y -- ) drop drop
; ( ret ) : print
( n -- ) drop
; ( ret ) : delay
( ms -- ) drop
; ( ret ) : setTextSize
( n -- ) drop
; ( ret ) : println
( s -- ) drop
; ( ret ) : waitForClick
( -- )
; ( ret ) :: randomSeed
( n -- ) drop
; ( ret ) :: analogRead ( n -- n ) ( forth ) ; ( ret ) : fillScreen
( c -- ) 0 0 128 160 4 pick fillRect drop
; ( ret ) : EEPROM_Write
( num MemPos -- ) drop drop
; ( ret ) : EEPROM_Read
( num MemPos -- ) drop drop
; ( ret ) : init
( -- )
; ( ret ) : initR
( n -- ) drop
; ( ret ) :: TFTW 128 ; :: TFTH 160 ; :: TFTW2 64 ; :: TFTH2 80 ; :: SPEED 1 ; :: GRAVITY 10 ; :: JUMP_FORCE 2000 ; :: SKIP_TICKS 25 ; :: MAX_FRAMESKIP 5 ; :: BIRDW 8 ; :: BIRDH 8 ; :: BIRDW2 4 ; :: BIRDH2 4 ; :: PIPEW 12 ; :: GAPHEIGHT 36 ; :: FLOORH 20 ; :: GRASSH 4 ; 1
maxScore ( var ) 0 maxScore ! ( init ) 1
BCKGRDCOL ( var ) 0xFFFF00 BCKGRDCOL ! ( init ) 1
BIRDCOL ( var ) 0x00FFFF BIRDCOL ! ( init ) 1
PIPECOL ( var ) 0x00FF00 PIPECOL ! ( init ) 1
PIPEHIGHCOL ( var ) 0xFFFFFF PIPEHIGHCOL ! ( init ) 1
PIPESEAMCOL ( var ) 0x000000 PIPESEAMCOL ! ( init ) 1
FLOORCOL ( var ) 0x00FFFF FLOORCOL ! ( init ) 1
GRASSCOL ( var ) 0x00FF00 GRASSCOL ! ( init ) 1
GRASSCOL2 ( var ) 0x00FFFF GRASSCOL2 ! ( init ) :: C0 0xFFFF00 ; :: C1 0x0000FF ; :: C2 0x00FFFF ; :: C3 0xFFFFFF ; :: C4 0x0000FF ; :: C5 0x00FFFF ; 0
birdcol ( var ) C0 , ( init ) C0 , ( init ) C1 , ( init ) C1 , ( init ) C1 , ( init ) C1 , ( init ) C1 , ( init ) C0 , ( init ) C0 , ( init ) C1 , ( init ) C2 , ( init ) C2 , ( init ) C2 , ( init ) C1 , ( init ) C3 , ( init ) C1 , ( init ) C0 , ( init ) C2 , ( init ) C2 , ( init ) C2 , ( init ) C2 , ( init ) C1 , ( init ) C3 , ( init ) C1 , ( init ) C1 , ( init ) C1 , ( init ) C1 , ( init ) C2 , ( init ) C2 , ( init ) C3 , ( init ) C1 , ( init ) C1 , ( init ) C1 , ( init ) C2 , ( init ) C2 , ( init ) C2 , ( init ) C2 , ( init ) C2 , ( init ) C4 , ( init ) C4 , ( init ) C1 , ( init ) C2 , ( init ) C2 , ( init ) C2 , ( init ) C1 , ( init ) C5 , ( init ) C4 , ( init ) C0 , ( init ) C0 , ( init ) C1 , ( init ) C2 , ( init ) C1 , ( init ) C5 , ( init ) C5 , ( init ) C5 , ( init ) C0 , ( init ) C0 , ( init ) C0 , ( init ) C1 , ( init ) C5 , ( init ) C5 , ( init ) C5 , ( init ) C0 , ( init ) C0 , ( init ) 5
bird ( var ) 3
pipe ( var ) 1
score ( var ) 1
tmpx ( var ) 1
tmpy ( var ) 1
GAMEH ( var ) 1
grassx ( var ) 1
delta ( var ) 1
old_time ( var ) 1
next_game_tick ( var ) 1
current_time ( var ) 1
loops ( var ) 1
passed_pipe ( var ) 1
px ( var ) 1
mode ( var ) 0 mode ! ( init ) : game_init
( -- ) BCKGRDCOL @ fillScreen score 0 <=> drop bird 20 <=> drop bird 1 + bird 2 + TFTH2 BIRDH - <=> <=> drop bird 4 + 0 <=> drop tmpx tmpy 0 <=> <=> drop 3 analogRead randomSeed pipe TFTW <=> drop pipe 1 + 20 TFTH 60 - random <=> drop
; ( ret ) : game_start
( -- ) mode @ 0 if ( if ) return ( ret ) then ( endif ) 0 fillScreen 10 TFTH2 20 - TFTW 20 - 1 16777215 fillRect 10 TFTH2 32 + TFTW 20 - 1 16777215 fillRect 16711935 setTextColor 3 setTextSize TFTW2 6 9 * - TFTH2 16 - setCursor "FLAPPY" println 3 setTextSize TFTW2 6 9 * - TFTH2 8 + setCursor "-BIRD-" println 16777215 setTextColor 0 setTextSize 10 TFTH2 48 - setCursor "Arduino UNO" println 255 setTextColor TFTW2 17 3 * - 1 - TFTH2 54 + setCursor "DRUK OP LINKSE KNOP" println waitForClick game_init
; ( ret ) : game_loop_x
( -- ) mode @ 0 if ( if ) return ( ret ) then ( endif ) GAMEH TFTH FLOORH - <=> drop 0 GAMEH @ TFTW 0 drawFastHLine 0 GAMEH @ 1 + TFTW2 GRASSH GRASSCOL @ fillRect TFTW2 GAMEH @ 1 + TFTW2 GRASSH GRASSCOL2 @ fillRect 0 GAMEH @ GRASSH + TFTW 0 drawFastHLine 0 GAMEH @ GRASSH + 1 + TFTW FLOORH GRASSH - 1 - FLOORCOL @ fillRect grassx TFTW <=> drop next_game_tick current_time millis <=> <=> drop passed_pipe 0 <=> drop mode <++i> drop
; ( ret ) : game_loop
( -- ) mode @ 1 if ( if ) return ( ret ) then ( endif ) loops 0 <=> drop begin millis next_game_tick @ <>> if loops @ MAX_FRAMESKIP <<>
else 0 then while ( while ) A if ( if ) bird 1 + @ BIRDH2 1 >> <>> if ( if ) bird 4 + JUMP_FORCE <-x> <=> drop else ( else ) bird 4 + 0 <=> drop then ( endif ) then ( endif ) old_time current_time @ <=> drop bird 4 + GRAVITY current_time millis <=> old_time @ - * <+=> drop bird 1 + bird 4 + @ 10 >> <+=> drop pipe SPEED <-=> drop pipe @ PIPEW <-x> <<> if ( if ) pipe TFTW <=> drop pipe 1 + 10 GAMEH @ 10 GAPHEIGHT + - random <=> drop then ( endif ) next_game_tick SKIP_TICKS <+=> drop loops
drop repeat ( endwhile ) pipe @ 0 <>=> if pipe @ TFTW <<>
else 0 then if ( if ) pipe @ 3 + 0 pipe 1 + @ PIPECOL @ drawFastVLine pipe @ 3 + pipe 1 + @ GAPHEIGHT + 1 + GAMEH @ pipe 1 + @ GAPHEIGHT + 1 + - PIPECOL @ drawFastVLine pipe @ 0 pipe 1 + @ PIPEHIGHCOL @ drawFastVLine pipe @ pipe 1 + @ GAPHEIGHT + 1 + GAMEH @ pipe 1 + @ GAPHEIGHT + 1 + - PIPEHIGHCOL @ drawFastVLine pipe @ pipe 1 + @ PIPESEAMCOL @ drawPixel pipe @ pipe 1 + @ GAPHEIGHT + PIPESEAMCOL @ drawPixel pipe @ pipe 1 + @ 6 - PIPESEAMCOL @ drawPixel pipe @ pipe 1 + @ GAPHEIGHT + 6 + PIPESEAMCOL @ drawPixel pipe @ 3 + pipe 1 + @ 6 - PIPESEAMCOL @ drawPixel pipe @ 3 + pipe 1 + @ GAPHEIGHT + 6 + PIPESEAMCOL @ drawPixel then ( endif ) pipe @ TFTW <<=> if ( if ) pipe @ PIPEW + 0 GAMEH @ BCKGRDCOL @ drawFastVLine then ( endif ) tmpx BIRDW 1 - <=> drop 1 begin while ( do ) px bird @ tmpx @ + BIRDW + <=> drop tmpy BIRDH 1 - <=> drop 1 begin while ( do ) px @ bird 2 + @ tmpy @ + BCKGRDCOL @ drawPixel tmpy
repeat ( while ) tmpy BIRDH 1 - <=> drop 1 begin while ( do ) px @ bird 1 + @ tmpy @ + birdcol tmpx @ tmpy @ BIRDW * + + @ drawPixel tmpy
repeat ( while ) tmpx
repeat ( while ) bird 2 + bird 1 + @ <=> drop grassx SPEED <-=> drop grassx @ 0 <<> if ( if ) grassx TFTW <=> drop then ( endif ) grassx @ TFTW <%> GAMEH @ 1 + GRASSH 1 - GRASSCOL @ drawFastVLine grassx @ 64 + TFTW <%> GAMEH @ 1 + GRASSH 1 - GRASSCOL2 @ drawFastVLine bird 1 + @ GAMEH @ BIRDH - <>> if ( if ) mode <--i> drop return ( ret ) then ( endif ) bird @ BIRDW + pipe @ BIRDW2 - <>=> if bird @ pipe @ PIPEW + BIRDW - <<=>
else 0 then if ( if ) bird 1 + @ pipe 1 + @ <<> if 1 else bird 1 + @ BIRDH + pipe 1 + @ GAPHEIGHT + <>>
then if ( if ) mode <--i> drop return ( ret ) else ( else ) passed_pipe 1 <=> drop then ( endif ) else ( else ) bird @ pipe @ PIPEW + BIRDW - <>> if passed_pipe @
else 0 then if ( if ) passed_pipe 0 <=> drop BCKGRDCOL @ setTextColor TFTW2 4 setCursor score @ print 16777215 setTextColor score
drop then ( endif ) then ( endif ) TFTW2 4 setCursor score @ print 1200 delay
; ( ret ) : game_over
( -- ) mode @ 2 if ( if ) return ( ret ) then ( endif ) 0 fillScreen maxScore 0 EEPROM_Read score @ maxScore @ <>> if ( if ) score 0 EEPROM_Write maxScore score @ <=> drop 255 setTextColor 0 setTextSize 4 120 setCursor "NIEUWE HOOGSTE SCORE" println then ( endif ) 16711680 setTextColor 2 setTextSize TFTW2 5 6 * - TFTH2 30 - setCursor 65535 setTextColor "EINDE" println 16777215 setTextColor 0 setTextSize 40 TFTH2 50 - setCursor "SCORE: " print score @ print TFTW2 15 3 * - TFTH2 12 + setCursor "DRUK OP DE KNOP" println 10 130 setCursor "MAXIMUM SCORE:" print maxScore @ print waitForClick mode 0 <=> drop
; ( ret ) : resetMaxScore
( -- ) maxScore 0 EEPROM_Write
; ( ret ) : setup
( -- ) init 0 initR
; ( ret ) : scene
( -- ) game_start game_loop_x game_loop game_over
; ( ret ) : main
( -- ) setup
; ( ret ) : "onmousedown"
( -- ) key 1 <=> drop
; ( ret ) : "onmouseup"
( -- ) key 0 <=> drop
; ( ret ) : 0
( -- ) scene redraw
; ( ret ) : 25
( -- ) time 25 <+=> drop
; ( ret )
main