Mercurial > rinkhals
comparison 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 |
comparison
equal
deleted
inserted
replaced
16:10a2bf50e005 | 17:cbbc5da7708a |
---|---|
1 import random | 1 import random |
2 | 2 |
3 from pgu import gui, tilevid | 3 from pgu import gui, tilevid |
4 from pygame.locals import MOUSEBUTTONDOWN | |
4 | 5 |
5 import data | 6 import data |
6 | 7 |
7 | 8 |
8 class GameBoard(object): | 9 class GameBoard(object): |
9 TILE_DIMENSIONS = (20, 20) | 10 TILE_DIMENSIONS = (20, 20) |
11 TOOLBAR_WIDTH = 22 | |
10 | 12 |
11 def __init__(self): | 13 def __init__(self): |
12 self.tv = tilevid.Tilevid() | 14 self.tv = tilevid.Tilevid() |
13 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) | 15 self.tv.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) |
14 self.tv.tga_load_level(data.filepath('level1.tga')) | 16 self.tv.tga_load_level(data.filepath('level1.tga')) |
15 | 17 |
16 self.tools = tilevid.Tilevid() | 18 self.tools = tilevid.Tilevid() |
17 self.tools.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) | 19 self.tools.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS) |
18 self.tools.resize((1, 2)) | |
19 self.populate_toolbar() | 20 self.populate_toolbar() |
20 | 21 |
22 self.selected_tool = None | |
23 | |
21 def populate_toolbar(self): | 24 def populate_toolbar(self): |
25 self.tools.resize((1, 2)) | |
22 self.tools.set((0,0), 2) | 26 self.tools.set((0,0), 2) |
23 self.tools.set((0,1), 3) | 27 self.tools.set((0,1), 3) |
24 | 28 |
25 def split_screen(self, screen): | 29 def split_screen(self, screen): |
26 leftbar_rect = screen.get_rect() | 30 leftbar_rect = screen.get_rect() |
27 leftbar_rect.width = self.TILE_DIMENSIONS[0] + 2 | 31 leftbar_rect.width = self.TOOLBAR_WIDTH |
28 main_rect = screen.get_rect() | 32 main_rect = screen.get_rect() |
29 main_rect.width -= leftbar_rect.width | 33 main_rect.width -= leftbar_rect.width |
30 main_rect.left += leftbar_rect.width | 34 main_rect.left += leftbar_rect.width |
31 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect) | 35 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect) |
32 | 36 |
45 updates.extend(self.update_vid(self.tools, leftbar)) | 49 updates.extend(self.update_vid(self.tools, leftbar)) |
46 updates.extend(self.update_vid(self.tv, main)) | 50 updates.extend(self.update_vid(self.tv, main)) |
47 return updates | 51 return updates |
48 | 52 |
49 def loop(self): | 53 def loop(self): |
54 return | |
50 x = random.randint(0, self.tv.size[0]-1) | 55 x = random.randint(0, self.tv.size[0]-1) |
51 y = random.randint(0, self.tv.size[1]-1) | 56 y = random.randint(0, self.tv.size[1]-1) |
52 tile = random.randint(0, 4) | 57 tile = random.randint(0, 4) |
53 self.tv.set((x, y), tile) | 58 self.tv.set((x, y), tile) |
54 | 59 |
60 def select_tool(self, e): | |
61 tool_pos = self.tools.screen_to_tile(e.pos) | |
62 if tool_pos[1] < 2: | |
63 self.selected_tool = self.tools.get(tool_pos) | |
64 else: | |
65 self.selected_tool = None | |
66 | |
67 def use_tool(self, e): | |
68 if self.selected_tool is None: | |
69 return | |
70 pos = self.tv.screen_to_tile((e.pos[0] - self.TOOLBAR_WIDTH, e.pos[1])) | |
71 self.tv.set(pos, self.selected_tool) | |
72 | |
55 def event(self, e): | 73 def event(self, e): |
56 pass | 74 if e.type == MOUSEBUTTONDOWN: |
75 if e.pos[0] < self.TOOLBAR_WIDTH: | |
76 self.select_tool(e) | |
77 else: | |
78 self.use_tool(e) |