Mercurial > dw
changeset 29:f952dcc56f9e
Rearranged files.
| author | sheep@cerber.labs.wmid.amu.edu.pl |
|---|---|
| date | Tue, 18 Dec 2007 09:10:30 +0100 |
| parents | 2ccb6995d85d |
| children | 760f734672d0 |
| files | data/image/abi2.png data/image/box.png data/image/brain.png data/image/corridor.png data/image/floor.png data/image/jello.png data/image/joe.png data/image/shadow.png data/image/skeleton.png data/image/stairs.png data/image/zombie.png data/map.txt image/abi2.png image/box.png image/brain.png image/corridor.png image/floor.png image/jello.png image/joe.png image/shadow.png image/skeleton.png image/stairs.png image/zombie.png lib/game.py lib/level.py lib/monster/__init__.py lib/monster/jello.py lib/monster/player.py lib/shadow.py main.py monster/__init__.py monster/jello.py monster/player.py |
| diffstat | 33 files changed, 273 insertions(+), 295 deletions(-) [+] |
line wrap: on
line diff
--- a/data/map.txt Tue Dec 18 02:10:40 2007 +0100 +++ b/data/map.txt Tue Dec 18 09:10:30 2007 +0100 @@ -6,7 +6,7 @@ S <floor.png> <jello> B <floor.png> <jello> Z <floor.png> <jello> -& <floor.png> <crate> +& <floor.png> <jello> @ <floor.png> <player> ::::::XXXXXXXXXXXXXX
--- a/lib/game.py Tue Dec 18 02:10:40 2007 +0100 +++ b/lib/game.py Tue Dec 18 09:10:30 2007 +0100 @@ -5,19 +5,24 @@ from util import * from level import Level from mesg import Mesg -from monster import MoveError, MoveBump, MoveBlocked + +data_path = "" +image_path = "image" class Game: - def __init__(self): + def __init__(self, data_path, image_path): # Init + self.data_path = data_path + self.image_path = image_path pygame.init() self.screen = pygame.display.set_mode((480, 320)) self.clock = pygame.time.Clock() # Prapare the map - self.level = Level(os.path.join('data', 'map.txt')) + self.level = Level(os.path.join(data_path, 'map.txt'), image_path) def try_walk(self, d): + from monster import MoveError, MoveBump, MoveBlocked try: try: self.level.player.walk(d)
--- a/lib/level.py Tue Dec 18 02:10:40 2007 +0100 +++ b/lib/level.py Tue Dec 18 09:10:30 2007 +0100 @@ -2,16 +2,17 @@ from pygame.locals import * import os +import game from util import * -from shadow import Shadow import sprites from map import Map class Level: - def __init__(self, filename): + def __init__(self, filename, image_path): + self.image_path = image_path self.sprites = sprites.SortedUpdates() self.shadows = pygame.sprite.RenderUpdates() - self.effects = pygame.sprite.RenderUpdates() + self.effects = pygame.sprite.RenderUpdates() self.scrollpos = [-48,-32] self.map = Map(filename) self.monsters = {} @@ -23,7 +24,8 @@ try: return self.cache[filename] except KeyError: - self.cache[filename] = pygame.image.load(filename).convert() + filepath = os.path.join(self.image_path, filename) + self.cache[filename] = pygame.image.load(filepath).convert() return self.cache[filename] def create_map(self, m): @@ -38,7 +40,7 @@ urls = m.map_key[tile] if urls: if urls[0]: - image = self.get_image(os.path.join('image', urls[0])) + image = self.get_image(urls[0]) background.blit(image , (x*24+48, y*16+32)) for url in urls[1:]: self.create_monster(x,y, url)
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/monster/__init__.py Tue Dec 18 09:10:30 2007 +0100 @@ -0,0 +1,154 @@ +import pygame +from pygame.locals import * +import os + +from mesg import Mesg +from shadow import Shadow +from util import * + +class MoveError(Exception): + pass + +class MoveBump(MoveError): + def __init__(self, pos, monster): + self.pos = pos + self.monster = monster + + def __repr__(self): + return "bump (%d, %d), %s" % (self.pos[0], self.pos[1], repr(self.monster)) + +class MoveBlocked(MoveError): + def __init__(self, pos): + self.pos = pos + + def __repr__(self): + return "block (%d, %d)" % (self.pos[0], self.pos[1]) + + +class Monster(pygame.sprite.Sprite): + '''Monsters only have two frames of animation.''' + + def __init__(self, pos, level): + pygame.sprite.Sprite.__init__(self) + self.frame = 0 + self.delay = 0 + self.step = 0 + self.level = level + self.x, self.y = pos + self.h = 0 + self.dead = False + self.place(pos) + self.shadow = Shadow(self) + self.level.add_sprite(self) + self.anim = self.anim_stand() + self.shadow.update() + self.update() + + def place(self, pos): + x, y = pos + self.move(pos) + self.z = y*16-12 + self.rect = Rect(self.x*24+48, self.z+32, 24, 24) + try: + self.shadow.update() + except AttributeError: + pass + + def move(self, pos): + x, y = pos + if self.level.map.is_blocked(x,y): + raise MoveBlocked(pos) + if self.level.monsters.get(pos, None): + raise MoveBump(pos, self.level.monsters[pos]) + try: + del self.level.monsters[(self.x, self.y)] + except KeyError: + pass + self.x, self.y = pos + self.level.monsters[pos] = self + + def animate(self, frames, delay=0): + if self.delay>=delay: + self.delay = 0 + self.frame += 1 + if self.frame>=len(frames): + self.frame = 0 + else: + self.delay += 1 + self.image = frames[self.frame] + + def anim_die(self): + self.step -= 1 + if self.step==15: + self.rect.move_ip(0, -4) + self.h += 4 + if self.step<=1: + self.die() + else: + self.image.set_alpha(self.step*32) + + def anim_stand(self): + self.animate(self.frames[0], 3) + + def anim_walk(self): + self.rect.move_ip(delta_x[self.facing]*3, delta_y[self.facing]*2) + self.z += delta_y[self.facing]*2 + self.animate(self.frames[0], 1) + + def anim_attack(self): + dx = delta_x[self.facing]*6 + dy = delta_y[self.facing]*4-1 + if self.step<=2: + dx = -dx + dy = -dy + self.h-=1 + else: + self.h+=1 + self.rect.move_ip(dx, dy) + self.animate(self.frames[0], 0) + + + def destroy(self): + try: + self.shadow.kill() + except AttributeError: + pass + self.kill() + try: + del self.level.monsters[(self.x, self.y)] + except KeyError: + pass + + def hit(self, damage=5): + if self.dead: + return + Mesg(self, "-%d" % damage, color=(255,0,0)) + self.die() + + def die(self): + if not self.dead: + self.dead = True + self.image = self.image.convert() + self.anim = self.anim_die + self.step += 8 + + def rotate(self, direction): + self.facing = direction + + def update(self): + if self.step<=0: + if self.dead: + self.destroy() + self.anim = self.anim_stand + self.anim() + if self.step>0: + self.step -= 1 + + def walk(self, direction): + self.rotate(direction) + self.move((self.x+delta_x[direction],self.y+delta_y[direction])) + self.step += 8 + self.anim = self.anim_walk + + def act(self): + pass
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/monster/jello.py Tue Dec 18 09:10:30 2007 +0100 @@ -0,0 +1,31 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +import random +import os + +import monster +from util import * + +class Mob(monster.Monster): + + frames = read_frames(os.path.join("data", "image", "jello.png")) + + def __init__(self, pos, level): + monster.Monster.__init__(self, pos, level) + + def act(self): + direction = random.randint(1, 4) + try: + try: + self.walk(direction) + except monster.MoveBump, e: + self.attack(direction, e.monster) + except monster.MoveError: + pass + + def attack(self, direction, monster): + self.step = 4 + self.anim = self.anim_attack + monster.hit() +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/monster/player.py Tue Dec 18 09:10:30 2007 +0100 @@ -0,0 +1,63 @@ +import pygame +from pygame.locals import * + +import os + +from monster import Monster +from mesg import Mesg +from util import * + +class Mob(Monster): + frames = read_frames(os.path.join("data", "image", "abi2.png")) + + def __init__(self, pos, level): + self.facing = 0 + Monster.__init__(self, pos, level) + self.rotate(3) + self.z += 1 + self.level.player = self + + def anim_stand(self): + self.image = self.frames[self.facing-1][0] + + def anim_walk(self): + self.rect.move_ip(delta_x[self.facing]*3, delta_y[self.facing]*2) + self.z += delta_y[self.facing]*2 + self.animate(self.frames[self.facing-1], 1) + + def anim_attack(self): + dx = delta_x[self.facing]*6 + dy = delta_y[self.facing]*4-1 + if self.step<=2: + dx = -dx + dy = -dy + self.h-=1 + else: + self.h+=1 + self.rect.move_ip(dx, dy) + self.animate(self.frames[self.facing-1], 0) + + def update(self): + if self.step<=0: + self.anim = self.anim_stand + self.anim() + if self.step>0: + self.step -= 1 + + + def rotate(self, facing): + self.facing = facing + self.image = self.frames[self.facing-1][0] + + def wait(self): + self.step = 8 + self.anim = self.anim_stand + + def attack(self, direction, monster): + self.rotate(direction) + self.step = 4 + self.anim = self.anim_attack + monster.hit() + + def hit(self, damage=5): + Mesg(self, "-%d" % damage, color=(255,0,0))
--- a/lib/shadow.py Tue Dec 18 02:10:40 2007 +0100 +++ b/lib/shadow.py Tue Dec 18 09:10:30 2007 +0100 @@ -11,11 +11,12 @@ class Shadow(pygame.sprite.Sprite): """Transparent shadows""" + image = read_frames(os.path.join("data", "image", "shadow.png"))[0][0] + def __init__(self, sprite): pygame.sprite.Sprite.__init__(self) self.dh = 10 self.z = -1000 - self.image = read_frames(os.path.join("image", "shadow.png"))[0][0] self.image.set_alpha(128) self.rect = Rect(0,0, 24, 24) self.sprite = sprite
--- a/main.py Tue Dec 18 02:10:40 2007 +0100 +++ b/main.py Tue Dec 18 09:10:30 2007 +0100 @@ -1,51 +1,11 @@ #!/usr/bin/env python -''' -Summary: --------- - This is a mockup test for some animated sprites. - The map is read from the file "map.txt", the graphics - is taken from attached image files. The rest is - hardcoded in the program. - - This is a python program, it requires the python - interpreter (available at http://python.org) and - the PyGame python library (available at - http://pygame.org). - - -- Radomir 'The Sheep' Dopieralski <dw@sheep.art.pl> - -Keys: ------ - arrows -- walk around - esc -- quit - - -Change Log: ------------ +import os +import sys -0.0.5 - - Fixed bug with wrong rectange sizes - - Made the animation frames into subsurfaces - - Now using true transparency in the images - - Pressing a key kind of skips the animation - (character jumps immediately to new position) - - All the tiles are now 32x32 in size, - but spaced like 24x24 tiles and overlapping - - Flattened class structure - - Moved the map to a separate class Level - - Moved the classes to separate files - - Moved the tile-reading code into separate file - - Moved the map tiles to one image - - Added monsters to map format - - Added different monster graphics - -0.0.4 - - The map is read from a file - ... -''' - -import sys -sys.path.insert(0, "lib") +base_path = os.path.split(os.path.abspath(__file__))[0] +sys.path.insert(0, os.path.join(base_path, "lib")) import game -game.Game().main_loop() +data_path = os.path.join(base_path, "data") +image_path = os.path.join(data_path, "image") +game.Game(data_path, image_path).main_loop()
--- a/monster/__init__.py Tue Dec 18 02:10:40 2007 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,154 +0,0 @@ -import pygame -from pygame.locals import * -import os - -from mesg import Mesg -from shadow import Shadow -from util import * - -class MoveError(Exception): - pass - -class MoveBump(MoveError): - def __init__(self, pos, monster): - self.pos = pos - self.monster = monster - - def __repr__(self): - return "bump (%d, %d), %s" % (self.pos[0], self.pos[1], repr(self.monster)) - -class MoveBlocked(MoveError): - def __init__(self, pos): - self.pos = pos - - def __repr__(self): - return "block (%d, %d)" % (self.pos[0], self.pos[1]) - - -class Monster(pygame.sprite.Sprite): - '''Monsters only have two frames of animation.''' - - def __init__(self, pos, level, image_file="monster.png"): - pygame.sprite.Sprite.__init__(self) - self.frame = 0 - self.delay = 0 - self.step = 0 - self.level = level - self.x, self.y = pos - self.h = 0 - self.dead = False - self.place(pos) - self.frames = read_frames(os.path.join("image", image_file)) - self.shadow = Shadow(self) - self.level.add_sprite(self) - self.anim = self.anim_stand() - self.shadow.update() - self.update() - - def place(self, pos): - x, y = pos - self.move(pos) - self.z = y*16-12 - self.rect = Rect(self.x*24+48, self.z+32, 24, 24) - try: - self.shadow.update() - except AttributeError: - pass - - def move(self, pos): - x, y = pos - if self.level.map.is_blocked(x,y): - raise MoveBlocked(pos) - if self.level.monsters.get(pos, None): - raise MoveBump(pos, self.level.monsters[pos]) - try: - del self.level.monsters[(self.x, self.y)] - except KeyError: - pass - self.x, self.y = pos - self.level.monsters[pos] = self - - def animate(self, frames, delay=0): - if self.delay>=delay: - self.delay = 0 - self.frame += 1 - if self.frame>=len(frames): - self.frame = 0 - else: - self.delay += 1 - self.image = frames[self.frame] - - def anim_die(self): - self.step -= 1 - if self.step==15: - self.rect.move_ip(0, -4) - self.h += 4 - if self.step<=1: - self.die() - else: - self.image.set_alpha(self.step*32) - - def anim_stand(self): - self.animate(self.frames[0], 3) - - def anim_walk(self): - self.rect.move_ip(delta_x[self.facing]*3, delta_y[self.facing]*2) - self.z += delta_y[self.facing]*2 - self.animate(self.frames[0], 1) - - def anim_attack(self): - dx = delta_x[self.facing]*6 - dy = delta_y[self.facing]*4-1 - if self.step<=2: - dx = -dx - dy = -dy - self.h-=1 - else: - self.h+=1 - self.rect.move_ip(dx, dy) - self.animate(self.frames[0], 0) - - - def destroy(self): - try: - self.shadow.kill() - except AttributeError: - pass - self.kill() - try: - del self.level.monsters[(self.x, self.y)] - except KeyError: - pass - - def hit(self, damage=5): - if self.dead: - return - Mesg(self, "-%d" % damage, color=(255,0,0)) - self.die() - - def die(self): - if not self.dead: - self.dead = True - self.anim = self.anim_die - self.step += 8 - - def rotate(self, direction): - self.facing = direction - - def update(self): - if self.step<=0: - if self.dead: - self.destroy() - self.anim = self.anim_stand - self.anim() - if self.step>0: - self.step -= 1 - - def walk(self, direction): - self.rotate(direction) - self.move((self.x+delta_x[direction],self.y+delta_y[direction])) - self.step += 8 - self.anim = self.anim_walk - - def act(self): - pass
--- a/monster/jello.py Tue Dec 18 02:10:40 2007 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,25 +0,0 @@ -#!/usr/bin/python -# -*- coding: utf-8 -*- - -import random -import monster - -class Mob(monster.Monster): - def __init__(self, pos, level): - monster.Monster.__init__(self, pos, level, "jello.png") - - def act(self): - direction = random.randint(1, 4) - try: - try: - self.walk(direction) - except monster.MoveBump, e: - self.attack(direction, e.monster) - except monster.MoveError: - pass - - def attack(self, direction, monster): - self.step = 4 - self.anim = self.anim_attack - monster.hit() -
--- a/monster/player.py Tue Dec 18 02:10:40 2007 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,59 +0,0 @@ -import pygame -from pygame.locals import * - -from monster import Monster -from mesg import Mesg -from util import * - -class Mob(Monster): - def __init__(self, pos, level): - self.facing = 0 - Monster.__init__(self, pos, level, "abi2.png") - self.rotate(3) - self.z += 1 - self.level.player = self - - def anim_stand(self): - self.image = self.frames[self.facing-1][0] - - def anim_walk(self): - self.rect.move_ip(delta_x[self.facing]*3, delta_y[self.facing]*2) - self.z += delta_y[self.facing]*2 - self.animate(self.frames[self.facing-1], 1) - - def anim_attack(self): - dx = delta_x[self.facing]*6 - dy = delta_y[self.facing]*4-1 - if self.step<=2: - dx = -dx - dy = -dy - self.h-=1 - else: - self.h+=1 - self.rect.move_ip(dx, dy) - self.animate(self.frames[self.facing-1], 0) - - def update(self): - if self.step<=0: - self.anim = self.anim_stand - self.anim() - if self.step>0: - self.step -= 1 - - - def rotate(self, facing): - self.facing = facing - self.image = self.frames[self.facing-1][0] - - def wait(self): - self.step = 8 - self.anim = self.anim_stand - - def attack(self, direction, monster): - self.rotate(direction) - self.step = 4 - self.anim = self.anim_attack - monster.hit() - - def hit(self, damage=5): - Mesg(self, "-%d" % damage, color=(255,0,0))
