-
-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathgame.py
More file actions
128 lines (113 loc) · 3.52 KB
/
Copy pathgame.py
File metadata and controls
128 lines (113 loc) · 3.52 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from grid import Grid
from blocks import *
import random
import pygame
from constants import (
GAME_SPEED_MS, MIN_SPEED_MS, SPEED_STEP, SPEED_SCORE_INTERVAL,
GRID_OFFSET_X, GRID_OFFSET_Y, HIGHSCORE_FILE
)
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.paused = False
self.score = 0
self.high_score = self.load_high_score()
self.rotate_sound = None
self.clear_sound = None
try:
self.rotate_sound = pygame.mixer.Sound("Sounds/rotate.ogg")
self.clear_sound = pygame.mixer.Sound("Sounds/clear.ogg")
pygame.mixer.music.load("Sounds/music.ogg")
pygame.mixer.music.play(-1)
except:
pass
def load_high_score(self):
try:
with open(HIGHSCORE_FILE, "r") as f:
return int(f.read())
except:
return 0
def save_high_score(self):
try:
with open(HIGHSCORE_FILE, "w") as f:
f.write(str(self.high_score))
except:
pass
def get_speed(self):
reduction = (self.score // SPEED_SCORE_INTERVAL) * SPEED_STEP
return max(MIN_SPEED_MS, GAME_SPEED_MS - reduction)
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
if self.score > self.high_score:
self.high_score = self.score
self.save_high_score()
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
def move_left(self):
self.current_block.move(0, -1)
if not self.block_inside() or not self.block_fits():
self.current_block.move(0, 1)
def move_right(self):
self.current_block.move(0, 1)
if not self.block_inside() or not self.block_fits():
self.current_block.move(0, -1)
def move_down(self):
self.current_block.move(1, 0)
if not self.block_inside() or not self.block_fits():
self.current_block.move(-1, 0)
self.lock_block()
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:
if self.clear_sound:
self.clear_sound.play()
self.update_score(rows_cleared, 0)
if not self.block_fits():
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
def block_fits(self):
tiles = self.current_block.get_cell_positions()
for tile in tiles:
if not self.grid.is_empty(tile.row, tile.column):
return False
return True
def rotate(self):
self.current_block.rotate()
if not self.block_inside() or not self.block_fits():
self.current_block.undo_rotation()
else:
if self.rotate_sound:
self.rotate_sound.play()
def block_inside(self):
tiles = self.current_block.get_cell_positions()
for tile in tiles:
if not self.grid.is_inside(tile.row, tile.column):
return False
return True
def draw(self, screen):
self.grid.draw(screen)
self.current_block.draw(screen, GRID_OFFSET_X, GRID_OFFSET_Y)