0% found this document useful (0 votes)
17 views3 pages

Bruh

hebeuhshehehe

Uploaded by

samemhamidi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Bruh

hebeuhshehehe

Uploaded by

samemhamidi0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

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 = []

x_turn_font = font.render('X\'s turn',True,'Black')


x_turn_rect = x_turn_font.get_rect(center = (300,50))

o_turn_font = font.render('O\'s turn',True,'Black')


o_turn_rect = o_turn_font.get_rect(center = (300,50))

x_won = font2.render('X has won!',True,'Blue')


x_won_rect = x_won.get_rect(center = (300,275))

o_won = font2.render('O has won!',True,'Blue')


o_won_rect = o_won.get_rect(center = (300,275))

draw = font3.render('The game is a draw!',True,'Blue')


draw_rect = draw.get_rect(center = (300,275))

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 event.type == pygame.MOUSEBUTTONDOWN and hovering():


if turn_number % 2 != 0:
turn = 'X'
places[hovering_index] = turn
elif turn_number % 2 == 0:
turn = 'O'
places[hovering_index] = turn

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()

You might also like