-
Notifications
You must be signed in to change notification settings - Fork 0
/
piece.py
36 lines (30 loc) · 1.13 KB
/
piece.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import os
import pygame
class Piece(pygame.sprite.Sprite):
def __init__(self, filename, cols, rows):
pygame.sprite.Sprite.__init__(self)
self.pieces = {
"white_pawn": 5,
"white_knight": 3,
"white_bishop": 2,
"white_rook": 4,
"white_king": 0,
"white_queen": 1,
"black_pawn": 11,
"black_knight": 9,
"black_bishop": 8,
"black_rook": 10,
"black_king": 6,
"black_queen": 7
}
self.spritesheet = pygame.image.load(filename).convert_alpha()
self.cols = cols
self.rows = rows
self.cell_count = cols * rows
self.rect = self.spritesheet.get_rect()
w = self.cell_width = self.rect.width // self.cols
h = self.cell_height = self.rect.height // self.rows
self.cells = list([(i % cols * w, i // cols * h, w, h) for i in range(self.cell_count)])
def draw(self, surface, piece_name, coords):
piece_index = self.pieces[piece_name]
surface.blit(self.spritesheet, coords, self.cells[piece_index])