annotate gamelib/gameboard.py @ 17:cbbc5da7708a

Interaction with the game board.
author Jeremy Thurgood <firxen@gmail.com>
date Sun, 30 Aug 2009 17:15:23 +0000
parents d7f295c06a4b
children 7584453f4944
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
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
3 from pgu import gui, tilevid
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
4 from pygame.locals import MOUSEBUTTONDOWN
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
6 import data
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):
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
14 self.tv = tilevid.Tilevid()
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)
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
16 self.tv.tga_load_level(data.filepath('level1.tga'))
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
17
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
18 self.tools = tilevid.Tilevid()
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
19 self.tools.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
20 self.populate_toolbar()
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
21
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
22 self.selected_tool = None
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
23
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
24 def populate_toolbar(self):
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
25 self.tools.resize((1, 2))
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
26 self.tools.set((0,0), 2)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
27 self.tools.set((0,1), 3)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
28
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
29 def split_screen(self, screen):
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
30 leftbar_rect = screen.get_rect()
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
31 leftbar_rect.width = self.TOOLBAR_WIDTH
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
32 main_rect = screen.get_rect()
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
33 main_rect.width -= leftbar_rect.width
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
34 main_rect.left += leftbar_rect.width
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
35 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
36
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
37 def paint(self, screen):
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
38 leftbar, main = self.split_screen(screen)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
39 self.tools.paint(leftbar)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
40 self.tv.paint(main)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
41
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
42 def update_vid(self, vid, subsurface):
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
43 offset = subsurface.get_offset()
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
44 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
45
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
46 def update(self, screen):
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
47 leftbar, main = self.split_screen(screen)
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
48 updates = []
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
49 updates.extend(self.update_vid(self.tools, leftbar))
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
50 updates.extend(self.update_vid(self.tv, main))
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
51 return updates
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
52
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
53 def loop(self):
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
54 return
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
55 x = random.randint(0, self.tv.size[0]-1)
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
56 y = random.randint(0, self.tv.size[1]-1)
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
57 tile = random.randint(0, 4)
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
58 self.tv.set((x, y), tile)
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)