Ejemplo n.º 1
0
 def __init__(self, file):
     """
     Initial parameters
     """
     pygame.init()
     self._running = True
     self.width = 672
     self.height = 704
     self.size = (self.width, self.height)
     self.game_map = Grid()
     self.game_map.initialize_maze(file)
     self._screen = pygame.display.set_mode(
         self.size, FULLSCREEN
     )
     self.maze = Maze(self.game_map, self._screen)
Ejemplo n.º 2
0
    def setUp(self):

        self.pedestrians = [((0, 0), 1), ((1, 2), 1)]
        self.target = (4, 5)
        self.obstacles = [(2, 1), (2, 3)]
        self.grid = Grid(rows=5,
                         columns=6,
                         pedestrians=self.pedestrians,
                         obstacles=self.obstacles,
                         target=self.target)
Ejemplo n.º 3
0
    def update(self, grid: Grid, avoid_overlapping: bool) -> None:
        """ 
        This function calles another function to determine the costs of the cells and then decides to which cells the pedestrians are supposed to move
        :param grid: The grid for which the update step is going to be done
        :param avoid_overlapping: If set to true, then the pedestrians are not going to overlap (a lot slower)
        """

        if avoid_overlapping:
            for key in grid.pedestrians.keys():
                costs = self.get_costs(grid)
                grid.update_pedestrian(
                    self.move_pedestrian(grid.pedestrians[key], costs), key)
        else:
            costs = self.get_costs(grid)

            # move the pedestrians
            moved_pedestrians = {
                key: self.move_pedestrian(grid.pedestrians[key], costs)
                for key in grid.pedestrians.keys()
            }

            # update the pedestrians
            grid.update_pedestrians(moved_pedestrians)
Ejemplo n.º 4
0
class Game:
    """Game loop"""
    def __init__(self, file):
        """
        Initial parameters
        """
        pygame.init()
        self._running = True
        self.width = 672
        self.height = 704
        self.size = (self.width, self.height)
        self.game_map = Grid()
        self.game_map.initialize_maze(file)
        self._screen = pygame.display.set_mode(
            self.size, FULLSCREEN
        )
        self.maze = Maze(self.game_map, self._screen)

    def on_event(self, event):
        """
        Stop the game if quit or escape.
        Return keydown
        """
        if event.type == QUIT:
            self._running = False
            return
        elif event.type == KEYDOWN and event.key == K_ESCAPE:
            self._running = False
            return
        elif event.type == KEYDOWN:
            return event.key

    def on_execute(self):
        """Game"""

        self.maze.init_map(self.game_map)
        while self._running:
            pygame.time.delay(120)
            for event in pygame.event.get():
                if (event.type == QUIT
                        or (event.type == KEYDOWN
                            and event.key == K_ESCAPE)):
                    self._running = False
            self.maze.player.enter_key(self.game_map)
            self.maze.player.print_position()
            self.maze.on_render(self.game_map)

            if self.maze.player.position == self.maze.guardian.position:
                if not self.maze.bag.full:
                    self.maze.death_animation()
                    guardian_position = self.maze.blit_item(
                        self.maze.guard, self.maze.guardian.position)
                    pygame.display.update(guardian_position)
                    self.maze.game_over()
                    pygame.time.delay(2000)
                    self._running = False
                else:
                    for element in self.maze.list_guardian_animation:
                        self.maze.blit_item(
                            element, self.maze.guardian.position)
                    guardian_position = self.maze.blit_item(
                        self.maze.macgyver, self.maze.guardian.position)
                    pygame.display.update(guardian_position)
            if self.maze.player.position == self.game_map.exit:
                self.maze.win_game()
                pygame.time.delay(2000)
                self._running = False
Ejemplo n.º 5
0
    def create_widgets(self):
        """
        Créé et affiche tous les éléments de la fenêtre d'édition de grille
        """
        # Créer trois conteneurs
        left_bar = Frame(self, padding=(10, 10, 10, 10))
        left_bar.columnconfigure(0, weight=1)
        left_bar.grid(row=0, column=0, sticky=N + S + W)

        mid_bar = Frame(self)
        mid_bar.columnconfigure(0, weight=1)
        mid_bar.grid(row=0, column=1, sticky=N + S + W + E)

        right_bar = Frame(self, padding=(10, 10, 10, 10))
        right_bar.columnconfigure(0, weight=1)
        right_bar.grid(row=0, column=2, sticky=N + S + W)

        # Ajouter deux zones de texte dans la barre de gauche
        # Titre principal
        Label(left_bar,
              text=self.TITLE,
              font=("Helvetica", 16),
              padding=(0, 0, 0, 10)).grid(row=0, column=0, sticky=N)
        # Texte d'aide
        Label(
            left_bar,
            text=self.HELPTEXT,
            font=("Helvetica", 12),
            wraplength=250,
            anchor=N,
            justify=LEFT,
        ).grid(row=1, column=0, sticky=N)

        # Créer une nouvelle grille vide avec les bonnes dimensions
        self.dosun_grid = Grid(
            self.grid_dimensions[0],
            self.grid_dimensions[1],
            self.solvable,
            self.grid["blacks"],
            self.grid["zones"],
            master=mid_bar,
        )
        self.dosun_grid.grid(row=0, column=1, sticky=W + E + N + S)

        # Ajouter les boutons
        Button(
            right_bar,
            text="Create zone from selection",
            command=self.dosun_grid.make_zone_from_selection,
        ).grid(row=0, column=0, sticky=W + E)
        Button(
            right_bar,
            text="Make selection solid",
            command=self.dosun_grid.toggle_selection_solid,
        ).grid(row=1, column=0, sticky=W + E)
        Button(right_bar, text="Solve!",
               command=self.dosun_grid.solve).grid(row=2,
                                                   column=0,
                                                   sticky=W + E)
        # Dessiner la zone de texte associée au StringVar self.solvable
        Label(right_bar, textvariable=self.solvable,
              font=("Helvetica", 12)).grid(row=3,
                                           column=0,
                                           sticky=S,
                                           pady=(10, 10))
Ejemplo n.º 6
0
class Editor_Frame(Frame):
    """
    Fenêtre d'édition de grille de Dosun Fuwari. Permet de créer une nouvelle
    grille (ou d'en charger une préexistante depuis un fichier), puis de la
    résoudre avec pycosat, de l'enregistrer dans un fichier JSON, ou d'exporter
    la formule de solvabilité dans un fichier DIMACS.
    """

    # Variables de classe contenant les chaines statiques à afficher
    TITLE = "Dosun Fuwari Solver"
    HELPTEXT = ("Click on a cell to select it, then use the buttons below to "
                "set it to black or to create a zone from the selection.")

    def __init__(self, grid_w, grid_h, grid, master=None):
        """
        Initialisation automatique à la création de la fenêtre d'édition.
        Arguments:
          - grid_w: largeur de la grille
          - grid_h: hauteur de la grille
          - grid: dictionnaire contenant la liste des cases noires et la liste
                  des zones de la grille. Format: {"blacks": [], "zones": []}
          - master (optional): objet Tk parent
        """
        super().__init__(master)
        # assigner les variables d'instance
        self.master = master
        self.grid_dimensions = (grid_w, grid_h)  # dimensions de la grille
        # StringVar qui contiendra le résultat du satsolver à afficher
        self.solvable = StringVar()
        self.grid = grid

        # création des éléments à afficher
        self.create_widgets()

    def create_widgets(self):
        """
        Créé et affiche tous les éléments de la fenêtre d'édition de grille
        """
        # Créer trois conteneurs
        left_bar = Frame(self, padding=(10, 10, 10, 10))
        left_bar.columnconfigure(0, weight=1)
        left_bar.grid(row=0, column=0, sticky=N + S + W)

        mid_bar = Frame(self)
        mid_bar.columnconfigure(0, weight=1)
        mid_bar.grid(row=0, column=1, sticky=N + S + W + E)

        right_bar = Frame(self, padding=(10, 10, 10, 10))
        right_bar.columnconfigure(0, weight=1)
        right_bar.grid(row=0, column=2, sticky=N + S + W)

        # Ajouter deux zones de texte dans la barre de gauche
        # Titre principal
        Label(left_bar,
              text=self.TITLE,
              font=("Helvetica", 16),
              padding=(0, 0, 0, 10)).grid(row=0, column=0, sticky=N)
        # Texte d'aide
        Label(
            left_bar,
            text=self.HELPTEXT,
            font=("Helvetica", 12),
            wraplength=250,
            anchor=N,
            justify=LEFT,
        ).grid(row=1, column=0, sticky=N)

        # Créer une nouvelle grille vide avec les bonnes dimensions
        self.dosun_grid = Grid(
            self.grid_dimensions[0],
            self.grid_dimensions[1],
            self.solvable,
            self.grid["blacks"],
            self.grid["zones"],
            master=mid_bar,
        )
        self.dosun_grid.grid(row=0, column=1, sticky=W + E + N + S)

        # Ajouter les boutons
        Button(
            right_bar,
            text="Create zone from selection",
            command=self.dosun_grid.make_zone_from_selection,
        ).grid(row=0, column=0, sticky=W + E)
        Button(
            right_bar,
            text="Make selection solid",
            command=self.dosun_grid.toggle_selection_solid,
        ).grid(row=1, column=0, sticky=W + E)
        Button(right_bar, text="Solve!",
               command=self.dosun_grid.solve).grid(row=2,
                                                   column=0,
                                                   sticky=W + E)
        # Dessiner la zone de texte associée au StringVar self.solvable
        Label(right_bar, textvariable=self.solvable,
              font=("Helvetica", 12)).grid(row=3,
                                           column=0,
                                           sticky=S,
                                           pady=(10, 10))

    def save_grid(self):
        """
        Enregistre la grille au format JSON.
        """
        filename = asksaveasfilename(
            initialdir=".",
            filetypes=(("JSON File", "*.json"), ("All Files", "*.*")),
            title="Choose a file.",
        )
        if filename:
            grid = self.dosun_grid.get_grid()
            fio.save_grid(grid, filename)

    def export_dimacsSAT(self):
        """
        Exporte le fichier DIMACS associé à la grille.
        """
        filename = asksaveasfilename(
            initialdir=".",
            filetypes=(("DIMACS File", "*.cnf"), ("All Files", "*.*")),
            title="Choose a file.",
        )
        if filename:
            grid = self.dosun_grid.get_grid()
            sat = gen_cnf(grid["width"], grid["height"], grid["zones"],
                          grid["blacks"])
            fio.save_dimacs(sat, filename)

    def export_dimacs3SAT(self):
        """
        Exporte le fichier DIMACS (réduit en 3-SAT) associé à la grille.
        """
        filename = asksaveasfilename(
            initialdir=".",
            filetypes=(("DIMACS File", "*.cnf"), ("All Files", "*.*")),
            title="Choose a file.",
        )
        if filename:
            grid = self.dosun_grid.get_grid()
            cnf = gen_cnf(grid["width"], grid["height"], grid["zones"],
                          grid["blacks"])
            tab = sat_3sat(cnf, grid["height"], grid["width"])
            fio.save_dimacs(tab, filename)

    def new_grid(self):
        """
        Ferme la fenêtre d'édition et retourne à la fenêtre de choix de
        dimensions afin de créer une nouvelle grille.
        """
        self.destroy()
        self.master.change(Start_Frame, [])

    def open_grid(self):
        """
        Ouvre une grille depuis un fichier JSON dans une nouvelle fenêtre
        d'édition.
        """
        filename = askopenfilename(
            initialdir=".",
            filetypes=(("JSON File", "*.json"), ("All Files", "*.*")),
            title="Choose a file.",
        )
        if filename:
            grid = fio.read_grid(filename)
            self.destroy()
            self.master.change(Editor_Frame,
                               [grid["width"], grid["height"], grid])

    def create_menu(self, root):
        """
        Dessine le "menu" - la barre d'options en haut de la fenêtre.
        """
        menubar = Menu(root)
        fileMenu = Menu(menubar, tearoff=0)
        fileMenu.add_command(label="New grid", command=self.new_grid)
        fileMenu.add_command(label="Open grid", command=self.open_grid)
        fileMenu.add_command(label="Save grid", command=self.save_grid)
        fileMenu.add_command(label="Export DIMACS SAT",
                             command=self.export_dimacsSAT)
        fileMenu.add_command(label="Export DIMACS 3SAT",
                             command=self.export_dimacs3SAT)
        fileMenu.add_separator()
        fileMenu.add_command(label="Quit", command=quit)
        menubar.add_cascade(label="File", menu=fileMenu)
        return menubar
Ejemplo n.º 7
0
 def setUp(self):
     self.grid = Grid(rows=5,
                      columns=6,
                      target=(3, 5),
                      obstacles=[(2, 3)],
                      pedestrians=[((1, 0), 1), ((0, 1), 1)])
Ejemplo n.º 8
0
    def __generate_rooms(self):
        (rows, cols) = self.__puzzle.size()
        grid = Grid(rows, cols)

        for row in range(rows):
            self.__rooms.append([])
            for col in range(cols):
                letter = self.__puzzle.letter_at(row,col)
                letter_txt = pyfiglet.figlet_format(letter)

                button = Object(
                    "button",
                    "A large illuminated button.",
                    aliases=("illuminated button",),
                    state="OFF",
                    color=Fore.RED,
                    action=lambda: self.state="ON" if self.state == "OFF" else self.state="OFF"
                )

                letter_obj = Object(
                    F"Monolithic Letter '{letter.upper()}'",
                    letter_txt,
                    aliases=(
                        F'letter {letter.lower()}',
                        F'letter {letter.upper()}',
                        'letter',
                        F'{letter.lower()}',
                        F'{letter.upper()}'
                    ),
                    color=Fore.WHITE
                )
                letter_obj.parts.add(button)

                room = Space(
                    F"Puzzle Room - Location[{row},{col}]",
                    F"A large, mostly empty room. There's a {letter_obj} in the center.",
                    # items=[items.lantern],
                    objects=[letter_obj],
                )
                room.location = (row, col)
                self.__rooms[row].append(room)

        # Assign Exits
        for row in range(rows):
            for col in range(cols):
                room = self.__rooms[row][col]
                valid_dirs = grid.valid_directions(row, col)

                if valid_dirs['N']:
                    room.north = self.get_room(valid_dirs['N'][0], valid_dirs['N'][1])
                if valid_dirs['NE']:
                    room.northeast = self.get_room(valid_dirs['NE'][0], valid_dirs['NE'][1])
                if valid_dirs['E']:
                    room.east = self.get_room(valid_dirs['E'][0], valid_dirs['E'][1])
                if valid_dirs['SE']:
                    room.southeast = self.get_room(valid_dirs['SE'][0], valid_dirs['SE'][1])
                if valid_dirs['S']:
                    room.south = self.get_room(valid_dirs['S'][0], valid_dirs['S'][1])
                if valid_dirs['SW']:
                    room.southwest = self.get_room(valid_dirs['SW'][0], valid_dirs['SW'][1])
                if valid_dirs['W']:
                    room.west = self.get_room(valid_dirs['W'][0], valid_dirs['W'][1])
                if valid_dirs['NW']:
                    room.northwest = self.get_room(valid_dirs['NW'][0], valid_dirs['NW'][1])
Ejemplo n.º 9
0
def predator_prey_activity(interactive=True, blind_folded = True, prey_distribtion = '', environment_complexity = ''):    #mute pygame startup prompt
    #remove the startup message
    import os
    import datetime
    os.environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
    
    
    #import pygame and my modules
    import pygame
    from lib.grid import Grid
    from lib.sprite import Sprite
    
    #global list of supported types
    TYPES_OF_PREY_DIST = ['even','random','clumped']
    TYPES_OF_ENV_COMP = ['simple','moderate','complex']
    
    #if you run this as predator_prey_activity(False) you can get terminal assignment of simulation type
    #this is skipped when the next two parameters are filled or if they want it interactive
    if (prey_distribtion == '' or environment_complexity == '') and not interactive:
        
        # get user input
        prey_distribtion = input("Enter Prey Distribution Selection (Options:" + str(TYPES_OF_PREY_DIST) + "): ")
        environment_complexity = input("Enter Enviroment Complexity Selection (Options:" + str(TYPES_OF_ENV_COMP) + "): ")
        
        #if they skip past it, then assign a default
        if prey_distribtion == '':
            prey_distribtion = 'random'
        if environment_complexity == '':
            environment_complexity == 'moderate'
            
        print(f'Running Simulation using a(n) {prey_distribtion} prey distribution and a {environment_complexity} enviornment complexity. ')
    
    
    #initialize pygame and ensure all packages
    pygame.init()
    pygame.font.init()
    
    #title the window
    pygame.display.set_caption("Predatory Prey Simulation")
    
    #variables for running loops
    run = True #main game loop
    count_down = 3 #the seconds to wait before the game starts
    delay = 200 #how long the main game loops waits each iteration to feel natural
    time = 0 # this is a display variabe
    max_time = 60
    
        
    #play again flags
    play_again = False
    selected = False
    
    #for start button
    start_pressed = False
    
    
    #create some assorted sized fonts for typing
    bigfont = pygame.font.SysFont(None, 40)
    mfont = pygame.font.SysFont(None, 30)
    font = pygame.font.SysFont(None, 20)
    
    #create our window
    game_window_width = 1000
    game_window_height = 750
    game_window_size = (game_window_width,game_window_height)
    game_window = pygame.display.set_mode(game_window_size)
    
    #how much of the right half of the screen is the HUD's
    HUD_scale = 0.25
    
    #colors for the backgrounds and to remove magic numbers
    simulation_background_color = (51,51,51)
    highlighted_color = (101,101,101)
    HUD_background_color = (151,151,151)
    color_black = (0,0,0)
    
    # here is the offset of the choice box
    choice_offset = game_window_width/50
    
    #text for descriptions
    prey_dist_text = bigfont.render('Prey Distributions', True, color_black)
    env_type_text = bigfont.render('Enviroment Complexities', True, color_black)
    start_text = bigfont.render('Start', True, color_black)
    
    #text for prey dist choice box & container
    even_text = bigfont.render('EVEN', True, color_black)
    random_text = bigfont.render('RANDOM', True, color_black)
    clumped_text = bigfont.render('CLUMPED', True, color_black)
    prey_text_options = [even_text, random_text, clumped_text]
    
    #text for env dist choice box & container
    simple_text = bigfont.render('SIMPLE', True, color_black)
    moderate_text = bigfont.render('MODERATE', True, color_black)
    complex_text = bigfont.render('COMPLEX', True, color_black)
    env_text_options = [simple_text, moderate_text, complex_text]
    
    #text for env dist choice box & container
    play_again_text = bigfont.render('Play Again?', True, color_black)
    play_nomore = bigfont.render('Exit', True, color_black)
    save_to_file = bigfont.render('Save Results', True, color_black)
    saved_to = font.render('', True, color_black)
    game_over = mfont.render('Simulation Complete', True, color_black)
    
    #varible for the mouse locationfor choice box
    mouse_pos = (-1,-1)  

    #let them see distribution for a second      
    peek = 500
    
    #the onscreen startup text and images
    image_1 = pygame.image.load('lib/supported_input.png')
    intro_1 = bigfont.render('Predator Prey Simulation!', True, color_black)
    intro_2 = mfont.render('This simulation was made to model real life predator, prey, and eviornment relationships!', True, color_black)
    intro_3 = mfont.render('Here is the list of supported prey distributions and enviornment complexities:', True, color_black)
    intro_4 = mfont.render('The simulation will ask for each of these as inputs seperately, and after a countdown start it', True, color_black)
    intro_3 = mfont.render('If \'blindfolded\' the prey will only show for ' + str(peek/1000) + " seconds.", True, color_black)
    intro_5 = mfont.render('Use the arrow keys to overlap the red predator square with the white prey square to kill it', True, color_black)
    intro_6 = mfont.render('Avoid black obstacles, get as many as possible in 60 seconds, record the kills!', True, color_black)
    intro_text = [intro_1, intro_2, intro_3, intro_4, intro_5, intro_6]
    intro_text.append(image_1)

    
    #start the intro
    intro = True
    while intro:
        pygame.time.delay(delay)
        #event handling like skipping everything v just the intro
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                intro = False
                selected = True
                prey_distribtion = '-1'
                environment_complexity = '-1'
                count_down = -1
                start_pressed = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                intro = False
        #background
        pygame.draw.rect(game_window, HUD_background_color, (0,0,game_window_width,game_window_height ) )
        #draw each element in the intro_text container 
        for i, element in enumerate(intro_text):
            game_window.blit(element, (50, i*25+25))
        #draw the screen
        pygame.display.update()
        
    #if at this point the prey distribution etc isnt picked then i assume we need it and want it graphically
    prey_colors = [HUD_background_color,HUD_background_color,HUD_background_color]
    env_colors = [HUD_background_color,HUD_background_color,HUD_background_color]
    
    while not start_pressed:
        pygame.time.delay(delay)
        
        #check for program exit and mouse clicks
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                selected = True
                prey_distribtion = '-1'
                environment_complexity = '-1'
                count_down = -1
                start_pressed = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = event.pos
        #background
        pygame.draw.rect(game_window, simulation_background_color, (0, 0, game_window_width, game_window_height) )
        height = 200
        game_window.blit(prey_dist_text, (choice_offset,choice_offset))
        game_window.blit(env_type_text, (choice_offset,14*choice_offset))
        for o in range(3):
            pygame.draw.rect(game_window, prey_colors[o], ((game_window_width/3*o)+2*choice_offset,3*choice_offset,game_window_width/3-3*choice_offset, height) )
            game_window.blit(prey_text_options[o], ((game_window_width/3*o)+2*choice_offset,3*choice_offset))
            
            pygame.draw.rect(game_window, env_colors[o], ((game_window_width/3*o)+2*choice_offset,height+6*choice_offset,game_window_width/3-3*choice_offset, height) )
            game_window.blit(env_text_options[o], ((game_window_width/3*o)+2*choice_offset,height+6*choice_offset))
        
        pygame.draw.rect(game_window, HUD_background_color, ((game_window_width/3*1)+2*choice_offset,2*height+8*choice_offset,game_window_width/3-3*choice_offset, height/2) )
        game_window.blit(start_text, ((game_window_width/3*1)+2*choice_offset,2*height+8*choice_offset))  
        for o in range(3):
            if mouse_pos[0] > (game_window_width/3*o)+2*choice_offset and mouse_pos[0] < (game_window_width/3*o)+2*choice_offset + game_window_width/3-3*choice_offset:
                if mouse_pos[1] < height + 3*choice_offset:
                    prey_colors = [HUD_background_color,HUD_background_color,HUD_background_color]
                    prey_colors[o] = highlighted_color
                    prey_distribtion = TYPES_OF_PREY_DIST[o]
                    mouse_pos = (-1,-1)
                elif mouse_pos[1] < 2*height + 6*choice_offset:
                    env_colors = [HUD_background_color,HUD_background_color,HUD_background_color]
                    env_colors[o] = highlighted_color
                    environment_complexity = TYPES_OF_ENV_COMP[o]
                    mouse_pos = (-1,-1)
        if mouse_pos[0] > (game_window_width/3*1)+2*choice_offset and mouse_pos[1] < (game_window_width/3*1)+2*choice_offset + game_window_width/3-3*choice_offset:
            if mouse_pos[1] > 2*height+8*choice_offset:
                if not (prey_distribtion == '' or environment_complexity == ''):
                    start_pressed = True
                    mouse_pos = (-1,-1)
        mouse_pos = (-1,-1)
        #update the screen
        pygame.display.update()
        
    #create the grid now that we have all of our data
    simulation_grid = Grid(pygame, game_window, HUD_scale)
    simulation_grid.simulation_setup(prey_distribtion,environment_complexity)

    
    #start the countdown before the simulation
    while count_down > 0:
        #wait
        pygame.time.delay(delay)
        
        #count the wait time
        time = time + delay
        if time > 1000: #if its 1000ms
            count_down = count_down - 1 #reduce count
            time = 0 #resent counter
        #create the text of count down
        count_down_text = bigfont.render('START IN... ' + str(count_down) + " second(s)", True, color_black)
        #background
        pygame.draw.rect(game_window, simulation_background_color, (0, 0, game_window_width, game_window_height) )
        #put it on the screen
        game_window.blit(count_down_text, (game_window_width/2, game_window_height/2))
        #check for user exit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                selected = True
                count_down = -1
            if event.type == pygame.MOUSEBUTTONDOWN:
                count_down = count_down - 1
        #draw
        pygame.display.update()
    #reset timer
    time = 0
    while run:
                            
        #wait
        pygame.time.delay(delay)
        time = time + delay
        
        #reset elements in HUD
        prey_distribtion_text = font.render('Prey Distribution: ' + prey_distribtion, True, color_black)
        environment_complexity_text = font.render('Enviornment Complexity: ' + environment_complexity, True, color_black)
        HUD = [prey_distribtion_text, environment_complexity_text]        
        prey_count_text = font.render('Prey Alive: ' + str(len(simulation_grid.prey)), True, color_black)
        HUD.append(prey_count_text)
        
        obstacle_count_text = font.render('Obstacle Count: ' + str(len(simulation_grid.obstacles)), True, color_black)
        HUD.append(obstacle_count_text)
        
        #update time if simulation is still running
        if round(time/1000) <= max_time and len(simulation_grid.prey) > 0:
            time_text = font.render('Seconds: ' + str(round(time/1000)), True, color_black)
        else:
            run = False
        #attach time, same if above not ran
        HUD.append(time_text)
        
        #how many are touched
        touched_prey_text = font.render('Prey Killed: ' + str(simulation_grid.dead_prey), True, color_black)
        HUD.append(touched_prey_text)
        
        #check for program exit
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                run = False
                selected = True
        
        #key input
        keys = pygame.key.get_pressed()
        
        if interactive:
            if keys[pygame.K_LEFT] or keys[pygame.K_a]:
                simulation_grid.predator.set_x_direction(Sprite.LEFT)
                #simulation_grid.predator.update()
            if keys[pygame.K_RIGHT] or keys[pygame.K_d]:
                simulation_grid.predator.set_x_direction(Sprite.RIGHT)
                #simulation_grid.predator.update()
            if keys[pygame.K_UP] or keys[pygame.K_w]:
                simulation_grid.predator.set_y_direction(Sprite.UP)
                #simulation_grid.predator.update()
            if keys[pygame.K_DOWN] or keys[pygame.K_s]:
                simulation_grid.predator.set_y_direction(Sprite.DOWN)
                #simulation_grid.predator.update()
            if keys[pygame.K_0]:
                run = False
            if keys[pygame.K_ESCAPE]:
                run = False
                selected = True
        
        #update
        if round(time/1000) < 60 and len(simulation_grid.prey) > 0:
            simulation_grid.update()
        
        #render
        pygame.draw.rect(game_window, simulation_background_color, (0, 0, game_window_width*(1-HUD_scale), game_window_height) )
        pygame.draw.rect(game_window, HUD_background_color, (game_window_width*(1-HUD_scale)+2, 0, game_window_width*HUD_scale+5, game_window_height) )
        
        #write text to the HUD sode
        for i, element in enumerate(HUD):
            game_window.blit(element, (game_window_width*(1-HUD_scale)+5,i*25+5))
        
        #render the grid
        if blind_folded and peek > 0:
            simulation_grid.render(not blind_folded)
            peek = peek - delay
        else:
            simulation_grid.render(blind_folded)
        #draw
        pygame.display.update()
    
    #here is the play again selection/end of simulation menu
    #while not selected, is bypassed if quit was pressed from anywhere above
    while not selected:
        #wait still
        pygame.time.delay(delay)
        
        #see if they pressed the X, or clicked
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                selected = True
            if event.type == pygame.MOUSEBUTTONDOWN:
                mouse_pos = event.pos
        
        #if the width of the buttons
        if mouse_pos[0] > game_window_width*(1-HUD_scale)+20 and mouse_pos[0] < game_window_width*(1-HUD_scale)+20+200:
            
            #if the height of the play again
            if mouse_pos[1] > 300 and mouse_pos[1] < 350:
                selected = True
                play_again = True
                mouse_pos = (-1,-1)
                
            #if the height of exit
            if mouse_pos[1] > 400 and mouse_pos[1] < 450:
                selected = True
                play_again = False
                mouse_pos = (-1,-1)
                
            if mouse_pos[1] > 500 and mouse_pos[1] < 550:
                date_time = datetime.datetime.now()
                date = date_time.strftime("%m/%d/%y")
                time = date_time.strftime("%H:%M:%S %p")
                count = 0
                fn = "Results/predator_prey_simulation_" + prey_distribtion + "_" + environment_complexity + "_data.txt"
                while os.path.exists(fn):
                    count = count + 1
                    buffer = "_" + str(count)
                    fn = "Results/predator_prey_simulation_" + prey_distribtion + "_" + environment_complexity + "_data" + buffer + ".txt"
                with open(fn, "w") as file:
                    file.write("Simulation taken on " + date + " at " + time + "\n")
                    file.write("Simulation Prey Distribution: " + prey_distribtion + "\n")
                    file.write("Simulation Enviroment Complexity: " + environment_complexity + "\n")
                    file.write("Simulation Time Durration: " + str(max_time) + "\n")
                    file.write("Simulation Total Prey in Simulation: " + str(len(simulation_grid.prey)+simulation_grid.dead_prey)  + "\n")
                    file.write("Simulation Total Prey Left Alive: " + str(len(simulation_grid.prey))  + "\n")
                    file.write("Simulation Total Prey Killed: " + str(simulation_grid.dead_prey) + "\n")
                mouse_pos = (-1,-1)
                saved_to = font.render(fn, True, color_black)
        
        pygame.draw.rect(game_window, simulation_background_color, (game_window_width*(1-HUD_scale)+20, 300, 200, 50) )
        pygame.draw.rect(game_window, simulation_background_color, (game_window_width*(1-HUD_scale)+20, 400, 200, 50) )
        pygame.draw.rect(game_window, simulation_background_color, (game_window_width*(1-HUD_scale)+20, 500, 200, 50) )
        
        game_window.blit(game_over, (game_window_width*(1-HUD_scale)+5, 275))
        game_window.blit(play_again_text, (game_window_width*(1-HUD_scale)+25, 310))
        game_window.blit(play_nomore, (game_window_width*(1-HUD_scale)+25, 410))
        game_window.blit(save_to_file, (game_window_width*(1-HUD_scale)+25, 510))
        game_window.blit(saved_to, (game_window_width*(1-HUD_scale)+2, 600))
        
        #show the user what is left, since they cannot move
        simulation_grid.render(not blind_folded)
        
        #update
        pygame.display.update()
    
    #close pygame
    pygame.quit()
    #return data
    return play_again
Ejemplo n.º 10
0
    parser.add_argument('--trafo',
                        default='identity',
                        type=str,
                        choices=list(TRAFOS.keys()))
    parser.add_argument('--scaling',
                        default='standard',
                        type=str,
                        choices=list(SCALING.keys()))
    parser.add_argument('--dim', default=784, type=int)
    parser.add_argument('--seeds', nargs='+', default=range(10), type=int)
    parser.add_argument('--sample_sizes', nargs='+', type=int)
    parser.add_argument('--hyperopt', action="store_true", default=False)
    args = parser.parse_args()

    # TODO: set up folder structure if necessary
    if not os.path.exists('results'):
        os.makedirs('results')
    if not os.path.exists('nn_weights'):
        os.makedirs('nn_weights')

    config = Config(DATA[args.data],
                    SCALING[args.scaling](),
                    TRAFOS[args.trafo](n_components=args.dim),
                    MODELS[args.model],
                    Grid(args.model, args.grid),
                    seeds=args.seeds,
                    sample_sizes=args.sample_sizes,
                    hyperopt=args.hyperopt)

    run(config)