Esempio n. 1
0
class Application(tk.Frame):
    def __init__(self, master=None):
        super(Application, self).__init__(master)
        self.world = None
        self.autoplay = tk.IntVar()

        self.pack()
        self.create_widgets()

    def create_widgets(self):
        """create and bind control buttons and place them"""
        self.turn_btn = ttk.Button(self,
                                   text="TURN",
                                   command=self.turn,
                                   style="TButton")
        self.quit_btn = ttk.Button(self,
                                   text="QUIT",
                                   command=root.destroy,
                                   style="TButton")
        self.regen_btn = ttk.Button(self, text="REGENERATE", style="TButton")

        self.play_btn = tk.Checkbutton(self,
                                       text="PLAY",
                                       variable=self.autoplay,
                                       command=self.autoplay_turn)

        self.logging_stext = tkst.ScrolledText(self, state="disabled")
        self.logging_stext.configure(font="TkDefaultFont")
        self.logging_stext.tag_config("INFO", foreground="black")
        self.logging_stext.tag_config("DEBUG", foreground="gray")
        self.logging_stext.tag_config("WARNING", foreground="orange")
        self.logging_stext.tag_config("ERROR", foreground="red")
        self.logging_stext.tag_config("CRITICAL",
                                      foreground="red",
                                      underline=1)

    def create_world(self, dimensions=WORLD_DIMENSION):
        """construct world, create board"""
        if self.world:
            raise Exception("world exists")

        self.world = World(dimensions, mode="hex")
        self.board_display = WorldBoard(self,
                                        self.world.dimensions,
                                        mode="hex")

        self.world.generate()

        self.regen_btn["command"] = self.regen_world

        BoardStylist(self.world.factory.organisms)

        self.update_board()
        self.place()

    def place(self):
        self.board_display.pack(side="right", padx=10, pady=10)
        self.logging_stext.pack(side="top")
        self.turn_btn.pack(side="left", padx=5)
        self.play_btn.pack(side="left", padx=5)
        self.quit_btn.pack(side="left", padx=5)
        self.regen_btn.pack(side="left", padx=5)

    def turn(self):
        """Will execute world turn and update UI board with data from world"""
        self.world.turn()
        self.turn_btn["text"] = "TURN {}".format(self.world.turn_count)
        self.update_board()

    def update_board(self):
        self.board_display.update_with(self.world.board)

    def regen_world(self):
        self.world.generate()
        self.world.turn_count = 0
        self.update_board()
        self.logging_stext.configure(state="normal")
        self.logging_stext.delete(1.0, tk.END)
        self.logging_stext.configure(state="disabled")

    def autoplay_turn(self):
        if self.autoplay:
            self.turn()
            self.after(200, self.autoplay_turn)
Esempio n. 2
0
import logging

from simulation.world import World
from simulation.life import *

logging.basicConfig(level=logging.ERROR)

logger = logging.getLogger(__name__)

WORLD_DIMENSION = (20, 20)
TURNS = 20

if __name__ == "__main__":
    world = World(WORLD_DIMENSION)
    world.generate()

    for turn in range(TURNS):
        world.turn()
        world.print_board()