changeset 1:65933bee61ee

Basic display loop
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 27 Apr 2013 13:41:42 +0200
parents 5fdb079f7206
children f701c97a27ba
files blow/display.py blow/game.py blow/hero.png blow/resources.py
diffstat 4 files changed, 85 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blow/display.py	Sat Apr 27 13:41:42 2013 +0200
@@ -0,0 +1,37 @@
+# -*- coding: utf-8 -*-
+
+import pygame
+
+
+class SortedUpdates(pygame.sprite.RenderUpdates):
+    def sprites(self):
+        return sorted(self.spritedict.keys(),
+                      key=lambda s: s.rect.bottom + getattr(s, 'depth', 0))
+
+
+
+class Display(object):
+    def __init__(self):
+        self.width = 480
+        self.height = 480
+        self.screen = pygame.display.set_mode((self.width, self.height))
+        self.background = self.screen.copy()
+        self.clock = pygame.time.Clock()
+        self.sprites = SortedUpdates()
+
+    def update(self, whole_screen=False):
+        if whole_screen:
+            self.screen.blit(self.background, (0, 0))
+            self.sprites.update()
+            self.sprites.draw(self.screen)
+            pygame.display.update()
+        else:
+            self.sprites.clear(self.screen, self.background)
+            self.sprites.update()
+            dirty = self.sprites.draw(self.screen)
+            pygame.display.update(dirty)
+        for event in pygame.event.get():
+            if event.type == pygame.QUIT:
+                raise SystemExit()
+        self.clock.tick(24)
+
--- a/blow/game.py	Sat Apr 27 12:54:22 2013 +0200
+++ b/blow/game.py	Sat Apr 27 13:41:42 2013 +0200
@@ -1,6 +1,18 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 
+from blow.display import Display
+from blow.resources import Resources
+
+
 class Game(object):
+    def __init__(self):
+        self.display = Display()
+        self.resources = Resources()
+        self.game_over = False
+
     def run(self):
-        pass
+        self.display.update(whole_screen=True)
+        while not self.game_over:
+            self.display.update()
+
Binary file blow/hero.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/blow/resources.py	Sat Apr 27 13:41:42 2013 +0200
@@ -0,0 +1,35 @@
+# -*- coding: utf-8 -*-
+
+import StringIO
+import pkgutil
+import pygame
+
+
+MODULE = 'blow'
+
+
+class Resources(object):
+    def __init__(self):
+        self.hero = self.load_atlas('hero.png', 64, 64)
+
+    @staticmethod
+    def load_sound(filename):
+        f = StringIO.StringIO(pkgutil.get_data(MODULE, filename))
+        return pygame.mixer.Sound(f)
+
+    @staticmethod
+    def load_image(filename):
+        f = StringIO.StringIO(pkgutil.get_data(MODULE, filename))
+        return pygame.image.load(f, filename)
+
+    @classmethod
+    def load_atlas(cls, filename, width=32, height=None):
+        image = cls.load_image(filename)
+        image_width, image_height = image.get_size()
+        w, h = width, height or width
+        return [
+            image.subsurface((x * w, y * h, w, h))
+            for y in xrange(0, image_height // h)
+            for x in xrange(0, image_width // w)
+        ]
+