annotate gamelib/gameboard.py @ 39:ec79aabe2bf1

Scroll game window with arrow keys.
author Jeremy Thurgood <firxen@gmail.com>
date Mon, 31 Aug 2009 14:19:13 +0000
parents 497b53b69280
children 678421bd58ee
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
1 import pygame
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
2 from pygame.locals import MOUSEBUTTONDOWN, KEYDOWN, K_UP, K_DOWN, K_LEFT, K_RIGHT
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
3 from pgu import gui
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
4
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
5 import data
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
6 import tiles
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
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
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
9 SELL_CHICKEN = None
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
10 SELL_EGG = None
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
11 BUY_FENCE = 2
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
12 BUY_HENHOUSE = 3
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
13
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
14
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
15 class ToolBar(gui.Table):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
16 def __init__(self, gameboard, **params):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
17 gui.Table.__init__(self, **params)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
18 self.gameboard = gameboard
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
19 self.add_tool_button("Sell chicken", SELL_CHICKEN)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
20 self.add_tool_button("Sell egg", SELL_EGG)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
21 self.add_tool_button("Buy fence", BUY_FENCE)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
22 self.add_tool_button("Buy henhouse", BUY_HENHOUSE)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
23
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
24 def add_tool_button(self, text, tool):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
25 button = gui.Button(text)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
26 button.connect(gui.CLICK, lambda: self.gameboard.set_selected_tool(tool))
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
27 self.tr()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
28 self.add(button)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
29
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
30
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
31 class VidWidget(gui.Widget):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
32 def __init__(self, gameboard, vid, **params):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
33 gui.Widget.__init__(self, **params)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
34 self.gameboard = gameboard
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
35 vid.bounds = pygame.Rect((0, 0), vid.tile_to_view(vid.size))
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
36 self.vid = vid
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
37 self.width = params.get('width', 0)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
38 self.height = params.get('height', 0)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
39
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
40 def paint(self, surface):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
41 self.vid.paint(surface)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
42
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
43 def update(self, surface):
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
44 return self.vid.update(surface)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
45
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
46 def resize(self, width=0, height=0):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
47 if width is not None:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
48 self.width = width
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
49 if height is not None:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
50 self.height = height
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
51 return self.width, self.height
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
52
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
53 def move_view(self, x, y):
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
54 self.vid.view.move_ip((x, y))
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
55
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
56 def event(self, e):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
57 if e.type == MOUSEBUTTONDOWN:
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
58 self.gameboard.use_tool(e)
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
59
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
60
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
61 class GameBoard(object):
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
62 TILE_DIMENSIONS = (20, 20)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
63 TOOLBAR_WIDTH = 140
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
64
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
65 def __init__(self):
24
7584453f4944 Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents: 17
diff changeset
66 self.tv = tiles.FarmVid()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
67 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
68 self.tv.png_folder_load_tiles(data.filepath('tiles'))
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
69 self.tv.tga_load_level(data.filepath('level1.tga'))
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
70 self.selected_tool = None
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
71 self.chickens = []
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
72 self.foxes = []
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
73 self.create_disp()
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
74
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
75 def create_disp(self):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
76 width, height = pygame.display.get_surface().get_size()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
77 tbl = gui.Table()
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
78 tbl.tr()
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
79 tbl.td(ToolBar(self), width=self.TOOLBAR_WIDTH)
37
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
80 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
81 tbl.td(self.tvw)
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
82 self.disp = gui.App()
36
5569430fd82e Display cleanup and rationalisation.
Jeremy Thurgood <firxen@gmail.com>
parents: 35
diff changeset
83 self.disp.init(tbl)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
84
12
8a7319e4853a Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 11
diff changeset
85 def paint(self, screen):
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
86 self.disp.paint(screen)
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
87
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
88 def update(self, screen):
37
497b53b69280 Always update the vidwidget.
Jeremy Thurgood <firxen@gmail.com>
parents: 36
diff changeset
89 self.tvw.reupdate()
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
90 return self.disp.update(screen)
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
91
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
92 def loop(self):
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
93 self.tv.loop()
9
3b045083631e Basic game board logic.
Jeremy Thurgood <firxen@gmail.com>
parents:
diff changeset
94
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
95 def set_selected_tool(self, tool):
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
96 self.selected_tool = tool
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
97
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
98 def use_tool(self, e):
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
99 if self.selected_tool is None:
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
100 return
35
8f6c6a54a099 Fixed gameboard display.
Jeremy Thurgood <firxen@gmail.com>
parents: 29
diff changeset
101 pos = self.tv.screen_to_tile(e.pos)
17
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
102 self.tv.set(pos, self.selected_tool)
cbbc5da7708a Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents: 14
diff changeset
103
14
d7f295c06a4b Split gameboard screen.
Jeremy Thurgood <firxen@gmail.com>
parents: 12
diff changeset
104 def event(self, e):
39
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
105 if e.type == KEYDOWN:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
106 if e.key == K_UP:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
107 self.tvw.move_view(0, -self.TILE_DIMENSIONS[1])
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
108 if e.key == K_DOWN:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
109 self.tvw.move_view(0, self.TILE_DIMENSIONS[1])
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
110 if e.key == K_LEFT:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
111 self.tvw.move_view(-self.TILE_DIMENSIONS[0], 0)
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
112 if e.key == K_RIGHT:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
113 self.tvw.move_view(self.TILE_DIMENSIONS[0], 0)
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
114 else:
ec79aabe2bf1 Scroll game window with arrow keys.
Jeremy Thurgood <firxen@gmail.com>
parents: 37
diff changeset
115 self.disp.event(e)
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
116
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
117 def clear_foxes(self):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
118 for fox in self.foxes:
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
119 self.tv.sprites.remove(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
120 self.foxes = [] # Remove all the foxes
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
121
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
122 def move_foxes(self):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
123 for fox in self.foxes:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
124 fox.move(self)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
125
25
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
126 def add_chicken(self, chicken):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
127 self.chickens.append(chicken)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
128 self.tv.sprites.append(chicken)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
129
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
130 def add_fox(self, fox):
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
131 self.foxes.append(fox)
6d6ab0c1479d Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents: 24
diff changeset
132 self.tv.sprites.append(fox)
29
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
133
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
134 def remove_fox(self, fox):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
135 if fox in self.foxes:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
136 self.foxes.remove(fox)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
137 self.tv.sprites.remove(fox)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
138
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
139 def remove_chicken(self, chick):
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
140 if chick in self.chickens:
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
141 self.chickens.remove(chick)
2e88c680672c Minimal fox raid logic
Neil Muller <drnlmuller@gmail.com>
parents: 25
diff changeset
142 self.tv.sprites.remove(chick)