annotate gamelib/gameboard.py @ 60:e2631c8e2cd6

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