0

I'm trying to make a connect 4 game in Python turtle graphics, and it was going well, but then this happened: xy simply won't change! Here is the code:

import turtle
from turtle import *
from turtle import Turtle, Screen
speed(0)
xy = 1
def circle1 ():
    forward(90)
    fillcolor("white")
    begin_fill()
    circle(40)
    end_fill()
def getCoords(x,y):
    global xy
    #print("( " + str(x) + ", " + str(y) + " )")
    if y < 252 and x < -316:
        print ("test")
        xy = 1
    elif y < 252 and x > -316 and x < -187:
        xy = 2
    elif y < 252 and x > -187 and x < -59:
        xy = 3
    elif y < 252 and x > -59 and x < 65:
        xy = 4
    elif y < 252 and x > 65 and x < 194:
        xy = 5
    elif y < 252 and x > 194 and x < 327:
        xy = 6
    elif y < 252 and x > 327 and x < 453:
        xy = 7
window = Screen()
wn = window
wn.screensize()
wn.setup(width = 1.0, height = 1.0)
begin_fill()
width(5)
penup()
goto(-450,250)
right(90)
pendown()
forward(580)
left(90)
forward(450*2)
left(90)
forward(580)
left(90)
forward(450*2)
end_fill()
right(180)
forward(30)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
goto(-450,250)
left(90)
forward(160)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(290)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(410)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(540)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(670)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
penup()
goto(-450,250)
pendown()
left(90)
forward(800)
right(90)
forward(50)
fillcolor("white")
begin_fill()
circle(40)
end_fill()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
circle1 ()
turtle.onscreenclick(getCoords)
turtle.listen()
if xy == 1:
    textinput("test","dis is 1")

For some odd reason when I tabbed everything over it copied a part of it, so ignore that last bit.

2
  • Quasi-duplicate, see answer: stackoverflow.com/a/42803541/1730895 , so getCoords() is not called. Commented Dec 19, 2018 at 2:46
  • @Kingsley, OP is setting up the event handler correctly and getCoords() does get called. So not a duplicate of that particular answer. (I'm assuming an implied mainloop() call, e.g. executing under IDLE or some such.) Commented Dec 19, 2018 at 7:08

1 Answer 1

1

it was going well

You know things aren't really going well when you import the same module three different ways:

import turtle
from turtle import *
from turtle import Turtle, Screen

xy simply won't change!

It is changing, but there's nothing in your code that is capable of displaying the change. The only statement that looks at xy:

if xy == 1:
    textinput("test","dis is 1")

completes before xy ever changes and is never run again. Let's add some code to display xy in the turtle window so you can see your clicks are having an effect. While we're at it, we'll clean up your code:

from turtle import *

def circle6():
    fillcolor("white")
    for _ in range(6):
        begin_fill()
        circle(40)
        end_fill()
        forward(90)

def getCoords(x, y):
    global xy

    if y < 252:
        if x < -316:
            xy = 1
        elif -316 < x < -187:
            xy = 2
        elif -187 < x < -59:
            xy = 3
        elif -59 < x < 65:
            xy = 4
        elif 65 < x < 194:
            xy = 5
        elif 194 < x < 327:
            xy = 6
        elif 327 < x < 453:
            xy = 7

        marker.undo()
        marker.write("xy = {}".format(xy), xy, font=('Arial', 18, 'normal'))

xy = 0

wn = Screen()
wn.setup(width=1.0, height=1.0)

marker = Turtle(visible=False)
marker.penup()
marker.goto(0, 300)
marker.write("xy = {}".format(xy), font=('Arial', 18, 'normal'))

hideturtle()
speed('fastest')
penup()

goto(-450, 250)
right(90)

begin_fill()
forward(580)
left(90)
forward(450 * 2)
left(90)
forward(580)
left(90)
forward(450 * 2)
end_fill()

left(90)

for distance in range(25, 815, 130):

    goto(-450, 250)
    left(90)
    forward(distance)
    right(90)
    forward(60)

    circle6()

wn.onscreenclick(getCoords)

wn.mainloop()

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.