Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
5305 codemaster 1
; HANG PROGRAMMER
2
 
3
WINDOW.W=720
4
WINDOW.H=512
5
 
6
include 'a.inc'
7
include 'include/words.txt'
8
 
9
text title.t(32)='Hang Programmer'
10
text t(256), t2(256)
11
 
12
integer scene
13
numeric SCENE.*, TITLE, PLAY, GAME.OVER
14
 
15
integer guesses, word.index
16
BOX my.box
17
 
18
text word.t(64), used.letters(16)
19
 
20
align
21
 
22
IMAGE8 \
23
 board.i='board', logo.i='logo',\
24
 stand1.i='stand1', stand2.i='stand2',\
25
 head.i='head', body.i='body',\
26
 arm1.i='arm1', arm2.i='arm2',\
27
 leg1.i='leg1', leg2.i='leg2',\
28
 smile.i='smile', money.i='money',\
29
 prize.i='1000'
30
 
31
IMAGE close.i='x'
32
 
33
text abc='ABCDEFGHIJKLM', xyz='NOPQRSTUVWXYZ'
34
 
35
text help.t='CLICK TO START',\
36
 winner.t='WINNER', looser.t='LOOSER'
37
 
38
text example.t='DYNAMIC BINARY TRANSLATION'
39
 
40
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41
 
42
function reset.game
43
  . guesses=0
44
  text.zero used.letters
45
  text.zero word.t
46
  . r0=word.index, r0*4, r0+words.pa
47
  . (u32) r0=*r0
48
  text.copy word.t, r0
49
  . word.index++
50
  if word.index>=(N.WORDS-1)
51
    . word.index=0
52
  end
53
endf
54
 
55
; exchange pointer array elements.
56
; words.pa[a] and [b]
57
 
58
function exchange.words, a, b
59
  alias pa=r0, i=r0, p=r1, q=r2, x=r3
60
  . pa=words.pa
61
  . p=a, p*4, p+pa, q=b, q*4, q+pa
62
  . (u32) i=*p, (u32) x=*q
63
  . (u32) *p=x, (u32) *q=i
64
endf
65
 
66
; do this once when game begins.
67
; initialize pointer array to words[0-n]
68
; then randomize indices
69
 
70
function initialize
71
  locals i, n, j
72
  alias p=r0, x=r1
73
  . p=words.pa, i=0, n=N.WORDS
74
  loop n, x=i, (u32) x=*(words.ta+r1*4)
75
    . (u32) *p++=x, i++
76
  endl
77
  . i=N.WORDS
78
  loop i
79
    get j=random (N.WORDS-1)
80
    exchange.words i, j
81
  endl
82
  . word.index=0 ; random (N.WORDS-1)
83
endf
84
 
85
; is winner? if all characters in
86
; word.t have been used. return
87
; winner.t/looser.t
88
 
89
function is.winner
90
  locals i, q, n, win
91
  alias p=r0, c=r1
92
  . q=word.t
93
  get n=text.n q
94
  . win=1
95
  loop n, p=q, c=*p++
96
    if c<>' '
97
      text.find used.letters, c
98
      if false
99
        . win=0, break
100
      end
101
    end
102
    . q++
103
  endl
104
  if win, r0=winner.t
105
  else, r0=looser.t, end
106
endf
107
 
108
function on.create
109
  initialize
110
  reset.game
111
endf
112
 
113
function insert.c, c
114
  if c>='a', c-32, end
115
  if c<'A', return, end
116
  if c>'Z', return, end
117
  text.find used.letters, c
118
  if true, return, end
119
  text.attach.c used.letters, c
120
  is.winner
121
  if r0=winner.t
122
    go .reset
123
  end
124
  text.find word.t, c
125
  if true, return, end
126
  . guesses++
127
  if guesses=6
128
    .reset: . scene=SCENE.GAME.OVER
129
  end
130
endf
131
 
132
; special "my" font
133
 
134
align
135
alphabet.p: dd \
136
 a.i, b.i, c.i, d.i, e.i, f.i, g.i, h.i, i.i,\
137
 j.i, k.i, l.i, m.i, n.i, o.i, p.i, q.i, r.i,\
138
 s.i, t.i, u.i, v.i, w.i, x.i, y.i, z.i
139
 
140
irps _,\
141
 a b c d e f g h i j k l m \
142
 n o p q r s t u v w x y z {
143
  IMAGE8 _#.i='a/'#`_
144
}
145
 
146
function index.my.c, c
147
  . r0=c, r0-'A', r0*4, r0+alphabet.p
148
  . (u32) r0=*r0
149
endf
150
 
151
function get.my.cw, c
152
  if c=' ', return 0, end
153
  index.my.c c
154
  . (u32) r0=*(r0+?image.w)
155
endf
156
 
157
function get.my.tw, t
158
  locals n, w
159
  alias p=r0, c=r1
160
  get n=text.n t
161
  . w=0
162
  loop n, p=t, c=*p
163
    get.my.cw c
164
    . w+r0, t++
165
  endl
166
endf w
167
 
168
function draw.my.c, c, x, y, co
169
  locals im, w
170
  if c=' ', return, end
171
  get im=index.my.c c
172
  . (u32) r1=*(r0+?image.w), w=r1
173
  draw.image.v8 im, x, y, co
174
endf w
175
 
176
function draw.my.text, t, x, y
177
  locals p, n
178
  get n=text.n t
179
  . p=t
180
  loop n, r0=p, r0=*r0
181
    draw.my.c r0, x, y, WHITE
182
    . x+r0, x+4, p++
183
  endl
184
endf
185
 
186
function draw.my.word, t, x, y
187
  locals p, n, w, c, co
188
  get n=text.n t
189
  . p=t
190
  loop n, r0=p, r0=*r0, c=r0
191
    get w=get.my.cw c
192
    text.find used.letters, c
193
    if true
194
      . r0=x, r0+18, r1=w, r1/2, r0-r1
195
      draw.my.c c, r0, y, WHITE
196
    end
197
    . r0=x, r0+2, r1=y, r1+48
198
    draw.box r0, r1, 32, 3, WHITE
199
    . r0=36, x+r0, p++
200
  endl
201
endf
202
 
203
function draw.message
204
  locals n, x, y, w
205
  get n=text.count.w word.t
206
  if n=0, n=1, end
207
  set.source word.t
208
  set.token t
209
  . x=250, y=80
210
  loop n
211
    skip.space
212
    copy.until C.SPACE
213
    set.box my.box, 250, 65, 390, 220
214
    text.n t
215
    . r0*36, w=r0, r0=my.box.x, r0+195
216
    . r1=w, r1/2, r0-r1
217
    draw.my.word t, r0, y
218
    . y+70
219
  endl
220
endf
221
 
222
function draw.letters, t, x, y
223
  locals n, p, c, co
224
  get n=text.n t
225
  . p=t
226
  loop n, r0=p, r0=*r0, c=r0
227
    index.my.c c
228
    . r0+?image.box
229
    memory.copy my.box, r0, 16
230
    . my.box.x=x, my.box.y=y
231
    if.select my.box
232
      if mouse.1
233
        insert.c c
234
      end
235
    end
236
    . co=WHITE
237
    text.find used.letters, c
238
    if true, co=409040h, end
239
    draw.my.c c, x, y, co
240
    . r0+4, x+r0, p++
241
  endl
242
endf
243
 
244
function draw.alphabet
245
  draw.letters abc, 255, 308
246
  draw.letters xyz, 248, 370
247
endf
248
 
249
function draw.man
250
  locals x, y
251
  . x=40, y=62
252
  . alpha.bias=A.DARK
253
  draw.image.v.8 stand1.i, x, y, WHITE
254
  . r0=x, r0+27
255
  draw.image.v.8 stand2.i, r0, y, WHITE
256
  . alpha.bias=A.DARKER
257
  if guesses>0
258
    . r0=x, r0+80, r1=y, r1+40
259
    draw.image.v.8 head.i, r0, r1, WHITE
260
  end
261
  if guesses>1
262
    . r0=x, r0+95, r1=y, r1+150
263
    draw.image.v.8 body.i, r0, r1, WHITE
264
  end
265
  if guesses>2
266
    . r0=x, r0+70, r1=y, r1+150
267
    draw.image.v.8 arm1.i, r0, r1, WHITE
268
  end
269
  if guesses>3
270
    . r0=x, r0+143, r1=y, r1+150
271
    draw.image.v.8 arm2.i, r0, r1, WHITE
272
  end
273
  if guesses>4
274
    . r0=x, r0+87, r1=y, r1+228
275
    draw.image.v.8 leg1.i, r0, r1, WHITE
276
  end
277
  if guesses>5
278
    . r0=x, r0+118, r1=y, r1+228
279
    draw.image.v.8 leg2.i, r0, r1, WHITE
280
  end
281
  . alpha.bias=0
282
endf
283
 
284
function draw.hang.man
285
  . guesses=6
286
  draw.man
287
  . guesses=0
288
endf
289
 
290
function draw.winner
291
  locals x, y
292
  . alpha.bias=A.DARKER
293
  . x=40, y=62
294
  . r0=x, r0+80, r1=y, r1+40
295
  draw.image.v.8 head.i, r0, r1, WHITE
296
  . r0=x, r0+95, r1=y, r1+150
297
  draw.image.v.8 body.i, r0, r1, WHITE
298
  . r0=x, r0+67, r1=y, r1+87
299
  draw.image.viy.8 arm1.i, r0, r1, WHITE
300
  . r0=x, r0+147, r1=y, r1+87
301
  draw.image.viy.8 arm2.i, r0, r1, WHITE
302
  . r0=x, r0+87, r1=y, r1+228
303
  draw.image.v.8 leg1.i, r0, r1, WHITE
304
  . r0=x, r0+118, r1=y, r1+228
305
  draw.image.v.8 leg2.i, r0, r1, WHITE
306
  . alpha.bias=A.LIGHT
307
  draw.image.v.8 smile.i, 547, 307, WHITE
308
  draw.image.v.8 smile.i, 310, 307, WHITE
309
  . alpha.bias=A.DARK
310
  draw.image.v.8 money.i, 547, 342, 0DDFFDDh
311
  draw.image.v.8 prize.i, 367, 377, WHITE
312
  . alpha.bias=0
313
endf
314
 
315
function draw.title.scene
316
  draw.my.text help.t, 237, 100
317
  draw.hang.man
318
endf
319
 
320
function draw.play.scene
321
  draw.message
322
  draw.alphabet
323
  draw.man
324
endf
325
 
326
function draw.game.over
327
  locals p
328
  get p=is.winner
329
  draw.my.text p, 362, 308
330
  draw.message
331
  if p=winner.t
332
    draw.winner
333
  else
334
    draw.hang.man
335
  end
336
endf
337
 
338
function on.draw
339
  clear.screen 283D25h
340
  draw.image.8 board.i, 0, 0
341
  . alpha.bias=A.DARKER
342
  draw.image.v.8 logo.i, 240, 32, WHITE
343
  . alpha.bias=0
344
  if scene=SCENE.TITLE
345
    draw.title.scene
346
  else.if scene=SCENE.PLAY
347
    draw.play.scene
348
  else.if scene=SCENE.GAME.OVER
349
    draw.game.over
350
  end
351
  draw.image close.i, 663, 28
352
endf
353
 
354
function on.key
355
  if key.event='k'
356
    if scene=SCENE.PLAY
357
      insert.c key
358
    else.if scene=SCENE.TITLE
359
      reset.game
360
      . scene=SCENE.PLAY
361
    else.if scene=SCENE.GAME.OVER
362
      reset.game
363
      . scene=SCENE.TITLE
364
    end
365
    render
366
  end
367
endf
368
 
369
function on.mouse
370
  if mouse.event='c'
371
    . r0=&close.i.x
372
    if.select r0
373
      exit
374
    end
375
    if scene=SCENE.TITLE
376
      reset.game
377
      . scene=SCENE.PLAY
378
    else.if scene=SCENE.GAME.OVER
379
      reset.game
380
      . scene=SCENE.TITLE
381
    end
382
    render
383
  end
384
endf
385
 
386
function on.timer
387
  ; ...
388
endf
389
 
390
function on.exit
391
  ; ...
392
endf