Skip to content

Commit 1f5bb96

Browse files
committed
1.0
Added sound
1 parent b888b5a commit 1f5bb96

File tree

11 files changed

+232
-219
lines changed

11 files changed

+232
-219
lines changed

Sounds/clear.ogg

21.2 KB
Binary file not shown.

Sounds/music.ogg

1.65 MB
Binary file not shown.

Sounds/rotate.ogg

10.2 KB
Binary file not shown.

block.py

Lines changed: 28 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,41 @@
1-
import pygame
2-
from position import Position
31
from colors import Colors
2+
from position import Position
3+
import pygame
44

5-
class Block():
6-
def __init__(self, start_offset, id ):
7-
self.start_off_x = start_offset.row
8-
self.start_off_y = start_offset.column
9-
self.offset = start_offset
10-
self.tiles = {}
5+
class Block:
6+
def __init__(self, id):
117
self.id = id
8+
self.cells = {}
9+
self.cell_size = 30
10+
self.row_offset = 0
11+
self.column_offset = 0
12+
self.colors = Colors.get_cell_colors()
1213
self.rotation_state = 0
13-
self.colors = Colors.get_tile_colors()
1414

15-
def tile_positions(self):
16-
tiles = self.tiles[self.rotation_state]
17-
new_tiles = []
15+
def get_cells_positions(self):
16+
tiles = self.cells[self.rotation_state]
17+
moved_tiles = []
1818
for position in tiles:
19-
position = Position(position.row + self.offset.row, position.column + self.offset.column)
20-
new_tiles.append(position)
21-
return new_tiles
22-
23-
def rotate(self):
24-
self.rotation_state = (self.rotation_state + 1) % len(self.tiles)
25-
26-
def undo_rotate(self):
27-
self.rotation_state = (self.rotation_state -1) % len(self.tiles)
19+
position = Position(position.row + self.row_offset, position.column + self.column_offset)
20+
moved_tiles.append(position)
21+
return moved_tiles
2822

2923
def move(self, rows, columns):
30-
self.offset.row += rows
31-
self.offset.column += columns
24+
self.row_offset += rows
25+
self.column_offset += columns
3226

33-
def reset(self):
34-
self.rotation_state = 0
35-
self.offset.row = self.start_off_x
36-
self.offset.column = self.start_off_y
27+
def rotate(self):
28+
self.rotation_state += 1
29+
if self.rotation_state == len(self.cells):
30+
self.rotation_state = 0
3731

38-
def draw(self, screen):
39-
tiles = self.tile_positions()
40-
for tile in tiles:
41-
tile_rect = pygame.Rect(10 + (tile.column) * 25 + 1, 10 + tile.row* 25 +1, 24, 24)
42-
pygame.draw.rect(screen, self.colors[self.id], tile_rect)
32+
def undo_rotation(self):
33+
self.rotation_state -= 1
34+
if self.rotation_state == -1:
35+
self.rotation_state = len(self.cells) - 1
4336

44-
def draw_small_icon(self, screen):
45-
tiles = self.tile_positions()
37+
def draw(self, screen, offset_x, offset_y):
38+
tiles = self.get_cells_positions()
4639
for tile in tiles:
47-
offset_x = 0
48-
offset_y = 0
49-
if self.id == 2:
50-
offset_y = 15
51-
offset_x = -8
52-
elif self.id == 4:
53-
offset_x = -10
54-
tile_rect = pygame.Rect(offset_x + 240 + (tile.column) * 20 , offset_y + 230 + tile.row* 20, 19, 19)
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)
5541
pygame.draw.rect(screen, self.colors[self.id], tile_rect)

blocks.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,77 +3,75 @@
33

44
class LBlock(Block):
55
def __init__(self):
6-
super().__init__(Position(0, 3), id=0)
7-
self.tiles = {
6+
super().__init__ ( id = 1)
7+
self.cells = {
88
0: [Position(0, 2), Position(1, 0), Position(1, 1), Position(1, 2)],
99
1: [Position(0, 1), Position(1, 1), Position(2, 1), Position(2, 2)],
1010
2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 0)],
1111
3: [Position(0, 0), Position(0, 1), Position(1, 1), Position(2, 1)]
1212
}
13+
self.move(0, 3)
1314

1415
class JBlock(Block):
1516
def __init__(self):
16-
super().__init__(Position(0,3), id=1)
17-
self.tiles = {
17+
super().__init__(id = 2)
18+
self.cells = {
1819
0: [Position(0, 0), Position(1, 0), Position(1, 1), Position(1, 2)],
1920
1: [Position(0, 1), Position(0, 2), Position(1, 1), Position(2, 1)],
2021
2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 2)],
2122
3: [Position(0, 1), Position(1, 1), Position(2, 0), Position(2, 1)]
2223
}
24+
self.move(0, 3)
25+
2326

2427
class IBlock(Block):
2528
def __init__(self):
26-
super().__init__(Position(-1, 3), id=2)
27-
self.tiles = {
29+
super().__init__(id = 3)
30+
self.cells = {
2831
0: [Position(1, 0), Position(1, 1), Position(1, 2), Position(1, 3)],
2932
1: [Position(0, 2), Position(1, 2), Position(2, 2), Position(3, 2)],
3033
2: [Position(2, 0), Position(2, 1), Position(2, 2), Position(2, 3)],
3134
3: [Position(0, 1), Position(1, 1), Position(2, 1), Position(3, 1)]
3235
}
33-
34-
class LBlock(Block):
35-
def __init__(self):
36-
super().__init__(Position(0, 3), id=3)
37-
self.tiles = {
38-
0: [Position(0, 2), Position(1, 0), Position(1, 1), Position(1, 2)],
39-
1: [Position(0, 1), Position(1, 1), Position(2, 1), Position(2, 2)],
40-
2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 0)],
41-
3: [Position(0, 0), Position(0, 1), Position(1, 1), Position(2, 1)]
42-
}
36+
self.move(-1, 3)
4337

4438
class OBlock(Block):
4539
def __init__(self):
46-
super().__init__(Position(0, 4), id = 4)
47-
self.tiles = {
40+
super().__init__(id = 4)
41+
self.cells = {
4842
0: [Position(0, 0), Position(0, 1), Position(1, 0), Position(1, 1)],
4943
}
44+
self.move(0, 4)
5045

5146
class SBlock(Block):
5247
def __init__(self):
53-
super().__init__(Position(0, 3), id=5)
54-
self.tiles = {
48+
super().__init__(id = 5)
49+
self.cells = {
5550
0: [Position(0, 1), Position(0, 2), Position(1, 0), Position(1, 1)],
5651
1: [Position(0, 1), Position(1, 1), Position(1, 2), Position(2, 2)],
5752
2: [Position(1, 1), Position(1, 2), Position(2, 0), Position(2, 1)],
5853
3: [Position(0, 0), Position(1, 0), Position(1, 1), Position(2, 1)]
5954
}
55+
self.move(0, 3)
6056

6157
class TBlock(Block):
6258
def __init__(self):
63-
super().__init__(Position(0, 3), id=6)
64-
self.tiles = {
59+
super().__init__(id = 6)
60+
self.cells = {
6561
0: [Position(0, 1), Position(1, 0), Position(1, 1), Position(1, 2)],
6662
1: [Position(0, 1), Position(1, 1), Position(1, 2), Position(2, 1)],
6763
2: [Position(1, 0), Position(1, 1), Position(1, 2), Position(2, 1)],
6864
3: [Position(0, 1), Position(1, 0), Position(1, 1), Position(2, 1)]
6965
}
66+
self.move(0, 3)
7067

7168
class ZBlock(Block):
7269
def __init__(self):
73-
super().__init__(Position(0, 3), id=7)
74-
self.tiles = {
70+
super().__init__(id = 7)
71+
self.cells = {
7572
0: [Position(0, 0), Position(0, 1), Position(1, 1), Position(1, 2)],
7673
1: [Position(0, 2), Position(1, 1), Position(1, 2), Position(2, 1)],
7774
2: [Position(1, 0), Position(1, 1), Position(2, 1), Position(2, 2)],
7875
3: [Position(0, 1), Position(1, 0), Position(1, 1), Position(2, 0)]
79-
}
76+
}
77+
self.move(0, 3)

colors.py

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
class Colors:
2-
empty = (26, 31, 40)
3-
green = (47, 230, 23)
4-
orange = (226, 116, 17)
5-
red = (232, 18, 18)
6-
purple = (166, 0, 247)
7-
yellow = (237, 234, 4)
8-
cyan = (21, 204, 209)
9-
blue = (13, 64, 216)
10-
white = (255, 255, 255)
11-
black = (0, 0, 0)
12-
grey = (15, 15, 15)
13-
dark_blue = (44, 44, 127)
14-
light_blue = (59, 85, 162)
2+
dark_grey = (26, 31, 40)
3+
green = (47, 230, 23)
4+
orange = (226, 116, 17)
5+
red = (232, 18, 18)
6+
purple = (166, 0, 247)
7+
yellow = (237, 234, 4)
8+
cyan = (21, 204, 209)
9+
blue = (13, 64, 216)
10+
white = (255, 255, 255)
11+
dark_blue = (44, 44, 127)
12+
light_blue = (59, 85, 162)
1513

16-
@classmethod
17-
def get_tile_colors(cls):
18-
return [cls.empty, cls.green, cls.orange, cls.red, cls.purple, cls.yellow, cls.cyan, cls.blue]
14+
@classmethod
15+
def get_cell_colors(cls):
16+
return [cls.dark_grey, cls.green, cls.orange, cls.red, cls.purple, cls.yellow, cls.cyan, cls.blue]

game.py

Lines changed: 68 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,103 @@
1-
from game_grid import GameGrid
2-
import random
1+
from grid import Grid
32
from blocks import *
3+
import random, pygame
44

55
class Game:
66
def __init__(self):
7-
self.grid = GameGrid(20, 10)
7+
self.grid = Grid()
88
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
99
self.current_block = self.get_random_block()
1010
self.next_block = self.get_random_block()
1111
self.game_over = False
1212
self.score = 0
13+
self.rotate_sound = pygame.mixer.Sound('Sounds/rotate.ogg')
14+
self.clear_sound = pygame.mixer.Sound('Sounds/clear.ogg')
15+
self.rotate_sound.set_volume(0.1)
16+
self.clear_sound.set_volume(0.3)
17+
pygame.mixer.music.load('Sounds/music.ogg')
18+
pygame.mixer.music.play(-1)
1319

14-
def block_fits(self):
15-
tiles = self.current_block.tile_positions()
16-
for position in tiles:
17-
if self.grid.is_empty(position.row, position.column) == False :
20+
def get_random_block(self):
21+
if len(self.blocks)==0:
22+
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
23+
block = random.choice(self.blocks)
24+
self.blocks.remove(block)
25+
return block
26+
27+
def block_inside(self):
28+
tiles = self.current_block.get_cells_positions()
29+
for tile in tiles:
30+
if self.grid.is_inside(tile.row, tile.column) == False:
1831
return False
1932
return True
2033

21-
def rotate_block_cw(self):
22-
self.current_block.rotate()
23-
if self.block_fits() == False:
24-
self.current_block.undo_rotate()
34+
def block_fits(self):
35+
tiles = self.current_block.get_cells_positions()
36+
for tile in tiles:
37+
if self.grid.is_empty(tile.row, tile.column) == False:
38+
return False
39+
return True
2540

26-
def set_current_block(self):
27-
self.current_block = self.next_block
28-
self.current_block.reset()
29-
self.next_block = self.get_random_block()
30-
self.next_block.reset()
41+
def draw(self, screen):
42+
self.grid.draw(screen)
43+
self.current_block.draw(screen, 11, 11)
44+
if self.next_block.id == 3:
45+
self.next_block.draw(screen, 255, 290)
46+
elif self.next_block.id == 4:
47+
self.next_block.draw(screen, 255, 280)
48+
else:
49+
self.next_block.draw(screen, 270, 270)
3150

3251
def move_left(self):
3352
self.current_block.move(0, -1)
34-
if self.block_fits() == False:
53+
if self.block_inside() == False or self.block_fits() == False:
3554
self.current_block.move(0, 1)
3655

3756
def move_right(self):
3857
self.current_block.move(0, 1)
39-
if self.block_fits() == False:
58+
if self.block_inside() == False or self.block_fits() == False:
4059
self.current_block.move(0, -1)
4160

42-
def place_block(self):
43-
tiles = self.current_block.tile_positions()
61+
def move_down(self):
62+
self.current_block.move(1, 0)
63+
if self.block_inside() == False or self.block_fits() == False:
64+
self.current_block.move(-1, 0)
65+
self.lock_block()
66+
67+
def rotate(self):
68+
self.current_block.rotate()
69+
if self.block_inside() == False or self.block_fits() == False:
70+
self.current_block.undo_rotation()
71+
else:
72+
self.rotate_sound.play()
73+
74+
def lock_block(self):
75+
tiles = self.current_block.get_cells_positions()
4476
for position in tiles:
4577
self.grid.grid[position.row][position.column] = self.current_block.id
46-
cleared_rows = self.grid.clear_full_rows()
78+
rows_cleared = self.grid.clear_full_rows()
79+
if rows_cleared > 0:
80+
self.clear_sound.play()
81+
self.update_score(rows_cleared, 0)
82+
self.current_block = self.next_block
83+
self.next_block = self.get_random_block()
84+
if self.block_fits() == False:
85+
self.game_over = True
4786

48-
if cleared_rows == 1:
49-
self.score += 40
50-
elif cleared_rows == 2:
87+
def update_score(self, lines_cleared, move_down_points):
88+
89+
if lines_cleared == 1:
5190
self.score += 100
52-
elif cleared_rows == 3:
91+
elif lines_cleared == 2:
5392
self.score += 300
54-
elif cleared_rows == 4:
55-
self.score += 1200
56-
57-
self.set_current_block()
93+
elif lines_cleared == 3:
94+
self.score += 500
5895

59-
def move_down(self):
60-
self.current_block.move(1,0)
61-
if self.block_fits() == False:
62-
for tile in self.current_block.tile_positions():
63-
if tile.row == 1:
64-
self.game_over = True
65-
self.current_block.move(-1, 0)
66-
self.place_block()
67-
self.score += 4
96+
self.score += move_down_points
6897

6998
def reset(self):
7099
self.grid.reset()
71-
self.set_current_block()
72-
self.score = 0
73-
self.game_over = False
74100
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
75101
self.current_block = self.get_random_block()
76102
self.next_block = self.get_random_block()
77-
78-
def get_random_block(self):
79-
if len(self.blocks) != 0:
80-
return self.blocks.pop(random.randrange(len(self.blocks)))
81-
else:
82-
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
83-
return self.blocks.pop(random.randrange(len(self.blocks)))
103+
self.score = 0

0 commit comments

Comments
 (0)