git.fiddlerwoaroof.com
Browse code

Added the player and allowed him to move around

edwlan authored on 26/07/2013 08:14:00
Showing 1 changed files
... ...
@@ -10,12 +10,38 @@ class Player(object):
10 10
 		self.x = x
11 11
 		self.y = y
12 12
 		self.char = ord('@')
13
-		self.color = (255,255,255)
13
+		self.color = tc.Color(255,255,255)
14 14
 		self.map = map
15
+
16
+	def draw(self):
17
+		self.map.add(self)
18
+
15 19
 	def move(self, dx, dy):
16
-		self.map.move(self, dx,dy)
20
+		print self.pos,
21
+		dx, dy = self.map.move(self, dx,dy)
17 22
 		self.x += dx
18 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)
19 45
 
20 46
 class Console(object):
21 47
 	# TBI: must have a self.con member with the console to be drawn on
... ...
@@ -30,37 +56,64 @@ class Screen(object):
30 56
 		tc.console_init_root(self.width, self.height, title, fullscreen, 2)
31 57
 		return self
32 58
 
59
+def squeeze(val, low, high):
60
+	return min(max(val, low), high)
61
+
33 62
 class Map(object):
34 63
 	def __init__(self, w, h):
35 64
 		self.width = w
36 65
 		self.height = h
37 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('#')
38 69
 		self.fgcolors = np.random.random_integers(100,255, (w,h,3) )
70
+		self.fgcolors[::10,::10] = (0,0,0)
39 71
 		self.bgcolors = np.random.random_integers(0,100, (w,h,3) )
40
-		overlays = {} # (x,y): { set( (char,fg), ... ) }
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)
41 77
 
42 78
 	def move(self, object, dx,dy):
43
-		overlays[object.pos].remove(object)
79
+		print self.overlays,
80
+		self.overlays[object.pos].remove(object)
44 81
 		ox,oy = object.pos
45
-		x = ox+dx
46
-		y = oy+dy
47
-		if 0 < x < self.width and 0 < y < self.height:
48
-			self.overlays[x,y] = object
49
-			return x,y
50
-		#elif 
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))
51 93
 
52 94
 	def get_rgb(self, fg=True,slices=(slice(0),slice(0))):
53 95
 		if fg:
54
-			return np.rollaxis(self.fgcolors[slices], 2)
96
+			result = np.rollaxis(self.fgcolors[slices], 2)
55 97
 		else:
56
-			return np.rollaxis(self.bgcolors[slices], 2)
98
+			result = np.rollaxis(self.bgcolors[slices], 2)
99
+		return [x.transpose() for x in result]
57 100
 
58 101
 	def draw(self, con, tl=(0,0)):
59 102
 		br = tl[0]+con.width, tl[1]+con.height
60
-		slices = tuple(map(slice, tl,br))
103
+		slices = slice(tl[0], tl[0]+con.width), slice(tl[1], tl[1]+con.height)
61 104
 		tc.console_fill_foreground(con.con, *self.get_rgb(slices=slices))
62 105
 		tc.console_fill_background(con.con, *self.get_rgb(False,slices=slices))
63
-		tc.console_fill_char(con.con, self.map[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())
64 117
 
65 118
 class EventHandler(object):
66 119
 	def __init__(self):
... ...
@@ -68,6 +121,9 @@ class EventHandler(object):
68 121
 		self.mouse = tc.Mouse()
69 122
 		self.cbs = {}
70 123
 
124
+	def register(self, event, cb):
125
+		self.cbs.setdefault(event,[]).append(cb)
126
+
71 127
 	def tick(self):
72 128
 		tc.sys_check_for_event(tc.EVENT_KEY_PRESS|tc.EVENT_MOUSE|tc.KEY_PRESSED,
73 129
 			self.key, self.mouse
... ...
@@ -86,21 +142,23 @@ class EventHandler(object):
86 142
 
87 143
 class Application(object):
88 144
 	def __init__(self):
89
-		self.screen = Screen(120,75)
90
-		self.map = Map(120,78)
91
-		self.player = Player(40,25, self.map)
145
+		self.screen = Screen(200,125)
146
+		self.map = Map(200,125)
147
+		self.player = Player(4,5, self.map)
92 148
 		self.events = EventHandler()
149
+		ArrowHandler(self.player, self.events)
93 150
 
94 151
 		tc.sys_set_fps(60)
95 152
 
96 153
 	def init(self):
97 154
 		self.screen.init("test")
155
+		self.player.draw()
98 156
 
99 157
 	def run(self):
100 158
 		while not tc.console_is_window_closed():
101 159
 			self.events.tick()
102 160
 			self.map.draw(self.screen)
103
-			tc.console_print(0, 0,0, '%d' % tc.sys_get_fps())
161
+			tc.console_print(0, 0,1, '%d' % tc.sys_get_fps())
104 162
 			tc.console_flush()
105 163
 
106 164