Mercurial > rinkhals
annotate gamelib/gameboard.py @ 35:8f6c6a54a099
Fixed gameboard display.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 12:40:05 +0000 |
parents | 2e88c680672c |
children | 5569430fd82e |
rev | line source |
---|---|
35 | 1 import pygame |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
2 from pygame.locals import MOUSEBUTTONDOWN |
35 | 3 from pgu import gui |
9 | 4 |
5 import data | |
24
7584453f4944
Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
17
diff
changeset
|
6 import tiles |
9 | 7 |
35 | 8 SELL_CHICKEN = None |
9 SELL_EGG = None | |
10 BUY_FENCE = 2 | |
11 BUY_HENHOUSE = 3 | |
12 | |
13 | |
14 class ToolBar(gui.Table): | |
15 def __init__(self, gameboard, **params): | |
16 gui.Table.__init__(self, **params) | |
17 self.gameboard = gameboard | |
18 self.add_tool_button("Sell chicken", SELL_CHICKEN) | |
19 self.add_tool_button("Sell egg", SELL_EGG) | |
20 self.add_tool_button("Buy fence", BUY_FENCE) | |
21 self.add_tool_button("Buy henhouse", BUY_HENHOUSE) | |
22 | |
23 def add_tool_button(self, text, tool): | |
24 style = { | |
25 "padding_bottom": 15, | |
26 } | |
27 td_kwargs = { | |
28 "style": style, | |
29 "width": self.gameboard.TOOLBAR_WIDTH, | |
30 } | |
31 button = gui.Button(text) | |
32 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool)) | |
33 self.tr() | |
34 self.td(button, **td_kwargs) | |
35 | |
36 | |
37 class VidWidget(gui.Widget): | |
38 def __init__(self, gameboard, vid, **params): | |
39 gui.Widget.__init__(self, **params) | |
40 self.gameboard = gameboard | |
41 self.vid = vid | |
42 self.width = params.get('width', 0) | |
43 self.height = params.get('height', 0) | |
44 | |
45 def paint(self, surface): | |
46 self.vid.paint(surface) | |
47 | |
48 def update(self, surface): | |
49 offset = surface.get_offset() | |
50 return [r.move(offset) for r in self.vid.update(surface)] | |
51 | |
52 def resize(self, width=0, height=0): | |
53 if width is not None: | |
54 self.width = width | |
55 if height is not None: | |
56 self.height = height | |
57 return self.width, self.height | |
58 | |
59 def event(self, e): | |
60 if e.type == MOUSEBUTTONDOWN: | |
61 self.gameboard.use_tool(e) | |
62 | |
63 | |
64 class DispTable(gui.Table): | |
65 def __init__(self, gameboard, **params): | |
66 gui.Table.__init__(self, **params) | |
67 self.gameboard = gameboard | |
68 self.tr() | |
69 self.td(self.gameboard.tools, width=self.gameboard.TOOLBAR_WIDTH) | |
70 self.td(self.gameboard.vidwidget) | |
71 | |
9 | 72 |
73 class GameBoard(object): | |
74 TILE_DIMENSIONS = (20, 20) | |
35 | 75 TOOLBAR_WIDTH = 140 |
9 | 76 |
77 def __init__(self): | |
24
7584453f4944
Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
17
diff
changeset
|
78 self.tv = tiles.FarmVid() |
9 | 79 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
|
80 self.tv.png_folder_load_tiles(data.filepath('tiles')) |
9 | 81 self.tv.tga_load_level(data.filepath('level1.tga')) |
35 | 82 self.selected_tool = None |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
83 self.chickens = [] |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
84 self.foxes = [] |
35 | 85 self.create_disp() |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
86 |
35 | 87 def create_disp(self): |
88 width, height = pygame.display.get_surface().get_size() | |
89 self.tools = ToolBar(self) | |
90 self.vidwidget = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height) | |
91 self.disp = gui.App() | |
92 c = gui.Container(align=0, valign=0) | |
93 tbl = DispTable(self) | |
94 c.add(tbl, 0, 0) | |
95 self.disp.init(c) | |
14 | 96 |
97 def split_screen(self, screen): | |
98 leftbar_rect = screen.get_rect() | |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
99 leftbar_rect.width = self.TOOLBAR_WIDTH |
14 | 100 main_rect = screen.get_rect() |
101 main_rect.width -= leftbar_rect.width | |
102 main_rect.left += leftbar_rect.width | |
103 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect) | |
104 | |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
105 def paint(self, screen): |
35 | 106 self.disp.paint(screen) |
14 | 107 |
35 | 108 def update_subscreen(self, vid, subsurface): |
14 | 109 offset = subsurface.get_offset() |
110 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
|
111 |
9 | 112 def update(self, screen): |
35 | 113 return self.disp.update(screen) |
9 | 114 |
115 def loop(self): | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
116 self.tv.loop() |
9 | 117 |
35 | 118 def set_selected_tool(self, tool): |
119 self.selected_tool = tool | |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
120 |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
121 def use_tool(self, e): |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
122 if self.selected_tool is None: |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
123 return |
35 | 124 # pos = self.tv.screen_to_tile((e.pos[0] - self.TOOLBAR_WIDTH, e.pos[1])) |
125 pos = self.tv.screen_to_tile(e.pos) | |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
126 self.tv.set(pos, self.selected_tool) |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
127 |
14 | 128 def event(self, e): |
35 | 129 self.disp.event(e) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
130 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
131 def clear_foxes(self): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
132 for fox in self.foxes: |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
133 self.tv.sprites.remove(fox) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
134 self.foxes = [] # Remove all the foxes |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
135 |
29 | 136 def move_foxes(self): |
137 for fox in self.foxes: | |
138 fox.move(self) | |
139 | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
140 def add_chicken(self, chicken): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
141 self.chickens.append(chicken) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
142 self.tv.sprites.append(chicken) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
143 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
144 def add_fox(self, fox): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
145 self.foxes.append(fox) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
146 self.tv.sprites.append(fox) |
29 | 147 |
148 def remove_fox(self, fox): | |
149 if fox in self.foxes: | |
150 self.foxes.remove(fox) | |
151 self.tv.sprites.remove(fox) | |
152 | |
153 def remove_chicken(self, chick): | |
154 if chick in self.chickens: | |
155 self.chickens.remove(chick) | |
156 self.tv.sprites.remove(chick) |