Skip to content

Commit e8f1aa4

Browse files
committed
Video Code
The video version of the game.
1 parent e163167 commit e8f1aa4

File tree

7 files changed

+115
-116
lines changed

7 files changed

+115
-116
lines changed

block.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from colors import Colors
2-
from position import Position
32
import pygame
3+
from position import Position
44

55
class Block:
66
def __init__(self, id):
@@ -9,33 +9,34 @@ def __init__(self, id):
99
self.cell_size = 30
1010
self.row_offset = 0
1111
self.column_offset = 0
12-
self.colors = Colors.get_cell_colors()
1312
self.rotation_state = 0
13+
self.colors = Colors.get_cell_colors()
14+
15+
def move(self, rows, columns):
16+
self.row_offset += rows
17+
self.column_offset += columns
1418

15-
def get_cells_positions(self):
19+
def get_cell_positions(self):
1620
tiles = self.cells[self.rotation_state]
1721
moved_tiles = []
1822
for position in tiles:
1923
position = Position(position.row + self.row_offset, position.column + self.column_offset)
2024
moved_tiles.append(position)
2125
return moved_tiles
2226

23-
def move(self, rows, columns):
24-
self.row_offset += rows
25-
self.column_offset += columns
26-
2727
def rotate(self):
2828
self.rotation_state += 1
2929
if self.rotation_state == len(self.cells):
3030
self.rotation_state = 0
3131

3232
def undo_rotation(self):
3333
self.rotation_state -= 1
34-
if self.rotation_state == -1:
34+
if self.rotation_state == 0:
3535
self.rotation_state = len(self.cells) - 1
3636

3737
def draw(self, screen, offset_x, offset_y):
38-
tiles = self.get_cells_positions()
38+
tiles = self.get_cell_positions()
3939
for tile in tiles:
40-
tile_rect = pygame.Rect(offset_x + tile.column * self.cell_size, offset_y + tile.row * self.cell_size, self.cell_size - 1, self.cell_size -1)
41-
pygame.draw.rect(screen, self.colors[self.id], tile_rect)
40+
tile_rect = pygame.Rect(offset_x + tile.column * self.cell_size,
41+
offset_y + tile.row * self.cell_size, self.cell_size -1, self.cell_size -1)
42+
pygame.draw.rect(screen, self.colors[self.id], tile_rect)

blocks.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
from position import Position
33

44
class LBlock(Block):
5-
def __init__(self):
6-
super().__init__ ( id = 1)
7-
self.cells = {
8-
0: [Position(0, 2), Position(1, 0), Position(1, 1), Position(1, 2)],
9-
1: [Position(0, 1), Position(1, 1), Position(2, 1), Position(2, 2)],
10-
2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 0)],
11-
3: [Position(0, 0), Position(0, 1), Position(1, 1), Position(2, 1)]
12-
}
13-
self.move(0, 3)
5+
def __init__(self):
6+
super().__init__(id = 1)
7+
self.cells = {
8+
0: [Position(0, 2), Position(1, 0), Position(1, 1), Position(1, 2)],
9+
1: [Position(0, 1), Position(1, 1), Position(2, 1), Position(2, 2)],
10+
2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 0)],
11+
3: [Position(0, 0), Position(0, 1), Position(1, 1), Position(2, 1)]
12+
}
13+
self.move(0, 3)
1414

1515
class JBlock(Block):
1616
def __init__(self):
@@ -23,7 +23,6 @@ def __init__(self):
2323
}
2424
self.move(0, 3)
2525

26-
2726
class IBlock(Block):
2827
def __init__(self):
2928
super().__init__(id = 3)
@@ -39,9 +38,9 @@ class OBlock(Block):
3938
def __init__(self):
4039
super().__init__(id = 4)
4140
self.cells = {
42-
0: [Position(0, 0), Position(0, 1), Position(1, 0), Position(1, 1)],
41+
0: [Position(0, 0), Position(0, 1), Position(1, 0), Position(1, 1)]
4342
}
44-
self.move(0, 4)
43+
self.move(0, 4)
4544

4645
class SBlock(Block):
4746
def __init__(self):

colors.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Colors:
22
dark_grey = (26, 31, 40)
33
green = (47, 230, 23)
4-
orange = (226, 116, 17)
54
red = (232, 18, 18)
6-
purple = (166, 0, 247)
5+
orange = (226, 116, 17)
76
yellow = (237, 234, 4)
7+
purple = (166, 0, 247)
88
cyan = (21, 204, 209)
99
blue = (13, 64, 216)
1010
white = (255, 255, 255)
@@ -13,4 +13,4 @@ class Colors:
1313

1414
@classmethod
1515
def get_cell_colors(cls):
16-
return [cls.dark_grey, cls.green, cls.orange, cls.red, cls.purple, cls.yellow, cls.cyan, cls.blue]
16+
return [cls.dark_grey, cls.green, cls.red, cls.orange, cls.yellow, cls.purple, cls.cyan, cls.blue]

game.py

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from grid import Grid
22
from blocks import *
3-
import random, pygame
3+
import random
4+
import pygame
45

56
class Game:
67
def __init__(self):
@@ -10,42 +11,28 @@ def __init__(self):
1011
self.next_block = self.get_random_block()
1112
self.game_over = False
1213
self.score = 0
13-
self.rotate_sound = pygame.mixer.Sound('Sounds/rotate.ogg')
14-
self.clear_sound = pygame.mixer.Sound('Sounds/clear.ogg')
15-
pygame.mixer.music.load('Sounds/music.ogg')
14+
self.rotate_sound = pygame.mixer.Sound("Sounds/rotate.ogg")
15+
self.clear_sound = pygame.mixer.Sound("Sounds/clear.ogg")
16+
17+
pygame.mixer.music.load("Sounds/music.ogg")
1618
pygame.mixer.music.play(-1)
1719

20+
def update_score(self, lines_cleared, move_down_points):
21+
if lines_cleared == 1:
22+
self.score += 100
23+
elif lines_cleared == 2:
24+
self.score += 300
25+
elif lines_cleared == 3:
26+
self.score += 500
27+
self.score += move_down_points
28+
1829
def get_random_block(self):
19-
if len(self.blocks)==0:
30+
if len(self.blocks) == 0:
2031
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
2132
block = random.choice(self.blocks)
2233
self.blocks.remove(block)
2334
return block
2435

25-
def block_inside(self):
26-
tiles = self.current_block.get_cells_positions()
27-
for tile in tiles:
28-
if self.grid.is_inside(tile.row, tile.column) == False:
29-
return False
30-
return True
31-
32-
def block_fits(self):
33-
tiles = self.current_block.get_cells_positions()
34-
for tile in tiles:
35-
if self.grid.is_empty(tile.row, tile.column) == False:
36-
return False
37-
return True
38-
39-
def draw(self, screen):
40-
self.grid.draw(screen)
41-
self.current_block.draw(screen, 11, 11)
42-
if self.next_block.id == 3:
43-
self.next_block.draw(screen, 255, 290)
44-
elif self.next_block.id == 4:
45-
self.next_block.draw(screen, 255, 280)
46-
else:
47-
self.next_block.draw(screen, 270, 270)
48-
4936
def move_left(self):
5037
self.current_block.move(0, -1)
5138
if self.block_inside() == False or self.block_fits() == False:
@@ -62,40 +49,54 @@ def move_down(self):
6249
self.current_block.move(-1, 0)
6350
self.lock_block()
6451

65-
def rotate(self):
66-
self.current_block.rotate()
67-
if self.block_inside() == False or self.block_fits() == False:
68-
self.current_block.undo_rotation()
69-
else:
70-
self.rotate_sound.play()
71-
7252
def lock_block(self):
73-
tiles = self.current_block.get_cells_positions()
53+
tiles = self.current_block.get_cell_positions()
7454
for position in tiles:
7555
self.grid.grid[position.row][position.column] = self.current_block.id
56+
self.current_block = self.next_block
57+
self.next_block = self.get_random_block()
7658
rows_cleared = self.grid.clear_full_rows()
7759
if rows_cleared > 0:
7860
self.clear_sound.play()
7961
self.update_score(rows_cleared, 0)
80-
self.current_block = self.next_block
81-
self.next_block = self.get_random_block()
8262
if self.block_fits() == False:
8363
self.game_over = True
8464

85-
def update_score(self, lines_cleared, move_down_points):
86-
87-
if lines_cleared == 1:
88-
self.score += 100
89-
elif lines_cleared == 2:
90-
self.score += 300
91-
elif lines_cleared == 3:
92-
self.score += 500
93-
94-
self.score += move_down_points
95-
9665
def reset(self):
9766
self.grid.reset()
9867
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
9968
self.current_block = self.get_random_block()
10069
self.next_block = self.get_random_block()
101-
self.score = 0
70+
self.score = 0
71+
72+
def block_fits(self):
73+
tiles = self.current_block.get_cell_positions()
74+
for tile in tiles:
75+
if self.grid.is_empty(tile.row, tile.column) == False:
76+
return False
77+
return True
78+
79+
def rotate(self):
80+
self.current_block.rotate()
81+
if self.block_inside() == False or self.block_fits() == False:
82+
self.current_block.undo_rotation()
83+
else:
84+
self.rotate_sound.play()
85+
86+
def block_inside(self):
87+
tiles = self.current_block.get_cell_positions()
88+
for tile in tiles:
89+
if self.grid.is_inside(tile.row, tile.column) == False:
90+
return False
91+
return True
92+
93+
def draw(self, screen):
94+
self.grid.draw(screen)
95+
self.current_block.draw(screen, 11, 11)
96+
97+
if self.next_block.id == 3:
98+
self.next_block.draw(screen, 255, 290)
99+
elif self.next_block.id == 4:
100+
self.next_block.draw(screen, 255, 280)
101+
else:
102+
self.next_block.draw(screen, 270, 270)

grid.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
class Grid:
55
def __init__(self):
66
self.num_rows = 20
7-
self.num_columns = 10
7+
self.num_cols = 10
88
self.cell_size = 30
9-
self.grid = [[0 for j in range(self.num_columns)] for i in range(self.num_rows)]
9+
self.grid = [[0 for j in range(self.num_cols)] for i in range(self.num_rows)]
1010
self.colors = Colors.get_cell_colors()
1111

1212
def print_grid(self):
1313
for row in range(self.num_rows):
14-
for column in range(self.num_columns):
15-
print(self.grid[row][column], end=' ')
14+
for column in range(self.num_cols):
15+
print(self.grid[row][column], end = " ")
1616
print()
1717

1818
def is_inside(self, row, column):
19-
if row >= 0 and row < self.num_rows and column >= 0 and column < self.num_columns:
19+
if row >= 0 and row < self.num_rows and column >= 0 and column < self.num_cols:
2020
return True
2121
return False
2222

@@ -26,18 +26,18 @@ def is_empty(self, row, column):
2626
return False
2727

2828
def is_row_full(self, row):
29-
for column in range(self.num_columns):
29+
for column in range(self.num_cols):
3030
if self.grid[row][column] == 0:
3131
return False
3232
return True
3333

34-
def move_row_down(self, row, num_rows):
35-
for column in range(self.num_columns):
36-
self.grid[row+num_rows] [column] = self.grid[row][column]
34+
def clear_row(self, row):
35+
for column in range(self.num_cols):
3736
self.grid[row][column] = 0
3837

39-
def clear_row(self, row):
40-
for column in range(self.num_columns):
38+
def move_row_down(self, row, num_rows):
39+
for column in range(self.num_cols):
40+
self.grid[row+num_rows][column] = self.grid[row][column]
4141
self.grid[row][column] = 0
4242

4343
def clear_full_rows(self):
@@ -49,15 +49,16 @@ def clear_full_rows(self):
4949
elif completed > 0:
5050
self.move_row_down(row, completed)
5151
return completed
52-
52+
53+
def reset(self):
54+
for row in range(self.num_rows):
55+
for column in range(self.num_cols):
56+
self.grid[row][column] = 0
57+
5358
def draw(self, screen):
5459
for row in range(self.num_rows):
55-
for column in range(self.num_columns):
60+
for column in range(self.num_cols):
5661
cell_value = self.grid[row][column]
57-
cell_rect = pygame.Rect(11 + column*self.cell_size, 11 + row*self.cell_size, self.cell_size -1 , self.cell_size - 1)
62+
cell_rect = pygame.Rect(column*self.cell_size + 11, row*self.cell_size + 11,
63+
self.cell_size -1, self.cell_size -1)
5864
pygame.draw.rect(screen, self.colors[cell_value], cell_rect)
59-
60-
def reset(self):
61-
for row in range(self.num_rows):
62-
for column in range(self.num_columns):
63-
self.grid[row][column] = 0

0 commit comments

Comments
 (0)