Пример #1
0
class Model(object):
    """ Class that holds the state of the entire game """
    def __init__(self, cell_size = (40, 40), grid_size = (46, 20)):
        """Initialize the model.

        cell_size: Tuple of the dimension of each cell in pixels
        grid_size: Tuple of the dimensions of the grid in cells"""

        self.cell_size = cell_size
        self.grid_size = grid_size
        self.screen_size = (self.cell_size[0]*self.grid_size[0],
        self.cell_size[1]*self.grid_size[1]+160)
        self.endgame = False
        self.obstacles = []
        self.cleared_obstacles = []

        self.make_grid()
        self.choose_flag()
        self.make_colors()
        self.make_player()
        self.generate_level()
        self.make_darkness()
        self.make_banner()

    def make_grid(self):
        """Instantiate grid cells for game map.

        Creates a dictionary with keys that are cell
        coordinates with values that are cell objects"""
        self.grid_cells = {}
        cell_size = (self.cell_size) # cell size in pixels
        for i in range(self.grid_size[0]): # for the x cells
            for j in range(self.grid_size[1]): # for the y cells
                cell_coord = (i*self.cell_size[0], j*self.cell_size[1]+160)
                self.grid_cells[(i,j)] = Cell(cell_coord, (i, j))

    def choose_flag(self):
        """Randomly choose which flag to play the game with."""
        self.flag = Flag({
            1:"ace",
            2:"alt-lesbian",
            3:"bi",
            4:"intersex",
            5:"l-lesbian",
            6:"nb",
            7:"pan",
            8:"poc",
            9:"pride",
            10:"trans",
            11:"gqueer"
            }[random.randint(1, 10)])

    def make_colors(self):
        """Instantiate Color objects for each color in the chosen flag and place in random cells """
        self.color_objs = []

        for color in self.flag.colors:
            x_cell = random.randint(0, self.grid_size[0]-1)
            y_cell = random.randint(0, self.grid_size[1]-1)

            cell_coord = self.grid_cells[(x_cell,y_cell)].cell_coord
            grid_coord = self.grid_cells[(x_cell,y_cell)].label

            self.color_objs.append(actors.Color(color, self.cell_size, cell_coord, grid_coord, self.grid_cells[grid_coord]))
            self.grid_cells[(x_cell,y_cell)].occupied = True
            self.grid_cells[(x_cell,y_cell)].type = 'color'

    def generate_level(self):
        """ Generates playable level """

        #gets position of player for play path
        player_pos = self.player.get_draw_position()
        player_grid_pos = (math.floor((player_pos[0]-40)/self.cell_size[0]), math.floor((player_pos[1] - 160)/self.cell_size[1]))
        player_cell = self.grid_cells[(player_grid_pos)]

        #initializes order of goals for random playable path
        path_order_colors = self.color_objs
        random.shuffle(path_order_colors)
        path_order = [color.cell_in for color in path_order_colors]
        path_order.insert(0,player_cell)

        #generates path, places obstacles accordingly
        self.path = []
        ind = 0
        for i in list(range(len(path_order)-1)):
        #for i in range(2):
            zigzag_path = get_zigzag_path(self.grid_cells, path_order[ind], path_order[ind+1], 3)
            self.place_obstacles(zigzag_path, ind)
            if not ind == 0:
                for cell in zigzag_path:
                    cell.type == 'none'

            self.path.extend(zigzag_path)
            ind += 1

    def place_obstacles(self, path, ind):
        """ Generate obstacles in the grid
        path = list of Cell objects that comprise a path through the grid
        ind = number path that is currently being generated """

        obstacle_types = self.flag.colors[:ind+1]
        for i in range(round(350/(len(self.flag.colors)))): # always place 350 obstacles
            x_cell = random.randint(0, self.grid_size[0]-1)
            y_cell = random.randint(0, self.grid_size[1]-1)
            while self.grid_cells[(x_cell, y_cell)].occupied: # re-randomize cells if chosen ones are occupied
                x_cell = random.randint(0, self.grid_size[0]-1)
                y_cell = random.randint(0, self.grid_size[1]-1)

            coord = self.grid_cells[(x_cell,y_cell)].cell_coord
            type = random.choice(obstacle_types)            # randomly chooses this obstacle's type
            obstacle = actors.Obstacle((self.cell_size),coord,type)

            obstacle.make_groups(obstacle, self.obstacles)    # add obstacle to group based on what the obstacle's type is

            self.grid_cells[(x_cell,y_cell)].occupied = True
            self.grid_cells[(x_cell,y_cell)].type = 'obstacle'

    def erase_obstacles(self, key = pygame.K_SPACE):
        """ Removes obstacles from self.model.obstacles while spacebar is held """
        if pygame.key.get_pressed()[key] == 1:
            for color in self.player.collided_with:   # iterates through list of colors that have been collided with
                for group in self.obstacles:        # iterates through all groups of obstacles
                    if group.type == color.color:      # finds group that corresponds to color that was just touched
                        self.obstacles.remove(group)
                        self.cleared_obstacles.append(group)
        else:
            for group in self.cleared_obstacles:
                self.cleared_obstacles.remove(group)
                self.obstacles.append(group)

    def make_player(self):
        """ Instantiate Player object """
        self.player = actors.Player((400, 400), self.screen_size,
        self.obstacles, self.color_objs)

        for i in range(14, 16):     # range of x_coord cells where the player spawns
            for j in range(10,12):  # range of y_coord cells where player spawns
                self.grid_cells[(i,j)].occupied = True      # marks cells as occupied so that nothing else spawns there

    def make_darkness(self):
        """ Instantiate Darkness object """
        self.darkness = actors.Darkness(self.player, (self.cell_size[0]*self.grid_size[0], self.cell_size[1]*self.grid_size[0]))

    def make_banner(self):
        """ Instatiate Banner object """
        colors = self.flag.colors
        screen_size = self.screen_size

        self.banner = Banner(self.flag.name, screen_size)
        self.banner.scale_text(40)      # we want the text to be 40 pixels tall
        self.banner.scale_logo(100)     # and we want the logo to be 100 pixels tall

        self.banner.center_text()

    def make_endscreen(self):
        """ Instantiate Endscreen object """
        self.endscreen = EndScreen(self.flag.name, (1920, 1080))