1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
extern mod term;
use term::{KeyCharacter,KeyEscape,KeyUp,KeyDown,KeyLeft,KeyRight};
fn term_app (body: &fn (r: &mut term::Term)) {
do term::ios::preserve {
let mut term = term::Term(true);
term.init_term_app();
body(&mut term);
}
}
fn draw_map (term: &mut term::Term, rows: uint, cols: uint) {
for uint::range(0, rows) |i| {
term.move(0, i);
term.write(str::repeat(".", cols));
}
}
fn draw_character (term: &mut term::Term, x: uint, y: uint) {
term.move(x, y);
term.write("@");
term.move(x, y);
}
fn draw_ground (term: &mut term::Term, x: uint, y: uint) {
term.move(x, y);
term.write(".");
}
fn main () {
let (cols, rows) = term::size();
do term_app |term| {
draw_map(term, rows, cols);
let mut (x, y) = (0u, 0u);
let mut cursor = true;
loop {
draw_character(term, x, y);
let k = match term.read() {
Some(key) => key,
None => break,
};
draw_ground(term, x, y);
match k {
KeyCharacter('q') | KeyEscape => { break }
KeyCharacter('h') | KeyLeft if x > 0 => { x -= 1 }
KeyCharacter('j') | KeyDown if y < rows - 1 => { y += 1 }
KeyCharacter('k') | KeyUp if y > 0 => { y -= 1 }
KeyCharacter('l') | KeyRight if x < cols - 1 => { x += 1 }
KeyCharacter(' ') => { term.cursor(cursor); cursor = !cursor }
_ => { }
}
}
}
}
|