Debian Codegraph @

testsuite/systemtap.examples/stapgames/block.stp

raw
1 #!/usr/bin/env stap
2 # block breaker
3 # Copyright (C) 2008 Masami Hiramatsu <[email protected]>
4 #
5 # This file is free software and is distributed under the terms of the GNU
6 # General Public License (GPL); either version 2, or (at your option) any
7 # later version.
8
9 #TODO
10 # - count scores
11 # - reflection control
12 # - special items
13 # - more stages
14
15 global b, bx, by, bar, dx, dy, dbar, sbar
16
17 probe begin {
18 for (i = 1; i < 6; i++)
19 for (j = 0; j < 10; j++)
20 b[j,i] = 6 - i
21 bx = 20; by = 19;
22 dx = 1; dy = -1; dbar = 0
23 bar = 18
24
25 cursor_cls()
26 for (i = 1; i < 6; i++)
27 printf("|[%02d][%02d][%02d][%02d][%02d][%02d][%02d][%02d][%02d][%02d]|\n",
28 b[0,i], b[1,i], b[2,i], b[3,i], b[4,i], b[5,i],
29 b[6,i], b[7,i], b[8,i], b[9,i])
30 for (i = 5; i < 20; i++)
31 printf("|%40s|\n", " ");
32 cursor_move(bx+2, by); print("o")
33 cursor_move(bar+2, 20); print("=====")
34 }
35
36 probe game.kbd {
37 if (down) {
38 if (code == GM_KBD_RIGHT)
39 dbar = 1
40 else if (code == GM_KBD_LEFT)
41 dbar = -1
42 } else
43 sbar = 1
44 }
45
46 probe timer.ms(100) {
47 if (dbar*dbar < 9)
48 dbar *= 2
49 }
50
51 function update_block(x,y) {
52 b[x,y] --;
53 if (b[x,y] == 0) {
54 delete b[x,y]
55 cursor_move(x*4+2,y); print(" ")
56 } else {
57 cursor_move(x*4+2,y); printf("[%02d]", b[x,y])
58 }
59 }
60
61 function hit_block() {
62 x = bx/4; y = by
63 if (b[x,y]) {
64 update_block(x,y)
65 dy = -dy
66 dx = -dx
67 bx += dx; by += dy
68 return 0;
69 }
70 x = bx/4; y = by+dy
71 if (y <= 0 || b[x,y]) {
72 if (b[x,y])
73 update_block(x,y)
74 dy = -dy
75 }
76 y = by; x = (bx+dx)/4
77 if (bx+dx < 0 || x >= 10 || b[x,y]) {
78 if (b[x,y])
79 update_block(x,y)
80 dx = -dx
81 }
82 }
83
84 function no_blocks() {
85 foreach ([x,y] in b)
86 return 0
87 return 1
88 }
89
90 probe timer.ms(200) {
91 cursor_move(bx+2, by); print(" ")
92 cursor_move(bar+2, 20); print(" ")
93
94 bar+= dbar;
95 if (sbar) { dbar = 0; sbar = 0 }
96 if (bar <= 0) { bar = 0; dbar = 0}
97 if (bar >= 35) { bar = 35; dbar = 0}
98
99 bx += dx; by += dy
100 hit_block()
101 if (by == 19 && (bx >= bar && bx < bar + 5) ) {
102 dy = -dy;
103 }
104 if (by >= 21)
105 exit()
106
107 if (no_blocks()) {
108 cursor_move(15,19)
109 println("Stage clear!")
110 exit()
111 }
112 cursor_move(bx+2, by); print("o")
113 cursor_move(bar+2, 20); print("=====")
114 cursor_move(43,20)
115 }
116
117 probe end {
118 cursor_move(16, 10);
119 println("-GAME-OVER-")
120 cursor_move(1, 20);
121 }