Matrix   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 58
rs 10
wmc 17

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __init__() 0 8 1
F checkLines() 0 48 16
1
import pygame
2
3
4
class Matrix(pygame.Surface):
5
    def __init__(self, *args):
6
        pygame.Surface.__init__(self, *args)
7
        pygame.draw.rect(self, (255, 255, 255), self.get_rect(), 1)
8
        self.sprites = list()
9
        self.sounds = dict()
10
        self.sounds["line"] = pygame.mixer.Sound("res/line.wav")
11
        self.sounds["tetris"] = pygame.mixer.Sound("res/tetris.wav")
12
        self.scores = (40, 100, 300, 1200)
13
14
    def checkLines(self):
15
        lines = dict()
16
        # loop for all groups of sprites
17
        for sprite in self.sprites:
18
            # then all sprites within
19
            for block in sprite.sprites():
20
                # group them by line
21
                if block.rect.top in lines:
22
                    lines[block.rect.top]['count'] += 1
23
                    lines[block.rect.top]['sprites'].append(block)
24
                else:
25
                    lines[block.rect.top] = dict()
26
                    lines[block.rect.top]['count'] = 1
27
                    lines[block.rect.top]['sprites'] = list()
28
                    lines[block.rect.top]['sprites'].append(block)
29
30
        empty_lines = [(line / self.block_size, details['sprites'])
31
                       for line, details
32
                       in lines.iteritems()
33
                       if details['count'] == 10]
34
        for empty_line in empty_lines:
35
            # clear matrix
36
            self.blit(self.background, (0, 0))
37
            for sprite in empty_line[1]:
38
                sprite.kill()
39
        move_down_blocks = list()
40
        for sprite in self.sprites:
41
            for block in sprite.sprites():
42
                for empty_line in empty_lines:
43
                    # move sprites down
44
                    if block.rect.top / self.block_size < empty_line[0]:
45
                        move_down_blocks.append(block)
46
        # then move down for each occurence in the list
47
        for block in move_down_blocks:
48
            block.rect.top += self.block_size
49
50
        score = 0
51
        if empty_lines:
52
            score = self.scores[len(empty_lines) - 1]
53
54
            # redraw sprites
55
            for sprite in self.sprites:
56
                sprite.draw(self)
57
            if len(empty_lines) == 4:
58
                self.sounds["tetris"].play()
59
            else:
60
                self.sounds["line"].play()
61
        return len(empty_lines), score
62