-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
124 lines (101 loc) · 4.14 KB
/
game.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
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import pygame
from const import *
from board import Board
from dragger import Dragger
from config import Config
from square import Square
class Game:
def __init__(self):
self.next_player = 'white'
self.hovered_sqr = None
self.board = Board()
self.dragger = Dragger()
self.config = Config()
# blit methods
def show_bg(self, surface):
theme = self.config.theme
for row in range(ROWS):
for col in range(COLS):
# color
color = theme.bg.light if (row + col) % 2 == 0 else theme.bg.dark
# rect
rect = (col * SQSIZE, row * SQSIZE, SQSIZE, SQSIZE)
# blit
pygame.draw.rect(surface, color, rect)
# row coordinates
if col == 0:
# color
color = theme.bg.dark if row % 2 == 0 else theme.bg.light
# label
lbl = self.config.font.render(str(ROWS-row), 1, color)
lbl_pos = (5, 5 + row * SQSIZE)
# blit
surface.blit(lbl, lbl_pos)
# col coordinates
if row == 7:
# color
color = theme.bg.dark if (row + col) % 2 == 0 else theme.bg.light
# label
lbl = self.config.font.render(Square.get_alphacol(col), 1, color)
lbl_pos = (col * SQSIZE + SQSIZE - 20, HEIGHT - 20)
# blit
surface.blit(lbl, lbl_pos)
def show_pieces(self, surface):
for row in range(ROWS):
for col in range(COLS):
# piece ?
if self.board.squares[row][col].has_piece():
piece = self.board.squares[row][col].piece
# all pieces except dragger piece
if piece is not self.dragger.piece:
piece.set_texture(size=80)
img = pygame.image.load(piece.texture)
img_center = col * SQSIZE + SQSIZE // 2, row * SQSIZE + SQSIZE // 2
piece.texture_rect = img.get_rect(center=img_center)
surface.blit(img, piece.texture_rect)
def show_moves(self, surface):
theme = self.config.theme
if self.dragger.dragging:
piece = self.dragger.piece
# loop all valid moves
for move in piece.moves:
# color
color = theme.moves.light if (move.final.row + move.final.col) % 2 == 0 else theme.moves.dark
# rect
rect = (move.final.col * SQSIZE, move.final.row * SQSIZE, SQSIZE, SQSIZE)
# blit
pygame.draw.rect(surface, color, rect)
def show_last_move(self, surface):
theme = self.config.theme
if self.board.last_move:
initial = self.board.last_move.initial
final = self.board.last_move.final
for pos in [initial, final]:
# color
color = theme.trace.light if (pos.row + pos.col) % 2 == 0 else theme.trace.dark
# rect
rect = (pos.col * SQSIZE, pos.row * SQSIZE, SQSIZE, SQSIZE)
# blit
pygame.draw.rect(surface, color, rect)
def show_hover(self, surface):
if self.hovered_sqr:
# color
color = (180, 180, 180)
# rect
rect = (self.hovered_sqr.col * SQSIZE, self.hovered_sqr.row * SQSIZE, SQSIZE, SQSIZE)
# blit
pygame.draw.rect(surface, color, rect, width=3)
# other methods
def next_turn(self):
self.next_player = 'white' if self.next_player == 'black' else 'black'
def set_hover(self, row, col):
self.hovered_sqr = self.board.squares[row][col]
def change_theme(self):
self.config.change_theme()
def play_sound(self, captured=False):
if captured:
self.config.capture_sound.play()
else:
self.config.move_sound.play()
def reset(self):
self.__init__()