Changeset 35:8f6c6a54a099


Ignore:
Timestamp:
Aug 31, 2009, 12:40:05 PM (11 years ago)
Author:
Jeremy Thurgood <firxen@…>
Branch:
default
Convert:
svn:b4e93282-eac8-4b8b-b765-0f5d36de2b68@36
Message:

Fixed gameboard display.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gamelib/gameboard.py

    r29 r35  
    1 import random
    2 
     1import pygame
    32from pygame.locals import MOUSEBUTTONDOWN
     3from pgu import gui
    44
    55import data
    66import tiles
    77
     8SELL_CHICKEN = None
     9SELL_EGG = None
     10BUY_FENCE = 2
     11BUY_HENHOUSE = 3
     12
     13
     14class 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
     37class 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
     64class 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
    872
    973class GameBoard(object):
    1074    TILE_DIMENSIONS = (20, 20)
    11     TOOLBAR_WIDTH = 22
     75    TOOLBAR_WIDTH = 140
    1276
    1377    def __init__(self):
     
    1680        self.tv.png_folder_load_tiles(data.filepath('tiles'))
    1781        self.tv.tga_load_level(data.filepath('level1.tga'))
    18 
    19         self.tools = tiles.FarmVid()
    20         self.tools.tga_load_tiles(data.filepath('tiles.tga'), self.TILE_DIMENSIONS)
    21         self.tools.png_folder_load_tiles(data.filepath('tiles'))
    22         self.populate_toolbar()
     82        self.selected_tool = None
    2383        self.chickens = []
    2484        self.foxes = []
     85        self.create_disp()
    2586
    26         self.selected_tool = None
    27 
    28     def populate_toolbar(self):
    29         self.tools.resize((1, 2))
    30         self.tools.set((0,0), 2)
    31         self.tools.set((0,1), 3)
     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)
    3296
    3397    def split_screen(self, screen):
     
    40104
    41105    def paint(self, screen):
    42         leftbar, main = self.split_screen(screen)
    43         self.tools.paint(leftbar)
    44         self.tv.paint(main)
     106        self.disp.paint(screen)
    45107
    46     def update_vid(self, vid, subsurface):
     108    def update_subscreen(self, vid, subsurface):
    47109        offset = subsurface.get_offset()
    48110        return [r.move(offset) for r in vid.update(subsurface)]
    49111
    50112    def update(self, screen):
    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
     113        return self.disp.update(screen)
    56114
    57115    def loop(self):
    58116        self.tv.loop()
    59117
    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
     118    def set_selected_tool(self, tool):
     119        self.selected_tool = tool
    66120
    67121    def use_tool(self, e):
    68122        if self.selected_tool is None:
    69123            return
    70         pos = self.tv.screen_to_tile((e.pos[0] - self.TOOLBAR_WIDTH, e.pos[1]))
     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)
    71126        self.tv.set(pos, self.selected_tool)
    72127
    73128    def event(self, e):
    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)
     129        self.disp.event(e)
    79130
    80131    def clear_foxes(self):
Note: See TracChangeset for help on using the changeset viewer.