Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pygame, random, re, colorsys
- from tkinter import *
- #from Classes.ButtonClass import Button
- pygame.init();
- width, height = 250, 200
- screen1 = pygame.display.set_mode((width, height))
- pygame.display.set_caption('Cellular Automata')
- clock = pygame.time.Clock()
- crashed = False
- quitted = False
- first = True
- #fontPath = 'C:/Users/Moist/Desktop/CACA/Fonts/Little Conquest.ttf'
- fontPath = 'F:/CACA/Fonts/Little Conquest.ttf'
- fontPath2 = 'F:/CACA/Fonts/Nineteen Ninety Three.otf'
- small = pygame.font.Font(fontPath, 15)
- small2 = pygame.font.Font(fontPath2, 15)
- medium = pygame.font.Font(fontPath, 30)
- large = pygame.font.Font(fontPath, 60)
- buttons = []
- mode = "menu"
- loggedIn = False
- white = (255, 255, 255)
- black = (0, 0, 0)
- red = (255, 0, 0)
- green = (0, 255, 0);
- blue = (0, 0, 255)
- light_grey = (240, 240, 240)
- grey = (127, 127, 127)
- #python -m cProfile -o temp.dat F:\CACA\CellularAutomata.py
- ### ## ## #### #### ###### ####
- ### ## ## #### ## # ## # ## ## #
- ## ## ## ## ### ### ##### ###
- ## ## ###### ## ## ## ##
- ### ## ## ## ## # ## # ## ## # ##
- ### ######## ## ## ### ### ###### ###
- ######################## BUTTON ########################
- class PGButton:
- def __init__(self, xt, yt, wt, ht, name, valuet):
- self.x = xt
- self.y = yt
- self.w = wt
- self.h = ht
- self.rect = pygame.Rect(xt, yt, wt, ht)
- self.value = valuet
- self.text = small.render(name, False, black)
- def display(self):
- #Could have a value to say which screen to draw to
- if self.rect.collidepoint((mouseX, mouseY)):
- pygame.draw.rect(screen1, (210, 210, 210), self.rect, 0)
- else:
- pygame.draw.rect(screen1, light_grey, self.rect, 0)
- pygame.draw.line(screen1, black, (self.x, self.y), (self.x + self.w, self.y), 1)
- pygame.draw.line(screen1, black, (self.x + self.w, self.y), (self.x + self.w, self.y + self.h), 1)
- pygame.draw.line(screen1, black, (self.x + self.w, self.y + self.h), (self.x, self.y + self.h), 1)
- pygame.draw.line(screen1, black, (self.x, self.y + self.h), (self.x, self.y), 1)
- screen1.blit(self.text, (self.x + 3, self.y + 3))
- def pressed(self):
- self.b = False
- if self.rect.collidepoint((mouseX, mouseY)):
- if event.type == pygame.MOUSEBUTTONDOWN:
- self.b = True
- return self.b
- def val(self):
- return self.value
- ######################## Log In Class ########################
- class login:
- def __init__(self, master, errorMsg):
- self.submitted = False
- self.master = master
- master.title("Log In")
- #master.geometry("200x150")
- self.label = Label(master, text = "Enter log in details.")
- self.label.pack()
- if errorMsg != "":
- self.errorMsgLab = Label(master, text = errorMsg, fg = "red")
- self.errorMsgLab.pack()
- self.uNameB = Entry(master)
- self.labelU = Label(master, text = "Username:")
- self.labelU.pack() #side = LEFT)
- self.uNameB.pack(padx = 10)
- self.pWordB = Entry(master)
- self.labelP = Label(master, text = "Password:")
- self.labelP.pack() #side = LEFT)
- self.pWordB.pack(padx = 10)
- self.submitButt = Button(master, text = "Submit", command = self.submit)
- self.submitButt.pack(pady = 5)
- def submit(self):
- self.submitted = True
- info = [self.uNameB.get(), self.pWordB.get()]
- self.master.destroy()
- logIn(True, info)
- def getInfo(self):
- if self.submitted:
- return info
- ######################## Sign Up Class ########################
- class signup:
- def __init__(self, master, errorMsg):
- self.submitted = False
- self.master = master
- master.title("Sign Up")
- self.label = Label(master, text = "Fill out this form to sign up.")
- self.label.pack()
- if errorMsg != "":
- self.errorMsgLab = Label(master, text = errorMsg, fg = "red")
- self.errorMsgLab.pack()
- self.uNameB = Entry(master)
- self.labelU = Label(master, text = "Username:")
- self.labelU.pack() #side = LEFT)
- self.uNameB.pack(padx = 10)
- self.pWordB = Entry(master)
- self.labelP = Label(master, text = "Password:")
- self.labelP.pack() #side = LEFT)
- self.pWordB.pack(padx = 10)
- self.submitButt = Button(master, text = "Submit", command = self.submit)
- self.submitButt.pack(pady = 5)
- def submit(self):
- self.submitted = True
- info = [self.uNameB.get(), self.pWordB.get()]
- self.master.destroy()
- signUp(True, info)
- def getInfo(self):
- if self.submitted:
- return info
- ######################## Pixel Class ########################
- class pixel():
- def __init__(self, idt): #, xt, yt, colt):
- self.x = random.randrange(width) #xt
- self.y = random.randrange(height) #yt
- self.px = self.x
- self.py = self.y
- self.pos = int(self.y * width + self.x)
- self.id = idt
- r, g, b = colorsys.hsv_to_rgb(self.id / 255, 1, 1)
- self.col = (r * 255, g * 255, b * 255)
- def move(self):
- s = self.surrounding()
- surroundingBool = [False if i == None else True for i in s]
- r1 = random.random()
- r2 = random.random()
- r3 = random.random()
- if r1 <= 0.333:
- if r3 < 0.5 and not surroundingBool[4]: self.x += 1
- elif not surroundingBool[3]: self.x -= 1
- elif r1 <= 0.666:
- if r3 < 0.5 and not surroundingBool[6]: self.y += 1
- elif not surroundingBool[1]: self.y -= 1
- else:
- if r2 < 0.25 and not surroundingBool[7]:
- self.x += 1
- self.y += 1
- elif r2 < 0.5 and not surroundingBool[2]:
- self.x += 1
- self.y -= 1
- elif r2 < 0.75 and not surroundingBool[5]:
- self.x -= 1
- self.y += 1
- elif not surroundingBool[0]:
- self.x -= 1
- self.y -= 1
- self.x %= width
- self.y %= height
- self.pos = self.y * width + self.x
- def surrounding(self):
- self.surr = [None, None, None, None, None, None, None, None]
- for i in range(9):
- #for x in range(-1, 2):
- #for y in range(-1, 2):
- if i != 5:
- x = i % 3 - 2
- y = int((i % 3) / 3) - 1
- if x == 0 and y == 0:
- pass
- else:
- PAI = allPixels[(self.x + x) % width][(self.y + y) % height] if allPixels[(self.x + x) % width][(self.y + y) % height] != None else None
- self.surr[(y * 3) + x] = (PAI)
- return self.surr
- ######################## Cellular Automata Class ########################
- class CellularAuto:
- def __init__(self, idt):
- self.pixels = []
- self.id = idt
- self.pixels.append(pixel(self.id))
- def display(self):
- for pix in self.pixels:
- screen2.set_at((pix.x, pix.y), pix.col)
- def update(self):
- for pix in self.pixels:
- pix.px = pix.x
- pix.py = pix.y
- pix.move()
- def addPix(self):
- self.pixels.append(pixel(self.id))
- ###### ## ## ## ## ### ########## ## ##### ## ## ####
- ## ## ## #### ## ### ## ## ## ## ## #### ## ## #
- ##### ## ## ## ## ## ## ## ## ## ## ## ## ## ###
- ## ## ## ## #### ## ## ## ## ## ## #### ##
- ## ## ## ## ### ### ## ## ## ## ## ## ### # ##
- ## ##### ## ## ### ## ## ##### ## ## ###
- ######################## LOG OUT ########################
- def logout():
- global first, loggedIn, USERNAME
- loggedIn = False
- USERNAME = ""
- first = True
- ######################## MENU ########################
- def menu():
- global first, screen1, buttons, mode
- if first:
- first = False
- buttons = []
- if not loggedIn:
- width, height = 250, 230
- buttons.append(PGButton(10, 80, 40, 20, "Start", "start"))
- buttons.append(PGButton(10, 110, 135, 20, "Custom Automaton", "custom"))
- buttons.append(PGButton(10, 140, 55, 20, "Log In", "login"))
- buttons.append(PGButton(10, 170, 115, 20, "Create Account", "signup"))
- buttons.append(PGButton(10, 200, 45, 20, "Quit", "quit"))
- else:
- sw, sh = large.size(USERNAME)
- if sw > 250:
- w = sw
- else:
- w = 250
- width, height = w, 280
- buttons.append(PGButton(10, 130, 40, 20, "Start", "start"))
- buttons.append(PGButton(10, 160, 135, 20, "Custom Automaton", "custom"))
- buttons.append(PGButton(10, 190, 110, 20, "Your Automata", "yours"))
- buttons.append(PGButton(10, 220, 60, 20, "Log Out", "logout"))
- buttons.append(PGButton(10, 250, 45, 20, "Quit", "quit"))
- screen1 = pygame.display.set_mode((width, height))
- screen1.fill((white));
- if not loggedIn:
- screen1.blit(large.render("Welcome", False, black), (10, 10))
- else:
- screen1.blit(large.render("Welcome", False, black), (10, 10))
- screen1.blit(large.render(USERNAME, False, black), (10, 60))
- but = doButtons()
- if but != "":
- if but == "start":
- mode = "startmenu"
- first = True
- startMenu()
- elif but == "custom":
- mode = "CA"
- first = True
- CustomA()
- elif but == "login":
- mode = "login"
- first = True
- logIn(False, "")
- elif but == "signup":
- mode = "signup"
- first = True
- signUp(False, "")
- elif but == "quit":
- global quitted
- quitted = True
- elif but == "yours":
- mode = "YA"
- first = True
- YA()
- elif but == "logout":
- mode = "menu"
- logout()
- ######################## Log In Function ########################
- def logIn(parsed, loginDetails):
- global first, loggedIn
- if first:
- first = False
- root = Tk()
- if parsed == False and loginDetails != "":
- gui = login(root, loginDetails)
- else:
- gui = login(root, "")
- root.mainloop()
- if parsed:
- if loginDetails[0] == "" or loginDetails[1] == "":
- first = True
- logIn(False, "Please fill both fields.")
- elif len(loginDetails[1]) < 8:
- first = True
- logIn(False, "Password is too short")
- else:
- users = eval(open("F:/CACA/Users.txt").read())
- uNameTrue = False
- for uName in users:
- if not loggedIn:
- if loginDetails[0] == uName and loginDetails[1] == users[uName]:
- loggedIn = True
- global USERNAME
- USERNAME = uName
- first = True
- elif loginDetails[0] == uName:
- uNameTrue == True
- if not loggedIn:
- if uNameTrue:
- first = True
- logIn(False, "Incorrect Password")
- else:
- first = True
- logIn(False, "Incorrect Username")
- ######################## Sign Up Function ########################
- def signUp(parsed, signUpDetails):
- global first, loggedIn
- problem = False
- if first:
- first = False
- root = Tk()
- if parsed == False and signUpDetails != "":
- gui = signup(root, signUpDetails)
- else:
- gui = signup(root, "")
- root.mainloop()
- if parsed:
- if len(signUpDetails[1]) < 8:
- first = True
- signUp(False, "Your password is too short.")
- else:
- users = eval(open("F:/CACA/Users.txt").read())
- for uName in users:
- if not problem:
- if signUpDetails[0] == uName:
- problem = True
- first = True
- signUp(False, "Existing username")
- if not problem:
- if re.match(r'[A-Za-z0-9@#$%^&+=]{8,}', signUpDetails[1]):
- usersfile = open("F:/CACA/Users.txt", "w")
- usersfile.seek(0)
- usersfile.truncate()
- usersfile.write("{")
- for uName in users:
- usersfile.write("\"" + uName + "\":\"" + users[uName] + "\", ")
- users[signUpDetails[0]] = signUpDetails[1]
- usersfile.write("\"" + signUpDetails[0] + "\":\"" + signUpDetails[1] + "\"}")
- loggedIn = True
- global USERNAME
- USERNAME = signUpDetails[0]
- first = True
- else:
- first = True
- signUp(False, "Invalid characters in password")
- def startMenu():
- global first, screen1, buttons, mode
- if first:
- first = False
- buttons = []
- buttons.append(PGButton(10, 220, 60, 20, "Go Back", "back"))
- buttons.append(PGButton(75, 220, 120, 20, "Start simulation", "startSim"))
- width, height = 205, 250
- screen1 = pygame.display.set_mode((width, height))
- screen1.fill((white))
- screen1.blit(small2.render("This will do shit yanno", False, black), (15, 10))
- screen1.blit(small2.render("More damned lines", False, black), (15, 26))
- screen1.blit(small2.render("More linezzzzzzzzzzz", False, black), (15, 42))
- screen1.blit(small2.render("Line number four", False, black), (15, 58))
- screen1.blit(small2.render("This will have more ", False, black), (15, 74))
- screen1.blit(small2.render("informative stuff in", False, black), (15, 90))
- screen1.blit(small2.render("the end i promise lol", False, black), (15, 106))
- but = doButtons()
- if but == "back":
- mode = "menu"
- first = True
- menu()
- elif but == "startSim":
- mode = "CA"
- first = True
- CA()
- def CA():
- global first, buttons, allPixels, CAs, screen2, width, height
- if first:
- first = False
- width, height = 1200, 600
- buttons = []
- allPixels = []
- CAs = []
- CAs.append(CellularAuto(int(random.random() * 255)))
- CAs.append(CellularAuto(int(random.random() * 255)))
- for x in range(width):
- allPixels.append([])
- for y in range(height):
- allPixels[x].append(None)
- screen2 = pygame.display.set_mode((width, height))
- screen2.fill(white)
- for i in range(10):
- CAs[0].addPix()
- CAs[1].addPix()
- for CellularAutomata in CAs:
- for pix in CellularAutomata.pixels:
- allPixels[pix.px][pix.py] = None
- for CellularAutomata in CAs:
- for pix in CellularAutomata.pixels:
- allPixels[pix.x][pix.y] = pix
- CellularAutomata.update()
- CellularAutomata.display()
- def doButtons():
- but = ""
- for b in buttons:
- b.display()
- if b.pressed():
- but = b.val()
- return but
- ######################## Main Loop ########################
- while not quitted:
- for event in pygame.event.get():
- if event.type == pygame.QUIT:
- quitted = True
- mouseX, mouseY = pygame.mouse.get_pos()
- pygame.display.update()
- if mode == "menu":
- menu()
- elif mode == "startmenu":
- startMenu()
- elif mode == "CA":
- CA()
- elif mode == "login":
- logIn(False, "")
- elif mode == "signup":
- signUp(False, "")
- elif mode == "run":
- run()
- clock.tick(60)
- pygame.quit()
- quit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement