示例#1
0
    def __init__(self, input_service, output_service):
        """The class constructor.

        Args:
            self (Director): an instance of Director.
        """
        self._buffer = Buffer()
        self._input_service = input_service
        self._keep_playing = True
        self._output_service = output_service
        self._score = Score()
        self._word = Word()
示例#2
0
    def __init__(self, input_service, output_service):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._input_service = input_service
        self._output_service = output_service
        self._buffer = Buffer()
        self._word = Word('test')
        self._current_words = []
        self._text_words = []
        self._inputted_letter = ''
        self._score = Score()

        self._keep_playing = True
示例#3
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
    
    Stereotype:
        Controller

    Attributes:
        food (Food): The snake's target.
        input_service (InputService): The input mechanism.
        keep_playing (boolean): Whether or not the game can continue.
        output_service (OutputService): The output mechanism.
        score (Score): The current score.
        snake (Snake): The player or snake.
    """
    def __init__(self, input_service, output_service):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._buffer = Buffer()
        self._input_service = input_service
        self._keep_playing = True
        self._output_service = output_service
        self._score = Score()
        self._words = Words()
        self._slow = 0

    def start_game(self):
        """Starts the game loop to control the sequence of play.
        
        Args:
            self (Director): an instance of Director.
        """
        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()
            sleep(constants.FRAME_LENGTH)

    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. In this case,
        that means getting the desired direction and moving the snake.

        Args:
            self (Director): An instance of Director.
        """
        self._buffer.add_letter(letter)
        letter = self._input_service.get_letter()
        self._words.move_words()

    def _do_updates(self):
        """Updates the important game information for each round of play. In 
        this case, that means checking for a collision and updating the score.

        Args:
            self (Director): An instance of Director.
        """
        self._add_new_words()
        self._handle_matching_words()

    def _do_outputs(self):
        """Outputs the important game information for each round of play. In 
        this case, that means checking if there are stones left and declaring 
        the winner.

        Args:
            self (Director): An instance of Director.
        """
        self._output_service.clear_screen()
        self._output_service.draw_actor(self._buffer)
        self._output_service.draw_actors(self._words.get_all())
        self._output_service.draw_actor(self._score)
        self._output_service.flush_buffer()

    def _handle_body_collision(self):  #Not sure with this reference help
        """Handles collisions between the snake's head and body. Stops the game 
        if there is one.

        Args:
            self (Director): An instance of Director.
        """
        head = self._snake.get_head()
        body = self._snake.get_body()
        for segment in body:
            if head.get_position().equals(segment.get_position()):
                self._keep_playing = False
                break

    def _handle_food_collision(self):  #See comment above.
        """Handles collisions between the snake's head and the food. Grows the 
        snake, updates the score and moves the food if there is one.

        Args:
            self (Director): An instance of Director.
        """
        head = self._snake.get_head()
        if head.get_position().equals(self._food.get_position()):
            points = self._food.get_points()
            for n in range(points):
                self._snake.grow_tail()
            self._score.add_points(points)
            self._food.reset()
示例#4
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.

    Stereotype:
        Controller

    Attributes:
        Buffer (Buffer): The Word's target.
        input_service (InputService): The input mechanism.
        keep_playing (boolean): Whether or not the game can continue.
        output_service (OutputService): The output mechanism.
        score (Score): The current score.
        Word (Word): The player or Word.
    """
    def __init__(self, input_service, output_service):
        """The class constructor.

        Args:
            self (Director): an instance of Director.
        """
        self._buffer = Buffer()
        self._input_service = input_service
        self._keep_playing = True
        self._output_service = output_service
        self._score = Score()
        self._word = Word()

    def start_game(self):
        """Starts the game loop to control the sequence of play.

        Args:
            self (Director): an instance of Director.
        """

        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()
            sleep(constants.FRAME_LENGTH)

    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. In this case,
        that means getting the desired direction and moving the Word.

        Args:
            self (Director): An instance of Director.
        """
        letter = self._input_service.get_letter()
        # VVV Make sure to include that if they hit enter it clears buffer.
        if letter != None:
            self._buffer.add_letter(letter)

    def _do_updates(self):
        """Updates the important game information for each round of play. In 
        this case, that means checking for a collision and updating the score.

        Args:
            self (Director): An instance of Director.
        """
        # self._handle_body_collision()
        self._word.generate_words()
        # self._buffer._buffer_clear_word(self._word.get_all())
        self.check_words(self._buffer.get_buffer(), self._word.get_all())

    def _do_outputs(self):
        """Outputs the important game information for each round of play. In 
        this case, that means checking if there are stones left and declaring 
        the winner.

        Args:
            self (Director): An instance of Director.
        """
        self._output_service.clear_screen()
        self._output_service.draw_actor(self._buffer)
        # self._output_service.draw_actors(self._word.get_all())
        self._output_service.draw_actor(self._score)
        self._output_service.flush_buffer()

    def check_words(self, buffer, words):
        # Remove word from screen if it is in buffer
        for word in words:
            if word in buffer:
                words.remove(word)
示例#5
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
    
    Stereotype:
        Controller


    """
    def __init__(self, input_service, output_service):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self._input_service = input_service
        self._output_service = output_service
        self._buffer = Buffer()
        self._word = Word('test')
        self._current_words = []
        self._text_words = []
        self._inputted_letter = ''
        self._score = Score()

        self._keep_playing = True

    def start_game(self):
        """
            Begins the game cycle
        """
        self.setup_game()
        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()
            sleep(constants.FRAME_LENGTH)

    def _get_random_word(self):
        """
        This function retrieves a random word from the txt file.
        """
        words = open('speed\game\words.txt').read().splitlines()

        return random.choice(words)

    def _make_words(self):
        """
        This function makes word actors with random words pulled from the txt file.
        """

        for i in range(random.randint(1, 2)):
            word = Word(self._get_random_word())
            self._current_words.append(word)
            self._text_words.append(word.get_text())

    def move_word_list(self, actor_list):
        """
        This function takes the list of word actors and moves them all according to their velocity. 
        """
        for actor in actor_list:
            actor.move_next()

    def setup_game(self):
        """
        Resets the buffer so that it displays correctly at the beginning
        """
        self._buffer.reset_buffer()

    def _get_inputs(self):
        """
        Gets the keystrokes of the player
        """

        self._inputted_letter = self._input_service.get_letter()

    def _do_updates(self):
        """
        With the keystrokes, this function writes the letters to the buffer,
        checks if the keystrokes match the moving word actors, and moves the
        actors continuely. 
        """
        if self._inputted_letter:
            if self._inputted_letter == '*':
                self._buffer.reset_buffer()

            else:
                self._buffer.set_letter(self._inputted_letter)

        if self._buffer.get_text_buffer() != '':
            buffer = self._buffer.get_text_buffer()
            if 'bombz' in buffer:
                self._score.set_score(random.randint(1000, 10000))
                self._score.set_text(f'Score : {self._score.get_score()}')
                self._text_words.clear()
                self._current_words.clear()
            self._text_words
            for i in self._text_words:
                if i in buffer:
                    self._score.set_word_points(i)

                    index = self._text_words.index(i)

                    del self._text_words[index]
                    del self._current_words[index]
                    break

        self.move_word_list(self._current_words)

    def _do_outputs(self):
        """
        This function refreshes the asciimatics screen, and adds more words to the screen 
        if there are less than 15 currently being displayed. It also draws all of the game
        components to the screen. 
        """
        self._output_service.clear_screen()

        test = len(self._current_words)
        if len(self._current_words) < 15:

            self._make_words()

        self._output_service.draw_actor(self._score)
        self._output_service.draw_actors(self._current_words)
        self._output_service.draw_actor(self._buffer)

        self._output_service.flush_buffer()
示例#6
0
class Director:
    """A code template for a person who directs the game. The responsibility of 
    this class of objects is to control the sequence of play.
    
    Stereotype:
        Controller

    Attributes:
        food (Food): The snake's target.
        input_service (InputService): The input mechanism.
        keep_playing (boolean): Whether or not the game can continue.
        output_service (OutputService): The output mechanism.
        score (Score): The current score.
        snake (Snake): The player or snake.
    """
    def __init__(self, input_service, output_service):
        """The class constructor.
        
        Args:
            self (Director): an instance of Director.
        """
        self.buffer = Buffer()
        self._input_service = input_service
        self._keep_playing = True
        self._output_service = output_service
        self._score = Score()
        self._words = Word()

    def start_game(self):
        """Starts the game loop to control the sequence of play.
        
        Args:
            self (Director): an instance of Director.
        """
        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()
            sleep(constants.FRAME_LENGTH)

    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. In this case,
        that means getting the desired direction and moving the snake.

        Args:
            self (Director): An instance of Director.
        """
        letter = self._input_service.get_letter()
        if letter == '*':
            self.buffer.empty_word()
        else:
            self.buffer.prep_display(letter)
        self._words.move_words()

    def _do_updates(self):
        """Updates the important game information for each round of play. In 
        this case, that means checking for a collision and updating the score.

        Args:
            self (Director): An instance of Director.
        """
        self._compare_word()

    def _do_outputs(self):
        """Outputs the important game information for each round of play. In 
        this case, that means checking if there are stones left and declaring 
        the winner.

        Args:
            self (Director): An instance of Director.
        """
        self._output_service.clear_screen()
        self._output_service.draw_actors(self._words.get_words())
        self._output_service.draw_actor(self._score)
        self._output_service.draw_actor(self.buffer)
        self._output_service.flush_buffer()

    def _compare_word(self):
        """Compares the word typed in the buffer and words on the screen for a match.

        Args:
            self (Director): An instance of Director.
        """
        list_of_words = self._words.get_words()
        compare = self.buffer.get_typed_word()
        count = -1
        for n in list_of_words:
            count = count + 1
            if compare == n.get_text():
                self._words.remove_word(count)
                self._score.add_points(1)
                self._words.new_word()
示例#7
0
class Director:
    """A code template for a person who directs the game. The responsibility of
    this class of objects is to control the sequence of play.

    Stereotype:
        Controller

    Attributes:
        buffer (Buffer): The area that holds and displays the user input.
        input_service (InputService): The input mechanism.
        keep_playing (boolean): Whether or not the game can continue.
        output_service (OutputService): The output mechanism.
        score (Score): The current score.
        words (Words): The words that move across the screen.
    """
    def __init__(self, input_service, output_service):
        """The class constructor.

        Args:
            self (Director): an instance of Director.
        """
        self._buffer = Buffer()
        self._input_service = input_service
        self._keep_playing = True
        self._output_service = output_service
        self._score = Score()
        self._words = []

    def start_game(self):
        """Starts the game loop to control the sequence of play.

        Args:
            self (Director): an instance of Director.
        """
        self._setup()

        while self._keep_playing:
            self._get_inputs()
            self._do_updates()
            self._do_outputs()
            sleep(constants.FRAME_LENGTH)
            if self._check_offscreen():
                self._setup()

    def _setup(self):

        # TODO create 5 words and add them to the list
        for i in range(5):
            text = "test"
            w = Word(text)
            self._words.append(w)

        # self._words._new_words.clear()
        # self._words._segments.clear()
        # self._words = Words()

    def _check_offscreen(self):
        # TODO go through each word and see if it's off the screen

        # TODO if the word is off the screen, remove from list and create a new one
        # seg = self._words.get_segments()
        # if seg[-1]._position.get_x() == (constants.MAX_X - 1):
        #     return True
        # return False
        pass

    def _check_matches(self):
        # TODO check if the buffer matches any of the words
        for word in self._words:
            if self._buffer.check_match(word):
                # TODO pull out of list and update score
                score = word.get_points()
                # TODO add to words class
        # TODO if so, pull it off and update the score accordingly

        pass

    def _get_inputs(self):
        """Gets the inputs at the beginning of each round of play. In this case,
        that means getting the desired direction and moving the snake.

        Args:
            self (Director): An instance of Director.
        """
        letter = self._input_service.get_letter()
        self._buffer.add_letter(letter)

        # if the letter is not a letter or not pushed, don't add it to buffer
        # direction = Location(1,0)
        #self._words.move_words(direction)
        # for i in range(5):
        #     self._words.move_words(direction, i)

    def _do_updates(self):
        """Updates the important game information for each round of play. In
        this case, that means checking for a collision and updating the score.

        Args:
            self (Director): An instance of Director.
        """
        for word in self._words:
            word.move_next()

        self._check_matches()
        self._check_offscreen()

    def _do_outputs(self):
        """Outputs the important game information for each round of play. In
        this case, that means checking if there are stones left and declaring
        the winner.

        Args:
            self (Director): An instance of Director.
        """
        # TODO draw actor: buffer
        # TODO go through each word and call draw_actor()
        self._output_service.clear_screen()
        self._output_service.draw_actor(self._buffer)
        self._output_service.draw_actor(self._score)
        # self._output_service.draw_actors(self._words.get_segments())

        for word in self._words:
            self._output_service.draw_actor(word)

        self._output_service.flush_buffer()