I'm currently writing a chess program in pygame and I'm trying to display text on the screen when someone makes an invalid move. However, when I make the code initialise the font inside of the loop that draws the pieces on the screen, it takes 8 seconds to initialise the font every time someone makes a mistake, making the experience sluggish and slow.
The code that I've got at the moment has the font initialisation taking place at the top of the program but then the error font has not been initialised appears constantly and I've got no idea why. Any help on the issue would be great!
Written using python and the pygame module by the way.
import pygame as p
from Kess import chessEngine
WIDTH = HEIGHT = 512 # 512 is a power of 2 and is easily divisible by 8 for the squares of the ui
DIMENSION = 8
SQ_SIZE = HEIGHT // DIMENSION
MAX_FPS = 15 # for animations - may be able to delete later
IMAGES = {}
WHITE = (237, 240, 207)
BLACK = (108, 155, 79)
TEXT_COLOUR = (0, 0, 0)
MOVING = False
invalid = False
gs = chessEngine.GameState()
font = p.font.SysFont('timesnewromanbold', 32)
'''
Global dictionary of images which will be called once. Can contain multiple image sets for theme
customisation
'''
def load_images():
pieces = ["bR", "bN", "bB", "bQ", "bK", "bP",
"wR", "wN", "wB", "wQ", "wK", "wP"]
for piece in pieces:
IMAGES[piece] = p.transform.scale(p.image.load("images/" + piece + ".png"), (SQ_SIZE, SQ_SIZE))
# The p.transform.scale function scales the imported images to fit the dimensions of the square
# The for loop allows for the pieces to be imported together rather than a long list of individual calls.
def main():
global player_click_region
p.init()
# Initiates the pygame.
screen = p.display.set_mode((WIDTH, HEIGHT))
# Sets the display screen to be 512x512 as the set variables at the top of the file.
clock = p.time.Clock()
# Starts the inbuilt chess clock.
screen.fill(WHITE)
# Sets a background with a colour of white.
load_images()
running = True
# This while loop draws the black tiles on the board by alternating the beginning x value (beginning 64x64
# square coordinate) and then skipping a tile (white tile) and doing the same.
draw_pieces(screen)
square_selected = () # Keeps track of what square in what row/column the user has currently selected
player_click_region = [] # Keeps track of original square and destination square e.g. (2, 4) (4, 2)
while running:
clock.tick(60)
for event in p.event.get():
if event.type == p.QUIT:
running = False
elif event.type == p.MOUSEBUTTONDOWN:
location = p.mouse.get_pos() # Returns (x,y) pos of mouse
# print(location)
column = location[0] // SQ_SIZE
row = location[1] // SQ_SIZE
# print(row, column)
if square_selected == (row, column):
square_selected = ()
player_click_region = []
else:
square_selected = (row, column)
player_click_region.append(square_selected)
# print(piece)
if len(player_click_region) == 2:
location = p.mouse.get_pos()
piece = gs.board[player_click_region[0][0]][player_click_region[0][1]]
valid_move(piece)
destination_column = location[0] // SQ_SIZE
destination_row = location[1] // SQ_SIZE
if invalid is True:
print("INVALID MOVE: PLEASE TRY AGAIN")
else:
gs.board[destination_row][destination_column] = piece
gs.board[player_click_region[0][0]][player_click_region[0][1]] = "--"
player_click_region = []
square_selected = ()
print(gs.board)
draw_pieces(screen)
p.display.update()
# Update board using pairs of tuples...
def draw_pieces(screen):
global font
x = 1
y = 0
while y < DIMENSION:
while x < DIMENSION:
p.draw.rect(screen, BLACK, (x * SQ_SIZE, y * SQ_SIZE, SQ_SIZE, SQ_SIZE))
# Ask Mr. Clarke how to access a specific list within a class of another file and then how to get an index
# from within that list.
x += 2
if x == 9:
x = 0
else:
x = 1
y += 1
y = 0
x = 0
while y < DIMENSION:
while x < DIMENSION:
p.draw.rect(screen, WHITE, (x * SQ_SIZE, y * SQ_SIZE, SQ_SIZE, SQ_SIZE))
# Ask Mr. Clarke how to access a specific list within a class of another file and then how to get an index
# from within that list.
x += 2
if x == 9:
x = 0
else:
x = 1
y += 1
y = 0
x = 0
while y < DIMENSION:
while x < DIMENSION:
piece = gs.board[y][x]
if piece != "--":
screen.blit(IMAGES[piece], (x * SQ_SIZE, y * SQ_SIZE, SQ_SIZE, SQ_SIZE))
# Ask Mr. Clarke how to access a specific list within a class of another file and then how to get an index
# from within that list.
x += 1
x = 0
y += 1
if invalid is True:
text = font.render('INVALID MOVE', True, TEXT_COLOUR)
screen.blit(text, (HEIGHT/2, WIDTH/2))
def valid_move(piece):
global invalid
# print(gs.board[player_click_region[1][0]][player_click_region[1][1]])
invalid = False
change_x = abs(player_click_region[0][0] - player_click_region[1][0]) # abs() finds absolute value - always pos
change_y = abs(player_click_region[0][1] - player_click_region[1][1])
linear_x = player_click_region[0][0] - player_click_region[1][0]
# linear_y = player_click_region[0][1] - player_click_region[1][1]
destination_piece = gs.board[player_click_region[1][0]][player_click_region[1][1]]
print("x change", str(change_x))
print("y change", str(change_y))
if piece[0] == destination_piece[0]:
invalid = True
print("invalid 3")
if "P" in piece:
if change_x != 1 and change_x != 2:
invalid = True
else:
if player_click_region[0][0] != 6 and player_click_region[0][0] != 1:
if change_x == 2:
invalid = True
if "b" in piece:
if player_click_region[0][0] == 1:
if gs.board[player_click_region[0][0]+1][player_click_region[0][1]] != "--":
invalid = True
if linear_x > 0:
invalid = True
if "w" in piece:
if player_click_region[0][0] == 6:
if gs.board[player_click_region[0][0]-1][player_click_region[0][1]] != "--":
invalid = True
print("1")
if linear_x < 0:
invalid = True
print("2")
if invalid is False:
if change_y == 0:
if destination_piece != "--":
invalid = True
if change_y == 1:
if change_x != 1:
invalid = True
if destination_piece == "--":
invalid = True
else:
print("capturing now")
if change_y > 1:
invalid = True
if "B" in piece:
if change_x != change_y:
invalid = True
print("you don messed up boy")
else:
i = 1
if linear_x < 0:
for i in range(i, change_x):
if gs.board[player_click_region[0][0]+i][player_click_region[0][1]+i] != "--":
invalid = True
i += 1
i = 1
if linear_x > 0:
for i in range(i, change_x):
if gs.board[player_click_region[0][0]-i][player_click_region[0][1]-i] != "--":
invalid = True
if "N" in piece:
if change_x + change_y != 3:
invalid = True
if change_x > 0 and change_y > 0:
if destination_piece != "--":
print("capture_piece()")
else:
invalid = True
if "R" in piece:
if change_x > 0 and change_y > 0:
invalid = True
else:
x = 1
if change_x > 0:
for x in range(x, change_x):
print(player_click_region[0][0]+x)
if player_click_region[0][0] > player_click_region[1][0]:
print("now uve done messed up")
if gs.board[player_click_region[1][0]+x][player_click_region[1][1]] != "--":
invalid = True
else:
print(gs.board[player_click_region[0][0] + x][player_click_region[0][1]])
if gs.board[player_click_region[0][0]+x][player_click_region[0][1]] != "--":
print(gs.board[player_click_region[0][0]+x][player_click_region[0][1]])
invalid = True
x+=1
else:
for x in range(x, change_y-1):
print(player_click_region)
if player_click_region[0][1] > player_click_region[1][1]:
if gs.board[player_click_region[1][0]][player_click_region[1][1]+x] != "--":
invalid = True
else:
if gs.board[player_click_region[0][0]][player_click_region[0][1]+x] != "--":
invalid = True
x+=1
if "Q" in piece:
print(change_x, change_y)
if change_x == change_y:
print("how does this work")
i = 1
if linear_x < 0:
for i in range(i, change_x):
if gs.board[player_click_region[0][0] + i][player_click_region[0][1] + i] != "--":
invalid = True
print("invalid 2")
i += 1
i = 1
if linear_x > 0:
for i in range(i, change_x):
if gs.board[player_click_region[0][0] - i][player_click_region[0][1] - i] != "--":
invalid = True
print("invalid 1")
elif change_x + change_y == 1:
if destination_piece != "--":
print("Capture")
# Check whether the piece taken is the same colour or not
else:
print("Empty Square")
else:
print("fucked up")
if change_x > 0 and change_y > 0:
invalid = True
else:
x = 1
if change_x > 0:
for x in range(x, change_x):
if player_click_region[0][0] > player_click_region[1][0]:
if gs.board[player_click_region[1][0] + x][player_click_region[1][1]] != "--":
invalid = True
else:
if gs.board[player_click_region[0][0] + x][player_click_region[0][1]] != "--":
invalid = True
x += 1
else:
for x in range(x, change_y - 1):
print(player_click_region)
if player_click_region[0][1] > player_click_region[1][1]:
if gs.board[player_click_region[1][0]][player_click_region[1][1] + x] != "--":
invalid = True
else:
if gs.board[player_click_region[0][0]][player_click_region[0][1] + x] != "--":
invalid = True
x += 1
if "K" in piece:
if change_x > 1 or change_y > 1:
invalid = True
# To implement the images. the coordinates of the tiles are 64x64 and you can multiply them by a number for the x value
# which would then move them over a certain number of tiles (e.g. 64*3 would give 3 tiles to the right), and same for
# the y values.
main()