-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
163 lines (119 loc) · 5.33 KB
/
main.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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pygame
import sys
from const import *
from game import Game
from square import Square
from move import Move
class Main:
def __init__(self):
pygame.init()
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.running = True
self.game = Game()
pygame.display.set_caption("Reev Chess")
def mainloop(self):
screen = self.screen
game = self.game
board = self.game.board
dragger = self.game.dragger
while self.running:
#show methods
game.show_background(screen)
game.show_last_moves(screen)
game.show_moves(screen)
game.show_pieces(screen)
game.show_hover(screen)
if dragger.dragging:
dragger.update_blit(screen)
for event in pygame.event.get():
#mouse events
#mouse button down / click
if event.type == pygame.MOUSEBUTTONDOWN:
# print(event.pos)
dragger.update_mouse(event.pos)
clicked_row = dragger.mouseY // SQSIZE
clicked_col = dragger.mouseX // SQSIZE
if board.squares[clicked_row][clicked_col].has_piece():
# print("has piece")
piece = board.squares[clicked_row][clicked_col].piece
# valid piece color ? /find next player turn
if piece.color == game.next_player:
board.calc_moves(piece, clicked_row, clicked_col, bool=True)
dragger.save_initial(event.pos)
dragger.drag_piece(piece)
#show methods
game.show_background(screen)
game.show_last_moves(screen)
game.show_moves(screen)
game.show_pieces(screen)
game.show_hover(screen)
#mouse motion / drag
elif event.type == pygame.MOUSEMOTION:
motion_row = event.pos[1] // SQSIZE
motion_col = event.pos[0] // SQSIZE
game.set_hover(motion_row, motion_col)
if dragger.dragging:
dragger.update_mouse(event.pos)
#show methods
game.show_background(screen)
game.show_last_moves(screen)
game.show_moves(screen)
game.show_pieces(screen)
game.show_hover(screen)
dragger.update_blit(screen)
#mouse button up / drop / leave /release
elif event.type == pygame.MOUSEBUTTONUP:
if dragger.dragging:
dragger.update_mouse(event.pos)
released_row = dragger.mouseY // SQSIZE
released_col = dragger.mouseX // SQSIZE
#check if piece was dropped on a valid square / create possible move
initial = Square(dragger.initial_row, dragger.initial_col)
final = Square(released_row, released_col)
move = Move(initial, final)
#check if move is valid
if board.valid_move(dragger.piece, move):
# print("valid move")
#normal capture
captured = board.squares[released_row][released_col].has_piece()
board.move(dragger.piece, move)
board.set_true_enpassant(dragger.piece)
game.play_sound(captured)
#show methods
game.show_background(screen)
game.show_last_moves(screen)
game.show_pieces(screen)
#change player turn
game.next_turn()
dragger.update_blit(screen)
dragger.drop_piece()
# print("dropped piece")
#quit game
elif event.type == pygame.QUIT:
self.running = False
pygame.quit()
sys.exit()
#keyboard events
#key down
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_t:
game.change_theme()
if event.key == pygame.K_r:
game.reset()
screen = self.screen
game = self.game
board = self.game.board
dragger = self.game.dragger
if event.key == pygame.K_q:
self.running = False
pygame.quit()
sys.exit()
#key up
elif event.type == pygame.KEYUP:
pass
pygame.display.update()
main = Main()
main.mainloop()
# Path: main.py