-
Notifications
You must be signed in to change notification settings - Fork 0
/
four_in_a_row.py
138 lines (104 loc) · 3.43 KB
/
four_in_a_row.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
"""
The GUI version of the four in a row game.
Reuses the connect module for game logic.
The game rendering is handled by pygame library.
"""
import pygame
import sys, math
from pprint import pprint
from connect import *
pygame.init
FPS = 20
FramePerSec = pygame.time.Clock()
BLACK = 0, 0, 0
BLUE = 0, 0, 255
RED = 255, 0, 0
GREEN = 0, 255, 0
YELLOW = 255, 255, 0
SQUARE_SIZE = 100
RAD = int(SQUARE_SIZE / 2 - 5)
def init(row, col):
"""Initialize the pygame screen.
The size of the screen is calculated from row and col
and SQUARE_SIZE
"""
width = col * SQUARE_SIZE
height = (row + 1) * SQUARE_SIZE
size = (width, height)
pygame.init()
return pygame.display.set_mode(size)
def create_show_victor(screen):
""" Returns a function that displays the victor on the screen """
myfont = pygame.font.SysFont("monospace", 75)
def show_victor(player):
width = COL * SQUARE_SIZE
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARE_SIZE))
msg = f"Player {player} wins !"
print(msg)
label = myfont.render(msg, 1, GREEN)
screen.blit(label, (40, 10))
return show_victor
def create_draw_board(screen):
""" Returns a function that draws the game board on the pygame screen """
circle_colors = {0: BLACK, 1: YELLOW, 2: RED}
def draw_board(board):
row, col = size(board)
print(board)
for r in range(row):
for c in range(col):
pygame.draw.rect(
screen,
BLUE,
(
c * SQUARE_SIZE,
r * SQUARE_SIZE + SQUARE_SIZE,
SQUARE_SIZE,
SQUARE_SIZE,
),
)
color = circle_colors[board[r][c]]
pygame.draw.circle(
screen,
color,
(
c * SQUARE_SIZE + SQUARE_SIZE / 2,
r * SQUARE_SIZE + SQUARE_SIZE + SQUARE_SIZE / 2,
),
RAD,
)
pygame.display.update()
return draw_board
def choose_column(player):
circle_colors = {0: BLACK, 1: YELLOW, 2: RED}
col = None
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
# Follow the mouse pointer and draw a circle of the current player
if event.type == pygame.MOUSEMOTION:
width = COL * SQUARE_SIZE
pygame.draw.rect(screen, BLACK, (0, 0, width, SQUARE_SIZE))
x, _ = event.pos
color = circle_colors[player]
pygame.draw.circle(screen, color, (x, SQUARE_SIZE / 2), RAD)
pygame.display.update()
# Choose column in case of a mouse click
if event.type == pygame.MOUSEBUTTONDOWN:
x, _ = event.pos
col = math.floor(x / SQUARE_SIZE)
print(col)
return col
if __name__ == "__main__":
ROW, COL = 6, 7
screen = init(ROW, COL)
board = create_board(ROW, COL)
draw_board = create_draw_board(screen)
show_victor = create_show_victor(screen)
four_in_a_row = create_four_in_a_row(choose_column, draw_board, show_victor)
game_over = False
draw_board(board)
while not game_over:
game_over = four_in_a_row(board)
FramePerSec.tick(FPS)
pygame.time.wait(3000)