# HG changeset patch # User Jeremy Thurgood # Date 1336741080 -7200 # Node ID 53277724645b6053345ba6e60f6a3eabb486c9d5 # Parent 221c340f5efd39dcd98c157c77a99998396e9e52 Science button juggling. diff -r 221c340f5efd -r 53277724645b gamelib/data.py --- a/gamelib/data.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/data.py Fri May 11 14:58:00 2012 +0200 @@ -7,6 +7,9 @@ import os +from pygame import image + + data_py = os.path.abspath(os.path.dirname(__file__)) data_dir = os.path.normpath(os.path.join(data_py, '..', 'data')) @@ -24,3 +27,7 @@ "mode" is passed as the second arg to open(). ''' return open(filepath(filename), mode) + + +def load_image(filename): + return image.load(filepath(filename)) diff -r 221c340f5efd -r 53277724645b gamelib/game_base.py --- a/gamelib/game_base.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/game_base.py Fri May 11 14:58:00 2012 +0200 @@ -39,10 +39,16 @@ PREREQUISITES = () ACQUISITION_CHANCE = 1.0 SCIENCE_TYPE = None + IMAGE_NAME = None def __init__(self, points=0): self.points = points + def get_image_name(self): + if self.IMAGE_NAME is not None: + return self.IMAGE_NAME + return type(self).__name__.lower() + def spend_point(self): self.points += 1 diff -r 221c340f5efd -r 53277724645b gamelib/gamegui.py --- a/gamelib/gamegui.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/gamegui.py Fri May 11 14:58:00 2012 +0200 @@ -15,7 +15,7 @@ from gamelib.game_base import get_save_filename from gamelib.gui_base import (Window, TextLabel, TextBox, font_small, font_medium, font_large) -from gamelib.gui import BigButton, ImageDrawable +from gamelib.gui import BigButton, ImageDrawable, IconTextButton from gamelib.engine import PopWindow, AddWindow, GameOver from gamelib.constants import WIDTH, HEIGHT, FAILURE, SUCCESS, GAME_WIN, INFO from gamelib.gamestate import Game @@ -91,19 +91,21 @@ AddWindow.post(self.new_window) -class ScienceWidget(BigButton): - - WIDTH = 200 - - BG_IMAGE_NORMAL = image.load(filepath('images/science_normal.png')) - BG_IMAGE_DOWN = image.load(filepath('images/science_down.png')) +class ScienceWidget(IconTextButton): def __init__(self, science, pos, parent): self.science = science self.points = 0 self.parent = parent - super(ScienceWidget, self).__init__(pos, '%s: %d' % (science.NAME, - science.points), font_small) + super(ScienceWidget, self).__init__( + pos, science.get_image_name(), self.make_text(), font_small) + + def make_text(self): + name = self.science.NAME[:4] + text = '%s: %d' % (name, self.science.points) + if self.points > 0: + text = "%s + %d" % (text, self.points) + return text def on_click(self): if (self.parent.game.get_available_points() > 0 and @@ -114,11 +116,7 @@ self.set_text() def set_text(self): - if self.points > 0: - self.text = '%s: %d + %d' % (self.science.NAME, - self.science.points, self.points) - else: - self.text = '%s: %d' % (self.science.NAME, self.science.points) + self.text = self.make_text() self._draw_text() def reset(self): diff -r 221c340f5efd -r 53277724645b gamelib/gui.py --- a/gamelib/gui.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/gui.py Fri May 11 14:58:00 2012 +0200 @@ -1,5 +1,3 @@ -from pygame import image - from gamelib import data from gamelib.gui_base import Drawable, TextButton, font_auto, ToggleButton @@ -17,8 +15,8 @@ class BigButton(TextButton): WIDTH = 128 HEIGHT = 64 - BG_IMAGE_NORMAL = image.load(data.filepath('images/button_normal.png')) - BG_IMAGE_DOWN = image.load(data.filepath('images/button_down.png')) + BG_IMAGE_NORMAL = data.load_image('images/button_normal.png') + BG_IMAGE_DOWN = data.load_image('images/button_down.png') def __init__(self, pos, text, font=font_auto, shadow=True): rect1 = (0, 0, self.WIDTH, self.HEIGHT) @@ -31,13 +29,13 @@ class IconButton(ToggleButton): WIDTH = 64 HEIGHT = 64 - BG_IMAGE_NORMAL = image.load(data.filepath('images/research_normal.png')) - BG_IMAGE_DOWN = image.load(data.filepath('images/research_down.png')) + BG_IMAGE_NORMAL = data.load_image('images/research_normal.png') + BG_IMAGE_DOWN = data.load_image('images/research_down.png') def __init__(self, pos, name): rect = (pos[0], pos[1], self.WIDTH, self.HEIGHT) - n_icon = image.load(data.filepath('images/icons/%s_normal.png' % name)) - d_icon = image.load(data.filepath('images/icons/%s_down.png' % name)) + n_icon = data.load_image('images/icons/%s_normal.png' % name) + d_icon = data.load_image('images/icons/%s_down.png' % name) n = ImageDrawable(rect, self.BG_IMAGE_NORMAL.copy()) n.image.blit(n_icon, (0, 0)) d = ImageDrawable(rect, self.BG_IMAGE_DOWN.copy()) @@ -46,6 +44,30 @@ super(IconButton, self).__init__(rect2, n, d) +class IconTextButton(TextButton): + WIDTH = 128 + HEIGHT = 64 + + TEXT_OFFSET = (50, 0) + + def __init__(self, pos, name, text, font=font_auto, shadow=True): + rect = (pos[0], pos[1], self.WIDTH, self.HEIGHT) + n = self._mk_image(name, 'normal') + d = self._mk_image(name, 'down') + text_rect = (self.TEXT_OFFSET, + (self.WIDTH - self.TEXT_OFFSET[0], + self.HEIGHT - self.TEXT_OFFSET[1])) + super(IconTextButton, self).__init__( + rect, n, d, text, font, shadow, text_rect) + + def _mk_image(self, name, suffix): + bg = data.load_image('images/button_%s.png' % (suffix,)) + icon = data.load_image('images/icons/%s_%s.png' % (name, suffix)) + drawable = ImageDrawable((0, 0, self.WIDTH, self.HEIGHT), bg.copy()) + drawable.image.blit(icon, (0, 0)) + return drawable + + class RadioButton(IconButton): SELECTED_BUTTON = None diff -r 221c340f5efd -r 53277724645b gamelib/gui_base.py --- a/gamelib/gui_base.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/gui_base.py Fri May 11 14:58:00 2012 +0200 @@ -121,7 +121,6 @@ self.state = -1 self.states = {} self.drawables = [] - self.surface = Surface((self.rect[2], self.rect[3])) def draw(self, surface): if self.state != -1 and self.drawables[self.state]: @@ -157,9 +156,11 @@ class TextButton(Button): def __init__(self, rect, normal_drawable, down_drawable, text, font, - shadow): + shadow, text_rect=None): super(TextButton, self).__init__(rect, normal_drawable, down_drawable) - self.surface = Surface((rect[2], rect[3]), SRCALPHA) + self.text_rect = Rect((0, 0), self.rect.size) + if text_rect is not None: + self.text_rect = Rect(*text_rect) self.text = text if font is font_auto: font = self._auto_font() @@ -170,7 +171,7 @@ def _auto_font(self): for font in (font_large, font_medium): h, w = font.size(self.text) - if w < self.rect.width and h < self.rect.height: + if w < self.text_rect.width and h < self.text_rect.height: return font return font_small @@ -186,15 +187,15 @@ temp.blit(self.text_surface, (0, 0)) self.text_surface = temp size = [s + 2 for s in size] - self.text_offset = ((self.rect[2] - size[0]) / 2, - (self.rect[3] - size[1]) / 2) + self.text_offset = Rect((0, 0), size) + self.text_offset.center = self.text_rect.center self.font.set_bold(False) def draw(self, surface): - self.surface.fill((0, 0, 0, 0)) - super(TextButton, self).draw(self.surface) - self.surface.blit(self.text_surface, self.text_offset) - surface.blit(self.surface, self.rect) + temp_surface = Surface(self.rect.size, SRCALPHA) + super(TextButton, self).draw(temp_surface) + temp_surface.blit(self.text_surface, self.text_offset) + surface.blit(temp_surface, self.rect) class ToggleButton(Button): diff -r 221c340f5efd -r 53277724645b gamelib/mainmenu.py --- a/gamelib/mainmenu.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/mainmenu.py Fri May 11 14:58:00 2012 +0200 @@ -15,7 +15,7 @@ from gamelib import data from gamelib.gui_base import Window -from gamelib.gui import BigButton, RadioButton +from gamelib.gui import BigButton from gamelib.engine import AddWindow from gamelib.gamegui import LabWindow @@ -72,19 +72,6 @@ self.screen = screen self.background_colour = (0, 0, 0) self.background_image = image.load(data.filepath('images/temp.jpg')) - self.add_child(RadioButton((0, 0), 'rocketry')) - self.add_child(RadioButton((64, 0), 'marinebiology')) - self.add_child(RadioButton((128, 0), 'robotics')) - self.add_child(RadioButton((192, 0), 'artificialintelligence')) - self.add_child(RadioButton((256, 0), 'space')) - self.add_child(RadioButton((320, 0), 'fusion')) - self.add_child(RadioButton((384, 0), 'electrickery')) - self.add_child(RadioButton((448, 0), 'lasers')) - self.add_child(RadioButton((512, 0), 'meteorology')) - self.add_child(RadioButton((576, 0), 'meteorology2')) - self.add_child(RadioButton((640, 0), 'physics')) - self.add_child(RadioButton((704, 0), 'biogenetics')) - self.add_child(RadioButton((768, 0), 'medicalexperiments')) button1 = NewGameButton(self) self.add_child(button1) button2 = QuitButton() diff -r 221c340f5efd -r 53277724645b gamelib/research.py --- a/gamelib/research.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/research.py Fri May 11 14:58:00 2012 +0200 @@ -11,6 +11,8 @@ class Psychology(ResearchArea): NAME = "Psychology" + # FIXME: + IMAGE_NAME = "fusion" class MedicalExperiments(ResearchArea): diff -r 221c340f5efd -r 53277724645b gamelib/schematics.py --- a/gamelib/schematics.py Fri May 11 10:15:40 2012 +0200 +++ b/gamelib/schematics.py Fri May 11 14:58:00 2012 +0200 @@ -41,6 +41,9 @@ BASE_POWER = None POWER_INCREMENT = None + # FIXME: Icons for equipment. + IMAGE_NAME = "physics" + def can_spend(self, lab, spend): extra = self.UPGRADE_REQUIREMENT * self.points + spend return lab.meet_requirements(self, extra)