Mercurial > rinkhals
annotate gamelib/gameboard.py @ 37:497b53b69280
Always update the vidwidget.
author | Jeremy Thurgood <firxen@gmail.com> |
---|---|
date | Mon, 31 Aug 2009 13:27:27 +0000 |
parents | 5569430fd82e |
children | ec79aabe2bf1 |
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 |
36
5569430fd82e
Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents:
35
diff
changeset
|
8 # FIXME: These should probably be proper events of some kind. |
35 | 9 SELL_CHICKEN = None |
10 SELL_EGG = None | |
11 BUY_FENCE = 2 | |
12 BUY_HENHOUSE = 3 | |
13 | |
14 | |
15 class ToolBar(gui.Table): | |
16 def __init__(self, gameboard, **params): | |
17 gui.Table.__init__(self, **params) | |
18 self.gameboard = gameboard | |
19 self.add_tool_button("Sell chicken", SELL_CHICKEN) | |
20 self.add_tool_button("Sell egg", SELL_EGG) | |
21 self.add_tool_button("Buy fence", BUY_FENCE) | |
22 self.add_tool_button("Buy henhouse", BUY_HENHOUSE) | |
23 | |
24 def add_tool_button(self, text, tool): | |
25 button = gui.Button(text) | |
26 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool)) | |
27 self.tr() | |
36
5569430fd82e
Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents:
35
diff
changeset
|
28 self.add(button) |
35 | 29 |
30 | |
31 class VidWidget(gui.Widget): | |
32 def __init__(self, gameboard, vid, **params): | |
33 gui.Widget.__init__(self, **params) | |
34 self.gameboard = gameboard | |
35 self.vid = vid | |
36 self.width = params.get('width', 0) | |
37 self.height = params.get('height', 0) | |
38 | |
39 def paint(self, surface): | |
40 self.vid.paint(surface) | |
41 | |
42 def update(self, surface): | |
36
5569430fd82e
Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents:
35
diff
changeset
|
43 return self.vid.update(surface) |
35 | 44 |
45 def resize(self, width=0, height=0): | |
46 if width is not None: | |
47 self.width = width | |
48 if height is not None: | |
49 self.height = height | |
50 return self.width, self.height | |
51 | |
52 def event(self, e): | |
53 if e.type == MOUSEBUTTONDOWN: | |
54 self.gameboard.use_tool(e) | |
55 | |
56 | |
9 | 57 class GameBoard(object): |
58 TILE_DIMENSIONS = (20, 20) | |
35 | 59 TOOLBAR_WIDTH = 140 |
9 | 60 |
61 def __init__(self): | |
24
7584453f4944
Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
17
diff
changeset
|
62 self.tv = tiles.FarmVid() |
9 | 63 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
|
64 self.tv.png_folder_load_tiles(data.filepath('tiles')) |
9 | 65 self.tv.tga_load_level(data.filepath('level1.tga')) |
35 | 66 self.selected_tool = None |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
67 self.chickens = [] |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
68 self.foxes = [] |
35 | 69 self.create_disp() |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
70 |
35 | 71 def create_disp(self): |
72 width, height = pygame.display.get_surface().get_size() | |
36
5569430fd82e
Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents:
35
diff
changeset
|
73 tbl = gui.Table() |
5569430fd82e
Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents:
35
diff
changeset
|
74 tbl.tr() |
5569430fd82e
Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents:
35
diff
changeset
|
75 tbl.td(ToolBar(self), width=self.TOOLBAR_WIDTH) |
37
497b53b69280
Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents:
36
diff
changeset
|
76 self.tvw = VidWidget(self, self.tv, width=width-self.TOOLBAR_WIDTH, height=height) |
497b53b69280
Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents:
36
diff
changeset
|
77 tbl.td(self.tvw) |
35 | 78 self.disp = gui.App() |
36
5569430fd82e
Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents:
35
diff
changeset
|
79 self.disp.init(tbl) |
14 | 80 |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
81 def paint(self, screen): |
35 | 82 self.disp.paint(screen) |
14 | 83 |
9 | 84 def update(self, screen): |
37
497b53b69280
Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents:
36
diff
changeset
|
85 self.tvw.reupdate() |
35 | 86 return self.disp.update(screen) |
9 | 87 |
88 def loop(self): | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
89 self.tv.loop() |
9 | 90 |
35 | 91 def set_selected_tool(self, tool): |
92 self.selected_tool = tool | |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
93 |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
94 def use_tool(self, e): |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
95 if self.selected_tool is None: |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
96 return |
35 | 97 pos = self.tv.screen_to_tile(e.pos) |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
98 self.tv.set(pos, self.selected_tool) |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
99 |
14 | 100 def event(self, e): |
35 | 101 self.disp.event(e) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
102 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
103 def clear_foxes(self): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
104 for fox in self.foxes: |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
105 self.tv.sprites.remove(fox) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
106 self.foxes = [] # Remove all the foxes |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
107 |
29 | 108 def move_foxes(self): |
109 for fox in self.foxes: | |
110 fox.move(self) | |
111 | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
112 def add_chicken(self, chicken): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
113 self.chickens.append(chicken) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
114 self.tv.sprites.append(chicken) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
115 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
116 def add_fox(self, fox): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
117 self.foxes.append(fox) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
118 self.tv.sprites.append(fox) |
29 | 119 |
120 def remove_fox(self, fox): | |
121 if fox in self.foxes: | |
122 self.foxes.remove(fox) | |
123 self.tv.sprites.remove(fox) | |
124 | |
125 def remove_chicken(self, chick): | |
126 if chick in self.chickens: | |
127 self.chickens.remove(chick) | |
128 self.tv.sprites.remove(chick) |