我正在使用OOP在pygame上进行二维国际象棋游戏。每当玩家移动时,我都无法让GUI类更新。 这是我课堂主要的主要游戏循环:
def main_loop(self):
while True:
self.ev = pg.event.get()
for event in self.ev:
if event.type == pg.MOUSEBUTTONDOWN:
self.pos = pg.mouse.get_pos()
print(self.pos)
self.onclick()
pg.display.flip()
存放棋盘所在位置的棋盘位置在棋盘上并在玩家点击棋子然后点击它可以移动到的有效方格后更新。然后调用GUI类的draw_board函数。 这是GUI draw_board函数:
def draw_board(self):
#Draw the board
print("GUI update")
for i in range(0,8):
for j in range(0,8):
print()
print("Piece at:")
print(i,j)
print(self.main.piece_at(i,j))
pg.draw.rect(self.window, self.sq_colour[i][j],(self.X,self.Y,self.SQ_DIM,self.SQ_DIM),0)
#print(i,j)
#print(self.main.piece_at(i,j))
if self.main.piece_at(i,j) == 0:
print("square empty")
pass
else:
print("square occupied")
self.window.blit(self.dict[self.main.piece_at(i,j)],(self.X,self.Y))
#Change of X coord
if self.X >(self.SQ_DIM*6):
self.X = 0
self.Ycounter = True
self.sqcounter = True
else:
self.X +=(self.SQ_DIM)
#Change of Y coord
if self.Ycounter == True:
self.Y +=self.SQ_DIM
self.Ycounter = False
else:
pass
单击鼠标时,将调用此代码。 GUI.SQ_DIM是一个常量,用于存储我在GUI中使用的方块的尺寸(50x50像素):
def onclick(self):
#Convert the coordinates suitable for 2d array
x = (self.pos[0]//self.GUI.SQ_DIM)
y = (self.pos[1]//self.GUI.SQ_DIM)
print(y,x)
print("current player")
print(self.crnt_player)
if self.crnt_player == "White":
self.crnt_side = self.pieces.white_pieces
print("selected before")
print(self.selectedB4)
if self.selectedB4 == False:
if x <8:
if (self.piece_at(y,x) in self.pieces.white_pieces) or self.piece_at(y,x) == 0:
self.highlight(y,x)
else:
self.highlight(y,x)
else:
self.crnt_side = self.pieces.black_pieces
if self.selectedB4 == False:
if x <8:
if (self.piece_at(y,x) in self.pieces.black_pieces) or self.piece_at(y,x) == 0:
self.highlight(y,x)
else:
self.highlight(y,x)
def highlight(self,y,x):
print("Call highlight")
#Check if there is an already selected piece
if self.selectedB4 == False:
self.checking = False
#Check if the mouse clicked outside the chessboard
if self.pos[0] > self.GUI.SQ_DIM*8:
print("outside boundary")
return
#Highlight a selected piece
if self.piece_at(y,x) == 0:
print("nothing there")
pass
elif self.piece_at(y,x) != 0:
print("highlight selected")
self.board.prv_pos_update(x,y)
self.GUI.window.blit(self.pieces.square,(x*self.GUI.SQ_DIM,y*self.GUI.SQ_DIM))
self.GUI.window.blit(self.dict[self.piece_at(y,x)],(x*self.GUI.SQ_DIM,y*self.GUI.SQ_DIM))
self.selectedB4 = True
#Calculate possible moves
self.calcMoves(y,x)
#Deselect piece
else:
self.color = self.colour_at(x,y)
print("self color")
print(self.color)
self.checking = False
if y < 8 and y > -1 and x < 8 and x > -1:
#If same piece is picked, de highlight
print("selection")
print(self.piece_at(y,x) == self.piece_at(self.prv_pos_at(1),self.prv_pos_at(0)))
if self.piece_at(y,x) == self.piece_at(self.prv_pos_at(1),self.prv_pos_at(0)):
pass
#If another destination picked, move
elif self.color != self.GUI.BLACK and self.color != self.GUI.WHITE and self.color != self.GUI.OLIVEGREEN:
print("Update board")
self.Update_board(y,x)
self.selectedB4 = False
self.GUI.draw_board()
这是类main中调用它的代码,y是垂直坐标,而x是电路板上的水平坐标。当移动时,板上所有位置的所有部分都被打印出来,并且确实表明该部件已移动。然而,GUI并没有显示并保持静态:
if y < 8 and y > -1 and x < 8 and x > -1:
#If same piece is picked, de highlight
print("selection")
print(self.piece_at(y,x) == self.piece_at(self.prv_pos_at(1),self.prv_pos_at(0)))
if self.piece_at(y,x) == self.piece_at(self.prv_pos_at(1),self.prv_pos_at(0)):
pass
#If another destination picked, move
elif self.color != self.GUI.BLACK and self.color != self.GUI.WHITE and self.color != self.GUI.OLIVEGREEN:
print("Update board")
self.Update_board(y,x)
self.selectedB4 = False
self.GUI.draw_board()
当我调用Update_board时,调用此函数从类板运行,该类板将棋子的所有位置存储在棋盘上。
def UpdatePos(self,x,y):
self.piece_pos[y][x] = self.piece_pos[self.prv_pos[1]][self.prv_pos[0]]
print("modified to")
print(self.piece_pos[y][x])
self.piece_pos[self.prv_pos[1]][self.prv_pos[0]] = 0
if self.main.crnt_player == "White":
self.main.crnt_player = "Black"
else:
self.main.crnt_player = "White"
print("Current player")
print(self.main.crnt_player)
the GUI when click on a piece and when I click on a valid destination (the light green square)