0

I've been following a youtube tutorial on how to create a chessboard but it doesn't display my board or my pieces

I have two separate python files one called ChessMain which handles user input and outputs the current game state and ChessEngine which stores the information about the current state of the chess game. ChessMain code:

import pygame as p
from Chess import ChessEngine


width = height = 400  #512 alt
dimension = 8  #8x8
sq_size = height // dimension
max_fps = 15
images = {}


def load_images():
    pieces = ["wp", "wR", "wN", "wB", "wQ", "wK", "bp", "bR", "bN", "bB", "bQ", "bK"]
    for piece in pieces:
        images[piece] = p.transform.scale(p.image.load("images/" + piece + ".png"), (sq_size, sq_size))
        print("images/" + piece + ".png")

def main():
    p.init()
    screen = p.display.set_mode((width, height))
    clock = p.time.Clock()
    screen.fill(p.Color(255,255,255))
    gs = ChessEngine.GameState()
    load_images()
    running = True
    while running:
        for i in p.event.get():
            if i.type == p.QUIT:
                running = False
        drawgamestate(screen, gs)
        clock.tick(max_fps)
        p.display.flip()


def drawgamestate(screen, gs):
    drawboard(screen)
    drawpieces(screen, gs.board)


def drawboard(screen):
    colours = [p.Color(255,255,255), p.Color(128,128,128)]
    for i in range(dimension):
        for e in range(dimension):
            colour = colours[(i+e) % 2]
            p.draw.rect(screen, colour, p.Rect(e * sq_size, i * sq_size, sq_size, sq_size))


def drawpieces(screen, board):
    for i in range(dimension):
        for e in range(dimension):
            piece = board[i][e]
            if piece != "--":
                screen.blit(images[piece], p.Rect(i * sq_size, e * sq_size, sq_size, sq_size))


if __name__ == "__main__":
    main()

ChessEngine:

class GameState:
    def __init__(self):
        self.board = [
            #first character is colour second is type
                ["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"],
                ["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
                ["--", "--", "--", "--", "--", "--", "--", "--"],
                ["--", "--", "--", "--", "--", "--", "--", "--"],
                ["--", "--", "--", "--", "--", "--", "--", "--"],
                ["--", "--", "--", "--", "--", "--", "--", "--"],
                ["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
                ["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"]]
        self.whiteToMove = True
        self.moveLog = []

here is a link to the youtube tutorial https://www.youtube.com/watch?v=EnYui0e73Rs

0 Answers0