-
Notifications
You must be signed in to change notification settings - Fork 0
/
GUI.py
168 lines (141 loc) · 6.12 KB
/
GUI.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
164
165
166
167
168
#################
# GUI Class #
#################
import pygame
import Board
# Get layout from board class so that there are not multiple instances of magic numbers
COL_COUNT = Board.COL_COUNT
ROW_COUNT = Board.ROW_COUNT
# COLORS defined with RGB values
BLUE = (0, 0, 255) # Background
BLACK = (0, 0, 0) # Player Slots
RED = (255, 0, 0) # Player 1
YELLOW = (255, 255, 0) # Player 2
# Square size in px (one side)
SQUARESIZE = 100
# COL_COUNT * SQUARESIZE
WIDTH = COL_COUNT * SQUARESIZE
# (ROW_COUNT+1) * SQUARESIZE
HEIGHT = (ROW_COUNT + 1) * SQUARESIZE
# Tuple to initilize pygame.display
SIZE = (WIDTH, HEIGHT)
# Best formula I found for good looking player slots
RADIUS = int(SQUARESIZE / 2 - 5)
class GUI:
"""This class encompases the graphical user interface and its methods.
:Attributes:
* :screen (*pygame.display*): GUI screen shown to user
* :SQUARESIZE (*int*): size of 1 square on the GUI screen
* :WIDTH (*int*): width of GUI screen
* :HEIGHT (*int*): height of GUI screen
* :SIZE (*tuple*): (WIDTH,HEIGHT) of GUI screen
* :RADIUS (*int*): size of game piece radius
* :BLUE (*RGB thruple*): BLUE color
* :BLACK (*RGB thruple*): BLACK color
* :RED (*RGB thruple*): RED color
* :YELLOW (*RGB thruple*): YELLOW color
* :BLUE (*RGB thruple*): BLUE color
* :FONT (*pygame.font*): FONT of text for GUI screen
"""
def __init__(self):
"""Constructor Method."""
pygame.init()
self.screen = pygame.display.set_mode(SIZE)
self.SQUARESIZE = SQUARESIZE
self.WIDTH = WIDTH
self.HEIGHT = HEIGHT
self.SIZE = SIZE
self.RADIUS = RADIUS
self.BLUE = BLUE
self.BLACK = BLACK
self.RED = RED
self.YELLOW = YELLOW
self.FONT = pygame.font.SysFont("monospace", 75)
def draw_board(self, board):
"""A function to draw the current board on the GUI screen.
:param board: board object to draw
:board type: :class:`Board.Board`
:return: *None*
"""
# Change title bar of pygame window for visual effect
pygame.display.set_caption("Connect-4")
# Draw black bar to cover any previous pieces above game board
pygame.draw.rect(self.screen, BLACK, (0, 0, WIDTH, SQUARESIZE))
for r in range(ROW_COUNT):
for c in range(COL_COUNT):
# Draw blue background
pygame.draw.rect(self.screen, BLUE,
(c * SQUARESIZE, r * SQUARESIZE + SQUARESIZE,
SQUARESIZE, SQUARESIZE))
if board.matrix[r][c] == 0:
# Draw BLACK circle if position is empty
pygame.draw.circle(
self.screen, BLACK,
(int(c * SQUARESIZE + SQUARESIZE / 2),
int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)),
RADIUS)
elif board.matrix[r][c] == 1:
# Draw RED circle if position belongs to Player 1
pygame.draw.circle(
self.screen, RED,
(int(c * SQUARESIZE + SQUARESIZE / 2),
int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)),
RADIUS)
elif board.matrix[r][c] == 2:
# Draw YELLOW circle if position belongs to Player 2
pygame.draw.circle(
self.screen, YELLOW,
(int(c * SQUARESIZE + SQUARESIZE / 2),
int(r * SQUARESIZE + SQUARESIZE + SQUARESIZE / 2)),
RADIUS)
# Update the pygame display so that the drawings show in window
pygame.display.update()
# A function to display the appropriate piece at the player's
def mouse_piece(self, mousePosition, turn):
"""A function to display the appropriate piece at the player's current mouse .
:param mousePosition: current mouse position on screen
:type mousePosition: int
:param turn: current turn count
:type turn: int
:return: *None*
"""
# Draw black bar to cover any previous pieces above game board
pygame.draw.rect(self.screen, BLACK, (0, 0, WIDTH, SQUARESIZE))
if turn % 2 == 0:
# Draw Red piece if it is Player 1's turn
pygame.draw.circle(self.screen, RED,
(mousePosition, int(SQUARESIZE / 2)), RADIUS)
else:
# Draw Yellow piece if it is Player 1's turn
mousePosition = (int(mousePosition * SQUARESIZE + SQUARESIZE / 2))
pygame.draw.circle(self.screen, YELLOW,
(mousePosition, int(SQUARESIZE / 2)), RADIUS)
pygame.display.update()
# A function to display a message to the winner, and close the game
def game_over(self, winner, waitTime):
""" A function to display a message to the winner, wait a few seconds, then close the game Window.
:param winner: player who won the game
:type winner: int
:param waitTime: time to wait before closing window (milliseconds)
:type waitTime: int
:return: *None*
"""
# Print win message on screen
if winner == 1:
winMessage = self.FONT.render("Player 1 Wins!", 1, RED)
self.screen.blit(winMessage, (18, 5))
else:
winMessage = self.FONT.render("Player 2 Wins!", 1, YELLOW)
self.screen.blit(winMessage, (18, 5))
# Update the window to display blit
pygame.display.update()
# Wait before closing window
self.wait(waitTime)
def wait(self, waitTime):
""" A function to make the screen wait for given time in milliseconds.
:param waitTime: time to wait before closing window (milliseconds)
:type waitTime: int
:return: *None*
"""
# Wait using pygame's time.wait function
pygame.time.wait(waitTime)