0

I hope it run and do not stop. I'm making Conway's Game of Life, if my code look stupid, please help me, I'm only 11. I use the details from wiki, if it's wrong , please tell me. Thankyou!

import turtle,random
START_POSITION=[]
def ReStart():
   global START_POSITION
   #START_POSITION.clear()
   y=500
   x=-500
   for i in range(1,26):
       for a in range(1,26):
           START_POSITION.append(eval(f"({x},{y})"))
           x+=20
       x=(0-300)
       y-=20
   return True
ReStart()
screen=turtle.Screen()
class Cell:
   def __init__(self):
       self.cells=[]
       self.make_body()
       self.a()
       self.Alive(screen)
   def make_body(self):
       global START_POSITION
       for i in START_POSITION:
           seg=turtle.Turtle(shape="square")
           seg.color("White")
           seg.penup()
           seg.goto(i[0],i[1])
           self.cells.append(seg) 

The error saids:

Traceback (most recent call last):
  File "C:/Users/****/Desktop/寫程式/the life game.py", line 145, in <module>
    cell=Cell()
  File "C:/Users/****/Desktop/寫程式/the life game.py", line 20, in __init__
    self.make_body()
  File "C:/Users/****/Desktop/寫程式/the life game.py", line 29, in make_body
    seg.goto(i[0],i[1])
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1777, in goto
    self._goto(Vec2D(x, y))
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 3180, in _goto
    self._update()
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 2661, in _update
    self._update_data()
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 2647, in _update_data
    self.screen._incrementudc()
  File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1293, in _incrementudc
    raise Terminator
turtle.Terminator

I'm totally stuck on this,please help me.

ggorlen
  • 44,755
  • 7
  • 76
  • 106

1 Answers1

0

I've reworked your code fragment into what I believe you are trying to do. Avoid eval as it can cause endless problems. Your use of global in this context isn't valid. See if this works for you:

from turtle import Screen, Turtle

class Body:
    def __init__(self, positions):
        self.cells = []
        self.make_body(positions)
        # self.a()
        # self.Alive(screen)

    def make_body(self, positions):
        for position in positions:
            cell = Turtle(shape='square')
            cell.fillcolor('white')
            cell.penup()
            cell.goto(position)

            self.cells.append(cell)

def restart():
    start_positions.clear()

    y = 250

    for _ in range(25):
        x = -250

        for _ in range(25):
            start_positions.append((x, y))
            x += 20

        y -= 20

start_positions = []

screen = Screen()
screen.setup(550, 550)
screen.bgcolor('black')
screen.tracer(False)

restart()

my_body = Body(start_positions)

screen.tracer(True)
screen.exitonclick()

Since I've turned off tracer() for speed, call screen.update() whenever you're ready for the user to see the most recent changes.

cdlane
  • 40,441
  • 5
  • 32
  • 81