Commits

Szymon Wróblewski committed 57153a0

Added layout module, reworked blocks module, added font

Comments (0)

Files changed (7)

alchemymadness/blocks.py

 """Mixing potion"""
 
+import random
+from itertools import izip, chain
+
 import pygame
-from pygame.locals import Rect
 from pygame.sprite import DirtySprite
 
 import resources
-
-GRID_SIZE_W = 9
-GRID_SIZE_H = 16
-
-BLOCK_W = 30
-BLOCK_H = 30
-
-# small block size.
-SBLOCK_W = BLOCK_W / 2
-SBLOCK_H = BLOCK_H / 2
+import layout
 
 class Block(DirtySprite):
     """ A single block.
     """
+    rect = layout.Main.block_rect
+    changed = False # True if sprite image need to be regenerated
 
-    changed = False
-
-    def __init__(self, color, small, *groups):
+    def __init__(self, color, *groups):
         """
-            color - (r,g,b) tuple.
-            small - True if we are to be drawn small.
+            color - (r,g,b) tuple
         """
         super(Block, self).__init__(*groups)
-
-        self.small = small
-        if self.small:
-            self.rect = Rect(0, 0, SBLOCK_W, SBLOCK_W)
-        else:
-            self.rect = Rect(0, 0, BLOCK_W, BLOCK_W)
-
         self._image = pygame.Surface(self.rect.size) # Sprite image
+        self.t_image = resources.block
         self._color = color  # Block color
         self.changed = True
         self.dirty = 1
     @property
     def image(self):
         if self.changed:
-            if self.small:
-                self._image = resources.sblock.copy()
-            else:
-                self._image = resources.block.copy()
+            self._image = self.t_image.copy()
             self._image.fill(self._color, special_flags=pygame.locals.BLEND_RGBA_MULT)
         return self._image
 
         self.dirty = 1
 
 
+class SmallBlock(Block):
+    """ A single small block.
+    """
+    rect = layout.Main.small_block_rect
+
+    def __init__(self, color, *groups):
+        super(SmallBlock, self).__init__(color, *groups)
+        self.t_image = resources.sblock
+
 
 class Blocks(object):
     """ contains 3 Block() instances.
     """
+    av_colors = [(255,0,0), (0,255,0), (0,0,255)] # available colors
+
     def __init__(self, parent):
         """ parent - a Grid instance.
         """
         super(Blocks, self).__init__()
-
+        self.sprite_group = pygame.sprite.Group()
         self.parent = parent
-        #TODO: randomise these colors.
-        self.colors = [(255,0,0), (0,255,0), (0,0,255)]
-
+        BlockC = Block if not self.parent.small else SmallBlock
+        self.sprite_group.add([BlockC(random.choice(self.av_colors)) for i in range(3)])
         # how do we handle location, and rotation? We say where the grid it is on.
         #     top, left is (0,0)
         self.grid_positions = [[0,0], [1,0], [2,0]]
 
-        self.blocks = [Block(c, self.parent.small) for c in self.colors]
+
         self.on_moved()
 
     def on_moved(self):
         """ called when we have moved.
         """
         i = 0
-        for b, pos in zip(self.blocks, self.grid_positions):
+        for b, pos in izip(self.sprite_group, self.grid_positions):
             #print "on_moved {} {}".format(i, pos)
             b.grid_pos(pos, self.parent.rect)
             i += 1
 
     def update(self, time):
         pass
-        
-
-
 
     def has_collided(self):
         """ checks if we have collided with some other blocks.
         raise NotImplementedError()
 
 
-
-
-
     # action_* methods are inputs.
 
     def action_move_left(self):
             # oops!  out of bounds, so reverse moves.
             for b in self.grid_positions:
                 b[0] += 1
-                
+
         self.on_moved()
 
     def action_move_right(self):
         doit = True
         for b in self.grid_positions:
             b[0] += 1
-            if b[0] >= GRID_SIZE_W:
+            if b[0] >= self.parent.grid_size.w:
                 doit = False
         if not doit:
             for b in self.grid_positions:
         if not doit:
             for b in self.grid_positions:
                 b[1] += 1
-                
+
         self.on_moved()
 
     def action_move_down(self):
         doit = True
         for b in self.grid_positions:
             b[1] += 1
-            if b[1] >= GRID_SIZE_H:
+            if b[1] >= self.parent.grid_size.h:
                 doit = False
         if not doit:
             for b in self.grid_positions:
                 b[1] -= 1
-                
+
         self.on_moved()
 
 
-
-
-
-
-
-
-
 class Grid(object):
     """ Contains Blocks()
     """
+    grid_size = layout.Main.grid_size
     def __init__(self, grid_num):
         super(Grid, self).__init__()
+        self._sprite_group = pygame.sprite.Group()
         self.grid_num = grid_num
-        # draw the first one big.
         self.small = grid_num != 0
-
-        self.GRID_SIZE_W = GRID_SIZE_W
-        self.GRID_SIZE_H = GRID_SIZE_H
-
-        if self.small:
-            # TODO: get correct coords for where the grids are on screen.
-            if self.grid_num == 1:
-                self.rect = pygame.Rect(300, 20, GRID_SIZE_W * SBLOCK_W, GRID_SIZE_H * SBLOCK_H)
-            if self.grid_num == 2:
-                self.rect = pygame.Rect(470, 20, GRID_SIZE_W * SBLOCK_W, GRID_SIZE_H * SBLOCK_H)
-            if self.grid_num == 3:
-                self.rect = pygame.Rect(630, 20, GRID_SIZE_W * SBLOCK_W, GRID_SIZE_H * SBLOCK_H)
-        else:
-            self.rect = pygame.Rect(30, 20, GRID_SIZE_W * BLOCK_W, GRID_SIZE_H * BLOCK_H)
+        self.rect = layout.Main.grid_rects[grid_num]
 
         self.restart()
 
+    @property
+    def sprite_group(self):
+        self._sprite_group = pygame.sprite.Group(list(chain(self.blocks.sprite_group, self.grid.itervalues())))
+        return self._sprite_group
+
     def restart(self):
         """ restart this Grid instance.
         """
-        self.init_grid()
-        self.blocks = Blocks(self)
-
-
+        self.grid = {} # grid in form of dict - position : Block
+        self.blocks = Blocks(self) # currently moving block
         # TODO: what about frozen Blocks?
-
-
-
+        # theory:
+        # - when Blocks hits Block or grid frame, it pushes Block-s to grid
+        # - Block-s continue to move until hitting other Block or grid frame
 
     def init_grid(self):
         """ init_grid initialises the Grid to be all clear.
         """
-        # init our 10x10 grid, to show the color of each grid part.
+        # init our grid, to show the color of each grid part.
         #[[(0,0,0), ...],
         # ...
         #]
         self.grid = []
-        for x in range(self.GRID_SIZE_H):
-            self.grid.append([(0,0,0)] * self.GRID_SIZE_W)
+        for x in range(self.grid_size.h):
+            self.grid.append([(0,0,0)] * self.grid_size.h)
 
         #import pprint
         #pprint.pprint(self.grid)
 
 
-
-
-
-
-
 class Grids(object):
     """ This contains all the main Grid objects.
     """
-    NUM_GRIDS = 4
     def __init__(self, sprite_group):
         """ sprite_group - which we add sprites to.
         """
         self.sprite_group = sprite_group
-        self.grids = [Grid(x) for x in range(self.NUM_GRIDS)]
+        self.grids = [Grid(x) for x in range(len(layout.Main.grid_rects))]
 
         for grd in self.grids:
-            for b in grd.blocks.blocks:
-                self.sprite_group.add(b)
+            self.sprite_group.add(grd.sprite_group)
 
 
Add a comment to this file

alchemymadness/data/The Alchemist.ttf

Binary file added.

alchemymadness/data/font_info.txt

+THE ALCHEMIST
+A TrueType Font by S. John Ross
+Cumberland Games & Diversions
+www.cumberlandgames.com
+
+Like "Pigeon Street" (see the main Fontworks page), The Alchemist is a font I drew with a particular character in mind - the spindly, mantis-like figure who appears briefly in Ring of Thieves. In truth, he's one of a small stable of "character actors" I use in many fantasy games, a favorite NPC to resurrect for a cameo now and again.
+
+So, this is how he writes. This font includes more glyphs than just about any other I've done - a full keyboard set plus an oddball smattering of extended characters. I'll probably fix a few things (like the too-faint period) before archiving it on the Fontworks, but I like it enough to do so even if nobody emails me about this one. Enjoy!
+
+This font is copyright 2001 by S. John Ross. "Cumberland Games & Diversions" is a trademark of S. John Ross. This font is freeware for personal, non-commercial use of any kind (including website decoration and so on). Contact me at sjohn@io.com if you're interested in a commercial license; rates will depend on the details.
+
+This ZIP archive may be freely distributed provided none of the contents are altered or removed.
+
+Download Ring of Thieves (mentioned above) for free from
+www.io.com/~sjohn/thieves.htm
+
+Version 2.0
+
+This version adds some additional foreign characters (added for a client in Norway) and extensive additional kerning.
+

alchemymadness/layout.py

+from pygame.locals import Rect
+
+__all__ = ('screen_size', 'Main')
+
+screen_size = (800, 600)
+
+class Main(object):
+    block_rect = Rect(0, 0, 30, 30)
+    small_block_rect = Rect(0, 0, 15, 15)
+    grid_size = Rect(0, 0, 9, 16) # size in blocks
+    grid_rects = (
+        Rect(30, 20, block_rect.w * grid_size[0], block_rect.h * grid_size[1]),
+        Rect(300, 20, small_block_rect.w * grid_size[0], small_block_rect.h * grid_size[1]),
+        Rect(470, 20, small_block_rect.w * grid_size[0], small_block_rect.h * grid_size[1]),
+        Rect(630, 20, small_block_rect.w * grid_size[0], small_block_rect.h * grid_size[1]),
+    )
+    potion_rect = Rect(450, 470, 170, 113)

alchemymadness/main.py

 from pygame.locals import *
 
 import resources
+import layout
 import potion
 import blocks
 
-SCREEN_SIZE = (800, 600)
-
-
 def main():
     """ your app starts here
     """
 
     pygame.init()
-    screen = pygame.display.set_mode(SCREEN_SIZE)
-    pygame.display.set_caption("alchemymadness")
+    screen = pygame.display.set_mode(layout.screen_size)
+    pygame.display.set_caption("Alchemy Madness")
     clock = pygame.time.Clock()
     going = True
 
     pot = potion.Potion()
     pot.set_formula(3, 5, 3)
     sprites = pygame.sprite.LayeredDirty(pot)
-
+    sprites.clear(screen, resources.background)
 
     grds = blocks.Grids(sprites)
 
                 pot.add_color((255,0,0))
             if e.type == KEYDOWN and e.key == K_s:
                 pygame.image.save(screen, "screeny.png")
-                
+
             if e.type == KEYDOWN:
                 if e.key == K_LEFT:
                     blks.action_move_left()
                     blks.action_move_up()
                 if e.key == K_DOWN:
                     blks.action_move_down()
-            
+
                 # press keys 1-4 to control player 1-4.
                 if e.key in [K_1, K_2, K_3, K_4]:
                     which_player = [K_1, K_2, K_3, K_4].index(e.key)
                     blks = grds.grids[which_player].blocks
-                    
+
             if e.type == QUIT or e.type == KEYDOWN and e.key == K_ESCAPE:
                 going = False
 
         # Update sprites
-        sprites.clear(screen, resources.background)
         sprites.update(time)
-
         # Draw sprites
         rects = sprites.draw(screen)
+        # TODO: Move text code somewhere else
+        screen.blit(resources.font[40].render('Alchemy Madness', True, (255,255,255)), (10,550))
         pygame.display.update(rects)
 
 

alchemymadness/potion.py

 from pygame.sprite import DirtySprite
 
 import resources
+import layout
 
 class Potion(DirtySprite):
     chems = 0  # Chem count
     col = (0,0,0)  # Current formula
     target_col = None
     max_chems = 0  # Target number of chems
-    rect = Rect(450, 470, 170, 113)
+    rect = layout.Main.potion_rect
     image_rect = Rect((0, rect.y), rect.size)
     target_y = None
     changed = False
 
-
     def __init__(self, *groups):
         """
         Init potion sprite.

alchemymadness/resources.py

 background = None
 block = None
 sblock = None
+font = None
 
 def init():
-    global background, block, sblock
+    global background, block, sblock, font
     background = pygame.image.load(skelutil.data_dir("bg.png")).convert_alpha()
     block = pygame.image.load(skelutil.data_dir("block.png")).convert_alpha()
-    sblock = pygame.transform.scale(block, (int(block.get_rect()[2]/2), int(block.get_rect()[3])))
+    sblock = pygame.transform.scale(block, (int(block.get_rect().w/2), int(block.get_rect().h/2)))
+    font = Font(skelutil.data_dir("The Alchemist.ttf"))
+
+class Font(object):
+    """multiresolution font"""
+    def __init__(self, path):
+        self.path = path
+        self.fonts = {}
+
+    def __getitem__(self, size):
+        """get font size"""
+        try:
+            return self.fonts[size]
+        except KeyError:
+            f = pygame.font.Font(self.path, size)
+            self.fonts[size] = f
+            return f
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.