0

I have seen all the other posts but I haven't had the same problems as the others. All of them forgot to put pygame.init() but I remembered. I simply can't think of what I did wrong. I will put the important bits of code below

    import pygame
    #A bunch of variables
    def checkWin():
  if board[0] == board[1] == board[2] or board[3] == board[4] == board[5] or board[6] == board[7] == board[8] or board[0] == board[3] == board[6] or board[2] == board[4] == board[7] or board[3] == board[5] == board[8] or board[0] == board[4] == board[8] or board[2] == board[4] == board[6]:
    if turn == 'X':
      window.blit(O_WIN, (WIDTH / 6, HEIGHT / 2))
      pygame.time.wait(5000)
      pygame.quit()
    if turn == 'O':
      window.blit(X_WIN, (WIDTH / 6, HEIGHT / 2))
      pygame.time.wait(5000)
      pygame.quit()


run = True
while run:
  #Set background
  window.fill(BLUE)

  #Draw lines
  pygame.draw.line(window, DARK_BLUE, (0, WIDTH / 3), (HEIGHT, WIDTH / 3), LINE_THICKNESS)
  pygame.draw.line(window, DARK_BLUE, (0, WIDTH / 3 * 2), (HEIGHT, WIDTH / 3 * 2), LINE_THICKNESS)
  pygame.draw.line(window, DARK_BLUE, (HEIGHT / 3, 0), (HEIGHT / 3, WIDTH), LINE_THICKNESS)
  pygame.draw.line(window, DARK_BLUE, (HEIGHT / 3 * 2, 0), (HEIGHT / 3 * 2, WIDTH), LINE_THICKNESS)

  #Click function
  if pygame.mouse.get_pressed()[0] == True:
    pos = pygame.mouse.get_pos()
    pos = (pos[0] // xpos, pos[1] // ypos)
    if turn == 'X':
      if pos == (0, 0):
        if board[0] == '':
          X00 = True
          board[0] = turn
          turn = 'O'
      elif pos == (1, 0):
        if board[1] == '':
          X10 = True
          board[1] = turn
          turn = 'O'
      elif pos == (2, 0):
        if board[2] == '':
          X20 = True
          board[2] = turn
          turn = 'O'
      elif pos == (0, 1):
        if board[3] == '':
          X01 = True
          board[3] = turn
          turn = 'O'
      elif pos == (1, 1):
        if board[4] == '':
         X11 = True
         board[4] = turn
         turn = 'O'
      elif pos == (2, 1):
        if board[5] == '':
         X21 = True
         board[5] = turn
         turn = 'O'
      elif pos == (0, 2):
        if board[6] == '':
          X02 = True
          board[6] = turn
          turn = 'O'
      elif pos == (1, 2):
        if board[7] == '':
         X12 = True
         board[7] = turn
         turn = 'O'
      elif pos == (2, 2):
        if board[8] == '':
          X22 = True
          board[8] = turn
          turn = 'O'
      pygame.time.wait(250)
    else:
      if pos == (0, 0):
        if board[0] == '':
          O00 = True
          board[0] = turn
          turn = 'X'
      elif pos == (1, 0):
        if board[1] == '':
          O10 = True
          board[1] = turn
          turn = 'X'
      elif pos == (2, 0):
        if board[2] == '':
         O20 = True
         board[2] = turn
         turn = 'X'
      elif pos == (0, 1):
        if board[3] == '':
          O01 = True
          board[3] = turn
          turn = 'X'
      elif pos == (1, 1):
        if board[4] == '':
         O11 = True
         board[4] = turn
         turn = 'X'
      elif pos == (2, 1):
        if board[5] == '':
         O21 = True
         board[5] = turn
         turn = 'X'
      elif pos == (0, 2):
        if board[6] == '':
          O02 = True
          board[6] = turn
          turn = 'X'
      elif pos == (1, 2):
        if board[7] == '':
          O12 = True
          board[7] = turn
          turn = 'X'
      elif pos == (2, 2):
        if board[8] == '':
         O22 = True
         board[8] = turn
         turn = 'X'
      pygame.time.wait(250)
  
  #show X's
  if X00:
    window.blit(X_OBJECT, (WIDTH / 50, 0))
  if X10:
    window.blit(X_OBJECT, (WIDTH / 50 + WIDTH / 3, 0))
  if X20:
    window.blit(X_OBJECT, (WIDTH / 50 + WIDTH / 3 * 2, 0))
  if X01:
    window.blit(X_OBJECT, (WIDTH / 50, HEIGHT / 3))
  if X11:
    window.blit(X_OBJECT, (WIDTH / 50 + WIDTH / 3, HEIGHT / 3))
  if X21:
    window.blit(X_OBJECT, (WIDTH / 50 + WIDTH / 3 * 2, HEIGHT / 3))
  if X02:
    window.blit(X_OBJECT, (WIDTH / 50, HEIGHT / 3 * 2))
  if X12:
    window.blit(X_OBJECT, (WIDTH / 50 +  WIDTH / 3, HEIGHT / 3 * 2))
  if X22:
    window.blit(X_OBJECT, (WIDTH / 50 + WIDTH / 3 * 2, HEIGHT / 3 * 2))
  
  #Show O's
  if O00:
    window.blit(O_OBJECT, (WIDTH / 50, 0))
  if O10:
    window.blit(O_OBJECT, (WIDTH / 50 + WIDTH / 3, 0))
  if O20:
    window.blit(O_OBJECT, (WIDTH / 50 + WIDTH / 3 * 2, 0))
  if O01:
    window.blit(O_OBJECT, (WIDTH / 50, HEIGHT / 3))
  if O11:
    window.blit(O_OBJECT, (WIDTH / 50 + WIDTH / 3, HEIGHT / 3))
  if O21:
    window.blit(O_OBJECT, (WIDTH / 50 + WIDTH / 3 * 2, HEIGHT / 3))
  if O02:
    window.blit(O_OBJECT, (WIDTH / 50, HEIGHT / 3 * 2))
  if O12:
    window.blit(O_OBJECT, (WIDTH / 50 +  WIDTH / 3, HEIGHT / 3 * 2))
  if O22:
    window.blit(O_OBJECT, (WIDTH / 50 + WIDTH / 3 * 2, HEIGHT / 3 * 2))

  #Detect a winner
  checkWin()

  #Refreshes screen
  pygame.display.flip()
  #When X is pressed window closes
  for event in pygame.event.get():
    if event.type == pygame.QUIT:
      run = False
pygame.quit()

I AM AWARE IT IS TRASH! Please don't focus on the fact that I probably did it the longest way possible when I could have just done nested for loops or something. I wanted to do something easier. But anyways, can any of you help? I also used replit so it is not me installing something incorrectly.

  • Welcome back to Stack Overflow. As a refresher, please read [ask] and remember that this is *not a discussion forum*. We are not interested in opinions about the code or your level of experience or anything else like that. We *are* interested in a clear, *specific* question that is [focused on the relevant code](https://stackoverflow.com/help/minimal-reproducible-example). Also, please make sure your code [appears exactly as you actually have it](https://stackoverflow.com/help/formatting). What you show would have syntax errors due to indentation. – Karl Knechtel Jan 22 '22 at 07:11
  • "All of them forgot to put pygame.init() but I remembered." I can't see where it is. – Karl Knechtel Jan 22 '22 at 07:13
  • "when I could have just done nested for loops or something. I wanted to do something easier." I don't understand. The loops are easier. With loops, I don't have to think about how to change the expressions for (as in this example) X and Y positions each time. I just have to think about the *rule that tells me* the correct position, once. – Karl Knechtel Jan 22 '22 at 07:14
  • For those of you saying you don't see where pygame.init() is, it is in the variable section which I left out for convenience – Ledip Ledip Jan 22 '22 at 07:26
  • Well, we can only tell you what is wrong with code that you actually show to us. Again, look at the linked information and make sure you can present information that makes it possible to help you. – Karl Knechtel Jan 22 '22 at 07:27

0 Answers0