-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathview.py
More file actions
73 lines (63 loc) · 2.4 KB
/
Copy pathview.py
File metadata and controls
73 lines (63 loc) · 2.4 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pygame
import sys
from constants import (
WIDTH, HEIGHT, CELL_SIZE, WHITE, IMAGE_OFFSET, RED
)
def app_init():
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption('chess game')
return screen
class BoardView():
def __init__(self, screen, chessboard_image):
self.chessboard_image = pygame.image.load(chessboard_image)
self.pieces = pygame.sprite.Group()
self.screen = screen
def update_chessboard(self, game):
"""
<chessboard> is a current state of game object
we have to translate abstract data {pawn : (1,1), ...}
to explicit e.g. {'pawn.img': Rect(124, 124, 45, 45)}
"""
self.pieces = pygame.sprite.Group()
self.selected_cell = None
selected_piece = game.get_selected_piece()
if selected_piece:
self.selected_cell = BoardView.coordinates_to_pixels(
*selected_piece.get_coordinates()
)
for piece in game.get_pieces():
x, y = BoardView.coordinates_to_pixels(
*piece.get_coordinates(), image=True
)
self.pieces.add(
PieceView(piece.get_type(), piece.get_color(), x, y)
)
def highlight_cell(self, cell, color):
cell_rect = pygame.Rect(cell, (CELL_SIZE, CELL_SIZE))
pygame.draw.rect(self.screen, color, cell_rect, 5)
def draw(self):
self.pieces.draw(self.screen)
if self.selected_cell:
self.highlight_cell(self.selected_cell, RED)
@classmethod
def coordinates_to_pixels(cls, x, y, image=False):
if not image:
return x * CELL_SIZE, y * CELL_SIZE
return x * CELL_SIZE + IMAGE_OFFSET, y * CELL_SIZE + IMAGE_OFFSET
@classmethod
def pixels_to_coordinates(self, x, y):
return x // CELL_SIZE, y // CELL_SIZE
def update_screen(self, game):
self.update_chessboard(game)
self.screen.fill(WHITE)
self.screen.blit(self.chessboard_image, (0, 0))
self.draw()
pygame.display.flip()
class PieceView(pygame.sprite.Sprite):
def __init__(self, type_, color, x, y):
# x, y are pixel coordinates
super(PieceView, self).__init__()
image_filepath = 'static/{}_{}.png'.format(type_, color)
self.image = pygame.image.load(image_filepath)
self.rect = self.image.get_rect().move(x, y)