forked from raulc03/Ubongo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fichas.py
157 lines (126 loc) · 4.09 KB
/
Fichas.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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import re
import pygame
# import backtracking_ubongo
def readTextFile():
def add_shape(all_figures, figure, shape):
figure["shape"] = shape
id = figure["id"]
all_figures[id] = figure
print(figure)
re_num = re.compile(r"Num:")
re_name = re.compile(r"Name:")
re_color = re.compile(r"Color:")
re_shape = re.compile(r"Shape:")
filename = "Fichas/fichas.txt"
all_figures = {}
figure = {}
shape = []
shape_mode = False
with open(filename, "rt") as fp:
for line1 in fp.readlines():
line2 = line1.strip()
if 0 == len(line2):
if shape:
add_shape(all_figures, figure, shape)
shape = []
shape_mode = False
elif re_num.match(line2):
temp = line2.split(":")
second = temp[1].strip()
figure = {"id": int(second)}
figure["name"] = second
elif re_name.match(line2):
temp = line2.split(":")
figure["name"] = temp[1].strip()
elif re_color.match(line2):
temp = line2.split(":")
second = temp[1].strip()
figure["color"] = second
elif re_shape.match(line2):
shape_mode = True
elif shape_mode:
shape.append([int(c) for c in line2])
if shape:
add_shape(all_figures, figure, shape)
return all_figures
class CFigura:
x = 0
y = 0
ini_x = 0
ini_y = 0
_id = 0
col = (0, 0, 0)
image = None
mat = []
ma_pos = []
def __init__(self, x, y, n, filename):
self.x = x
self.y = y
self.ini_x = self.x
self.ini_y = self.y
self._id = n
self.imagen(filename, True)
if self._id == 4 or self._id == 6 or self._id == 10:
self.col = self.image.get_at((51, 25))
else:
self.col = self.image.get_at((0, 0))
self.formaMatriz()
def formaMatriz(self):
ancho, alto = self.image.get_rect().size
self.mat = [[-1 for i in range(ancho // 50)] for j in range(alto // 50)]
for i in range(0, alto // 50):
for j in range(0, ancho // 50):
pixelcol = self.image.get_at((j * 50, i * 50))
if pixelcol == self.col:
self.mat[i][j] = self._id
def imagen(self, filename, transparent=False):
self.image = pygame.image.load(filename)
self.image = self.image.convert()
self.width = self.image.get_width()
self.height = self.image.get_height()
if transparent:
# color = self.image.get_at((0, 0))
self.image.set_colorkey((255, 255, 255), pygame.RLEACCEL)
def cargarImg(self, screen):
if self.image is None:
filename = f"Fichas/Ficha{self._id}.png"
self.imagen(filename, transparent=True)
screen.blit(self.image, (self.x, self.y))
def rotarImg(self, angulo):
if angulo == 0:
self.image = pygame.transform.rotate(self.image, 90)
else:
self.image = pygame.transform.rotate(self.image, -90)
def voltearImg(self):
self.image = pygame.transform.flip(self.image, True, False)
def reescalarImg(self):
if self._id == 1:
self.image = pygame.transform.scale(self.image, (150, 50))
def acomodarImg(self):
self.x -= self.x % 50
self.y -= self.y % 50
def getId(self):
return self._id
def getCol(self):
return self.col
def getPos(self):
return self.x, self.y
def setPos(self, x, y):
self.x = x
self.y = y
def getImage(self):
return self.image
# measured in pixels
def getWidth(self):
return self.width
def getHeight(self):
return self.height
def getMat(self):
return self.mat
def getMatPos(self):
return self.ma_pos
def setMatPos(self, pos):
self.ma_pos = pos
def setIniPos(self):
self.x = self.ini_x
self.y = self.ini_y