Jump to content

Python script


Recommended Posts

This is a script I wrote back in my first week of python for a one-dimensional game of life. It worked that first week, but I'm not sure the indents are still right. It gives me an error that self is not defined after the user says what cells are on to being with.

 

from Tkinter import *

#WORLD CREATION
numcells = input("How big is the world? ")
world = [0 for i in range(numcells)]

w = 800/numcells
h = 25

#RULES CREATION
#0 for off, 1 for on
#Prompts user with all permutations
print "What will happen to the center? "
rules = [0 for i in range(]
rules[0] = input("0 0 0 -> ")
rules[1] = input("0 0 1 -> ")
rules[2] = input("0 1 0 -> ")
rules[3] = input("0 1 1 -> ")
rules[4] = input("1 0 0 -> ")
rules[5] = input("1 0 1 -> ")
rules[6] = input("1 1 0 -> ")
rules[7] = input("1 1 1 -> ")

#START STATE
print "Which cells are on? Give -1 to stop."
index = input("CELL #")
while index <> -1:
  world[index-1] = 1
  index = input("CELL #")

prevworld = [0 for i in range(numcells)]

def nextworld():
  for i in range(numcells):
    c = i
    l = i-1
    r = i+1
    if l < 0:
      l = numcells-1
    if r == numcells:
      r = 0
    rule = 4*prevworld[l]+2*prevworld[c]+prevworld[r]
    world[c] = rules[rule]
  return world

class TkAutomaton:
    # Create our objects.
    def __init__(self):
        #
        # Initialize the object.
        #

        self.done = 0  # Flag; if 1, stop the simulation.
        self.go = 0 # Flag; if 1, step through simulation.

        #
        # Set up the Tk interface.
        #
       
        self.tk = Tk()
        self.tk.wm_geometry("800x600+20+40")
        self.tk.bind("«Return»", self.finish)
        self.tk.bind("«space»", self.step)
        self.canvas = Canvas(self.tk, width=800, height=600)
        self.canvas.pack()

        # Draw a single square.  Modify this for your purposes.
        # Returns the square as an object which can be stored.
for i in range(numcells):
  x1 = i*w
  y1 = 0
  x2 = (i+1)*w
  y2 = h
  if world[i] == 1:
    self.canvas.create_rectangle(x1, y1, x2, y2, fill="red", outline="red")
  elif world[i] == 0:
    self.canvas.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
    prevworld[i] = world[i]
    self.iter = 0
    self.tk.update()


#    def moveSquareUp(self, s):
#        self.canvas.move(s, 0, -50)

    def finish(self, e):
      self.done = 1

    def step(self, e):
      self.go = 1

    def update(self):
# put your update code here
      if self.go:
        self.iter = self.iter + 1
        wor = nextworld()
      for i in range(numcells):
        x1 = i*w
        y1 = self.iter*h
        x2 = (i+1)*w
        y2 = (self.iter+1)*h
      if wor[i] == 1:
        self.canvas.create_rectangle(x1, y1, x2, y2, fill="red", outline="red")
      elif wor[i] == 0:
        self.canvas.create_rectangle(x1, y1, x2, y2, fill="blue", outline="blue")
      prevworld[i] = wor[i]
    self.go = 0
  self.tk.update()


a = TkAutomaton()

while not a.done:
    a.update()

 

Anyone know what is going on?

Link to comment
Share on other sites

  • 4 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.