Pygame - Dudas sobre ciclo de eventos
Publicado por Juan Sebastian (2 intervenciones) el 25/10/2019 00:52:26
Antes que nada este es mi primer post y pido de antemano disculpas, no sé como funciona muy bien esto de publicar en foros (en especial no se si al publicar esto el código aparezca con el formato que es debido), sin alargarme más publico un pequeño avance utilizando la librería de "pygame" para la creación de un juego de ajedrez, he estado batallando para lograr que al seleccionar una pieza muestre los movimientos disponibles que puede realizar pero no veo manera de hacer que cambien de color las casillas de movimiento disponible de la ficha, espero me puedan ayudar y aconsejar sobre este tema.
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
import pygame as pg
import sys
#RESOLUCIÓN
screen_width = 480
screen_height = 480
#DEFINICIÓN DE COLORES
GREEN = (113, 141, 84)
WHITE = (236, 236, 214)
GRAY = (116, 116, 116)
surf = pg.display.set_mode((screen_width, screen_height))
surf.fill(GRAY)
square_side = screen_width/8
color_squareA = GREEN
color_squareB = WHITE
chessboard = [
['BR','BH','BB','BQ','BK','BB','BH','BR'],
['BP','BP','BP','BP','BP','BP','BP','BP'],
[None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None],
[None,None,None,None,None,None,None,None],
['WP','WP','WP','WP','WP','WP','WP','WP'],
['WR','WH','WB','WQ','WK','WB','WH','WR'],
]
def DrawChessboard():
x = 0
y = 0
aux = True
for f in range(8):
x = 0
for c in range(8):
# print(y)
# print(aux)
if aux == False:
pg.draw.rect(surf, color_squareA, (x,y,square_side, square_side))
x += square_side
aux = True
else:
pg.draw.rect(surf, color_squareB, (x,y,square_side, square_side))
x += square_side
aux = False
if aux == True:
aux = False
else:
aux = True
y += square_side
def LoadChessPieces():
for i in range(8):
for j in range(8):
if chessboard[i][j] == 'WP':
img = pg.image.load("ChessPieces/chesspiece_05.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'BP':
img = pg.image.load("ChessPieces/chesspiece_11.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'WR':
img = pg.image.load("ChessPieces/chesspiece_04.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'BR':
img = pg.image.load("ChessPieces/chesspiece_10.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'WH':
img = pg.image.load("ChessPieces/chesspiece_03.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'BH':
img = pg.image.load("ChessPieces/chesspiece_09.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'WB':
img = pg.image.load("ChessPieces/chesspiece_02.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'BB':
img = pg.image.load("ChessPieces/chesspiece_08.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'WQ':
img = pg.image.load("ChessPieces/chesspiece_01.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'BQ':
img = pg.image.load("ChessPieces/chesspiece_07.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'WK':
img = pg.image.load("ChessPieces/chesspiece_00.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
if chessboard[i][j] == 'BK':
img = pg.image.load("ChessPieces/chesspiece_06.png")
img = pg.transform.scale(img,(int(square_side),int(square_side)))
surf.blit(img,(j*square_side,i*square_side))
def MovControl(x,y):
if chessboard[y][x] == 'WP':
# pg.draw.rect(surf, GRAY, (x*square_side,(y-1)*square_side,square_side, square_side))
# pg.draw.rect(surf, GRAY, (x*square_side,(y-2)*square_side,square_side, square_side))
print("Seleccionaste un peón blanco")
if chessboard[y][x] == 'BP':
print("Seleccionaste un peón negro")
if chessboard[y][x] == 'WR':
print("Seleccionaste una torre blanca")
if chessboard[y][x] == 'BR':
print("Seleccionaste una torre negra")
if chessboard[y][x] == 'WH':
print("Seleccionaste un caballo blanco")
if chessboard[y][x] == 'BH':
print("Seleccionaste un caballo negro")
if chessboard[y][x] == 'WB':
print("Seleccionaste un alfil blanco")
if chessboard[y][x] == 'BB':
print("Seleccionaste una alfil negro")
if chessboard[y][x] == 'WQ':
print("Seleccionaste una reina blanca")
if chessboard[y][x] == 'BQ':
print("Seleccionaste una reina negra")
if chessboard[y][x] == 'WK':
print("Seleccionaste un rey blanco")
if chessboard[y][x] == 'BK':
print("Seleccionaste un rey negro")
def main():
pg.init()
pg.display.set_caption("Test")
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
sys.exit()
if event.type == pg.MOUSEBUTTONDOWN:
x = int(pg.mouse.get_pos()[0]/square_side)
y = int(pg.mouse.get_pos()[1]/square_side)
MovControl(x,y)
DrawChessboard()
LoadChessPieces()
pg.display.update()
main()
Valora esta pregunta


0