-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.py
109 lines (93 loc) · 4.1 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
import sys
import tkinter
import tkinter.messagebox
import pygame
import board
darkcolor = (201, 143, 186)
lightcolor = (247, 242, 137 )
color_1 = ( 214, 76, 133)
color_2 = (39, 35, 3 )
class Pane:
def __init__(self, row_count, column_count, square_size):
self.board = board.Board(row_count, column_count)
self.square_size = square_size
self.radius = square_size // 2 - 5
self.width = column_count * square_size
self.height = (row_count + 1) * square_size
self.offset = square_size
self.circle_offset = square_size // 2
self.screen = pygame.display.set_mode((self.width, self.height))
def mybg(self):
for r in range(self.board.row_count):
for c in range(self.board.column_count):
left = c * self.square_size
top = r * self.square_size + self.offset
pygame.draw.rect(self.screen, lightcolor, (left, top, self.square_size, self.square_size))
pygame.draw.circle(self.screen, darkcolor, (left + self.circle_offset, top + self.circle_offset), self.radius)
pygame.display.update()
def fill_mypos(self):
for r in range(self.board.row_count):
for c in range(self.board.column_count):
if self.board.grid[r, c] == 1:
current_color = color_1
elif self.board.grid[r, c] == 2:
current_color = color_2
else:
current_color = darkcolor
x_position = c * self.square_size + self.circle_offset
y_position = self.height - (r * self.square_size + self.circle_offset)
pygame.draw.circle(self.screen, current_color, (x_position, y_position), self.radius)
pygame.display.update()
def track_mouse_motion(self, x_position, current_color):
pygame.draw.rect(self.screen, darkcolor, (0, 0, self.width, self.square_size))
pygame.draw.circle(self.screen, current_color, (x_position, self.circle_offset), self.radius)
pygame.display.update()
def drop_mypos(self, x_position, turn):
column_selection = x_position // self.square_size
if self.board.crct_pos(column_selection):
row = self.board.next_mypos(column_selection)
self.board.drop_piece(row, column_selection, turn)
return True
return False
def reset(self):
self.screen = pygame.display.set_mode((self.width, self.height))
self.board.reset()
self.mybg()
self.fill_mypos()
def popup_result(winner = False):
title = 'Game Over!'
if winner:
message = f'Player {winner} wins! Would you like to start again?'
else:
message = 'The match was a draw. Would you like to start again?'
return tkinter.messagebox.askyesno(title=title, message=message)
def main():
tkinter.Tk().wm_withdraw()
pygame.init()
pygame.display.set_caption('Connect 4')
pane = Pane(6, 7, 90)
pane.mybg()
continue_playing = True
turn = 1
current_color = color_1
while continue_playing:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.MOUSEMOTION:
pane.track_mouse_motion(event.pos[0], current_color)
elif event.type == pygame.MOUSEBUTTONDOWN:
if pane.drop_mypos(event.pos[0], turn):
pane.fill_mypos()
if pane.board.win_move(turn):
continue_playing = popup_result(turn)
pane.reset()
elif pane.board.is_full():
continue_playing = popup_result()
pane.reset()
else:
turn = 1 if turn == 2 else 2
current_color = color_1 if turn == 1 else color_2
pane.track_mouse_motion(event.pos[0], current_color)
if __name__ == "__main__":
main()