I have the following code:
import pygame
import engine
BOARD_POS = (10,10)
def initialise_board(input):
background = pygame.Surface((TILESIZE*10, TILESIZE*10))
# background.fill((222, 184, 135))
dark = False
for i in range(8):
for j in range(8):
rect = pygame.Rect(i * TILESIZE, j * TILESIZE, TILESIZE, TILESIZE)
if dark:
pygame.draw.rect(background, (150,75,0), rect)
else:
pygame.draw.rect(background, pygame.Color('white'), rect)
dark = not dark
dark = not dark
input.blit(background, (0, 0))
pygame.display.flip()
return background
def get_ring(background):
(row, col) = get_mouse()
startX = col*900/8
endX = (col+1)*900/8
startY = row*900/8
endY = (row+1)*900/8
pygame.draw.line(background, pygame.Color("red"), (startX, startY), (endX, startY), 5)
pygame.draw.line(background, pygame.Color("red"), (startX, startY), (startX, endY), 5)
pygame.draw.line(background, pygame.Color("red"), (startX, endY), (endX, endY), 5)
pygame.draw.line(background, pygame.Color("red"), (endX, startY), (endX, endY), 5)
def show_board(input, board):
input.blit(board, (0, 0))
pygame.display.flip()
def get_position(mouseX, mouseY):
global row, col
for i in range(8, 0, -1):
if mouseX <= i*900/8:
col = i-1
if mouseY <= i*900/8:
row = i-1
return (row, col)
def get_mouse():
(mouseX, mouseY) = pygame.mouse.get_pos()
(row, col) = get_position(mouseX, mouseY)
# print(row, col)
return (row, col)
def show_pieces(input):
for row in range(8):
for col in range(8):
piece = chess_board[row][col]
midX = col * 900/8
midY = row * 900/8
if piece != 0:
string_piece = ids[piece]
picture = string_to_pngs[string_piece]
picture = pygame.transform.scale(picture, (110, 110))
input.blit(picture, (midX, midY))
chess_board = [
[-4, -2, -3, -5, -6, -3, -2, -4], # 8 0
[-1, -1, -1, -1, -1, -1, -1, -1], # 7 1
[ 0, 0, 0, 0, 0, 0, 0, 0], # 6 2
[ 0, 0, 0, 0, 0, 0, 0, 0], # 5 3
[ 0, 0, 0, 0, 0, 0, 0, 0], # 4 4
[ 0, 0, 0, 0, 0, 0, 0, 0], # 3 5
[ 1, 1, 1, 1, 1, 1, 1, 1], # 2 6
[ 4, 2, 3, 5, 6, 3, 2, 4], # 1 7
# a b c d e f g h file/rank
# 0 1 2 3 4 5 6 7 index
]
b_bishop = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_bdt60.png")
w_bishop = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_blt60.png")
b_rook = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_rdt60.png")
w_rook = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_rlt60.png")
b_queen = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_qdt60.png")
w_queen = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_qlt60.png")
b_pawn = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_pdt60.png")
w_pawn = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_plt60.png")
b_king= pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_kdt60.png")
w_king = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_klt60.png")
b_knight = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_ndt60.png")
w_knight = pygame.image.load("C:/Users/Daniel/PycharmProjects/chess-engine/src/Pngs/Chess_nlt60.png")
string_to_pngs = {
"white knight" : w_knight,
"white bishop" : w_bishop,
"white rook" : w_rook,
"white queen" : w_queen,
"white king" : w_king,
"white pawn" : w_pawn,
"black knight" : b_knight,
"black bishop" : b_bishop,
"black rook" : b_rook,
"black queen" : b_queen,
"black king" : b_king,
"black pawn" : b_pawn,
}
ids = {
# White
1: "white pawn",
2: "white knight",
3: "white bishop",
4: "white rook",
5: "white queen",
6: "white king",
# Black
-1: "black pawn",
-2: "black knight",
-3: "black bishop",
-4: "black rook",
-5: "black queen",
-6: "black king"
}
# TODO : SHOW VALID MOVES
def run():
global TILESIZE
TILESIZE = 112.5
input = pygame.display.set_mode((900,900))
pygame.display.set_caption('Spleb')
pygame.init()
board = initialise_board(input)
show_board(input, board)
show_pieces(input)
running = False
while not running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = True
if event.type == pygame.MOUSEBUTTONDOWN:
# print("Mouse down")
moving = True
(row, col) = get_mouse()
old_position = (row, col)
piece = chess_board[row][col]
if piece != 0:
string_piece = ids[piece]
old_picture = string_to_pngs[string_piece]
old_picture = pygame.transform.scale(old_picture, (110, 110))
if event.type == pygame.MOUSEBUTTONUP and old_picture is not None:
moving = False
picture = old_picture
# print("Mouse up")
(row, col) = get_mouse()
chess_board[row][col] = piece
chess_board[old_position[0]][old_position[1]] = 0
midX = col * 900 / 8
midY = row * 900 / 8
input.blit(picture, (midX, midY))
show_pieces(input)
pygame.display.update()
This sets up the board and allows me to move the images but I can't get it to delete the pieces after they move. How would I do this? I'm really stuck. The initialise board function creates the board, get ring shows a red border around the mouse but this currently doesn't work so how would I also add this back in?