Radomir Dopieralski avatar Radomir Dopieralski committed 03aeafa

Added a basic start screen

Comments (0)

Files changed (8)

jelly/__init__.py

 from jelly import display
 from jelly import mobs
 from jelly import rooms_level
+from jelly import message
 
 
 class Game(object):
     def __init__(self):
         self.display = display.Display()
         self.clock = pygame.time.Clock()
+
+    def run(self):
+        self.start()
+        self.main()
+
+    def start(self):
+        self.display.render_image('jelly/title.png')
+        exit = False
+        self.display.sprites.add(message.Text('Press ESC'))
+        while not exit:
+            self.clock.tick(15)
+            self.display.update()
+            self.display.draw()
+            for event in pygame.event.get():
+                if event.type == pg.ACTIVEEVENT and event.gain:
+                    self.display.refresh()
+                if event.type == pg.QUIT or (
+                        event.type == pg.KEYDOWN and
+                        event.key == pg.K_ESCAPE):
+                    exit = True
+
+    def main(self):
         self.game_over = False
-
         self.pc = mobs.Player()
         self.level = rooms_level.Level(self.pc)
         self.display.render_level(self.level)
-
-    def main(self):
         while not self.game_over:
             self.clock.tick(15)
             self.display.update()
             self.display.draw()
             for event in pygame.event.get():
+                if event.type == pg.ACTIVEEVENT and event.gain:
+                    self.display.refresh()
                 if event.type == pg.QUIT or (
                         event.type == pg.KEYDOWN and
                         event.key == pg.K_ESCAPE):
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
-import pkgutil
-try:
-    import cStringIO as StringIO
-except ImportError:
-    import StringIO
 import random
 import weakref
 
 from pygame import locals as pg
 
 from jelly import sprites
+from jelly import resource
 
 
 class SortedUpdates(pygame.sprite.RenderUpdates):
 class Display(object):
     def __init__(self):
         pygame.display.init()
+        pygame.font.init()
+        pygame.display.set_caption('Jelly')
         self.screen = pygame.display.set_mode((640, 480))
-        pygame.display.set_caption('Jelly')
         self.first_draw = True
-        self.tiles = load_tiles('jelly.png', 32, 32)
+        self.tiles = resource.load_tiles('jelly.png', 32, 32)
         for tile in self.tiles[36:40]:
             tile.set_alpha(192)
         for tile in self.tiles[46:50]:
         for step, tile in enumerate(self.tiles[16:20]):
             tile.set_alpha(255-step*32)
 
-        self.background = pygame.Surface(self.screen.get_size())
+    def render_image(self, image_file):
+        self.first_draw = True
+        self.fog = None
+        self.background = pygame.image.load(image_file).convert()
+        self.sprites = SortedUpdates()
+        self.shadows = pygame.sprite.RenderUpdates()
+        self.lights = pygame.sprite.LayeredDirty()
+
+    def render_level(self, level):
+        self.first_draw = True
         self.fog = pygame.Surface(self.screen.get_size(), pg.SRCALPHA)
         self.fog.fill((64, 128, 255, 255))
         self._grad(self.fog, self.fog.get_rect(),
                 (64, 128, 255), (16, 64, 224))
         self.memory = self.fog.copy()
+        self.sprites = SortedUpdates()
+        self.shadows = pygame.sprite.RenderUpdates()
         self.dirty = []
         self.mob_sprites = weakref.WeakValueDictionary()
         self.mob_lights = weakref.WeakValueDictionary()
-
-        self.sprites = SortedUpdates()
-        self.shadows = pygame.sprite.RenderUpdates()
         self.lights = pygame.sprite.LayeredDirty()
 
-    def render_level(self, level):
         self.background = render_background(level, self.tiles)
 
     def light_room(self, mob, room):
             sg += dg
             sb += db
 
+    def refresh(self):
+        self.screen.blit(self.background, (0, 0))
+        self.shadows.draw(self.screen)
+        self.sprites.draw(self.screen)
+        if self.fog:
+            self.lights.draw(self.fog)
+            self.screen.blit(self.fog, (0, 0))
+        pygame.display.update()
+        self.dirty = []
+
     def draw(self):
         if self.first_draw:
-            self.screen.blit(self.background, (0, 0))
-            self.lights.draw(self.fog)
-            self.shadows.draw(self.screen)
-            self.sprites.draw(self.screen)
-            self.screen.blit(self.fog, (0, 0))
-            self.dirty = [self.background.get_rect()]
+            self.refresh()
             self.first_draw = False
         else:
-            self.lights.clear(self.fog, self.memory)
-            visible = self.lights.draw(self.fog)
-            self.dirty.extend(visible)
-            for rect in self.dirty:
-                self.screen.blit(self.background, rect.topleft, rect)
-            self.shadows.draw(self.screen)
-            self.sprites.draw(self.screen)
-            for rect in self.dirty:
-                self.screen.blit(self.fog, rect.topleft, rect)
-        pygame.display.update(self.dirty)
-        self.dirty = []
+            if self.fog:
+                self.lights.clear(self.fog, self.memory)
+                visible = self.lights.draw(self.fog)
+                self.dirty.extend(visible)
+                for rect in self.dirty:
+                    self.screen.blit(self.background, rect.topleft, rect)
+                self.shadows.draw(self.screen)
+                self.sprites.draw(self.screen)
+                for rect in self.dirty:
+                    self.screen.blit(self.fog, rect.topleft, rect)
+            else:
+                self.sprites.clear(self.screen, self.background)
+                self.shadows.draw(self.screen)
+                self.dirty.extend(self.sprites.draw(self.screen))
+            pygame.display.update(self.dirty)
+            self.dirty = []
 
     def update(self):
         self.sprites.update()
             sprite.set_animation(sprite.spawner_animation())
 
 
-
-def load_tiles(filename, width, height):
-    f = StringIO.StringIO(pkgutil.get_data('jelly', filename))
-    image = pygame.image.load(f, filename).convert()
-    image_width, image_height = image.get_size()
-    tiles = []
-    for x in range(image_width/width):
-        for y in range(image_height/height):
-            rect = (x * width, y * height, width, height)
-            tiles.append(image.subsurface(rect))
-    return tiles
-
 def render_background(level, tiles):
     background = pygame.Surface((level.width * 20 + 20, level.height * 12 + 20))
     background.fill((8, 32, 112))
                 background.blit(tiles[6], pos)
             elif square == 2:
                 background.blit(tiles[7], pos)
-            elif square == 5:
-                background.blit(tiles[8], pos)
 
     return background
 
Add a comment to this file

jelly/jelly.png

Old
Old image
New
New image
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import pygame
+
+from jelly import resource
+
+
+class Text(pygame.sprite.Sprite):
+    font = None
+
+    def __init__(self, text):
+        super(Text, self).__init__()
+        if self.font is None:
+            self.font = resource.load_font('pf_ronda_seven_bold.ttf')
+        self.image = self.outlined_text(text, (255, 255, 255), (0, 0, 0))
+        self.rect = self.image.get_rect()
+
+    def outlined_text(self, text, color, border_color):
+        font = self.font
+        notcolor = [c^0xFF for c in border_color]
+        base = font.render(text, 0, border_color, notcolor)
+        size = base.get_width() + 2, base.get_height() + 2
+        image = pygame.Surface(size, 16)
+        image.fill(notcolor)
+        base.set_colorkey(0)
+        image.blit(base, (0, 0))
+        image.blit(base, (2, 0))
+        image.blit(base, (0, 2))
+        image.blit(base, (2, 2))
+        base.set_colorkey(0)
+        base.set_palette_at(1, color)
+        image.blit(base, (1, 1))
+        image.set_colorkey(notcolor)
+        return image
+
Add a comment to this file

jelly/pf_ronda_seven_bold.ttf

Binary file added.

jelly/resource.py

+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import pkgutil
+import tempfile
+try:
+    import cStringIO as StringIO
+except ImportError:
+    import StringIO
+
+import pygame
+
+
+def load_tiles(filename, width, height):
+    image = load_image(filename)
+    image_width, image_height = image.get_size()
+    tiles = []
+    for x in range(image_width/width):
+        for y in range(image_height/height):
+            rect = (x * width, y * height, width, height)
+            tiles.append(image.subsurface(rect))
+    return tiles
+
+
+def load_image(filename):
+    f = StringIO.StringIO(pkgutil.get_data('jelly', filename))
+    return pygame.image.load(f, filename).convert()
+
+
+def load_font(filename):
+    # XXX the "right" way causes segmentation fault
+    # f = StringIO.StringIO(pkgutil.get_data('jelly', filename))
+    with tempfile.NamedTemporaryFile() as f:
+        data = pkgutil.get_data('jelly', filename)
+        f.write(data)
+        f.seek(0)
+        font = pygame.font.Font(f.name, 8)
+    return font
+
 
 from jelly import Game
 
-Game().main()
+Game().run()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.