简体   繁体   中英

Python Turtle Graphics window crashes

def whatever()

def whatever2()

While trying to create battleships in python turtle i ran into an issue. I'm using def functions. But everytime the first def function finishes, the turtle graphics window crashes. I tried using turtle.done() after the first def function which prevents it from crashing. However by doing this it won't execute the second def function.

Whole code on request:

    import turtle
i=0
def speelveld():
    turtle.ht()
    x=200
    y=200
    turtle.tracer(10, 0)
    for i in range(10):
        y-=40
        turtle.penup()
        turtle.goto(0,y)
        turtle.pendown()
        x=200
        for i in range(10):
            x-=40
            turtle.goto(x,y)
            for i in range(4):
                turtle.forward(40)
                turtle.left(90)
    y=170
    for i in range(10):
        coordsN = ['1','2','3','4','5','6','7','8','9','10']
        turtle.penup()
        turtle.goto(-220,y)
        turtle.pendown()
        turtle.write(coordsN[i],font=("Calibri", 14, "normal"))
        y-=40
        i=+1
    i=0
    x=-180
    for i in range(10):
        coordsL = ['A','B','C','D','E','F','G','H','I','J']
        turtle.penup()
        turtle.goto(x,205)
        turtle.pendown()
        turtle.write(coordsL[i],font=("Calibri", 14, "normal"))
        x+=40
        i=+1   

speelveld()
x=y=0
def veldkleuren():
    gok = input('Typ een coördinaat in: ')
    if '1' in gok:
        y=150
    elif '2' in gok:
        y=110
    elif '3' in gok:
        y=70
    elif '4' in gok:
        y=30
    elif '5' in gok:
        y=-10
    elif '6' in gok:
        y=-50
    elif '7' in gok:
        y=-90
    elif '8' in gok:
        y=-130
    elif '9' in gok:
        y=-170
    elif '10' in gok:
        y=-210
    else:
        print('Ongeldige invoer')
    if 'A' in gok:
        x=-190
    elif 'B' in gok:
        x=-150
    elif 'C' in gok:
        x=-110
    elif 'D' in gok:
        x=-70
    elif 'E' in gok:
        x=-30
    elif 'F' in gok:
        x=10
    elif 'G' in gok:
        x=50
    elif 'H' in gok:
        x=90
    elif 'I' in gok:
        x=130
    elif 'J' in gok:
        x=170
    else:
        print('Ongeldige invoer')

    turtle.penup()
    turtle.goto(x,y)
    turtle.pendown()
    turtle.write('X',font=("Calibri", 39, "normal"))

veldkleuren()
exit = input('press enter to exit')

The first def is for drawing the battleships playingfield.

Second def is take an input and mark an 'X'on the spot of the input.

I am getting errors of

UnboundLocalError: local variable 'x' referenced before assignment UnboundLocalError: local variable 'y' referenced before assignment

Moving the initialization of x and y inside the second function fixes it.

Below is a rework of your code that attempts to remove the repetitive code (always a source of trouble) and fix some bugs.

One bug is that you have '1' and '10' as input possibilities -- as written, the '1' in '10' will trigger first so '10' will never trigger. I've fixed it in my rework simply by using '0' - '9', ie all unique characters, but there are other ways to solve this problem.

Also note that in code like this:

for i in range(10):
        # ...
        i=+1

The last line setting i makes no difference and can be removed. Even if you did i += 1 , it still wouldn't matter. The loop resets i .

Finally, the turtle pen does not have to be down to call .write() .

import turtle

SMALL_FONT = ("Calibri", 14, "normal")
LARGE_FONT_SIZE = 39
LARGE_FONT = ("Calibri", LARGE_FONT_SIZE, "normal")

coordsN = {'0': 180, '1': 140, '2': 100, '3': 60, '4': 20, '5': -20, '6': -60, '7': -100, '8': -140, '9': -180}

coordsL = {'A': -180, 'B': -140, 'C': -100, 'D': -60, 'E': -20, 'F': 20, 'G': 60, 'H': 100, 'I': 140, 'J': 180}

def speelveld():
    turtle.hideturtle()
    turtle.tracer(0)

    y = 200

    for _ in range(10):
        y -= 40
        turtle.penup()
        turtle.goto(0, y)
        turtle.pendown()
        x = 200

        for _ in range(10):
            x -= 40
            turtle.goto(x, y)

            for _ in range(4):
                turtle.forward(40)
                turtle.left(90)
    turtle.penup()
    turtle.setx(-220)
    y = 170

    for i in range(10):
        turtle.sety(y)
        turtle.write(i, align='right', font=SMALL_FONT)
        y -= 40

    turtle.sety(205)
    x = -180

    for i in range(10):
        turtle.setx(x)
        turtle.write(chr(ord('A') + i), align='center', font=SMALL_FONT)
        x += 40

    turtle.tracer(1)

def veldkleuren():
    x = y = 0

    gok = input('Typ een coördinaat in: ')

    for column in coordsL:
        if column in gok:
            x = coordsL[column]
            break
    else:  # no break
        exit('Ongeldige invoer')

    for row in coordsN:
        if row in gok:
            y = coordsN[row]
            break
    else:  # no break
        exit('Ongeldige invoer')

    turtle.penup()
    turtle.goto(x, y - LARGE_FONT_SIZE/2)

    turtle.write('X', align='center', font=LARGE_FONT)

speelveld()

veldkleuren()

input('press enter to exit')

You could compute the coordinates I stashed in coordsN and coordsL when you draw your grid rather than hardcoding that information as data, or in if statements as in your original.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM