Started work on website
[progcomp2013.git] / qchess / src / images.py
1 import pygame
2 import os
3
4 # Dictionary that stores the unicode character representations of the different pieces
5 # Chess was clearly the reason why unicode was invented
6 # For some reason none of the pygame chess implementations I found used them!
7 piece_char = {"white" : {"king" : u'\u2654',
8                          "queen" : u'\u2655',
9                          "rook" : u'\u2656',
10                          "bishop" : u'\u2657',
11                          "knight" : u'\u2658',
12                          "pawn" : u'\u2659',
13                          "unknown" : '?'},
14                 "black" : {"king" : u'\u265A',
15                          "queen" : u'\u265B',
16                          "rook" : u'\u265C',
17                          "bishop" : u'\u265D',
18                          "knight" : u'\u265E',
19                          "pawn" : u'\u265F',
20                          "unknown" : '?'}}
21
22 images = {"white" : {}, "black" : {}}
23 small_images = {"white" : {}, "black" : {}}
24
25 def create_images(grid_sz, font_name=os.path.join(os.path.curdir, "data", "DejaVuSans.ttf")):
26
27         # Get the font sizes
28         l_size = 5*(grid_sz[0] / 8)
29         s_size = 3*(grid_sz[0] / 8)
30
31         for c in piece_char.keys():
32                 
33                 if c == "black":
34                         for p in piece_char[c].keys():
35                                 images[c].update({p : pygame.font.Font(font_name, l_size).render(piece_char[c][p], True,(0,0,0))})
36                                 small_images[c].update({p : pygame.font.Font(font_name, s_size).render(piece_char[c][p],True,(0,0,0))})         
37                 elif c == "white":
38                         for p in piece_char[c].keys():
39                                 images[c].update({p : pygame.font.Font(font_name, l_size+1).render(piece_char["black"][p], True,(255,255,255))})
40                                 images[c][p].blit(pygame.font.Font(font_name, l_size).render(piece_char[c][p], True,(0,0,0)),(0,0))
41                                 small_images[c].update({p : pygame.font.Font(font_name, s_size+1).render(piece_char["black"][p],True,(255,255,255))})
42                                 small_images[c][p].blit(pygame.font.Font(font_name, s_size).render(piece_char[c][p],True,(0,0,0)),(0,0))
43         
44
45 def load_images(image_dir=os.path.join(os.path.curdir, "data", "images")):
46         if not os.path.exists(image_dir):
47                 raise Exception("Couldn't load images from " + image_dir + " (path doesn't exist)")
48         for c in piece_char.keys():
49                 for p in piece_char[c].keys():
50                         images[c].update({p : pygame.image.load(os.path.join(image_dir, c + "_" + p + ".png"))})
51                         small_images[c].update({p : pygame.image.load(os.path.join(image_dir, c + "_" + p + "_small.png"))})

UCC git Repository :: git.ucc.asn.au