Bruh
Bruh
import sys
pygame.init()
WIDTH,HEIGHT = 600,550
FPS = 60
WHITE = (255,255,255)
BLACK = (0,0,0)
window = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('Tic Tac Toe')
clock = pygame.time.Clock()
font = pygame.font.Font('assets/font.ttf',50)
font2 = pygame.font.Font('assets/font.ttf',100)
font3 = pygame.font.Font('assets/font.ttf',50)
places =
[pygame.Rect(0,100,200,150),pygame.Rect(200,100,200,150),pygame.Rect(400,100,200,15
0),
pygame.Rect(0,250,200,150),pygame.Rect(200,250,200,150),pygame.Rect(400,250,200,150
),
pygame.Rect(0,400,200,150),pygame.Rect(200,400,200,150),pygame.Rect(400,400,200,150
)]
place_coords = []
for place in places:
place_coords.append([place.x,place.y])
x_image = pygame.image.load('assets/X.xcf')
o_image = pygame.image.load('assets/O.xcf')
hovering_places = []
def draw_board():
for place in places:
if place != 'X' and place != 'O':
x,y,w,h = place
pygame.draw.rect(window,WHITE,(x,y,w,h))
pygame.draw.line(window,BLACK,(200,100),(200,550),5)
pygame.draw.line(window,BLACK,(400,100),(400,550),5)
pygame.draw.line(window,BLACK,(0,250),(600,250),5)
pygame.draw.line(window,BLACK,(0,400),(600,400),5)
def hovering():
global hovering_index
hovering_index = 0
mouse_pos = pygame.mouse.get_pos()
for place_num,place in enumerate(places):
if place != 'X' and place != 'O':
if place.collidepoint(mouse_pos):
hovering_places.append(place)
hovering_index = place_num
for place in hovering_places:
if place.collidepoint(mouse_pos):
return True
if mouse_pos != place:
hovering_places.remove(place)
def display_turn():
for place_num,place in enumerate(places):
if place == 'X':
window.blit(x_image,(place_coords[place_num][0],place_coords[place_num]
[1]))
elif place == 'O':
window.blit(o_image,(place_coords[place_num][0],place_coords[place_num]
[1]))
def won_x():
return ((places[0] == "X" and places[1] == "X" and places[2] == "X") or
#Straight lines horizontally
(places[3] == "X" and places[4] == "X" and places[5] == "X") or
(places[6] == "X" and places[7] == "X" and places[8] == "X") or
(places[0] == "X" and places[3] == "X" and places[6] == "X") or #Straight
lines vertically
(places[1] == "X" and places[4] == "X" and places[7] == "X") or
(places[2] == "X" and places[5] == "X" and places[8] == "X") or
(places[0] == "X" and places[4] == "X" and places[8] == "X") or #Lines
diagonally
(places[2] == "X" and places[4] == "X" and places[6] == "X"))
def won_o():
return ((places[0] == "O" and places[1] == "O" and places[2] == "O") or
#Straight lines horizontally
(places[3] == "O" and places[4] == "O" and places[5] == "O") or
(places[6] == "O" and places[7] == "O" and places[8] == "O") or
(places[0] == "O" and places[3] == "O" and places[6] == "O") or #Straight
lines vertically
(places[1] == "O" and places[4] == "O" and places[7] == "O") or
(places[2] == "O" and places[5] == "O" and places[8] == "O") or
(places[0] == "O" and places[4] == "O" and places[8] == "O") or #Lines
diagonally
(places[2] == "O" and places[4] == "O" and places[6] == "O"))
def main_loop():
while True:
window.fill(WHITE)
draw_board()
hovering()
turn_number = 0
turn = ''
available_places = [x for x in places if x != 'X' and x != 'O']
turn_number += 10 - len(available_places)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if turn_number % 2 != 0:
window.blit(x_turn_font,x_turn_rect)
elif turn_number % 2 == 0:
window.blit(o_turn_font,o_turn_rect)
display_turn()
if won_x():
window.blit(x_won,x_won_rect)
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
sys.exit()
if won_o():
window.blit(o_won,o_won_rect)
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
sys.exit()
elif won_x() == False and won_o() == False and turn_number == 10:
window.blit(draw,draw_rect)
pygame.display.update()
pygame.time.delay(2000)
pygame.quit()
sys.exit()
pygame.display.update()
clock.tick(FPS)
if __name__ == '__main__':
main_loop()