changeset 9:2a0fdca2802b

Moving files around
author Radomir Dopieralski <moindev@sheep.art.pl>
date Mon, 17 Dec 2007 01:35:20 +0100
parents ea712aee4501
children f692ad3bf510
files abi2.png box.png brain.png cache.py corridor.png data/map.txt floor.png game.py image/abi2.png image/box.png image/brain.png image/corridor.png image/floor.png image/jello.png image/joe.png image/skeleton.png image/stairs.png image/zombie.png jello.png joe.png level.py lib/game.py lib/level.py lib/map.py lib/mesg.py lib/sprites.py lib/util.py main.py map.txt mesg.py monster/__init__.py skeleton.png stairs.png util.py zombie.png
diffstat 35 files changed, 295 insertions(+), 253 deletions(-) [+]
line wrap: on
line diff
Binary file abi2.png has changed
Binary file box.png has changed
Binary file brain.png has changed
--- a/cache.py	Mon Dec 17 01:19:57 2007 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,21 +0,0 @@
-import pygame
-from pygame.locals import *
-
-class Cache:
-    def __init__(self):
-        self.images = {}
-
-    def get_image(self, filename=None):
-        '''Return an image from specified file.'''
-
-        if not filename in self.images:
-            try:
-                self.images[filename] = pygame.image.load(filename).convert()
-            except:
-                return None
-        return self.images[filename]
-
-    def get_object(self, url):
-        '''Return an object identified by specified url'''
-        
-        return None
Binary file corridor.png has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/map.txt	Mon Dec 17 01:35:20 2007 +0100
@@ -0,0 +1,31 @@
+X <>
+: <floor.png>
+. <corridor.png>
+> <stairs.png>
+M <floor.png> <jello>
+S <floor.png> <jello>
+B <floor.png> <jello>
+Z <floor.png> <jello>
+& <floor.png> <jello>
+@ <floor.png> <player>
+
+::::::XXXXXXXXXXXXXX
+::::::..::::XX::::::
+::M:::XX::::..::&:::
+::::::XXX.XXXX::::::
+XXX.XXXXX.XXXXXX.XXX
+XXX.XXX::::::XXX.XXX
+XXX.XXX::::::XXX.XXX
+XXX....::::Z:XXX.XXX
+XXX.XXX::::::X:::::X
+XXX.XXX@:::::X:::::X
+XXX.XXXXXX.XXX::M::X
+XXX.XXXXXX.XXX:::::X
+X::::::XX:::XXXX.XXX
+X::>:::XX:S:.....XXX
+XX.XXXXXX:::XXXX.XXX
+XX.XXXXXX.XXXXXX.XXX
+X::::::::::::::::::X
+.::::B:::::::::::::.
+X::::::::::::::::::X
+XXXXXXXXXXXXXXXXXXXX
Binary file floor.png has changed
--- a/game.py	Mon Dec 17 01:19:57 2007 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,54 +0,0 @@
-import pygame
-from pygame.locals import *
-
-from util import *
-from level import Level
-from avatar import Avatar
-from mesg import Mesg
-
-class Game:
-    def __init__(self):
-        # Init
-        pygame.init()
-        self.screen = pygame.display.set_mode((480, 480))
-        self.clock = pygame.time.Clock()
-
-        # Prapare the map 
-        self.level = Level('map.txt')
-
-    def try_walk(self, d):
-        av = self.level.player
-        if self.level.map.is_blocked(av.x+delta_x[d], av.y+delta_y[d]):
-            return
-        av.walk(d)
-        
-    def main_loop(self):
-        self.level.refresh(self.screen)
-
-        key_pressed = None
-        while 1:
-            # Update the screen
-            self.level.update(self.screen)
-            self.clock.tick(15)
-            # Process input
-            for event in pygame.event.get():
-                if event.type==QUIT:
-                    return
-                elif event.type==KEYDOWN:
-                    key_pressed = event.key
-            av = self.level.player
-            if key_pressed==K_SPACE:
-                self.level.animations.add(Mesg(sprite=av, text="-5", color=(255,0 ,0)))
-            if av.step==0:
-                keys = pygame.key.get_pressed()
-                if keys[K_UP] or key_pressed==K_UP:
-                    self.try_walk(1)
-                elif keys[K_DOWN] or key_pressed==K_DOWN:
-                    self.try_walk(3)
-                elif keys[K_LEFT] or key_pressed==K_LEFT:
-                    self.try_walk(4)
-                elif keys[K_RIGHT] or key_pressed==K_RIGHT:
-                    self.try_walk(2)
-                elif key_pressed==K_ESCAPE:
-                    return
-                key_pressed = None
Binary file image/abi2.png has changed
Binary file image/box.png has changed
Binary file image/brain.png has changed
Binary file image/corridor.png has changed
Binary file image/floor.png has changed
Binary file image/jello.png has changed
Binary file image/joe.png has changed
Binary file image/skeleton.png has changed
Binary file image/stairs.png has changed
Binary file image/zombie.png has changed
Binary file jello.png has changed
Binary file joe.png has changed
--- a/level.py	Mon Dec 17 01:19:57 2007 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,85 +0,0 @@
-import pygame
-from pygame.locals import *
-
-from monster import Monster
-from avatar import Avatar
-from cache import Cache
-from util import *
-from sprites import SortedUpdates
-from map import Map
-
-class MapFileError:
-    pass
-
-class Level:
-    def __init__(self, filename):
-        self.animations = SortedUpdates()
-        self.scrollpos = [-48,-32]
-        self.map = Map(filename)
-        self.cache = Cache()
-        self.monsters = {}
-        self.background = self.create_map(self.map)
-    
-    def create_map(self, m):
-        '''Create background and animations.'''
-        
-        background = pygame.Surface((self.map.w*24+96, self.map.h*16+64))
-        background.fill( (0, 0, 0) )
-        x =0
-        y =0
-        for line in m:
-            for tile in line:
-                urls = m.map_key[tile]
-                if urls:
-                    image = self.cache.get_image(urls[0])
-                    if image:
-                        background.blit(image , (x*24+48, y*16+32))
-                    for url in urls[1:]:
-                        monster = self.create_monster(x,y, url)
-                        if url=='player':
-                            self.player = monster
-                x += 1
-            x = 0
-            y += 1
-        return background
-    
-    def create_monster(self, x, y, kind):
-        module = __import__("monster.%s" % kind, globals(), {}, ["Mob"])
-        monster = module.Mob(x, y)
-        self.monster = self
-        self.add_sprite(monster)
-        self.monsters[(x,y)] = monster
-        return monster
-    
-    def add_sprite(self, sprite):
-        self.animations.add(sprite)
-        sprite.rect.move_ip(self.scrollpos[0], self.scrollpos[1])
-
-    def scroll(self, dx, dy):
-        self.scrollpos[0] += dx
-        self.scrollpos[1] += dy
-        for sprite in self.animations:
-            sprite.rect.move_ip(dx, dy)
-
-    def refresh(self, screen):
-        '''Redraw the whole level'''
-
-        screen.blit(self.background, self.scrollpos)
-        self.animations.draw(screen)
-
-        pygame.display.update()
-
-
-    def clear_func(self, screen, rect):
-        source = rect.move(-self.scrollpos[0], -self.scrollpos[1])
-        source = source.clip(self.background.get_clip())
-        patch = self.background.subsurface(source)
-        screen.blit(patch, rect)
-
-    def update(self, screen):
-        '''Redraw and update all the animated parts'''
-
-        self.animations.clear(screen, self.clear_func)
-        self.animations.update()
-        dirty = self.animations.draw(screen)
-        pygame.display.update(dirty)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/game.py	Mon Dec 17 01:35:20 2007 +0100
@@ -0,0 +1,54 @@
+import pygame
+from pygame.locals import *
+import os
+
+from util import *
+from level import Level
+from mesg import Mesg
+
+class Game:
+    def __init__(self):
+        # Init
+        pygame.init()
+        self.screen = pygame.display.set_mode((480, 480))
+        self.clock = pygame.time.Clock()
+
+        # Prapare the map 
+        self.level = Level(os.path.join('data', 'map.txt'))
+
+    def try_walk(self, d):
+        av = self.level.player
+        if self.level.map.is_blocked(av.x+delta_x[d], av.y+delta_y[d]):
+            return
+        av.walk(d)
+        
+    def main_loop(self):
+        self.level.refresh(self.screen)
+
+        key_pressed = None
+        while 1:
+            # Update the screen
+            self.level.update(self.screen)
+            self.clock.tick(15)
+            # Process input
+            for event in pygame.event.get():
+                if event.type==QUIT:
+                    return
+                elif event.type==KEYDOWN:
+                    key_pressed = event.key
+            av = self.level.player
+            if key_pressed==K_SPACE:
+                self.level.animations.add(Mesg(sprite=av, text="-5", color=(255,0 ,0)))
+            if av.step==0:
+                keys = pygame.key.get_pressed()
+                if keys[K_UP] or key_pressed==K_UP:
+                    self.try_walk(1)
+                elif keys[K_DOWN] or key_pressed==K_DOWN:
+                    self.try_walk(3)
+                elif keys[K_LEFT] or key_pressed==K_LEFT:
+                    self.try_walk(4)
+                elif keys[K_RIGHT] or key_pressed==K_RIGHT:
+                    self.try_walk(2)
+                elif key_pressed==K_ESCAPE:
+                    return
+                key_pressed = None
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/level.py	Mon Dec 17 01:35:20 2007 +0100
@@ -0,0 +1,85 @@
+import pygame
+from pygame.locals import *
+import os
+
+from monster import Monster
+from cache import Cache
+from util import *
+from sprites import SortedUpdates
+from map import Map
+
+class MapFileError:
+    pass
+
+class Level:
+    def __init__(self, filename):
+        self.animations = SortedUpdates()
+        self.scrollpos = [-48,-32]
+        self.map = Map(filename)
+        self.cache = Cache()
+        self.monsters = {}
+        self.background = self.create_map(self.map)
+    
+    def create_map(self, m):
+        '''Create background and animations.'''
+        
+        background = pygame.Surface((self.map.w*24+96, self.map.h*16+64))
+        background.fill( (0, 0, 0) )
+        x =0
+        y =0
+        for line in m:
+            for tile in line:
+                urls = m.map_key[tile]
+                if urls:
+                    image = self.cache.get_image(os.path.join('image', urls[0]))
+                    if image:
+                        background.blit(image , (x*24+48, y*16+32))
+                    for url in urls[1:]:
+                        monster = self.create_monster(x,y, url)
+                        if url=='player':
+                            self.player = monster
+                x += 1
+            x = 0
+            y += 1
+        return background
+    
+    def create_monster(self, x, y, kind):
+        module = __import__("monster.%s" % kind, globals(), {}, ["Mob"])
+        monster = module.Mob(x, y)
+        self.monster = self
+        self.add_sprite(monster)
+        self.monsters[(x,y)] = monster
+        return monster
+    
+    def add_sprite(self, sprite):
+        self.animations.add(sprite)
+        sprite.rect.move_ip(self.scrollpos[0], self.scrollpos[1])
+
+    def scroll(self, dx, dy):
+        self.scrollpos[0] += dx
+        self.scrollpos[1] += dy
+        for sprite in self.animations:
+            sprite.rect.move_ip(dx, dy)
+
+    def refresh(self, screen):
+        '''Redraw the whole level'''
+
+        screen.blit(self.background, self.scrollpos)
+        self.animations.draw(screen)
+
+        pygame.display.update()
+
+
+    def clear_func(self, screen, rect):
+        source = rect.move(-self.scrollpos[0], -self.scrollpos[1])
+        source = source.clip(self.background.get_clip())
+        patch = self.background.subsurface(source)
+        screen.blit(patch, rect)
+
+    def update(self, screen):
+        '''Redraw and update all the animated parts'''
+
+        self.animations.clear(screen, self.clear_func)
+        self.animations.update()
+        dirty = self.animations.draw(screen)
+        pygame.display.update(dirty)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/map.py	Mon Dec 17 01:35:20 2007 +0100
@@ -0,0 +1,47 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import re
+
+class Map:
+    def __init__(self, filename):
+        '''Read the map from a file.'''
+
+        self.map = []
+        self.blocking = []
+        self.map_key = {}
+        self.w = 0
+        for line in file(filename).readlines():
+            m = re.match('^(?P<key>.)\s+<(?P<urls>.*)>\s*$', line)
+            if m:
+                urls = re.split('>\s*<', m.group('urls'))
+                self.map_key[m.group('key')] = urls
+            elif re.match('^\s*$', line):
+                pass # Ignore empty lines
+            else:
+                map_line = []
+                block_line = []
+                for char in line:
+                    if char in self.map_key:
+                        map_line.append(char)
+                        if char=='X':
+                            block_line.append(True)
+                        else:
+                            block_line.append(False)
+                if self.w<len(map_line):
+                    self.w = len(map_line)
+                self.map.append(map_line)
+                self.blocking.append(block_line)
+        self.h = len(self.map)
+        
+    def __getitem__(self, key):
+        return self.map[key]
+    
+    def is_blocked(self, x, y):
+        if x<0 or y<0:
+            return True
+        try:
+            return self.blocking[y][x]
+        except IndexError:
+            return True
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/mesg.py	Mon Dec 17 01:35:20 2007 +0100
@@ -0,0 +1,37 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import pygame
+from pygame.locals import *
+import pygame.font
+
+from util import *
+
+
+class Mesg(pygame.sprite.Sprite):
+    """Messages float up and disappear."""
+    
+    def __init__(self, x=0, y=0, text="Hello!", color=(255, 255, 255), sprite=None):
+        pygame.sprite.Sprite.__init__(self)
+        self.x = x
+        self.y = y
+        self.z = y*16-8
+        self.dh = 10
+        self.image = font.render(text, False, color).convert()
+        self.alpha = 255
+        self.rect = Rect(0,0, self.image.get_width(), self.image.get_height())
+        self.rect.center = (self.x*24+12+48, self.y*16-8+32)
+        if sprite: self.rect.center = sprite.rect.center
+
+    def update(self):
+        if self.alpha <= 0:
+            self.kill()
+            return
+        self.dh -= 1
+        self.rect.move_ip(0, -self.dh)
+        self.image.set_alpha(self.alpha)
+        self.alpha -= 20
+        
+
+pygame.font.init()
+font = pygame.font.SysFont("Sans", 15, bold=True)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/sprites.py	Mon Dec 17 01:35:20 2007 +0100
@@ -0,0 +1,13 @@
+#!/usr/bin/python
+# -*- coding: utf-8 -*-
+
+import pygame
+from pygame.locals import *
+
+
+class SortedUpdates(pygame.sprite.RenderUpdates):
+    def z_compare(self, a, b): return a.z-b.z
+    def sprites(self):
+        l = self.spritedict.keys()
+        l.sort(self.z_compare)
+        return l
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/lib/util.py	Mon Dec 17 01:35:20 2007 +0100
@@ -0,0 +1,24 @@
+import pygame
+from pygame.locals import *
+
+delta_x = (0, 0, 1, 0, -1)
+delta_y = (0, -1, 0, 1, 0)
+facings = {
+    'stand':    0,
+    'north':    1,
+    'east':     2,
+    'south':    3,
+    'west':     4,
+}
+
+def read_frames(filename, size=24):
+    '''Read an image and split it into tiles.'''
+
+    image = pygame.image.load(filename).convert()
+    w, h = image.get_size()
+    frames = []
+    for x in range(0, w/size):
+        frames.append([])
+        for y in range(0, h/size):
+            frames[x].append(image.subsurface((x*size, y*size, size, size)))
+    return frames
--- a/main.py	Mon Dec 17 01:19:57 2007 +0100
+++ b/main.py	Mon Dec 17 01:35:20 2007 +0100
@@ -45,5 +45,7 @@
     ...
 '''
 
+import sys
+sys.path.insert(0, "lib")
 import game
 game.Game().main_loop()
--- a/map.txt	Mon Dec 17 01:19:57 2007 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-X <>
-: <floor.png>
-. <corridor.png>
-> <stairs.png>
-M <floor.png> <jello>
-S <floor.png> <jello>
-B <floor.png> <jello>
-Z <floor.png> <jello>
-& <floor.png> <jello>
-@ <floor.png> <player>
-
-::::::XXXXXXXXXXXXXX
-::::::..::::XX::::::
-::M:::XX::::..::&:::
-::::::XXX.XXXX::::::
-XXX.XXXXX.XXXXXX.XXX
-XXX.XXX::::::XXX.XXX
-XXX.XXX::::::XXX.XXX
-XXX....::::Z:XXX.XXX
-XXX.XXX::::::X:::::X
-XXX.XXX@:::::X:::::X
-XXX.XXXXXX.XXX::M::X
-XXX.XXXXXX.XXX:::::X
-X::::::XX:::XXXX.XXX
-X::>:::XX:S:.....XXX
-XX.XXXXXX:::XXXX.XXX
-XX.XXXXXX.XXXXXX.XXX
-X::::::::::::::::::X
-.::::B:::::::::::::.
-X::::::::::::::::::X
-XXXXXXXXXXXXXXXXXXXX
--- a/mesg.py	Mon Dec 17 01:19:57 2007 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,37 +0,0 @@
-#!/usr/bin/python
-# -*- coding: utf-8 -*-
-
-import pygame
-from pygame.locals import *
-import pygame.font
-
-from util import *
-
-
-class Mesg(pygame.sprite.Sprite):
-    """Messages float up and disappear."""
-    
-    def __init__(self, x=0, y=0, text="Hello!", color=(255, 255, 255), sprite=None):
-        pygame.sprite.Sprite.__init__(self)
-        self.x = x
-        self.y = y
-        self.z = y*16-8
-        self.dh = 10
-        self.image = font.render(text, False, color).convert()
-        self.alpha = 255
-        self.rect = Rect(0,0, self.image.get_width(), self.image.get_height())
-        self.rect.center = (self.x*24+12+48, self.y*16-8+32)
-        if sprite: self.rect.center = sprite.rect.center
-
-    def update(self):
-        if self.alpha <= 0:
-            self.kill()
-            return
-        self.dh -= 1
-        self.rect.move_ip(0, -self.dh)
-        self.image.set_alpha(self.alpha)
-        self.alpha -= 20
-        
-
-pygame.font.init()
-font = pygame.font.SysFont("Sans", 15, bold=True)
--- a/monster/__init__.py	Mon Dec 17 01:19:57 2007 +0100
+++ b/monster/__init__.py	Mon Dec 17 01:35:20 2007 +0100
@@ -1,5 +1,6 @@
 import pygame
 from pygame.locals import *
+import os
 
 from util import *
 
@@ -14,7 +15,7 @@
         self.y = y
         self.z = y*16-12
         self.rect = Rect(self.x*24+48, self.z+32, 24, 24)
-        self.frames = read_frames(image_file)
+        self.frames = read_frames(os.path.join("image", image_file))
         self.anim = self.frames[0]
         self.update()
         
Binary file skeleton.png has changed
Binary file stairs.png has changed
--- a/util.py	Mon Dec 17 01:19:57 2007 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,24 +0,0 @@
-import pygame
-from pygame.locals import *
-
-delta_x = (0, 0, 1, 0, -1)
-delta_y = (0, -1, 0, 1, 0)
-facings = {
-    'stand':    0,
-    'north':    1,
-    'east':     2,
-    'south':    3,
-    'west':     4,
-}
-
-def read_frames(filename, size=24):
-    '''Read an image and split it into tiles.'''
-
-    image = pygame.image.load(filename).convert()
-    w, h = image.get_size()
-    frames = []
-    for x in range(0, w/size):
-        frames.append([])
-        for y in range(0, h/size):
-            frames[x].append(image.subsurface((x*size, y*size, size, size)))
-    return frames
Binary file zombie.png has changed