Pygame Not Drawing X

Hello I am making a tic tac toe game in pygame the problem i encountered is that when i draw an x on the board it does not draw.it works when i draw a circle as shown in the code. please tell me how to fix also can you tell me how to take turns in this game because i cannot figure out how to draw an x and let the computer draw an o and so on.i want to wait for the user to draw an x and then let the computer draw an o. Here Is The Code:

import pygame,random

pygame.init()

width = 600

height = 600

res = (width,height)

screen = pygame.display.set_mode(res)

pygame.display.set_caption("Tic Tac Toe")

background = (255,150,150)

color_light = (170,170,170)

color_dark = (100,100,100)

hover_color = (255, 204, 203)

rect_list = [

    pygame.Rect(10, 10, 180, 190),
    pygame.Rect(200, 10, 190, 190),
    pygame.Rect(400, 10, 190, 190),
    pygame.Rect(10, 210, 180, 180),
    pygame.Rect(200, 210, 190, 180),
    pygame.Rect(400, 210, 190, 180),
    pygame.Rect(10, 400, 180, 190),
    pygame.Rect(200, 400, 190, 190),
    pygame.Rect(400, 400, 190, 190)]

clicked_list = [0 for _ in rect_list]

count = 0


def draw_x(x,y,width,height):

    for i in range(5):
        pygame.draw.aaline(screen,"blue",(x+i,y),(width+x+i,height+y))  # start_pos(x+thickness,y)---end_pos(width+x+thickness,height+y)
        pygame.draw.aaline(screen,"blue",(width+x+i,y),(x+i,height+y))  # start_pos(x+width+thickness,y)---end_pos(x+thickness,y+height)


def draw_line():

    line_color = (212, 212, 255)
    pygame.draw.rect(screen, line_color, (190,10,10,580))
    pygame.draw.rect(screen, line_color, (390, 10, 10, 580))
    pygame.draw.rect(screen, line_color, (10, 200, 580, 10))
    pygame.draw.rect(screen, line_color, (10, 390, 580, 10))


def highlight():

    for rect in rect_list:
        if rect.collidepoint(mouse):   
            pygame.draw.rect(screen,hover_color,rect)


def random_no():

    randomno = random.randint(0,len(rect_list)-1)
    if clicked_list[randomno] != 1:
        pass
    else:
        random_no()
    return randomno


def mouse_click():

    x_turn = True
    y_turn = False
    o_no = random_no()
    clicked_list[o_no] = 2
    for i,rect in enumerate(rect_list):
        if clicked_list[i] == 1:
            if x_turn and clicked:
                pygame.draw.rect(screen, background, rect)
                draw_x(rect.x,rect.y,rect.width,rect.height)
        if clicked_list[i] == 2:
            if y_turn:
                pygame.draw.rect(screen, background, rect)
                pygame.draw.ellipse(screen, "blue", rect, 5)
            # rect_list.remove(rect_list[i])
            # clicked_list.remove(clicked_list[i])
while True:

    mouse = pygame.mouse.get_pos()
    x = screen.get_at(mouse)[:3]
    screen.fill(background)
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            pygame.quit()
        if ev.type == pygame.MOUSEBUTTONDOWN:
            clicked = True
            for i, rect in enumerate(rect_list):
                if rect.collidepoint(ev.pos) and x == hover_color:
                    clicked_list[i] = 1
    draw_line()
    highlight()
    mouse_click()
    pygame.display.update()

1 Answer

The mouse click must be handled in the event loop. However, you need a function draw_borad that draws "X" and "O".

Add a variable that indicates if its the turn of "X" or "O":

x_turn = True

If the mouse is pressed and a field is empty (0), change the status of the field and change the x_turn variable:

if rect.collidepoint(ev.pos) and x == hover_color:
    if clicked_list[i] == 0:
        clicked_list[i] = 1 if x_turn else 2
        x_turn = not x_turn

Draw all the "X" and "O" in draw_borad:

def draw_borad():
    for i,rect in enumerate(rect_list):
        if clicked_list[i] == 1:
            pygame.draw.rect(screen, background, rect)
            draw_x(rect.x,rect.y,rect.width,rect.height)
        if clicked_list[i] == 2:
            pygame.draw.rect(screen, background, rect)
            pygame.draw.ellipse(screen, "blue", rect, 5)

x_turn = True
while True:
    mouse = pygame.mouse.get_pos()
    x = screen.get_at(mouse)[:3]
    screen.fill(background)
    for ev in pygame.event.get():
        if ev.type == pygame.QUIT:
            pygame.quit()
        if ev.type == pygame.MOUSEBUTTONDOWN:
            clicked = True
            for i, rect in enumerate(rect_list):
                if rect.collidepoint(ev.pos) and x == hover_color:
                    if clicked_list[i] == 0:
                        clicked_list[i] = 1 if x_turn else 2
                        x_turn = not x_turn

    draw_line()
    highlight()
    draw_borad()
    pygame.display.update()
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest