forked from shrawani21/gamer_21
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
191 lines (156 loc) · 5.86 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pygame
# Constants
SCREEN_SIZE = SCREEN_WIDTH, SCREEN_HEIGHT = 300, 300
CELL_SIZE = 40
PADDING = 20
ROWS = COLS = (SCREEN_WIDTH - 4 * PADDING) // CELL_SIZE
WHITE = (255, 255, 255)
RED = (252, 91, 122)
BLUE = (78, 193, 246)
GREEN = (0, 255, 0)
BLACK = (12, 12, 12)
# Initialize pygame
pygame.init()
win = pygame.display.set_mode(SCREEN_SIZE, pygame.NOFRAME)
font = pygame.font.SysFont('cursive', 25)
class Cell:
def __init__(self, row, col):
self.row = row
self.col = col
self.index = self.row * ROWS + self.col
self.rect = pygame.Rect((self.col * CELL_SIZE + 2 * PADDING,
self.row * CELL_SIZE + 3 * PADDING,
CELL_SIZE, CELL_SIZE))
self.edges = [
[(self.rect.left, self.rect.top), (self.rect.right, self.rect.top)],
[(self.rect.right, self.rect.top), (self.rect.right, self.rect.bottom)],
[(self.rect.right, self.rect.bottom), (self.rect.left, self.rect.bottom)],
[(self.rect.left, self.rect.bottom), (self.rect.left, self.rect.top)]
]
self.sides = [False, False, False, False]
self.winner = None
def check_win(self, winner):
if not self.winner:
if self.sides == [True] * 4:
self.winner = winner
self.color = GREEN if winner == 'X' else RED
self.text = font.render(self.winner, True, WHITE)
return 1
return 0
def update(self, surface):
if self.winner:
pygame.draw.rect(surface, self.color, self.rect)
surface.blit(self.text, (self.rect.centerx - 5, self.rect.centery - 7))
for index, side in enumerate(self.sides):
if side:
pygame.draw.line(surface, WHITE, self.edges[index][0], self.edges[index][1], 2)
def create_cells():
cells = []
for row in range(ROWS):
for col in range(COLS):
cell = Cell(row, col)
cells.append(cell)
return cells
def reset_game_state():
global cells, game_over, turn, players, player, next_turn, fill_count, p1_score, p2_score
cells = create_cells()
game_over = False
turn = 0
players = ['X', 'O']
player = players[turn]
next_turn = False
fill_count = 0
p1_score = 0
p2_score = 0
def handle_input_events():
global game_over, next_turn
for event in pygame.event.get():
if event.type == pygame.QUIT:
return False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = event.pos
if event.type == pygame.MOUSEBUTTONUP:
pos = None
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_q or event.key == pygame.K_ESCAPE:
return False
if event.key == pygame.K_r:
reset_game_state()
if not game_over:
if event.key == pygame.K_UP:
up = True
if event.key == pygame.K_RIGHT:
right = True
if event.key == pygame.K_DOWN:
bottom = True
if event.key == pygame.K_LEFT:
left = True
if event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
up = False
if event.key == pygame.K_RIGHT:
right = False
if event.key == pygame.K_DOWN:
bottom = False
if event.key == pygame.K_LEFT:
left = False
return True
def update_game_state():
global next_turn, fill_count, p1_score, p2_score
if ccell:
index = ccell.index
if not ccell.winner:
pygame.draw.circle(win, RED, (ccell.rect.centerx, ccell.rect.centery), 2)
if up and not ccell.sides[0]:
ccell.sides[0] = True
if index - ROWS >= 0:
cells[index - ROWS].sides[2] = True
next_turn = True
if right and not ccell.sides[1]:
ccell.sides[1] = True
if (index + 1) % COLS > 0:
cells[index + 1].sides[3] = True
next_turn = True
if bottom and not ccell.sides[2]:
ccell.sides[2] = True
if index + ROWS < len(cells):
cells[index + ROWS].sides[0] = True
next_turn = True
if left and not ccell.sides[3]:
ccell.sides[3] = True
if (index % COLS) > 0:
cells[index - 1].sides[1] = True
next_turn = True
res = ccell.check_win(player)
if res:
fill_count += res
if player == 'X':
p1_score += 1
else:
p2_score += 1
if fill_count == ROWS * COLS:
print(p1_score, p2_score)
game_over = True
if next_turn:
turn = (turn + 1) % len(players)
player = players[turn]
next_turn = False
def draw_game():
win.fill(BLACK)
pygame.draw.rect(win, WHITE, (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 2, border_radius=10)
for r in range(ROWS + 1):
for c in range(COLS + 1):
pygame.draw.circle(win, WHITE, (c * CELL_SIZE + 2 * PADDING,
r * CELL_SIZE + 3 * PADDING), 2)
for cell in cells:
cell.update(win)
if game_over:
rect = pygame.Rect((50, 100, SCREEN_WIDTH - 100, SCREEN_HEIGHT - 200))
pygame.draw.rect(win, BLACK, rect)
pygame.draw.rect(win, RED, rect, 2)
over = font.render('Game Over', True, WHITE)
win.blit(over, (rect.centerx - over.get_width() / 2, rect.y + 10))
winner = '1' if p1_score > p2_score else '2'
winner_img = font.render(f'Player {winner} Won', True, GREEN)
win.blit(winner_img, (rect.centerx - winner_img.get_width() / 2, rect.centery - 10))
msg = 'Press r:restart, q:'