Browse code
split up main.py into smaller files for classes
edwlan authored on 26/07/2013 08:21:48
Showing 6 changed files
Showing 6 changed files
... | ... |
@@ -1,152 +1,20 @@ |
1 | 1 |
import libtcodpy as tc |
2 | 2 |
import numpy as np |
3 | 3 |
|
4 |
-class Player(object): |
|
5 |
- @property |
|
6 |
- def pos(self): |
|
7 |
- return self.x, self.y |
|
8 |
- |
|
9 |
- def __init__(self, x,y, map): |
|
10 |
- self.x = x |
|
11 |
- self.y = y |
|
12 |
- self.char = ord('@') |
|
13 |
- self.color = tc.Color(255,255,255) |
|
14 |
- self.map = map |
|
15 |
- |
|
16 |
- def draw(self): |
|
17 |
- self.map.add(self) |
|
18 |
- |
|
19 |
- def move(self, dx, dy): |
|
20 |
- print self.pos, |
|
21 |
- dx, dy = self.map.move(self, dx,dy) |
|
22 |
- self.x += dx |
|
23 |
- self.y += dy |
|
24 |
- print self.pos |
|
25 |
- |
|
26 |
-class ArrowHandler(object): |
|
27 |
- def __init__(self, player, eh): |
|
28 |
- self.player = player |
|
29 |
- eh.register(tc.KEY_LEFT,self.left) |
|
30 |
- eh.register(tc.KEY_RIGHT,self.right) |
|
31 |
- eh.register(tc.KEY_UP,self.up) |
|
32 |
- eh.register(tc.KEY_DOWN,self.down) |
|
33 |
- def left(self): |
|
34 |
- print 'left' |
|
35 |
- self.player.move(-1, 0) |
|
36 |
- def right(self): |
|
37 |
- print 'right' |
|
38 |
- self.player.move(1, 0) |
|
39 |
- def up(self): |
|
40 |
- print 'up' |
|
41 |
- self.player.move(0, -1) |
|
42 |
- def down(self): |
|
43 |
- print 'down' |
|
44 |
- self.player.move(0, 1) |
|
45 |
- |
|
46 |
-class Console(object): |
|
47 |
- # TBI: must have a self.con member with the console to be drawn on |
|
48 |
- pass |
|
49 |
- |
|
50 |
-class Screen(object): |
|
51 |
- def __init__(self, w, h): |
|
52 |
- self.width = w |
|
53 |
- self.height = h |
|
54 |
- self.con = 0 |
|
55 |
- def init(self, title, fullscreen=False): |
|
56 |
- tc.console_init_root(self.width, self.height, title, fullscreen, 2) |
|
57 |
- return self |
|
58 |
- |
|
59 |
-def squeeze(val, low, high): |
|
60 |
- return min(max(val, low), high) |
|
61 |
- |
|
62 |
-class Map(object): |
|
63 |
- def __init__(self, w, h): |
|
64 |
- self.width = w |
|
65 |
- self.height = h |
|
66 |
- self.map = np.random.random_integers(ord('a'), ord('z'), (w,h) ) |
|
67 |
- self.map[3,5] = ord('*') |
|
68 |
- self.map[::10, ::10] = ord('#') |
|
69 |
- self.fgcolors = np.random.random_integers(100,255, (w,h,3) ) |
|
70 |
- self.fgcolors[::10,::10] = (0,0,0) |
|
71 |
- self.bgcolors = np.random.random_integers(0,100, (w,h,3) ) |
|
72 |
- self.bgcolors[::10,::10] = (255,255,255) |
|
73 |
- self.overlays = {} # (x,y): { set( object, ... ) } |
|
74 |
- |
|
75 |
- def add(self, object): |
|
76 |
- self.overlays.setdefault(object.pos,[]).append(object) |
|
77 |
- |
|
78 |
- def move(self, object, dx,dy): |
|
79 |
- print self.overlays, |
|
80 |
- self.overlays[object.pos].remove(object) |
|
81 |
- ox,oy = object.pos |
|
82 |
- x = squeeze(ox+dx, 0, self.width) |
|
83 |
- y = squeeze(oy+dy, 0, self.height) |
|
84 |
- self.overlays.setdefault((x,y), []).append(object) |
|
85 |
- self.update_overlay(ox,oy) |
|
86 |
- return x-ox, y-oy |
|
87 |
- |
|
88 |
- def update_overlay(self, x=None, y=None): |
|
89 |
- if x is None or y is None: pass |
|
90 |
- else: |
|
91 |
- if (x,y) in self.overlays and self.overlays[x,y] == []: |
|
92 |
- self.overlays.pop((x,y)) |
|
93 |
- |
|
94 |
- def get_rgb(self, fg=True,slices=(slice(0),slice(0))): |
|
95 |
- if fg: |
|
96 |
- result = np.rollaxis(self.fgcolors[slices], 2) |
|
97 |
- else: |
|
98 |
- result = np.rollaxis(self.bgcolors[slices], 2) |
|
99 |
- return [x.transpose() for x in result] |
|
100 |
- |
|
101 |
- def draw(self, con, tl=(0,0)): |
|
102 |
- br = tl[0]+con.width, tl[1]+con.height |
|
103 |
- slices = slice(tl[0], tl[0]+con.width), slice(tl[1], tl[1]+con.height) |
|
104 |
- tc.console_fill_foreground(con.con, *self.get_rgb(slices=slices)) |
|
105 |
- tc.console_fill_background(con.con, *self.get_rgb(False,slices=slices)) |
|
106 |
- |
|
107 |
- chars = np.copy(self.map[slices]) |
|
108 |
- for x,y in self.overlays: |
|
109 |
- screen_x = x-tl[0] |
|
110 |
- screen_y = y-tl[1] |
|
111 |
- if 0 <= screen_x < con.width and 0 <= screen_y < con.height: |
|
112 |
- obj = self.overlays[x,y][-1] |
|
113 |
- chars[screen_x,screen_y] = obj.char |
|
114 |
- tc.console_set_char_foreground(con.con, screen_x,screen_y, tc.black) |
|
115 |
- tc.console_set_char_background(con.con, screen_x,screen_y, tc.white) |
|
116 |
- tc.console_fill_char(con.con, chars.transpose()) |
|
117 |
- |
|
118 |
-class EventHandler(object): |
|
119 |
- def __init__(self): |
|
120 |
- self.key = tc.Key() |
|
121 |
- self.mouse = tc.Mouse() |
|
122 |
- self.cbs = {} |
|
123 |
- |
|
124 |
- def register(self, event, cb): |
|
125 |
- self.cbs.setdefault(event,[]).append(cb) |
|
126 |
- |
|
127 |
- def tick(self): |
|
128 |
- tc.sys_check_for_event(tc.EVENT_KEY_PRESS|tc.EVENT_MOUSE|tc.KEY_PRESSED, |
|
129 |
- self.key, self.mouse |
|
130 |
- ) |
|
131 |
- |
|
132 |
- char = chr(self.key.c) |
|
133 |
- if char != '\x00' and char in self.cbs: |
|
134 |
- for cb in self.cbs[char]: |
|
135 |
- cb() |
|
136 |
- |
|
137 |
- elif self.key.vk in self.cbs: |
|
138 |
- for cb in self.cbs[self.key.vk]: |
|
139 |
- cb() |
|
4 |
+from src import events |
|
5 |
+from src import player |
|
6 |
+from src import console |
|
7 |
+from src import map |
|
140 | 8 |
|
141 | 9 |
|
142 | 10 |
|
143 | 11 |
class Application(object): |
144 | 12 |
def __init__(self): |
145 |
- self.screen = Screen(200,125) |
|
146 |
- self.map = Map(200,125) |
|
147 |
- self.player = Player(4,5, self.map) |
|
148 |
- self.events = EventHandler() |
|
149 |
- ArrowHandler(self.player, self.events) |
|
13 |
+ self.screen = console.Screen(200,125) |
|
14 |
+ self.map = map.Map(200,125) |
|
15 |
+ self.player = player.Player(4,5, self.map) |
|
16 |
+ self.events = events.EventHandler() |
|
17 |
+ player.ArrowHandler(self.player, self.events) |
|
150 | 18 |
|
151 | 19 |
tc.sys_set_fps(60) |
152 | 20 |
|
154 | 22 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,14 @@ |
1 |
+import libtcodpy as tc |
|
2 |
+ |
|
3 |
+class Console(object): |
|
4 |
+ # TBI: must have a self.con member with the console to be drawn on |
|
5 |
+ pass |
|
6 |
+ |
|
7 |
+class Screen(object): |
|
8 |
+ def __init__(self, w, h): |
|
9 |
+ self.width = w |
|
10 |
+ self.height = h |
|
11 |
+ self.con = 0 |
|
12 |
+ def init(self, title, fullscreen=False): |
|
13 |
+ tc.console_init_root(self.width, self.height, title, fullscreen, 2) |
|
14 |
+ return self |
0 | 15 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,27 @@ |
1 |
+import libtcodpy as tc |
|
2 |
+ |
|
3 |
+class EventHandler(object): |
|
4 |
+ def __init__(self): |
|
5 |
+ self.key = tc.Key() |
|
6 |
+ self.mouse = tc.Mouse() |
|
7 |
+ self.cbs = {} |
|
8 |
+ |
|
9 |
+ def register(self, event, cb): |
|
10 |
+ self.cbs.setdefault(event,[]).append(cb) |
|
11 |
+ |
|
12 |
+ def tick(self): |
|
13 |
+ tc.sys_check_for_event(tc.EVENT_KEY_PRESS|tc.EVENT_MOUSE|tc.KEY_PRESSED, |
|
14 |
+ self.key, self.mouse |
|
15 |
+ ) |
|
16 |
+ |
|
17 |
+ char = chr(self.key.c) |
|
18 |
+ if char != '\x00' and char in self.cbs: |
|
19 |
+ for cb in self.cbs[char]: |
|
20 |
+ cb() |
|
21 |
+ |
|
22 |
+ elif self.key.vk in self.cbs: |
|
23 |
+ for cb in self.cbs[self.key.vk]: |
|
24 |
+ cb() |
|
25 |
+ |
|
26 |
+ |
|
27 |
+ |
0 | 28 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,61 @@ |
1 |
+import numpy as np |
|
2 |
+import libtcodpy as tc |
|
3 |
+ |
|
4 |
+def squeeze(val, low, high): |
|
5 |
+ return min(max(val, low), high) |
|
6 |
+ |
|
7 |
+class Map(object): |
|
8 |
+ def __init__(self, w, h): |
|
9 |
+ self.width = w |
|
10 |
+ self.height = h |
|
11 |
+ self.map = np.random.random_integers(ord('a'), ord('z'), (w,h) ) |
|
12 |
+ self.map[3,5] = ord('*') |
|
13 |
+ self.map[::10, ::10] = ord('#') |
|
14 |
+ self.fgcolors = np.random.random_integers(100,255, (w,h,3) ) |
|
15 |
+ self.fgcolors[::10,::10] = (0,0,0) |
|
16 |
+ self.bgcolors = np.random.random_integers(0,100, (w,h,3) ) |
|
17 |
+ self.bgcolors[::10,::10] = (255,255,255) |
|
18 |
+ self.overlays = {} # (x,y): { set( object, ... ) } |
|
19 |
+ |
|
20 |
+ def add(self, object): |
|
21 |
+ self.overlays.setdefault(object.pos,[]).append(object) |
|
22 |
+ |
|
23 |
+ def move(self, object, dx,dy): |
|
24 |
+ print self.overlays, |
|
25 |
+ self.overlays[object.pos].remove(object) |
|
26 |
+ ox,oy = object.pos |
|
27 |
+ x = squeeze(ox+dx, 0, self.width) |
|
28 |
+ y = squeeze(oy+dy, 0, self.height) |
|
29 |
+ self.overlays.setdefault((x,y), []).append(object) |
|
30 |
+ self.update_overlay(ox,oy) |
|
31 |
+ return x-ox, y-oy |
|
32 |
+ |
|
33 |
+ def update_overlay(self, x=None, y=None): |
|
34 |
+ if x is None or y is None: pass |
|
35 |
+ else: |
|
36 |
+ if (x,y) in self.overlays and self.overlays[x,y] == []: |
|
37 |
+ self.overlays.pop((x,y)) |
|
38 |
+ |
|
39 |
+ def get_rgb(self, fg=True,slices=(slice(0),slice(0))): |
|
40 |
+ if fg: |
|
41 |
+ result = np.rollaxis(self.fgcolors[slices], 2) |
|
42 |
+ else: |
|
43 |
+ result = np.rollaxis(self.bgcolors[slices], 2) |
|
44 |
+ return [x.transpose() for x in result] |
|
45 |
+ |
|
46 |
+ def draw(self, con, tl=(0,0)): |
|
47 |
+ br = tl[0]+con.width, tl[1]+con.height |
|
48 |
+ slices = slice(tl[0], tl[0]+con.width), slice(tl[1], tl[1]+con.height) |
|
49 |
+ tc.console_fill_foreground(con.con, *self.get_rgb(slices=slices)) |
|
50 |
+ tc.console_fill_background(con.con, *self.get_rgb(False,slices=slices)) |
|
51 |
+ |
|
52 |
+ chars = np.copy(self.map[slices]) |
|
53 |
+ for x,y in self.overlays: |
|
54 |
+ screen_x = x-tl[0] |
|
55 |
+ screen_y = y-tl[1] |
|
56 |
+ if 0 <= screen_x < con.width and 0 <= screen_y < con.height: |
|
57 |
+ obj = self.overlays[x,y][-1] |
|
58 |
+ chars[screen_x,screen_y] = obj.char |
|
59 |
+ tc.console_set_char_foreground(con.con, screen_x,screen_y, tc.black) |
|
60 |
+ tc.console_set_char_background(con.con, screen_x,screen_y, tc.white) |
|
61 |
+ tc.console_fill_char(con.con, chars.transpose()) |
0 | 62 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,44 @@ |
1 |
+import libtcodpy as tc |
|
2 |
+ |
|
3 |
+class Player(object): |
|
4 |
+ @property |
|
5 |
+ def pos(self): |
|
6 |
+ return self.x, self.y |
|
7 |
+ |
|
8 |
+ def __init__(self, x,y, map): |
|
9 |
+ self.x = x |
|
10 |
+ self.y = y |
|
11 |
+ self.char = ord('@') |
|
12 |
+ self.color = tc.Color(255,255,255) |
|
13 |
+ self.map = map |
|
14 |
+ |
|
15 |
+ def draw(self): |
|
16 |
+ self.map.add(self) |
|
17 |
+ |
|
18 |
+ def move(self, dx, dy): |
|
19 |
+ print self.pos, |
|
20 |
+ dx, dy = self.map.move(self, dx,dy) |
|
21 |
+ self.x += dx |
|
22 |
+ self.y += dy |
|
23 |
+ print self.pos |
|
24 |
+ |
|
25 |
+class ArrowHandler(object): |
|
26 |
+ def __init__(self, player, eh): |
|
27 |
+ self.player = player |
|
28 |
+ eh.register(tc.KEY_LEFT,self.left) |
|
29 |
+ eh.register(tc.KEY_RIGHT,self.right) |
|
30 |
+ eh.register(tc.KEY_UP,self.up) |
|
31 |
+ eh.register(tc.KEY_DOWN,self.down) |
|
32 |
+ def left(self): |
|
33 |
+ print 'left' |
|
34 |
+ self.player.move(-1, 0) |
|
35 |
+ def right(self): |
|
36 |
+ print 'right' |
|
37 |
+ self.player.move(1, 0) |
|
38 |
+ def up(self): |
|
39 |
+ print 'up' |
|
40 |
+ self.player.move(0, -1) |
|
41 |
+ def down(self): |
|
42 |
+ print 'down' |
|
43 |
+ self.player.move(0, 1) |
|
44 |
+ |