python game of life draw function (turtle) (invoking an instance method?)

548 Views Asked by At

I found this implementation to Conway's Game of Life. I never dealt with GUI, and I'm trying to understand this code (and implement my own)

there is a specific function that bugs me. the function that draws each 'organism' (black is alive, white is dead).

# import turtle (at the top)
def draw(self, x, y):
    "Update the cell (x,y) on the display."
    turtle.penup()
    key = (x, y)
    if key in self.state:
        turtle.setpos(x*CELL_SIZE, y*CELL_SIZE)
        turtle.color('black')
        turtle.pendown()
        turtle.setheading(0)
        turtle.begin_fill()
        for i in range(4):
            turtle.forward(CELL_SIZE-1)
            turtle.left(90)
        turtle.end_fill()

And this is the function that displays the whole board: def display(self):

"""Draw the whole board"""
turtle.clear()
for i in range(self.xsize):
    for j in range(self.ysize):
        self.draw(i, j)
turtle.update()

The code works of course, but Intellij says that he can't find reference to ALL these functions. I think it is because it's invoking an instance method, as a class method and the self is missing.

  1. I don't understand how it works.
  2. How can i fix it? I tried to make a new Turtle, but it didn't work (and not a good idea in my opinion). Maybe I should put a Turtle as an argument in the functions?

Stuck on this one for hours already. Would love some help.

1

There are 1 best solutions below

0
On BEST ANSWER

Intellij says that he can't find reference to ALL these functions

The turtle module is an odd bird. (Excuse the mixed metaphor.) It tries to be different things to different audiences and that causes confusion:

1) Function vs. methods

Turtle is an object-oriented module where you create instances of turtles & screens and invoke methods on them:

screen = turtle.Screen()
screen.setworldcoordinates(0, 0, xsize, ysize)

yertle = turtle.Turtle()
yertle.forward(100)

However, to accommodate beginning programmers and/or emulate other turtle languages, it also provides a functional interface:

turtle.setworldcoordinates(0, 0, xsize, ysize)
turtle.forward(100)  # move the "default" turtle forward

To prevent mixing up functions and methods, I recommend importing turtle this way:

from turtle import Turtle, Screen

which only allows the object interface and not the function one.

The IntelliJ warning happens because the functional interface to turtle is derived from the object method interface dynamically upon load -- there are no actual functions to refer back to in the file.

2) Standalone vs. Embedded

The turtle module is designed to either run standalone or embedded in a larger tkinter project. How you access turtles and the screen differ depending which you're doing.

As far as this implementation of Conway's Game of Life goes, it's using the functional interface when it probably should be using the object one and it's treating the turtle module as standalone but then using it to open other Tk-based panels:

from turtle import TK

it should be the other way around. There is no quick fix to make this module conform, every turtle and tkinter reference would need to be examined and rethought.