Skip to content

Commit 1af1461

Browse files
Rendering performance boost
1 parent aa1dc6e commit 1af1461

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Diff for: gui.py

+14-7
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,23 @@ def __init__(self, board, size, background_image, tiles):
99
self.size = size
1010
self.background_image = background_image
1111
self.tiles = tiles
12+
self.cached = None
1213

1314
def render(self):
14-
surface = pygame.Surface(self.size)
15-
surface.fill((255, 255, 255))
16-
surface.blit(self.background_image, (0, 0))
17-
for y, row in enumerate(self.board.grid):
18-
for x, value in enumerate(row):
19-
if value > 0:
20-
surface.blit(tiles[value - 1], (x *32, y * 32))
15+
if self.board.changed:
16+
surface = pygame.Surface(self.size)
17+
surface.fill((255, 255, 255))
18+
surface.blit(self.background_image, (0, 0))
19+
for y, row in enumerate(self.board.grid):
20+
for x, value in enumerate(row):
21+
if value > 0:
22+
surface.blit(tiles[value - 1], (x *32, y * 32))
23+
self.cached = surface
24+
self.board.changed = False
25+
else:
26+
surface = self.cached
2127
try:
28+
surface = surface.copy()
2229
piece = self.board.current_piece
2330
value = piece.color
2431
for point in piece.get_world_pattern():

Diff for: tetris_core.py

+2
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ def __init__(self, size, color_count):
141141
self.grid = [[0 for x in range(self.width)] for y in range(self.height)]
142142
self.color_count = color_count
143143
self.cleared_lines = 0
144+
self.changed = True
144145

145146
def set_next_piece(self):
146147
pattern = random.choice(TetrisPiece.Patterns)
@@ -160,6 +161,7 @@ def merge_piece(self, piece):
160161
self.grid[point.y][point.x] = piece.color
161162
self.last_piece_position = piece.position
162163
self.set_next_piece()
164+
self.changed = True
163165

164166
def move_piece_down_if_time(self, threshold, piece):
165167
if get_ticks() - self.last_down_time > threshold:

0 commit comments

Comments
 (0)