I'm programming chess, but the board isn't updating when I make a move, despite my putting the instruction "pygame.display.flip()".
As far as I can tell, the array I'm using updates the position of the piece, but for some reason this doesn't translate to the screen.
import pygame
import time
import os
import threading
import queue
pygame.init()
BLACK = (255, 0, 0)
WHITE = (255, 255, 255)
screen_width = 800
screen_height = 600
board_top_left_x = screen_width//5
board_top_left_y = screen_height//10
square_size = screen_height//10
screen = pygame.display.set_mode([screen_width, screen_height])
os.environ['SDL_VIDEO_WINDOWS_POS'] = '10, 10'
commands = queue.Queue
class Input(threading.Thread):
def run(self):
while True:
command = input()
commands.put(command)
class square(pygame.sprite.Sprite):
def __init__(self, colour):
super().__init__()
self.image = pygame.Surface([square_size, square_size])
self.rect = self.image.get_rect()
pygame.draw.rect(self.image, colour, [0, 0, square_size, square_size])
self.colour = colour
def load_image(name):
f = pygame.image.load(name + ".png")
return f
def set_up_board(board):
board[0][0] = "Rw"
board[1][0] = "Nw"
board[2][0] = "Bw"
board[3][0] = "Qw"
board[4][0] = "Kw"
board[5][0] = "Bw"
board[6][0] = "Nw"
board[7][0] = "Rw"
board[0][7] = "Rb"
board[1][7] = "Nb"
board[2][7] = "Bb"
board[3][7] = "Qb"
board[4][7] = "Kb"
board[5][7] = "Bb"
board[6][7] = "Nb"
board[7][7] = "Rb"
for i in range(8):
board[i][1] = "Pw"
board[i][6] = "Pb"
return board
def draw_board(board, squares):
squares.draw(screen)
for row in range(len(board)):
for col in range(len(board)):
if board[col][row] != " ":
i = load_image(board[col][row])
i = pygame.transform.scale(i, (square_size, square_size))
i.set_colorkey(WHITE)
screen.blit(i, [square_size*col + board_top_left_x, square_size*(7-row) + board_top_left_y])
pygame.display.flip()
def main():
squares = pygame.sprite.Group()
col = BLACK
I = Input()
I.start()
for i in range(8):
for j in range(8):
if col == BLACK:
col = WHITE
else:
col = BLACK
x = i*square_size + board_top_left_x
y = j*square_size + board_top_left_y
s = square(col)
s.rect.x = x
s.rect.y = y
squares.add(s)
if col == BLACK:
col = WHITE
else:
col = BLACK
board = []
for i in range(8):
a = []
for j in range(8):
a.append(' ')
board.append(a)
board = set_up_board(board)
draw_board(board, squares)
print("What move do you want to make?")
answered = False
while not answered:
try:
ans = commands.get(False)
answered = True
except queue.Empty:
ans = None
board[ord(ans[2]) - 97][int(ans[3]) - 1] = board[ord(ans[0]) - 97][int(ans[1]) - 1]
board[ord(ans[0]) - 97][int(ans[1]) - 1] = " "
draw_board(board, squares)
time.sleep(5)
main()
I would expect this to update the screen with the move, so long as it's legal. From my error checking, it executes the second if statement, and gives me the correct p.x and p.y after they have been changed. What have I missed?
[White bishop image][1][Black bishop image][1][Black king image][1][White king image][1][Black knight image][1][White knight image][1][Black pawn image][1][White pawn image][1][Black queen image][1][White queen image][1][Black rook image][1][White rook image][1]
Note: the input move is in the form "coordinates of piece, coordinates of move", e.g: d2d4
Note2: I have also tried this with piece_list instead of board, and the instruction piece_list.draw(screen) instead of the nested for loops. It has given me exactly the same results.