annotate gamelib/gameboard.py @ 25:6d6ab0c1479d

Add placing some chickens and foxes
author Neil Muller <drnlmuller@gmail.com>
date Sun, 30 Aug 2009 18:14:07 +0000
parents 7584453f4944
children 2e88c680672c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
1 import random
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
2
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
3 from pygame.locals import MOUSEBUTTONDOWN
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
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
7
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
8
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
9 class GameBoard(object):
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
10 TILE_DIMENSIONS = (20, 20)
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
11 TOOLBAR_WIDTH = 22
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
12
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
13 def __init__(self):
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
14 self.tv = tiles.FarmVid()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
15 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
16 self.tv.png_folder_load_tiles(data.filepath('tiles'))
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17 self.tv.tga_load_level(data.filepath('level1.tga'))
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
18
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
19 self.tools = tiles.FarmVid()
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
20 self.tools.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
21 self.tools.png_folder_load_tiles(data.filepath('tiles'))
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
22 self.populate_toolbar()
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
23 self.chickens = []
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
24 self.foxes = []
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
25
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
26 self.selected_tool = None
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
27
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
28 def populate_toolbar(self):
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
29 self.tools.resize((1, 2))
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
30 self.tools.set((0,0), 2)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
31 self.tools.set((0,1), 3)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
32
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
33 def split_screen(self, screen):
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
34 leftbar_rect = screen.get_rect()
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
35 leftbar_rect.width = self.TOOLBAR_WIDTH
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
36 main_rect = screen.get_rect()
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
37 main_rect.width -= leftbar_rect.width
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
38 main_rect.left += leftbar_rect.width
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
39 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
40
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
41 def paint(self, screen):
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
42 leftbar, main = self.split_screen(screen)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
43 self.tools.paint(leftbar)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
44 self.tv.paint(main)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
45
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
46 def update_vid(self, vid, subsurface):
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
47 offset = subsurface.get_offset()
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
48 return [r.move(offset) for r in vid.update(subsurface)]
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
49
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
50 def update(self, screen):
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
51 leftbar, main = self.split_screen(screen)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
52 updates = []
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
53 updates.extend(self.update_vid(self.tools, leftbar))
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
54 updates.extend(self.update_vid(self.tv, main))
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
55 return updates
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
56
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
57 def loop(self):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
58 self.tv.loop()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
59
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
60 def select_tool(self, e):
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
61 tool_pos = self.tools.screen_to_tile(e.pos)
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
62 if tool_pos[1] < 2:
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
63 self.selected_tool = self.tools.get(tool_pos)
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
64 else:
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
65 self.selected_tool = None
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
66
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
67 def use_tool(self, e):
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
68 if self.selected_tool is None:
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
69 return
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
70 pos = self.tv.screen_to_tile((e.pos[0] - self.TOOLBAR_WIDTH, e.pos[1]))
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
71 self.tv.set(pos, self.selected_tool)
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
72
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
73 def event(self, e):
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
74 if e.type == MOUSEBUTTONDOWN:
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
75 if e.pos[0] < self.TOOLBAR_WIDTH:
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
76 self.select_tool(e)
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
77 else:
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
78 self.use_tool(e)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
79
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
80 def clear_foxes(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
81 for fox in self.foxes:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
82 self.tv.sprites.remove(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
83 self.foxes = [] # Remove all the foxes
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
84
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
85 def add_chicken(self, chicken):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
86 self.chickens.append(chicken)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
87 self.tv.sprites.append(chicken)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
88
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
89 def add_fox(self, fox):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
90 self.foxes.append(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
91 self.tv.sprites.append(fox)