Cell-sim
import time from graphics import * import random print("Welcome, this program simulates viewing cells in real time") win = GraphWin("Field", 500, 500) Control = GraphWin("Control Panel",400, 300) Control.setBackground('grey') Control.setCoords(0, 0, 299, 199) win.setCoords(0, 0, 499, 499) temperature = 42 Speed = 10 def Controlpanel(): global te global temperature global Speed global spe global restart global heat faster = Rectangle(Point(35, 130), Point(85, 180)) faster.draw(Control) f = Text(Point(60, 155), "faster") f.draw(Control) pause = Rectangle(Point(95, 130), Point(145, 180)) pause.draw(Control) p = Text(Point(120, 155), "pause") p.draw(Control) food = Rectangle(Point(155, 130), Point(205, 180)) food.draw(Control) fo = Text(Point(180, 155), "DINNER") fo.draw(Control) warmer = Rectangle(Point(215, 130), Point(265, 180)) warmer.draw(Control) w = Text(Point(240, 155), "HOT") w.draw(Control) slower = Rectangle(Point(35, 20), Point(85, 70)) slower.draw(Control) s = Text(Point(60, 45), "slower") s.draw(Control) cooler = Rectangle(Point(215, 20), Point(265, 70)) cooler.draw(Control) c = Text(Point(240, 45), "cooler") c.draw(Control) speed = Rectangle(Point(95, 20), Point(145, 70)) speed.draw(Control) speed.setOutline('grey') speed.setFill('grey') sp = Text(Point(120, 53), "speed") sp.draw(Control) spe = Text(Point(120, 30), Speed) spe.draw(Control) temp = Rectangle(Point(155, 20), Point(205, 70)) temp.draw(Control) temp.setOutline('grey') temp.setFill('grey') t = Text(Point(180, 53), "temp") t.draw(Control) te = Text(Point(180, 30), temperature) te.draw(Control) def checkbutton(a): Controlpanel() flagStart =False; global temperature global te global Speed global spe blah = Control.checkMouse() if blah != None: x = blah.getX() y = blah.getY() if (35 <= x <= 85) and (130 <= y <= 180):#make it faster and change speed if a > 0.05: a -= 0.05 Speed = int(spe.getText()) + 1 spe.setText(Speed) return a if (95 <= x <= 145) and (130 <= y <= 180): if(flagStart==True): flagStart =False return(a) else: file = open('simData.txt', 'w+') file.write(str(win)) file.write(str(te)) file.write(str(a)) sim = Text(Point(120, 53), "Simulation Data Saved") sim.draw(win) flagStart =True win.getMouse() #pause works correctly elif (35 <= x <= 85) and (20 <= y <= 70):#make it slower and change speed a += 0.05 Speed = int(spe.getText()) - 1 spe.setText(Speed) return a if (155 <= x <= 205) and (130 <= y <= 180):#food i = random.randint(5, 494) c = random.randint(5, 494) rec = Rectangle(Point(i, c), Point(i + 5, c + 5)) rec.setFill('black') rec.draw(win) if (215 <= x <= 265) and (130 <= y <= 180):#warmer temperature = int(te.getText()) + 1 te.setText(temperature) elif (215 <= x <= 265) and (20 <= y <= 180):#cooler temperature = int(te.getText()) - 1 te.setText(temperature) return(a) def makeCrete(): mylist = [] for x in range(random.randint(5, 12)): i = Circle(Point(random.randint(50, 451), random.randint(50, 451)), 8) i.setFill('green') i.setOutline('green') mylist.append(i) return mylist def makeLaelaps(): mylist2 = [] for y in range(random.randint(3, 6)): p = Circle(Point(random.randint(100, 401), random.randint(100, 401)), 16) p.setFill('red') p.setOutline('red') laelist = [p, random.choice([-12, -10, -8, 8, 10, 12]), random.choice([-12, -10, -8, 8, 10, 12])] mylist2.append(laelist) return mylist2 def bounce(int1, int2): if int1 < 15 or int1 > 485: return ((-1) * int2) else: return(int2) def main(): Speedr = 0.25 Controlpanel() mylist2 = makeLaelaps() mylist = makeCrete() for x in mylist: x.draw(win) for j in range(len(mylist2)):#must do range(len(mylist2)) or they will move in a staggered way (like the Crete cells) mylist2[j][0].draw(win) while win.checkMouse()== None: Speedr = checkbutton(Speedr) time.sleep(Speedr) # make laelaps bounce off of crete for x in mylist: for j in range(len(mylist2)): pop = (x.getCenter().getX()) - (mylist2[j][0].getCenter().getX()) crackle = (x.getCenter().getY()) - (mylist2[j][0].getCenter().getY()) c = 28 if((pop**2) + (crackle**2) < (c**2)): mylist2[j][1] *= (-1) mylist2[j][2] *= (-1) #make crete bounce off each other for x in mylist: pop = (x.getCenter().getX()) crackle = (x.getCenter().getY()) c = 16 if abs((pop**2) + (crackle**2) < (c**2)): x.move(bounce((x.getCenter().getX()), random.choice([-20, 20])), bounce((x.getCenter().getY()), random.choice([-20, 20]))) #make laelaps bounce off each other for j in range(len(mylist2)): pop = (mylist2[j][0].getCenter().getX()) crackle = (mylist2[j][0].getCenter().getY()) c = 36 if abs((pop**2) + (crackle**2) < (c**2)): mylist2[j][1] *= (-1) #have to redefine this or else it will not move in a straight line mylist2[j][2] *= (-1) #makes crete cells bounce in separate directions for x in mylist: x.move(bounce((x.getCenter().getX()), random.randint(-6, 6)), bounce((x.getCenter().getY()), random.randint(-6, 6))) #laelaps bounce in separate directions for j in range(len(mylist2)): mylist2[j][1] = bounce(mylist2[j][0].getCenter().getX(), mylist2[j][1]) mylist2[j][2] = bounce(mylist2[j][0].getCenter().getY(), mylist2[j][2]) mylist2[j][0].move(mylist2[j][1], mylist2[j][2]) Control.close() win.close() print("This simulation has terminated!!!! Yay!!!! :) ") main()