Mercurial > rinkhals
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 |
rev | line source |
---|---|
9 | 1 import random |
2 | |
14 | 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 | 5 |
6 import data | |
7 | |
8 | |
9 class GameBoard(object): | |
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 | 12 |
13 def __init__(self): | |
14 self.tv = tilevid.Tilevid() | |
15 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) | |
16 self.tv.tga_load_level(data.filepath('level1.tga')) | |
17 | |
14 | 18 self.tools = tilevid.Tilevid() |
19 self.tools.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) | |
20 self.populate_toolbar() | |
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 | 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 | 26 self.tools.set((0,0), 2) |
27 self.tools.set((0,1), 3) | |
28 | |
29 def split_screen(self, screen): | |
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 | 32 main_rect = screen.get_rect() |
33 main_rect.width -= leftbar_rect.width | |
34 main_rect.left += leftbar_rect.width | |
35 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect) | |
36 | |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
37 def paint(self, screen): |
14 | 38 leftbar, main = self.split_screen(screen) |
39 self.tools.paint(leftbar) | |
40 self.tv.paint(main) | |
41 | |
42 def update_vid(self, vid, subsurface): | |
43 offset = subsurface.get_offset() | |
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 | 46 def update(self, screen): |
14 | 47 leftbar, main = self.split_screen(screen) |
48 updates = [] | |
49 updates.extend(self.update_vid(self.tools, leftbar)) | |
50 updates.extend(self.update_vid(self.tv, main)) | |
51 return updates | |
9 | 52 |
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 | 57 tile = random.randint(0, 4) |
58 self.tv.set((x, y), tile) | |
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 | 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) |