# HG changeset patch # User Stefano Rivera # Date 1315745831 -7200 # Node ID 047273a6305492db370944c67b2dd7a879ed5843 # Parent 9a0ae29c7f5e3622e3ba9b4cfd43f30370f5a824 Main menu says hello diff -r 9a0ae29c7f5e -r 047273a63054 mamba/engine.py --- a/mamba/engine.py Sun Sep 11 14:49:07 2011 +0200 +++ b/mamba/engine.py Sun Sep 11 14:57:11 2011 +0200 @@ -1,8 +1,11 @@ """Game engine and top-level game loop.""" +from mamba.constants import SCREEN from mamba.widgets.base import Container + import pygame.event import pygame.display +import pygame from pygame.locals import QUIT, USEREVENT @@ -31,13 +34,16 @@ class Habitat(object): def __init__(self): + self.surface = pygame.Surface(SCREEN) self.container = Container() def dispatch(self, ev): self.container.event(ev) def draw(self, surface): - self.container.draw(surface) + self.surface.fill(pygame.Color('black')) + self.container.draw(self.surface) + surface.blit(self.surface, self.surface.get_rect()) class UserEvent(object): diff -r 9a0ae29c7f5e -r 047273a63054 mamba/habitats/mainmenu.py --- a/mamba/habitats/mainmenu.py Sun Sep 11 14:49:07 2011 +0200 +++ b/mamba/habitats/mainmenu.py Sun Sep 11 14:57:11 2011 +0200 @@ -1,8 +1,11 @@ """Main menu.""" from mamba.engine import Habitat +from mamba.widgets.text import TextWidget class MainMenu(Habitat): - # TODO: make something interesting happen here. - pass + + def __init__(self): + super(MainMenu, self).__init__() + self.container.add(TextWidget((20, 20), "Hello", color='white')) diff -r 9a0ae29c7f5e -r 047273a63054 mamba/widgets/base.py --- a/mamba/widgets/base.py Sun Sep 11 14:49:07 2011 +0200 +++ b/mamba/widgets/base.py Sun Sep 11 14:57:11 2011 +0200 @@ -4,7 +4,8 @@ class Widget(object): def __init__(self, rect): - self.rect = pygame.Rect(rect) + if not isinstance(rect, pygame.Rect): + rect = pygame.Rect(rect[0], rect[1], 0, 0) def event(self, event): "Override me" diff -r 9a0ae29c7f5e -r 047273a63054 mamba/widgets/text.py --- a/mamba/widgets/text.py Sun Sep 11 14:49:07 2011 +0200 +++ b/mamba/widgets/text.py Sun Sep 11 14:57:11 2011 +0200 @@ -3,10 +3,11 @@ from mamba.widgets.base import Widget from mamba.data import filepath -class Text(Widget): +class TextWidget(Widget): fontcache = {} + def __init__(self, rect, text, fontsize=16, color='black'): - super(Widget, self).__init(rect) + super(TextWidget, self).__init__(rect) self.text = text self.fontsize = fontsize self.color = color @@ -15,14 +16,14 @@ def prepare(self): self.fontname = 'DejaVuSans.ttf' font = (self.fontname, self.fontsize) - if font not in Text.fontcache: + if font not in TextWidget.fontcache: fontfn = filepath('fonts/' + self.fontname) - Text.fontcache[font] = pygame.font.Font(fontfn, self.fontsize) - self.font = Text.fontcache[font] + TextWidget.fontcache[font] = pygame.font.Font(fontfn, self.fontsize) + self.font = TextWidget.fontcache[font] if not isinstance(self.color, pygame.Color): self.color = pygame.Color(self.color) self.surface = self.font.render(self.text, True, self.color) self.rect = self.surface.get_rect() - def do_draw(self, surface): - surface.blit(self.surface, self.pos) + def draw(self, surface): + surface.blit(self.surface, self.rect)