changeset 0:9ba84a825287

Initial commit
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sat, 05 May 2012 22:27:12 +0200
parents
children 0593652a8283
files .hgignore __main__.py mhrok/__init__.py mhrok/__main__.py mhrok/asset.py mhrok/game.py mhrok/mobs.py mhrok/whitey.png
diffstat 8 files changed, 166 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Sat May 05 22:27:12 2012 +0200
@@ -0,0 +1,13 @@
+syntax: glob
+*.pyc
+*~
+.*.swp
+build/*
+dist/*
+bin/*
+lib/*
+include/*
+local
+*.egg-info
+*.DS_Store
+.git
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/__main__.py	Sat May 05 22:27:12 2012 +0200
@@ -0,0 +1,4 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from mhrok import __main__
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mhrok/__init__.py	Sat May 05 22:27:12 2012 +0200
@@ -0,0 +1,3 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mhrok/__main__.py	Sat May 05 22:27:12 2012 +0200
@@ -0,0 +1,15 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import spyral
+
+from mhrok import game
+
+
+SIZE = (640, 480)
+
+
+spyral.init()
+spyral.director.init(SIZE)
+spyral.director.push(game.Game(SIZE))
+spyral.director.run()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mhrok/asset.py	Sat May 05 22:27:12 2012 +0200
@@ -0,0 +1,61 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import pkgutil
+import os
+import tempfile
+try:
+    import cStringIO as StringIO
+except ImportError:
+    import StringIO
+
+import pygame
+
+
+PACKAGE_NAME = 'mhrok'
+TILE_CACHE = {}
+FONT_CACHE = {}
+
+
+def load_tiles(filename, width, height):
+    try:
+        return TILE_CACHE[filename, width, height]
+    except KeyError:
+        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))
+        TILE_CACHE[filename, width, height] = tiles
+    return tiles
+
+
+def load_image(filename):
+    f = StringIO.StringIO(pkgutil.get_data(PACKAGE_NAME, filename))
+    return pygame.image.load(f, filename).convert()
+
+
+def load_font(filename, size=8):
+    try:
+        return FONT_CACHE[filename, size]
+    except KeyError:
+        # XXX the "right" way causes segmentation fault
+        # f = StringIO.StringIO(pkgutil.get_data(PACKAGE_NAME, filename))
+        tmpdir = tempfile.mkdtemp()
+        fname = os.path.join(tmpdir, filename)
+        try:
+            with open(fname, 'wb') as f:
+                data = pkgutil.get_data(PACKAGE_NAME, filename)
+                f.write(data)
+            font = pygame.font.Font(fname, size)
+        finally:
+            try:
+                os.remove(fname)
+                os.rmdir(tmpdir)
+            except:
+                pass
+        FONT_CACHE[filename, size] = font
+    return font
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mhrok/game.py	Sat May 05 22:27:12 2012 +0200
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+
+import pygame
+from pygame import locals as pg
+import spyral
+
+from mhrok import mobs
+
+
+BG_COLOR = (128, 128, 128)
+TICKS_PER_SECOND = 15
+
+
+class Game(spyral.scene.Scene):
+    """The main game scene."""
+
+    def __init__(self, size):
+        spyral.scene.Scene.__init__(self)
+        self.size = size
+        self.clock.max_ups = TICKS_PER_SECOND
+        self.camera = spyral.director.get_camera().make_child(self.size)
+        self.group = spyral.sprite.Group(self.camera)
+        self.initialized = False
+
+    def on_enter(self):
+        if self.initialized:
+            return
+        self.initialized = True
+        bg = spyral.util.new_surface(self.size)
+        bg.fill(BG_COLOR)
+        self.camera.set_background(bg)
+        self.player = mobs.Player()
+        self.group.add(self.player)
+
+    def render(self):
+        self.group.draw()
+
+    def update(self, tick):
+        self.group.update()
+        for event in pygame.event.get():
+            if event.type == pg.QUIT:
+                raise SystemExit()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mhrok/mobs.py	Sat May 05 22:27:12 2012 +0200
@@ -0,0 +1,28 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import spyral
+
+from mhrok import asset
+
+
+class Player(spyral.sprite.Sprite):
+    def __init__(self):
+        super(Player, self).__init__()
+        self.frames = asset.load_tiles('whitey.png', 48, 48)
+        self.image = self.frames[0]
+        self.rect.midbottom = 48, 48
+        self.animation = self.walk_anim(0)
+
+    def update(self, camera):
+        self.animation.next()
+
+    def walk_anim(self, face):
+        frame = 0
+        while True:
+            self.image = self.frames[frame + 4 * face]
+            frame = (frame + 1) % 4
+            for i in xrange(3):
+                yield
+
+
Binary file mhrok/whitey.png has changed