Ejemplo n.º 1
0
def init_game(config_data):
    """
    Initializes the game with configuration, defined in config_data.

    :param config_data: Dictionary, which contains configuration for the game, such as
                        game window dimensions, number of snakes, keyboard keys, etc.
    :type config_data: dict

    :return: Lists of initialized snakes and cherries.
    :rtype: tuple of list
    """
    # colors for snakes
    snake_colors = [(0, 255, 0),  # player 1 is green
                    (0, 0, 255),  # player 2 is blue
                    (255, 255, 50),  # player 3 is yellow
                    (205, 0, 205)]  # player 4 is purple

    # create snake instances
    init_snake_lst = []
    for i in range(config_data["num_snakes"]):
        keys = config_data["keys"][i]
        snake = Snake(start_pos=config_data["start_pos"][i],
                      move_keys={'up': pygame.__getattribute__(keys[0]),
                                 'right': pygame.__getattribute__(keys[1]),
                                 'down': pygame.__getattribute__(keys[2]),
                                 'left': pygame.__getattribute__(keys[3])},
                      color=snake_colors[i],
                      block_size=config_data["block_size"],
                      num_of_start_blocks=config_data["initial_snake_length"])

        init_snake_lst.append(snake)

    # create cherry instances
    init_cherry_lst = []
    for i in range(config_data["num_cherries"]):
        cherry = Cherry(block_size)
        cherry.set_new_random_position(init_snake_lst, config_data["main_window_size"])
        init_cherry_lst.append(cherry)

    return init_snake_lst, init_cherry_lst
Ejemplo n.º 2
0
    def convert_to_pygame_keys(self):
        """docstring for convert_to_pygame_keys"""
        config = read_config(self.config_file)

        # Keep only configuration keys
        self.keys_ = [kb for kb in config if kb.startswith('K_')]

        self.config_keys = {}
        for key_ in self.keys_:
            self.config_keys.__setitem__(key_, config[key_])

        self.pygame_keys = {"backspace":pygame.K_BACKSPACE,
                            "tab":pygame.K_TAB,
                            "return":pygame.K_RETURN,
                            "^[": pygame.K_ESCAPE, "esc":pygame.K_ESCAPE,
                            "escape":pygame.K_ESCAPE,
                            "colon": pygame.K_COLON,  ":":pygame.K_COLON,
                            "hash":pygame.K_HASH, "#":pygame.K_HASH,
                            "caret": pygame.K_CARET, "^":pygame.K_CARET,
                            "period":pygame.K_PERIOD, ".":pygame.K_PERIOD,
                            "space":pygame.K_SPACE,
                            "!":pygame.K_EXCLAIM,
                            "$":pygame.K_DOLLAR,
                            "&":pygame.K_AMPERSAND,
                            "'":pygame.K_QUOTE,
                            "(":pygame.K_LEFTPAREN,
                            ")":pygame.K_RIGHTPAREN,
                            "*":pygame.K_ASTERISK,
                            "+":pygame.K_PLUS,
                            ",":pygame.K_COMMA,
                            "-":pygame.K_MINUS,
                            "/":pygame.K_SLASH,
                            "\\":pygame.K_BACKSLASH,
                            ";":pygame.K_SEMICOLON, "semicolon":pygame.K_SEMICOLON,
                            "<":pygame.K_LESS,
                            "=":pygame.K_EQUALS,
                            ">":pygame.K_GREATER,
                            "?":pygame.K_QUESTION,
                            "@":pygame.K_AT,
                            "[":pygame.K_LEFTBRACKET,
                            "]":pygame.K_RIGHTBRACKET,
                            "_":pygame.K_UNDERSCORE,
                            "`":pygame.K_BACKQUOTE}

        # Let's be lazy
        alphanum = 'abcdefghijklmnopqrstuvwxyz0123456789'
        alphanum_pygame_values = map(lambda x: pygame.__getattribute__("K_%s" %x), alphanum)
        alphanum_pygame_keys   = dict(zip(alphanum, alphanum_pygame_values))
        self.pygame_keys.update(alphanum_pygame_keys)
Ejemplo n.º 3
0
 def addListenEvent(self, event):
     self.send({ "ADDLISTENEVENT" : pygame.__getattribute__(event),
                 "surface" : self.display},
                 "display_signal")
Ejemplo n.º 4
0
def addListenEvent(self, event):
    self.send({ "ADDLISTENEVENT" : pygame.__getattribute__(event),
                "surface" : self.display},
                "display_signal")
Ejemplo n.º 5
0
first = True
while running:
    while not params_list.button_started_pressed:
        menu1.display()
        menu1.draw_button("button_start")

        pygame.display.update()
        for event in pygame.event.get():
            if click_handler_begin([menu1.button_start],params=params_list, event=event):
                params_list.button_started_pressed = True

            if event.type == pygame.QUIT:
                pygame.quit() # quit the screen
            
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.__getattribute__((menu1.__getattribute__("key_to_start")).strip("\'\'")):

                    params_list.button_started_pressed = True
                    

    # handle every event since the last frame.
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit() # quit the screen
            running = False
        
    
    compteur_shoot += 1
    
    bird.handle_keys() # handle the keys
    bird.handle_shoot()