Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. ; BINARY MASTER
  2.  
  3. WINDOW.W=360
  4. WINDOW.H=492
  5.  
  6. include 'a.inc'
  7.  
  8. text t(256), title.t='Binary Master: %hh'
  9.  
  10. text help.t=+\
  11.  'Fun Game for Programmers.' RET\
  12.  'Click BITs. Count in binary.' RET\
  13.  'Match the decimal number' RET\
  14.  'in the red box to make rows' RET\
  15.  'disappear. Press any key.' RET\
  16.  'r=Reset. p=Pause. Esc=exit'
  17.  
  18. text pause.t=+\
  19.  'Paused. Press p to continue' RET\
  20.  'or r=Reset. Esc=exit'
  21.  
  22. text game.over.t=+\
  23.  'Game over. Score: %hh.' RET\
  24.  'Press any key'
  25.  
  26. align
  27.  
  28. integer scene, score
  29. numeric SCENE.*, TITLE, PLAY,\
  30.  PAUSE, GAME.OVER
  31. numeric EASY=5000, NORMAL=4000, HARD=2000
  32.  
  33. BOX board, my.box
  34. integer my.n, red.n, magic.n=10101101b
  35. integer cbit.x, cbit.y, bits.h
  36. numeric BIT.*, W=32, H=48
  37.  
  38. text my.numbers(8+4), red.numbers(8+4)
  39.  
  40. FONT main.font='font'
  41.  
  42. IMAGE bits.i='bits', bit1.i='1',\
  43.  bit0.i='0', close.i='x'
  44.  
  45. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  46.  
  47. function random.byte
  48.   locals n
  49.   .r:
  50.   random 3
  51.   if r0<2
  52.     random 16
  53.   else.if r0=2
  54.     random 128
  55.   else.if r0=3
  56.     random 255
  57.   end
  58.   . n=r0
  59.   text.find red.numbers, n
  60.   if true, go .r, end
  61.   . r0=n
  62.   if false, r0++, end
  63. endf
  64.  
  65. function reset.game
  66.   locals n, p
  67.   . score=0, bits.h=1
  68.   memory.zero my.numbers, 12
  69.   memory.zero red.numbers, 12
  70.   . n=8, p=red.numbers
  71.   loop n
  72.     random.byte
  73.     . r1=p, *r1++=r0, p=r1
  74.   endl
  75.   set.box board, 4, 70, BIT.W*8, BIT.H*8
  76.   . scene=SCENE.TITLE
  77. endf
  78.  
  79. function on.create
  80.   set.font main.font
  81.   set.timer NORMAL
  82.   reset.game
  83. endf
  84.  
  85. function remove.byte, t, i
  86.   locals n
  87.   alias p=r0, q=r1, x=r2
  88.   if i=7, go .new, end
  89.   . p=t, p+i, q=p, q++, x=7, x-i, n=x
  90.   loop n, *p++=*q++, endl
  91.   .new:
  92.   . p=my.numbers, *(p+7)=0
  93.   random.byte
  94.   . q=red.numbers, *(q+7)=r0
  95. endf
  96.  
  97. function remove.row, i
  98.   remove.byte my.numbers, i
  99.   remove.byte red.numbers, i
  100.   . bits.h--
  101.   if bits.h<1, bits.h=1, end
  102. endf
  103.  
  104. function check.numbers
  105.   locals i
  106.   . i=0
  107.   while i<8, r0=my.numbers, r0+i
  108.     . r1=*r0, r0=red.numbers
  109.     . r0+i, r2=*r0
  110.     if r1=r2, score+r1
  111.       remove.row i
  112.       return 1
  113.     end
  114.     . i++
  115.   endw
  116. endf 0
  117.  
  118. function draw.board
  119.   locals i, n, x, y, w, h
  120.   draw.image bits.i, 4, 35
  121.   draw.image bits.i, 4, 457
  122.    . x=0, y=0, w=32, h=48
  123.   while y<8, x=0
  124.     while x<8
  125.       . r0=x, r0*w, r0+board.x
  126.       . r1=y, r1*h, r1+board.y
  127.       set.box my.box, r0, r1, w, h
  128.       draw.box my.box, BLACK, GRAY
  129.       . x++
  130.     endw
  131.     . r0=x, r0*w, r0+board.x
  132.     . r1=y, r1*h, r1+board.y
  133.     set.box my.box, r0, r1, 48, h
  134.     draw.box.o my.box, WHITE
  135.     . my.box.x+48
  136.     draw.box.o my.box, RED
  137.     . r0=y, r1=8, r1-bits.h
  138.     if r0>=r1
  139.       . r0=my.numbers, r1=y, r2=8
  140.       . r2-bits.h, r1-r2, r0+r1
  141.       . r1=*r0, my.n=r1
  142.       . r0=red.numbers, r1=y, r2=8
  143.       . r2-bits.h, r1-r2, r0+r1
  144.       . r1=*r0, red.n=r1
  145.       u2t my.n, t
  146.       . my.box.x-40, my.box.y+11
  147.       draw.text t, my.box.x, my.box.y
  148.       . my.box.x+44
  149.       u2t red.n, t
  150.       draw.text t, my.box.x, my.box.y
  151.     end
  152.     . y++
  153.   endw
  154. endf
  155.  
  156. function draw.bit, n, x, y
  157.   if n
  158.     draw.image bit1.i, x, y
  159.   else
  160.     draw.image bit0.i, x, y
  161.   end
  162. endf
  163.  
  164. function draw.byte, n, x, y
  165.   locals i
  166.   . i=8
  167.   loop i, r0=n, r1=i, r1--, r0>>cl, r0&1
  168.     draw.bit r0, x, y
  169.     . x+BIT.W
  170.   endl
  171. endf
  172.  
  173. function draw.my.numbers
  174.   locals i, n, y
  175.   . i=bits.h, y=404
  176.   loop i, r0=my.numbers, r0+i, r0--
  177.     . r0=*r0, n=r0
  178.     draw.byte n, 4, y
  179.     . y-BIT.H
  180.   endl
  181. endf
  182.  
  183. function draw.title.scene
  184.   draw.text help.t, 16, 130
  185.   draw.byte magic.n, 50, 300
  186. endf
  187.  
  188. function draw.play.scene
  189.   draw.board
  190.   draw.my.numbers
  191. endf
  192.  
  193. function draw.pause.scene
  194.   draw.text pause.t, 16, 130
  195.   draw.byte magic.n, 50, 300
  196. endf
  197.  
  198. function draw.game.over
  199.   print t, game.over.t, score
  200.   draw.text t, 44, 170
  201.   draw.byte magic.n, 50, 300
  202. endf
  203.  
  204. function on.draw
  205.   locals x, y, w, h
  206.   clear.screen BLACK
  207.   print t, title.t, score
  208.   draw.text t, 4, 4
  209.   draw.image close.i, 324, 4
  210.   . r0=screen.w, r0--
  211.   . r1=screen.h, r1--
  212.   draw.outline 0, 0, r0, r1, GRAY
  213.   if scene=SCENE.TITLE
  214.     draw.title.scene
  215.   else.if scene=SCENE.PLAY
  216.     draw.play.scene
  217.   else.if scene=SCENE.PAUSE
  218.     draw.pause.scene
  219.   else.if scene=SCENE.GAME.OVER
  220.     draw.game.over
  221.   end
  222. endf
  223.  
  224. function on.key
  225.   if key.event='c'
  226.     if scene=SCENE.TITLE
  227.       . scene=SCENE.PLAY
  228.       go .draw
  229.     end
  230.     if scene=SCENE.GAME.OVER
  231.       go .reset
  232.     end
  233.     if key='r'
  234.       .reset:
  235.       reset.game
  236.       go .draw
  237.     end
  238.     if key='p'
  239.       .pause:
  240.       if scene=SCENE.PLAY
  241.         . scene=SCENE.PAUSE
  242.       else.if scene=SCENE.PAUSE
  243.         . scene=SCENE.PLAY
  244.       end
  245.       go .draw
  246.     end
  247.     .draw:
  248.     render
  249.   end
  250. endf
  251.  
  252. function on.mouse
  253.   if.select board
  254.     . r0=mouse.x, r0-WINDOW.X, r0-board.x
  255.     . r1=BIT.W, r0/r1, cbit.x=r0
  256.     . r0=mouse.y, r0-WINDOW.Y, r0-board.y
  257.     . r1=BIT.H, r0/r1, cbit.y=r0
  258.     if mouse.event='c'
  259.       . r0=cbit.y, r1=8, r1-bits.h
  260.       if r0>=r1, r0=my.numbers, r1=cbit.y
  261.         . r2=8, r2-bits.h, r1-r2, r0+r1
  262.         . r3=*r0, r2=1, r1=7, r1-cbit.x
  263.         . r2<<cl, r3><r2, *r0=r3
  264.       end
  265.     end
  266.   end
  267.   if mouse.event='r'
  268.     check.numbers
  269.     go .draw
  270.   end
  271.   if mouse.event='c'
  272.     . r0=&close.i.x
  273.     if.select r0
  274.       exit
  275.     end
  276.     if scene<>SCENE.PLAY
  277.       reset.game
  278.       . scene=SCENE.PLAY
  279.     end
  280.     .draw:
  281.     render
  282.   end
  283. endf
  284.  
  285. function on.timer
  286.   if scene<>SCENE.PLAY
  287.     return
  288.   end
  289.   if mouse.1, return, end
  290.   if bits.h<8, bits.h++
  291.   else
  292.     . scene=SCENE.GAME.OVER
  293.   end
  294.   render
  295. endf
  296.  
  297. function on.exit
  298.   ; ...
  299. endf