annotate gamelib/gameboard.py @ 57:08665fa60345

Implement henhouses and henhouse adding.
author Simon Cross <hodgestar@gmail.com>
date Mon, 31 Aug 2009 18:34:10 +0000
parents b8f64db0d39e
children e2631c8e2cd6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
1 import pygame
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
2 from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
3 from pgu import gui
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5 import data
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
6 import tiles
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
7 import constants
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
8 import buildings
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
9
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
10
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
11 class OpaqueLabel(gui.Label):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
12 def paint(self, s):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
13 s.fill(self.style.background)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
14 gui.Label.paint(self, s)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
15
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
16 def update_value(self, value):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
17 self.value = value
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
18 self.style.width, self.style.height = self.font.size(self.value)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
19 self.repaint()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
20
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
21
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
22 class ToolBar(gui.Table):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
23 def __init__(self, gameboard, **params):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
24 gui.Table.__init__(self, **params)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
25 self.gameboard = gameboard
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
26 self.cash_counter = OpaqueLabel("Groats: ", color=constants.FG_COLOR)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
27 self.tr()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
28 self.add(self.cash_counter)
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
29 self.add_tool_button("Sell chicken", constants.TOOL_SELL_CHICKEN)
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
30 self.add_tool_button("Sell egg", constants.TOOL_SELL_EGG)
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
31 self.add_tool_button("Buy fence", constants.TOOL_BUY_FENCE)
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
32 self.add_tool_button("Buy henhouse", constants.TOOL_BUY_HENHOUSE)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
33
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
34 def update_cash_counter(self, amount):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
35 self.cash_counter.update_value("Groats: %s" % amount)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
36 self.repaint()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
37
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
38 def add_tool_button(self, text, tool):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
39 button = gui.Button(text)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
40 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool))
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
41 self.tr()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
42 self.add(button)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
43
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
44
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
45 class VidWidget(gui.Widget):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
46 def __init__(self, gameboard, vid, **params):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
47 gui.Widget.__init__(self, **params)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
48 self.gameboard = gameboard
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
49 vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size))
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
50 self.vid = vid
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
51 self.width = params.get('width', 0)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
52 self.height = params.get('height', 0)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
53
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
54 def paint(self, surface):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
55 self.vid.paint(surface)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
56
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
57 def update(self, surface):
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
58 return self.vid.update(surface)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
59
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
60 def resize(self, width=0, height=0):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
61 if width is not None:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
62 self.width = width
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
63 if height is not None:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
64 self.height = height
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
65 return self.width, self.height
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
66
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
67 def move_view(self, x, y):
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
68 self.vid.view.move_ip((x, y))
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
69
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
70 def event(self, e):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
71 if e.type == MOUSEBUTTONDOWN:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
72 self.gameboard.use_tool(e)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
73
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
74
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
75 class GameBoard(object):
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
76 TILE_DIMENSIONS = (20, 20)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
77 TOOLBAR_WIDTH = 140
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
78
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
79 def __init__(self):
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
80 self.tv = tiles.FarmVid()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
81 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
82 self.tv.png_folder_load_tiles(data.filepath('tiles'))
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
83 self.tv.tga_load_level(data.filepath('level1.tga'))
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
84 self.create_disp()
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
85
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
86 self.selected_tool = None
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
87 self.chickens = []
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
88 self.foxes = []
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
89 self.henhouses = []
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
90 self.cash = 0
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
91 self.add_cash(constants.STARTING_CASH)
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
92
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
93 def create_disp(self):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
94 width, height = pygame.display.get_surface().get_size()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
95 tbl = gui.Table()
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
96 tbl.tr()
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
97 self.toolbar = ToolBar(self)
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
98 tbl.td(self.toolbar, width=self.TOOLBAR_WIDTH)
37
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
99 self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height)
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
100 tbl.td(self.tvw)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
101 self.disp = gui.App()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
102 self.disp.init(tbl)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
103
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
104 def paint(self, screen):
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
105 self.disp.paint(screen)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
106
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
107 def update(self, screen):
37
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
108 self.tvw.reupdate()
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
109 return self.disp.update(screen)
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
110
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
111 def loop(self):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
112 self.tv.loop()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
113
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
114 def set_selected_tool(self, tool):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
115 self.selected_tool = tool
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
116
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
117 def use_tool(self, e):
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
118 if self.selected_tool == constants.TOOL_SELL_CHICKEN:
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
119 self.sell_chicken(e.pos)
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
120 elif self.selected_tool == constants.TOOL_SELL_EGG:
52
0d4799866bcf Sell chickens and buy fences.
Jeremy Thurgood <firxen@gmail.com>
parents: 40
diff changeset
121 pass
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
122 elif self.selected_tool == constants.TOOL_BUY_FENCE:
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
123 self.buy_fence(self.tv.screen_to_tile(e.pos))
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
124 elif self.selected_tool == constants.TOOL_BUY_HENHOUSE:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
125 self.buy_henhouse(self.tv.screen_to_tile(e.pos))
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
126
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
127 def get_chicken(self, pos):
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
128 for chick in self.chickens:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
129 if chick.rect.collidepoint(pos):
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
130 return chick
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
131 return None
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
132
54
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
133 def sell_chicken(self, pos):
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
134 chick = self.get_chicken(pos)
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
135 if chick is None:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
136 return
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
137 if len(self.chickens) == 1:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
138 print "You can't sell your last chicken!"
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
139 return
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
140 self.add_cash(constants.SELL_PRICE_CHICKEN)
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
141 self.remove_chicken(chick)
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
142
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
143 def buy_fence(self, tile_pos):
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
144 if self.tv.get(tile_pos) != tiles.REVERSE_TILE_MAP['grassland']:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
145 return
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
146 if self.cash < constants.BUY_PRICE_FENCE:
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
147 print "You can't afford a fence."
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
148 return
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
149 self.add_cash(-constants.BUY_PRICE_FENCE)
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
150 self.tv.set(tile_pos, tiles.REVERSE_TILE_MAP['fence'])
b8f64db0d39e Cleaned up buying and selling a bit.
Jeremy Thurgood <firxen@gmail.com>
parents: 52
diff changeset
151
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
152 def buy_henhouse(self, tile_pos):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
153 if self.cash < constants.BUY_PRICE_HENHOUSE:
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
154 return
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
155 henhouse = buildings.HenHouse(tile_pos)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
156 if henhouse.place(self.tv):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
157 self.add_cash(-constants.BUY_PRICE_HENHOUSE)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
158 self.add_henhouse(henhouse)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
159
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
160 def event(self, e):
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
161 if e.type == KEYDOWN:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
162 if e.key == K_UP:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
163 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1])
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
164 if e.key == K_DOWN:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
165 self.tvw.move_view(0, self.TILE_DIMENSIONS[1])
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
166 if e.key == K_LEFT:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
167 self.tvw.move_view(-self.TILE_DIMENSIONS[0], 0)
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
168 if e.key == K_RIGHT:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
169 self.tvw.move_view(self.TILE_DIMENSIONS[0], 0)
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
170 else:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
171 self.disp.event(e)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
172
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
173 def clear_foxes(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
174 for fox in self.foxes:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
175 self.tv.sprites.remove(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
176 self.foxes = [] # Remove all the foxes
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
177
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
178 def move_foxes(self):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
179 for fox in self.foxes:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
180 fox.move(self)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
181
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
182 def add_chicken(self, chicken):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
183 self.chickens.append(chicken)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
184 self.tv.sprites.append(chicken)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
185
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
186 def add_fox(self, fox):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
187 self.foxes.append(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
188 self.tv.sprites.append(fox)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
189
57
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
190 def add_henhouse(self, henhouse):
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
191 self.henhouses.append(henhouse)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
192 self.tv.sprites.append(henhouse)
08665fa60345 Implement henhouses and henhouse adding.
Simon Cross <hodgestar@gmail.com>
parents: 54
diff changeset
193
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
194 def remove_fox(self, fox):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
195 if fox in self.foxes:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
196 self.foxes.remove(fox)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
197 self.tv.sprites.remove(fox)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
198
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
199 def remove_chicken(self, chick):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
200 if chick in self.chickens:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
201 self.chickens.remove(chick)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
202 self.tv.sprites.remove(chick)
40
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
203
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
204 def add_cash(self, amount):
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
205 self.cash += amount
678421bd58ee Money counter with very ugly display hack.
Jeremy Thurgood <firxen@gmail.com>
parents: 39
diff changeset
206 self.toolbar.update_cash_counter(self.cash)