forked from Samanthajulian/classic-tetris
-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
96 lines (83 loc) · 3.41 KB
/
game.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
from grid import Grid
from blocks import *
import random
class Game:
def __init__(self):
self.grid = Grid()
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
self.current_block = self.get_random_block()
self.next_block = self.get_random_block()
self.game_over = False
self.score = 0
def update_score(self, lines_cleared, move_down_points):
if lines_cleared == 1:
self.score += 100
elif lines_cleared == 2:
self.score += 300
elif lines_cleared == 3:
self.score += 500
self.score += move_down_points
def get_random_block(self):
if len(self.blocks) == 0:
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
block = random.choice(self.blocks)
self.blocks.remove(block)
return block
#for blocks to move with keyboard and stay inside the grid
def move_left(self):
self.current_block.move(0, -1)
if self.block_inside() == False or self.block_fits() == False:
self.current_block.move(0, 1)
def move_right(self):
self.current_block.move(0, 1)
if self.block_inside() == False or self.block_fits() == False:
self.current_block.move(0, -1)
def move_down(self):
self.current_block.move(1, 0)
if self.block_inside() == False or self.block_fits() == False:
self.current_block.move(-1, 0)
self.lock_block()
#for blocks to stay in position when they reach the floor
def lock_block(self):
tiles = self.current_block.get_cell_positions()
for position in tiles:
self.grid.grid[position.row][position.column] = self.current_block.id
self.current_block = self.next_block
self.next_block = self.get_random_block()
rows_cleared = self.grid.clear_full_rows()
if rows_cleared > 0:
self.update_score(rows_cleared, 0)
if self.block_fits() == False:
self.game_over = True
def reset(self):
self.grid.reset()
self.blocks = [IBlock(), JBlock(), LBlock(), OBlock(), SBlock(), TBlock(), ZBlock()]
self.current_block = self.get_random_block()
self.next_block = self.get_random_block()
self.score = 0
#checks if block is on an empty cell or not
def block_fits(self):
tiles = self.current_block.get_cell_positions()
for tile in tiles:
if self.grid.is_empty(tile.row, tile.column) == False:
return False
return True
def rotate(self):
self.current_block.rotate()
if self.block_inside() == False or self.block_fits() == False:
self.current_block.undo_rotation()
def block_inside(self):
tiles = self.current_block.get_cell_positions()
for tile in tiles:
if self.grid.is_inside(tile.row, tile.column) == False:
return False
return True
def draw(self, screen):
self.grid.draw(screen)
self.current_block.draw(screen, 11, 11)
if self.next_block.id == 3:
self.next_block.draw(screen, 255, 290)
elif self.next_block.id == 4:
self.next_block.draw(screen, 255, 280)
else:
self.next_block.draw(screen, 270, 270)