-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
66 lines (52 loc) · 1.73 KB
/
Copy pathmain.py
File metadata and controls
66 lines (52 loc) · 1.73 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
"""
Main game file
"""
import sys
from random import random
import pygame
from game_ui import GameUI
from piece import Turn, calculate_position
from constants import COLS, PADDING, RED, ROWS, SQUARE_HEIGHT,\
SQUARE_WIDTH,WHITE_BASE, WIDTH, HEIGHT
# Initialize Pygame
pygame.init()
# Initialize the screen
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Checkers")
def get_row_col_from_mouse(pos):
x, y = pos
row = ROWS - (y // SQUARE_HEIGHT) - 1
col = min(COLS - 1, x // SQUARE_WIDTH)
return row, col
def draw_mouse_under(screen, turn: Turn, r: int, c: int):
radius = min(SQUARE_WIDTH, SQUARE_HEIGHT) // 2 - PADDING
x, y = calculate_position(r, c)
color = RED if turn == Turn.RED else WHITE_BASE
pygame.draw.circle(screen, color, (x, y), radius)
def main():
run = True
clock = pygame.time.Clock()
turn = Turn(random() < 0.5)
game = GameUI(screen, turn)
while run and game.winner is None:
clock.tick(60) # Limit the frame rate to 60 FPS
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
_,col = get_row_col_from_mouse(pos)
game.select(col)
if game.winner is not None:
continue
game.update()
_, y = get_row_col_from_mouse(pygame.mouse.get_pos())
if game.current_state.is_move_legal(y):
h = game.current_state.board.get_height(y)
draw_mouse_under(screen, game.current_state.turn, h, y)
pygame.display.flip()
print(game.winner)
pygame.quit()
sys.exit()
if __name__ == "__main__":
main()