changeset 0:57a7e5537dd9 default tip

initial commit
author Radomir Dopieralski <sheep-devel@sheep.art.pl>
date Sun, 22 Apr 2012 14:03:08 +0200
parents
children
files __main__.py setup.py tinworld/__init__.py tinworld/tinworld.png
diffstat 4 files changed, 359 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/__main__.py	Sun Apr 22 14:03:08 2012 +0200
@@ -0,0 +1,5 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+
+import tinworld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/setup.py	Sun Apr 22 14:03:08 2012 +0200
@@ -0,0 +1,68 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from setuptools import setup
+import os
+import glob
+try:
+    import py2exe
+except ImportError:
+    pass
+else:
+    origIsSystemDLL = py2exe.build_exe.isSystemDLL
+    def isSystemDLL(pathname):
+        if os.path.basename(pathname).lower() in (
+                "libfreetype-6.dll", "sdl_ttf.dll", "libogg-0.dll"):
+                return 0
+        return origIsSystemDLL(pathname)
+    py2exe.build_exe.isSystemDLL = isSystemDLL
+
+    orig_byte_compile = py2exe.build_exe.byte_compile
+    def byte_compile(*args, **kwargs):
+        files = orig_byte_compile(*args, **kwargs)
+        files.extend(os.path.abspath(d) for d in glob.glob("tinworld/*.png"))
+        return files
+    py2exe.build_exe.byte_compile = byte_compile
+
+setup(
+    name='tinworld',
+    version='1.0',
+    author='Radomir Dopieralski',
+    packages=['tinworld'],
+    scripts=['__main__.py'],
+    include_package_data=True,
+    zip_safe=True,
+    install_requires=['distribute', 'pygame'],
+    platforms='any',
+    options = {
+        'py2exe': {
+            'bundle_files': 1,
+            'packages': 'tinworld',
+            'compressed': True,
+            'ascii': True,
+            'excludes': [
+                'doctest',
+                'pdb',
+                'unittest',
+                'difflib',
+                'inspect',
+                'locale',
+                'pyreadline',
+                'optparse',
+                'calendar',
+                'pickle',
+                'email',
+                '_ssl',
+            ]
+        },
+    },
+    data_files = [
+        ("tinworld", glob.glob("tinworld/*.png")),
+    ],
+    windows = [
+        {
+            'script': "__main__.py",
+        }
+    ],
+    zipfile = None,
+)
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tinworld/__init__.py	Sun Apr 22 14:03:08 2012 +0200
@@ -0,0 +1,286 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import pygame
+from pygame import locals as pg
+import random
+import sys
+import StringIO
+import pkgutil
+
+
+def load_tiles(filename, width, height):
+    f = StringIO.StringIO(pkgutil.get_data('tinworld', filename))
+    image = pygame.image.load(f, filename).convert_alpha()
+    image_width, image_height = image.get_size()
+    tiles = []
+    for y in range(image_height/height):
+        for x in range(image_width/width):
+            rect = (x * width, y * height, width, height)
+            tiles.append(image.subsurface(rect))
+    return tiles
+
+
+class SortedUpdates(pygame.sprite.RenderUpdates):
+    def sprites(self):
+        return sorted(self.spritedict.keys(),
+                      key=lambda sprite: getattr(sprite, "depth", 0))
+
+
+class Player(pygame.sprite.Sprite):
+    def __init__(self):
+        super(Player, self).__init__()
+        self.image = tiles[0]
+        self.rect = self.image.get_rect()
+        self.rect.midbottom = (72, 308)
+        self.animation = self.walk()
+        self.depth = self.rect.bottom
+        self.dx = 0
+        self.dy = 0
+        self.shooting = False
+
+    def walk(self):
+        while True:
+            self.rect.move_ip(self.dx, self.dy-2)
+            yield
+            self.rect.move_ip(self.dx, self.dy-1)
+            yield
+            self.rect.move_ip(self.dx, self.dy+1)
+            yield
+            self.rect.move_ip(self.dx, self.dy+2)
+            yield
+
+    def shoot(self):
+        yield
+        self.image = tiles[4]
+        for frame in range(6):
+            yield
+        self.animation = self.walk()
+        self.image = tiles[0]
+        self.shooting = False
+        yield
+
+    def die(self):
+        dy = -24
+        self.image = tiles[4]
+        for frame in range(20):
+            self.rect.move_ip(-24, dy)
+            dy += 2
+            yield
+        self.kill()
+        yield
+
+    def update(self, tick):
+        self.animation.next()
+        keys = pygame.key.get_pressed()
+        if keys[pg.K_SPACE] and not self.shooting:
+            self.shooting = True
+            self.animation = self.shoot()
+            sprites.add(Puff(self.rect.bottomright))
+            for robot in robots:
+                robot.rect.bottom
+                if (self.rect.bottom-20 <
+                        robot.rect.bottom < self.rect.bottom+20 and
+                        robot.rect.left - 300 < self.rect.left < robot.rect.left):
+                    robot.animation = robot.die()
+        if keys[pg.K_UP]:
+            self.dy = -4
+        elif keys[pg.K_DOWN]:
+            self.dy = 4
+        else:
+            self.dy = 0
+        if keys[pg.K_LEFT]:
+            self.dx = -6
+        elif keys[pg.K_RIGHT]:
+            self.dx = 6
+        else:
+            self.dx = 0
+        if self.rect.top + self.dy < 114 or self.rect.bottom + self.dy > 480:
+            self.dy = 0
+        if self.rect.left + self.dx < 0 or self.rect.right + self.dx > 640:
+            self.dx = 0
+        for robot in robots:
+            if (self.rect.bottom-20 <
+                    robot.rect.bottom < self.rect.bottom+20 and
+                    robot.rect.left - 30 <
+                    self.rect.left < robot.rect.left + 30):
+                self.animation = self.die()
+        self.depth = self.rect.bottom
+
+class Puff(pygame.sprite.Sprite):
+    def __init__(self, pos):
+        super(Puff, self).__init__()
+        self.image = tiles[6]
+        self.rect = self.image.get_rect()
+        self.rect.midbottom = pos
+        self.depth = 1000
+        self.animation = self.fly()
+
+    def fly(self):
+        for frame in range(3):
+            self.rect.move_ip(2, 1)
+            yield
+        self.kill()
+        yield
+
+    def update(self, tick):
+        self.animation.next()
+
+class Bush(pygame.sprite.Sprite):
+    def __init__(self):
+        super(Bush, self).__init__()
+        self.image = tiles[2]
+        self.rect = self.image.get_rect()
+        self.rect.midbottom = (
+                random.randint(0, 640),
+                random.randint(180, 480)
+            )
+        self.depth = self.rect.bottom
+
+class Robot(pygame.sprite.Sprite):
+    def __init__(self):
+        super(Robot, self).__init__()
+        self.image = tiles[1]
+        self.rect = self.image.get_rect()
+        self.rect.midbottom = (408, 0)
+        self.animation = self.fall()
+
+    def fall(self):
+        self.rect.midbottom = (random.randint(200, 608),
+                               random.randint(-144, 72))
+        dy = random.randint(1, 10)
+        for i in range(13):
+            self.rect.move_ip(0, dy)
+            dy += 4
+            yield
+        self.animation = self.walk()
+        yield
+
+
+    def walk(self):
+        while True:
+            self.rect.move_ip(-2, -2)
+            yield
+            self.rect.move_ip(-4, -1)
+            yield
+            self.rect.move_ip(-2, 1)
+            yield
+            self.rect.move_ip(0, 2)
+            yield
+
+    def die(self):
+        dy = -24
+        self.image = tiles[5]
+        for frame in range(20):
+            self.rect.move_ip(24, dy)
+            dy += 2
+            yield
+        self.kill()
+        yield
+
+
+    def update(self, tick):
+        self.animation.next()
+        self.depth = self.rect.bottom
+        if self.rect.right < 0:
+            self.kill()
+
+class Why(pygame.sprite.Sprite):
+    def __init__(self, pos):
+        super(Why, self).__init__()
+        self.image = tiles[7]
+        self.rect = self.image.get_rect()
+        self.rect.center = pos
+        self.animation = self.fall()
+
+    def fall(self):
+        dy = 0
+        for frame in range(4):
+            self.rect.move_ip(1, -1)
+            yield
+            self.rect.move_ip(-1, 1)
+            yield
+        for frame in range(20):
+            self.rect.move_ip(0, dy)
+            dy += 3
+            yield
+        self.kill()
+        yield
+
+    def update(self, tick):
+        self.animation.next()
+
+
+pygame.display.init()
+screen = pygame.display.set_mode((640, 480))
+tiles = load_tiles('tinworld.png', 72, 72)
+clock = pygame.time.Clock()
+background = screen.copy()
+
+while True:
+    background.fill((255, 255, 255))
+    for x in range(4):
+        background.blit(tiles[12+x], (72*x+160, 200))
+    background.blit(tiles[3], (280, 300))
+    sprites = SortedUpdates()
+    sprites.add(Why((300, 237)))
+
+    screen.blit(background, (0, 0))
+    sprites.draw(screen)
+    pygame.display.flip()
+
+    pressed = False
+    while not pressed:
+        tick = clock.tick(15)
+        sprites.clear(screen, background)
+        sprites.update(tick)
+        dirty = sprites.draw(screen)
+        pygame.display.update(dirty)
+        for event in pygame.event.get():
+            if event.type == pg.QUIT:
+                sys.exit()
+            elif event.type == pg.KEYUP and event.key == pg.K_SPACE:
+                pressed = True
+
+    background.fill((255, 255, 255), (0, 144, 640, 480))
+    background.fill((0, 0, 0), (0, 0, 640, 144))
+    for x in range(9):
+        background.blit(tiles[8 + x % 4], (72*x, 72))
+
+    sprites = SortedUpdates()
+    robots = pygame.sprite.Group()
+    for bush in range(40):
+        sprites.add(Bush())
+    player = Player()
+    sprites.add(player)
+
+    screen.blit(background, (0, 0))
+    sprites.draw(screen)
+    pygame.display.flip()
+    game_over = False
+    robot_spawn = 1
+    max_spawn = 100
+    ticks = 0
+    while not game_over:
+        tick = clock.tick(15)
+        ticks += 1
+        sprites.clear(screen, background)
+        robot_spawn -= 1
+        if robot_spawn <= 0:
+            robot_spawn = random.randint(max_spawn/2, max_spawn)
+            robot = Robot()
+            robots.add(robot)
+            sprites.add(robot)
+        if ticks % 200 == 0:
+            max_spawn = max(2, robot_spawn-5)
+        sprites.update(tick)
+        dirty = sprites.draw(screen)
+        pygame.display.update(dirty)
+        if not player.alive():
+            game_over = True
+        for event in pygame.event.get():
+            if event.type == pg.QUIT:
+                sys.exit()
+            elif event.type == pg.KEYDOWN and event.key == pg.K_ESCAPE:
+                game_over = True
+
Binary file tinworld/tinworld.png has changed