-
Notifications
You must be signed in to change notification settings - Fork 0
/
Square.py
92 lines (84 loc) · 2.82 KB
/
Square.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
import pygame
from Colors import *
class Square:
def __init__(self, i, j):
self.i = i
self.j = j
self.value = 0
self.new = False
def show(self, w, screen):
"""
Displays the square with its specific color
:param w: int
:param screen: surface
:return: None
"""
x = self.i * w
y = self.j * w
rect = pygame.Rect(x, y, w, w)
pygame.draw.rect(screen, BG, rect)
if self.new:
if self.value > 0:
rect.inflate(-50, -50)
pygame.draw.rect(screen, SqrColors[self.value], rect)
rect.inflate(10, 10)
pygame.draw.rect(screen, SqrColors[self.value], rect)
self.new = False
else:
if self.value > 0:
pygame.draw.rect(screen, SqrColors[self.value], rect)
pygame.draw.rect(screen, BORDER, rect, 10)
def display_value(self, screen, font, w):
"""
Display the value of the square
:param screen: surface
:param font: font
:param w: int
:return: None
"""
if self.value > 0:
textsurface = font.render(str(self.value), True, TEXT_COLOR1)
if self.value > 4:
textsurface = font.render(str(self.value), True, TEXT_COLOR2)
text_rect = textsurface.get_rect(center=(w * self.i + 1 // 2 + 50, w * self.j + 1 // 2 + 50))
screen.blit(textsurface, text_rect)
def search_index(self, i, j, grid):
"""
Search the cell's index in the vector
based on the given parameters
:param i: int
:param j: int
:param grid: list
:return: int
"""
for square in range(len(grid)):
if grid[square].i == i and grid[square].j == j:
return square
return -1
def move(self, grid, move_dir):
"""
moves every cell to the given destination,
can merge cells into each others
:param grid: list
:param move_dir: tuple
:return: bool (True if we moved a cell)
:return: int (points)
"""
index = self.search_index(self.i + move_dir[0], self.j + move_dir[1], grid)
if index >= 0:
cell = grid[index]
# Check if can merge
if self.value == cell.value and cell.value != 0 and not self.new:
cell.value *= 2
cell.new = True
self.value = 0
pygame.mixer.music.play()
return True, cell.value
# Move cell to next
elif cell.value == 0:
cell.value = self.value
self.value = 0
if cell.value == 0:
return False, 0
return True, 0
return False, 0