Mercurial > rinkhals
annotate gamelib/gameboard.py @ 25:6d6ab0c1479d
Add placing some chickens and foxes
author | Neil Muller <drnlmuller@gmail.com> |
---|---|
date | Sun, 30 Aug 2009 18:14:07 +0000 |
parents | 7584453f4944 |
children | 2e88c680672c |
rev | line source |
---|---|
9 | 1 import random |
2 | |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
3 from pygame.locals import MOUSEBUTTONDOWN |
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 |
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): | |
24
7584453f4944
Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
17
diff
changeset
|
14 self.tv = tiles.FarmVid() |
9 | 15 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
|
16 self.tv.png_folder_load_tiles(data.filepath('tiles')) |
9 | 17 self.tv.tga_load_level(data.filepath('level1.tga')) |
18 | |
24
7584453f4944
Add support for PNG tiles.
Simon Cross <hodgestar@gmail.com>
parents:
17
diff
changeset
|
19 self.tools = tiles.FarmVid() |
14 | 20 self.tools.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
|
21 self.tools.png_folder_load_tiles(data.filepath('tiles')) |
14 | 22 self.populate_toolbar() |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
23 self.chickens = [] |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
24 self.foxes = [] |
14 | 25 |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
26 self.selected_tool = None |
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
27 |
14 | 28 def populate_toolbar(self): |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
29 self.tools.resize((1, 2)) |
14 | 30 self.tools.set((0,0), 2) |
31 self.tools.set((0,1), 3) | |
32 | |
33 def split_screen(self, screen): | |
34 leftbar_rect = screen.get_rect() | |
17
cbbc5da7708a
Interaction with the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
14
diff
changeset
|
35 leftbar_rect.width = self.TOOLBAR_WIDTH |
14 | 36 main_rect = screen.get_rect() |
37 main_rect.width -= leftbar_rect.width | |
38 main_rect.left += leftbar_rect.width | |
39 return screen.subsurface(leftbar_rect), screen.subsurface(main_rect) | |
40 | |
12
8a7319e4853a
Hooked in the game board.
Jeremy Thurgood <firxen@gmail.com>
parents:
11
diff
changeset
|
41 def paint(self, screen): |
14 | 42 leftbar, main = self.split_screen(screen) |
43 self.tools.paint(leftbar) | |
44 self.tv.paint(main) | |
45 | |
46 def update_vid(self, vid, subsurface): | |
47 offset = subsurface.get_offset() | |
48 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
|
49 |
9 | 50 def update(self, screen): |
14 | 51 leftbar, main = self.split_screen(screen) |
52 updates = [] | |
53 updates.extend(self.update_vid(self.tools, leftbar)) | |
54 updates.extend(self.update_vid(self.tv, main)) | |
55 return updates | |
9 | 56 |
57 def loop(self): | |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
58 self.tv.loop() |
9 | 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) |
25
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
79 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
80 def clear_foxes(self): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
81 for fox in self.foxes: |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
82 self.tv.sprites.remove(fox) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
83 self.foxes = [] # Remove all the foxes |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
84 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
85 def add_chicken(self, chicken): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
86 self.chickens.append(chicken) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
87 self.tv.sprites.append(chicken) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
88 |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
89 def add_fox(self, fox): |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
90 self.foxes.append(fox) |
6d6ab0c1479d
Add placing some chickens and foxes
Neil Muller <drnlmuller@gmail.com>
parents:
24
diff
changeset
|
91 self.tv.sprites.append(fox) |