0

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?

Rabbid76
  • 202,892
  • 27
  • 131
  • 174
43zombiegit
  • 115
  • 1
  • 10

1 Answers1

0

You cannot delete the parts after they have been moved. The objects are just pixels on the display. You must redraw the entire scene. It is recommended to redraw the scene in every frame. The typical PyGame application loop has to:

def show_board(input, board):
    input.blit(board, (0, 0))
    # pygame.display.flip() <---- DELETE
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)

    running = False
    while not running:

        # handle events and update the game states
        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:
                # print("Mouse up")
                moving = False
                picture = old_picture
                (row, col) = get_mouse()
                chess_board[row][col] = piece
                chess_board[old_position[0]][old_position[1]] = 0

        # clear the entire display or draw the background
        input.blit(background, (0, 0))

        # draw the entire scene (2blit2 all the objects)
        show_board(input, board)
        show_pieces(input)
        
        # update the display
        pygame.display.update()
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Good answer @Rabbid76, only issue now is that the pieces constantly flicker as they constantly refresh, any ideas? – 43zombiegit Dec 12 '20 at 14:16
  • @43zombiegit Sou you've another issue. My code is not flickering. See [Why is the PyGame animation is flickering](https://stackoverflow.com/questions/62120723/why-is-the-pygame-animation-is-flickering/62120776#62120776) – Rabbid76 Dec 12 '20 at 14:19
  • I got it now, had to include the bottom functions in the for loop! – 43zombiegit Dec 12 '20 at 14:22
  • @43zombiegit No! Why do you do that. What a pity. Why do you ignore my advice. You have to draw the scene in the application loop. But call `pygame.display.update()` only once at the end of the loop and remove all other calls of `pygame.display.update()` and `pygame.display.flip()` from your code – Rabbid76 Dec 12 '20 at 14:46
  • @43zombiegit Use my code as stated in the answer, but remove `pygame.display.flip()` from `show_board`. – Rabbid76 Dec 12 '20 at 14:47