dimanche 20 juin 2021

Why is the if elif function not executing when the condition is met

this is a tic tac toe program I've been trying to get to work I have some if statements at the bottom of the program that say if the click_counter which is a variable that I added that counts if the circles have been clicked is clicked then it would choose a random function from an array that contains functions that draw a O. When I click on one of the circles it turns into an X and the click_counter variable is then equal to 1 this 1 is printed so you see the number of clicks. The bottom if statements say if click_counter == 1: then random.choice(player_array)() this worked once but now it doesn't work any ideas?

import turtle as trtl

from pynput.mouse import Listener

import random

#defining starting turtle
rob = trtl.Turtle()
rob.pensize(10)
rob.shape("circle")

#y and x axis drawing
def grid():
    rob.speed(100)
    rob.penup()
    rob.goto(125,300)
    rob.pendown()
    rob.right(90)
    rob.forward(600)
    rob.penup()
    rob.goto(-125,300)
    rob.pendown()
    rob.forward(600)
    rob.penup()
    rob.goto(-300,125)
    rob.left(90)
    rob.pendown()
    rob.forward(600)
    rob.penup()
    rob.goto(-300,-125)
    rob.pendown()
    rob.forward(600)
    rob.hideturtle()
grid()

#shape turtles
circle1 = trtl.Turtle()
circle1.shape("circle")
circle1.fillcolor("blue")
circle1.shapesize(5)
circle1.penup()
circle1.speed(100)
circle1.goto(250,250)

circle2 = trtl.Turtle()
circle2.shape("circle")
circle2.fillcolor("blue")
circle2.shapesize(5)
circle2.penup()
circle2.speed(100)
circle2.goto(-250,250)

circle3 = trtl.Turtle()
circle3.shape("circle")
circle3.fillcolor("blue")
circle3.shapesize(5)
circle3.penup()
circle3.speed(100)
circle3.goto(0,250)

circle4 = trtl.Turtle()
circle4.shape("circle")
circle4.fillcolor("blue")
circle4.shapesize(5)
circle4.penup()
circle4.speed(100)
circle4.goto(0,0)

circle5 = trtl.Turtle()
circle5.shape("circle")
circle5.fillcolor("blue")
circle5.shapesize(5)
circle5.penup()
circle5.speed(100)
circle5.goto(250,0)

circle6 = trtl.Turtle()
circle6.shape("circle")
circle6.fillcolor("blue")
circle6.shapesize(5)
circle6.penup()
circle6.speed(100)
circle6.goto(-250,0)

circle7 = trtl.Turtle()
circle7.shape("circle")
circle7.fillcolor("blue")
circle7.shapesize(5)
circle7.penup()
circle7.speed(100)
circle7.goto(0,-250)

circle8 = trtl.Turtle()
circle8.shape("circle")
circle8.fillcolor("blue")
circle8.shapesize(5)
circle8.penup()
circle8.speed(100)
circle8.goto(-250,-250)

circle9 = trtl.Turtle()
circle9.shape("circle")
circle9.fillcolor("blue")
circle9.shapesize(5)
circle9.penup()
circle9.speed(100)
circle9.goto(250,-250)
  
#Click counter variable
global click_counter
click_counter = 0

#X drawings
def go_x1(x,y):
    circle1.pendown()
    circle1.pensize(5)
    circle1.left(45)
    circle1.forward(100)
    circle1.left(180)
    circle1.forward(200)
    circle1.left(180)
    circle1.forward(100)
    circle1.left(90)
    circle1.forward(100)
    circle1.right(180)
    circle1.forward(200)
    circle1.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle1.onclick(go_x1)

def go_x2(x,y):
    circle2.pendown()
    circle2.pensize(5)
    circle2.left(45)
    circle2.forward(100)
    circle2.left(180)
    circle2.forward(200)
    circle2.left(180)
    circle2.forward(100)
    circle2.left(90)
    circle2.forward(100)
    circle2.right(180)
    circle2.forward(200)
    circle2.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle2.onclick(go_x2)

def go_x3(x,y):
    circle3.pendown()
    circle3.pensize(5)
    circle3.left(45)
    circle3.forward(100)
    circle3.left(180)
    circle3.forward(200)
    circle3.left(180)
    circle3.forward(100)
    circle3.left(90)
    circle3.forward(100)
    circle3.right(180)
    circle3.forward(200)
    circle3.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle3.onclick(go_x3)

def go_x4(x,y):
    circle4.pendown()
    circle4.pensize(5)
    circle4.left(45)
    circle4.forward(100)
    circle4.left(180)
    circle4.forward(200)
    circle4.left(180)
    circle4.forward(100)
    circle4.left(90)
    circle4.forward(100)
    circle4.right(180)
    circle4.forward(200)
    circle4.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle4.onclick(go_x4)

def go_x5(x,y):
    circle5.pendown()
    circle5.pensize(5)
    circle5.left(45)
    circle5.forward(100)
    circle5.left(180)
    circle5.forward(200)
    circle5.left(180)
    circle5.forward(100)
    circle5.left(90)
    circle5.forward(100)
    circle5.right(180)
    circle5.forward(200)
    circle5.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle5.onclick(go_x5)

def go_x6(x,y):
    circle6.pendown()
    circle6.pensize(5)
    circle6.left(45)
    circle6.forward(100)
    circle6.left(180)
    circle6.forward(200)
    circle6.left(180)
    circle6.forward(100)
    circle6.left(90)
    circle6.forward(100)
    circle6.right(180)
    circle6.forward(200)
    circle6.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle6.onclick(go_x6)

def go_x7(x,y):
    circle7.pendown()
    circle7.pensize(5)
    circle7.left(45)
    circle7.forward(100)
    circle7.left(180)
    circle7.forward(200)
    circle7.left(180)
    circle7.forward(100)
    circle7.left(90)
    circle7.forward(100)
    circle7.right(180)
    circle7.forward(200)
    circle7.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle7.onclick(go_x7)

def go_x8(x,y):
    circle8.pendown()
    circle8.pensize(5)
    circle8.left(45)
    circle8.forward(100)
    circle8.left(180)
    circle8.forward(200)
    circle8.left(180)
    circle8.forward(100)
    circle8.left(90)
    circle8.forward(100)
    circle8.right(180)
    circle8.forward(200)
    circle8.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)
    

circle8.onclick(go_x8)

def go_x9(x,y):
    circle9.pendown()
    circle9.pensize(5)
    circle9.left(45)
    circle9.forward(100)
    circle9.left(180)
    circle9.forward(200)
    circle9.left(180)
    circle9.forward(100)
    circle9.left(90)
    circle9.forward(100)
    circle9.right(180)
    circle9.forward(200)
    circle9.hideturtle()
    global click_counter
    click_counter += 1
    print(click_counter)

circle9.onclick(go_x9)

#O drawings
def go_o1():
    circle1.penup()
    circle1.goto(235,187.5)
    circle1.pendown()
    circle1.pensize(5)
    circle1.circle(75)
    circle1.hideturtle()
    del player_array[0]

def go_o2():
    circle2.penup()
    circle2.goto(-235,187.5)
    circle2.pendown()
    circle2.pensize(5)
    circle2.circle(75)
    circle2.hideturtle()
    del player_array[1]

def go_o3():
    circle3.penup()
    circle3.goto(0,187.5)
    circle3.pendown()
    circle3.pensize(5)
    circle3.circle(75)
    circle3.hideturtle()
    del player_array[2]

def go_o4():
    circle4.penup()
    circle4.goto(0,-62.5)
    circle4.pendown()
    circle4.pensize(5)
    circle4.circle(75)
    circle4.hideturtle()
    del player_array[3]

def go_o5():
    circle5.penup()
    circle5.goto(235,-62.5)
    circle5.pendown()
    circle5.pensize(5)
    circle5.circle(75)
    circle5.hideturtle()
    del player_array[4]

def go_o6():
    circle6.penup()
    circle6.goto(-235,-62.5)
    circle6.pendown()
    circle6.pensize(5)
    circle6.circle(75)
    circle6.hideturtle()
    del player_array[5]

def go_o7():
    circle7.penup()
    circle7.goto(0,-300)
    circle7.pendown()
    circle7.pensize(5)
    circle7.circle(75)
    circle7.hideturtle()
    del player_array[6]

def go_o8():
    circle8.penup()
    circle8.goto(-235,-300)
    circle8.pendown()
    circle8.pensize(5)
    circle8.circle(75)
    circle8.hideturtle()
    del player_array[7]

def go_o9():
    circle9.penup()
    circle9.goto(235,-300)
    circle9.pendown()
    circle9.pensize(5)
    circle9.circle(75)
    circle9.hideturtle()
    del player_array[8]


#player 2 player that draws O's
player_array = [go_o1, go_o2, go_o3, go_o4, go_o5, go_o6, go_o7, go_o8, go_o9]

if click_counter == 1:
    random.choice(player_array)()
elif click_counter == 2:
    print(random.choice(player_array))
elif click_counter == 3:
    random.choice(player_array)()
elif click_counter == 4:
    random.choice(player_array)()
elif click_counter == 5:
    random.choice(player_array)()
elif click_counter == 6:
    random.choice(player_array)()
elif click_counter == 7:
    random.choice(player_array)()
elif click_counter == 8:
    random.choice(player_array)()
elif click_counter == 9:
    random.choice(player_array)()
else:
    print("Click a circle")




if click_counter == 2:
    random.choice(player_array)()


if click_counter == 3:
    random.choice(player_array)()

if click_counter == 4:
    random.choice(player_array)()

if click_counter == 5:
    random.choice(player_array)()

if click_counter == 6:
    random.choice(player_array)()

if click_counter == 7:
    random.choice(player_array)()

if click_counter == 8:
    random.choice(player_array)()

if click_counter == 9:
    random.choice(player_array)()





wn = trtl.Screen()
wn.listen()
wn.mainloop()

Aucun commentaire:

Enregistrer un commentaire