comparison gamelib/gameboard.py @ 52:0d4799866bcf

Sell chickens and buy fences.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 31 Aug 2009 17:37:20 +0000
parents 678421bd58ee
children b8f64db0d39e
comparison
equal deleted inserted replaced
51:32fb395cf71c 52:0d4799866bcf
3 from pgu import gui 3 from pgu import gui
4 4
5 import data 5 import data
6 import tiles 6 import tiles
7 import constants 7 import constants
8
9 # FIXME: These should probably be proper events of some kind.
10 SELL_CHICKEN = None
11 SELL_EGG = None
12 BUY_FENCE = 2
13 BUY_HENHOUSE = 3
14 8
15 9
16 class OpaqueLabel(gui.Label): 10 class OpaqueLabel(gui.Label):
17 def paint(self, s): 11 def paint(self, s):
18 s.fill(self.style.background) 12 s.fill(self.style.background)
29 gui.Table.__init__(self, **params) 23 gui.Table.__init__(self, **params)
30 self.gameboard = gameboard 24 self.gameboard = gameboard
31 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR) 25 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR)
32 self.tr() 26 self.tr()
33 self.add(self.cash_counter) 27 self.add(self.cash_counter)
34 self.add_tool_button("Sell chicken", SELL_CHICKEN) 28 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN)
35 self.add_tool_button("Sell egg", SELL_EGG) 29 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG)
36 self.add_tool_button("Buy fence", BUY_FENCE) 30 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE)
37 self.add_tool_button("Buy henhouse", BUY_HENHOUSE) 31 self.add_tool_button("Buy henhouse", constants.TOOL_BUY_HENHOUSE)
38 32
39 def update_cash_counter(self, amount): 33 def update_cash_counter(self, amount):
40 self.cash_counter.update_value("Groats: %s" % amount) 34 self.cash_counter.update_value("Groats: %s" % amount)
41 self.repaint() 35 self.repaint()
42 36
90 84
91 self.selected_tool = None 85 self.selected_tool = None
92 self.chickens = [] 86 self.chickens = []
93 self.foxes = [] 87 self.foxes = []
94 self.cash = 0 88 self.cash = 0
89 self.add_cash(constants.STARTING_CASH)
95 90
96 def create_disp(self): 91 def create_disp(self):
97 width, height = pygame.display.get_surface().get_size() 92 width, height = pygame.display.get_surface().get_size()
98 tbl = gui.Table() 93 tbl = gui.Table()
99 tbl.tr() 94 tbl.tr()
113 108
114 def loop(self): 109 def loop(self):
115 self.tv.loop() 110 self.tv.loop()
116 111
117 def set_selected_tool(self, tool): 112 def set_selected_tool(self, tool):
118 if tool is None:
119 self.add_cash(10)
120 self.selected_tool = tool 113 self.selected_tool = tool
121 114
122 def use_tool(self, e): 115 def use_tool(self, e):
123 if self.selected_tool is None: 116 if self.selected_tool == constants.TOOL_SELL_CHICKEN:
124 return 117 for chick in self.chickens:
125 pos = self.tv.screen_to_tile(e.pos) 118 if chick.rect.collidepoint(e.pos):
126 self.tv.set(pos, self.selected_tool) 119 if len(self.chickens) == 1:
120 print "Can't sell your last chicken!"
121 else:
122 self.add_cash(constants.SELL_PRICE_CHICKEN)
123 self.remove_chicken(chick)
124 break
125 if self.selected_tool == constants.TOOL_SELL_EGG:
126 pass
127 if self.selected_tool == constants.TOOL_BUY_FENCE:
128 tile_pos = self.tv.screen_to_tile(e.pos)
129 if (self.cash >= constants.BUY_PRICE_FENCE and
130 self.tv.get(tile_pos) == tiles.REVERSE_TILE_MAP['grassland']):
131 self.add_cash(-constants.BUY_PRICE_FENCE)
132 self.tv.set(tile_pos, tiles.REVERSE_TILE_MAP['fence'])
133 if self.selected_tool == constants.TOOL_BUY_HENHOUSE:
134 pass
127 135
128 def event(self, e): 136 def event(self, e):
129 if e.type == KEYDOWN: 137 if e.type == KEYDOWN:
130 if e.key == K_UP: 138 if e.key == K_UP:
131 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1]) 139 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1])