changeset 38:047273a63054

Main menu says hello
author Stefano Rivera <stefano@rivera.za.net>
date Sun, 11 Sep 2011 14:57:11 +0200
parents 9a0ae29c7f5e
children 3ab5097e8757
files mamba/engine.py mamba/habitats/mainmenu.py mamba/widgets/base.py mamba/widgets/text.py
diffstat 4 files changed, 22 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- 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):
--- 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'))
--- 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"
--- 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)