Source

pygamegsoc12 / alchemymadness / blocks.py

Full commit
illume 4da263d 

Szymon Wróblewsk… 57153a0 


illume 4da263d 


Szymon Wróblewsk… 57153a0 
Szymon Wróblewsk… bd25010 
illume ec1b1a4 
Szymon Wróblewsk… bd25010 
illume 47e81b2 
Szymon Wróblewsk… 4525fc3 


illume 47e81b2 
Szymon Wróblewsk… 57153a0 
Szymon Wróblewsk… 4525fc3 
illume 47e81b2 
Szymon Wróblewsk… 4525fc3 
Szymon Wróblewsk… bd25010 
Szymon Wróblewsk… 4525fc3 
Szymon Wróblewsk… 57153a0 
Szymon Wróblewsk… 4525fc3 

illume 3f370bf 
Szymon Wróblewsk… 4525fc3 



illume 3f370bf 
Szymon Wróblewsk… 4525fc3 

illume ec1b1a4 
Szymon Wróblewsk… 4525fc3 


























illume 3f370bf 


Szymon Wróblewsk… 57153a0 




Szymon Wróblewsk… 4525fc3 

Szymon Wróblewsk… 57153a0 

illume 3f370bf 
illume a9b52f1 


Szymon Wróblewsk… 57153a0 

Szymon Wróblewsk… 4525fc3 

illume 47e81b2 
illume a9b52f1 
Szymon Wróblewsk… 57153a0 
Szymon Wróblewsk… 4525fc3 


illume 4da263d 

illume ff0a36c 
illume 3f370bf 


illume 47e81b2 

illume 8ba0824 
Szymon Wróblewsk… 57153a0 
illume 51bc962 
Szymon Wróblewsk… 4525fc3 
illume 8ba0824 
illume 3f370bf 
illume 8ba0824 
illume 4da263d 
illume 47e81b2 

illume f73f422 
illume 47e81b2 







illume f73f422 

illume 47e81b2 




illume a9b52f1 
illume ec1b1a4 
illume 8ba0824 
illume a9b52f1 


illume 8ba0824 

illume ec1b1a4 
illume 8ba0824 

Szymon Wróblewsk… 57153a0 
illume 4e0a69e 
illume a9b52f1 

illume 8ba0824 
illume a9b52f1 
Szymon Wróblewsk… c523ef9 
illume a9b52f1 
Szymon Wróblewsk… 4525fc3 
illume 8ba0824 




illume 4e0a69e 
illume a9b52f1 


illume 8ba0824 
illume a9b52f1 


illume 8ba0824 



Szymon Wróblewsk… 57153a0 
illume 4e0a69e 
illume a9b52f1 

illume 8ba0824 
illume a9b52f1 

Szymon Wróblewsk… 4525fc3 
illume 8ba0824 



Szymon Wróblewsk… 57153a0 
illume 4e0a69e 
illume 4da263d 
Szymon Wróblewsk… c523ef9 



illume 4da263d 

illume a9b52f1 
illume 4da263d 
Szymon Wróblewsk… 57153a0 
illume 85da37e 
illume 4da263d 
Szymon Wróblewsk… 57153a0 
illume 85da37e 

Szymon Wróblewsk… 57153a0 
Szymon Wróblewsk… 4525fc3 
illume 4da263d 
illume 47e81b2 
Szymon Wróblewsk… 57153a0 




illume 4da263d 
illume 47e81b2 

Szymon Wróblewsk… 57153a0 

illume f73f422 
Szymon Wróblewsk… 57153a0 


illume f73f422 
illume 4da263d 
illume 47e81b2 

Szymon Wróblewsk… 57153a0 
illume b6a1270 


illume 4da263d 
Szymon Wróblewsk… 57153a0 

illume 4da263d 
illume a9b52f1 

illume 4da263d 

illume a9b52f1 


illume f73f422 

illume a9b52f1 
illume f73f422 
Szymon Wróblewsk… 57153a0 
illume a9b52f1 
illume f73f422 
Szymon Wróblewsk… 57153a0 
illume a9b52f1 
"""Mixing potion"""

import random
from itertools import izip, chain

import pygame

import resources
import layout
from sprite import MaskedSprite

class Block(MaskedSprite):
    """ A single block.

    color: block color (r, g, b)
    parent: Blocks instance
    """
    rect = layout.Main.block_rect
    speed = 1.0

    def __init__(self, color, parent, *groups):
        super(Block, self).__init__(color, *groups)
        self.parent = parent
        self.t_image = resources.block
        self.position = [0, 0] # topleft corner of grid
        self._top = float(self.rect.top)

    def move(self, d):
        """Move block horizontally in given direction

        d: positive - move right, negative - move left
        """
        self.rect.centerx += self.parent.grid.grid_size.w * d
        self.dirty = 1

    @property
    def position(self):
        b_size = self.parent.grid.block_size
        g_pos = self.parent.grid.rect.topleft
        return ((self.rect.left - g_pos[0]) / b_size[0],
                (self.rect.top - g_pos[1]) / b_size[1])

    @position.setter
    def position(self, value):
        b_size = self.parent.grid.block_size
        g_pos = self.parent.grid.rect.topleft
        self.rect.left = value[0] * b_size[0] + g_pos[0]
        self.rect.top = value[1] * b_size[1] + g_pos[1]
        self.top = float(self.rect.top)
        self.dirty = 1

    def update(self, time):
        self.top += self.speed * self.rect.h * time / 100.
        self.rect.top = self.top
        pos = self.position
        g_h = self.parent.grid.grid_size.h
        print pos
        if (pos[0], pos[1]+1) in self.parent.grid.grid or pos[1] + 1 >= g_h:
            print pos
            self.parent.grid.grid[pos] = self
            self.parent.freze() # TODO: freze() in Blocks
            self.parent.sprite_group.remove(self)
        self.dirty = 1


class SmallBlock(Block):
    """ A single small block.
    """
    rect = layout.Main.small_block_rect

    def __init__(self, color, parent, *groups):
        super(SmallBlock, self).__init__(color, parent, *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, grid):
        """ grid - a Grid instance.
        """
        super(Blocks, self).__init__()
        self.sprite_group = pygame.sprite.Group()
        self.grid = grid
        BlockC = Block if not self.grid.small else SmallBlock
        self.sprite_group.add([BlockC(random.choice(self.av_colors), self) 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.on_moved()

    def on_moved(self):
        """ called when we have moved.
        """
        i = 0
        for b, pos in izip(self.sprite_group, self.grid_positions):
            #print "on_moved {} {}".format(i, pos)
            b.position = pos
            i += 1

        #print "-" * 20

    def has_collided(self):
        """ checks if we have collided with some other blocks.
              See if the blocks of the same color are below.
        """
        # TODO: self.parent is the grid.
        raise NotImplementedError()

    def make_frozen(self):
        """ makes this a frozen block in the grid.
        """
        # TODO: self.parent is the grid.
        #    Probably create a new grid.blocks...
        #      The Block instances will stay where they are in the sprite_group.
        raise NotImplementedError()


    # action_* methods are inputs.

    def action_move_left(self):
        # if any of them are going out of bounds, we don't move.
        doit = True
        for b in self.grid_positions:
            b[0] -= 1
            if b[0] < 0:
                doit = False
        if not doit:
            # 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] >= self.grid.grid_size.w:
                doit = False
        if not doit:
            for b in self.grid_positions:
                b[0] -= 1

        self.on_moved()


    def action_move_up(self):
        doit = True
        for b in self.grid_positions:
            b[1] -= 1
            if b[1] < 0:
                doit = False
        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] >= self.grid.grid_size.h:
                doit = False
        if not doit:
            for b in self.grid_positions:
                b[1] -= 1

        self.on_moved()

    def change_color(self, color):
        for b in self.sprite_group:
            b.mix_color(color, 1)


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
        self.small = grid_num != 0
        self.rect = layout.Main.grid_rects[grid_num]
        self.block_size = (self.rect.w / self.grid_size.w, self.rect.h / self.grid_size.h)
        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.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 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.h)

        #import pprint
        #pprint.pprint(self.grid)


class Grids(object):
    """ This contains all the main Grid objects.
    """
    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(len(layout.Main.grid_rects))]

        for grd in self.grids:
            self.sprite_group.add(grd.sprite_group)