-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.py
278 lines (220 loc) · 7.37 KB
/
sprites.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import pygame
import os
import defaults as df
import random
import time
import var
class Sprite():
def __init__(self):
self.file = ""
self.rect = []
self.image = []
self.w = 0
self.h = 0
def load_image(self):
self.image = pygame.image.load(self.file)
self.image = pygame.transform.scale(self.image, (int(self.w), int(self.h)))
self.rect = self.image.get_rect()
def draw(self):
var.frame.gameDisplay.blit(self.image, (self.rect.x, self.rect.y))
class Background(Sprite):
def __init__(self):
Sprite.__init__(self)
self.file = df.assetsDir + 'gradient-1761190_1280.jpg'
self.w = df.display_width
self.h = df.display_height
self.load_image()
self.rect.x = 0
self.rect.y = 0
print('created Background from:',self.file)
class Piece(Sprite):
def __init__(self, key, team, file1, file2):
Sprite.__init__(self)
self.subdir = 'pieces/'
if team =='w':filename = file1
else: filename = file2
self.file = df.assetsDir + self.subdir + filename
self.w = 75
self.h = self.w
self.load_image()
x, y = var.layer_board.get_xy_from_key(key)
var.layer_board.empty_square(key,False)
self.rect.x = x
self.rect.y = y
self.team = team
self.clicked = False
self.origin = (self.rect.x, self.rect.y)
self.key = key
print('created King:', self.file)
self.selected = False
def eat(self,key,enemy):
if self.rules(key):
print('comiendo enemigo:', type(self), type(enemy), key)
return True
return False
def move(self, to_key):
var.layer_board.empty_square(self.key,True)
var.layer_board.empty_square(to_key,False)
self.rect.x, self.rect.y = var.layer_board.get_xy_from_key(to_key)
self.selected = False
self.key = to_key
def highlight(self,color):
s = pygame.Surface((self.rect.w, self.rect.h))
s.set_alpha(50)
s.fill(color)
pygame.draw.rect(var.frame.gameDisplay, color, [self.rect.x, self.rect.y, self.rect.w, 3])
pygame.draw.rect(var.frame.gameDisplay, color, [self.rect.x, self.rect.y, 3, self.rect.h])
pygame.draw.rect(var.frame.gameDisplay, color, [self.rect.x, self.rect.y + self.rect.h, self.rect.w, 3])
pygame.draw.rect(var.frame.gameDisplay, color, [self.rect.x + self.rect.w, self.rect.y, 3, self.rect.h])
# var.gameDisplay.blit(s, (self.rect.x, self.rect.y))
def onClick(self,mouse):
if self.rect.collidepoint(mouse.get_pos()) == 1:
if mouse.get_pressed()[0]:
self.highlight(df.red)
# self.animating = True
# print('button clicked', self.file)
# self.index = 0
time.sleep(0.1)
return True
# else:
# self.highlight_color = self.hover_color
# if self.animating:self.animate()
# self.draw_sprite2()
# if self.animating or not self.clicked:
# return #continue running
# else:
# self.clicked = False
# return True #clicken event is true
return False
def get_cartesian(self,key):
self.x0 = int(ord(self.key[0]))
self.x1 = int(ord(key[0]))
self.y0 = int(self.key[1])
self.y1 = int(key[1])
def rules(self, key):
pass
class King(Piece):
def __init__(self, key, team):
file1 = 'wking.png'
file2 = 'bking.png'
Piece.__init__(self, key, team, file1, file2)
print('created:', self.file)
def rules(self, key):
self.get_cartesian(key)
print('Moviendo', type(self), 'de', self.key, 'hacia', key)
x1 = self.x1
x0 = self.x0
y1 = self.y1
y0 = self.y0
if abs(x0-x1) <2 and abs(y0 - y1) < 2:
return True
else:
print('Movimiento invalido para', type(self))
return False
class Queen(Piece):
def __init__(self, key, team):
file1 = 'wqueen.png'
file2 = 'bqueen.png'
Piece.__init__(self, key, team, file1, file2)
print('created:', self.file)
def rules(self, key):
self.get_cartesian(key)
print('Moviendo', type(self), 'de', self.key, 'hacia', key)
x1 = self.x1
x0 = self.x0
y1 = self.y1
y0 = self.y0
if x1 == x0 or y1== y0 or abs(x0 - x1) == abs(y0 - y1):
return True
else:
print('Movimiento invalido para', type(self))
return False
class Rook(Piece):
def __init__(self, key, team):
file1 = 'wrook.png'
file2 = 'brook.png'
Piece.__init__(self, key, team, file1, file2)
print('created:', self.file)
def rules(self, key):
self.get_cartesian(key)
print('Moviendo', type(self), 'de', self.key, 'hacia', key)
x1 = self.x1
x0 = self.x0
y1 = self.y1
y0 = self.y0
if x1 == x0 or y1 == y0:
return True
else:
print('Movimiento invalido para', type(self))
return False
class Bishop(Piece):
def __init__(self, key, team):
file1 = 'wbishop.png'
file2 = 'bbishop.png'
Piece.__init__(self, key, team, file1, file2)
print('created:', self.file)
def rules(self, key):
self.get_cartesian(key)
x1 = self.x1
x0 = self.x0
y1 = self.y1
y0 = self.y0
print('Moviendo', type(self), 'de', self.key, 'hacia', key)
if abs(x0 - x1) == abs(y0- y1):
return True
else:
print('Movimiento invalido para', type(self))
return False
class Knight(Piece):
def __init__(self, key, team):
file1 = 'wknight.png'
file2 = 'bknight.png'
Piece.__init__(self, key, team, file1, file2)
print('created:', self.file)
def rules(self, key):
print('Moviendo', type(self), 'de', self.key, 'hacia', key)
self.get_cartesian(key)
x1 = self.x1
x0 = self.x0
y1 = self.y1
y0 = self.y0
if abs(x1 - x0)**2 + abs(y1 - y0)**2 == 5:
return True
else:
print('Movimiento invalido para', type(self))
return False
class Pawn(Piece):
def __init__(self, key, team):
file1 = 'wpawn.png'
file2 = 'bpawn.png'
Piece.__init__(self, key, team, file1, file2)
print('created:', self.file)
def rules(self, key):
# pass
print('Moviendo', type(self), 'de', self.key, 'hacia', key)
self.get_cartesian(key)
x1 = self.x1
x0 = self.x0
y1 = self.y1
y0 = self.y0
if self.team =='w':factor = 1
else: factor = -1
if abs(x1-x0) ==0 and y1-y0 == factor:
return True
else:
print('Movimiento invalido para',type(self))
return False
def eat(self, key, enemy):
print('comiendo enemigo:', type(self), type(enemy),key)
self.get_cartesian(key)
x1 = self.x1
x0 = self.x0
y1 = self.y1
y0 = self.y0
if self.team =='w':factor = 1
else: factor = -1
if abs(x1-x0) ==1 and y1-y0 == factor:
return True
else:
print('Movimiento invalido',type(self))
return False