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()
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: 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. userinput (UserInput): The users input. timer (Timer): The remaining time left. """ def __init__(self, input_service, output_service): """The class constructor. Args: self (Director): an instance of Director. """ self.words = [] for x in range(5): #Generates 5 word actors newWord = Word() self.words.append(newWord) self._input_service = input_service self._keep_playing = True self._output_service = output_service self._score = Score() self._userinput = UserInput() self._timer = Timer() 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 checking the letters the user types. Args: self (Director): An instance of Director. """ self.input_letter = self._input_service.get_letter() def _do_updates(self): """Updates the important game information for each round of play. In this case, that means checking the words for correctness and subtracting time. Args: self (Director): An instance of Director. """ self._move_words() self._handle_letter_input() self._check_words() self._timer.subtract_time() self._timer.check_time() def _do_outputs(self): """Outputs the important game information for each round of play. In this case, that means drawing everything to the screen. Args: self (Director): An instance of Director. """ self._output_service.clear_screen() self._output_service.draw_actors(self.words) self._output_service.draw_actor(self._timer) self._output_service.draw_actor(self._userinput) self._output_service.draw_actor(self._score) self._output_service.flush_buffer() def _check_words(self): """Handles the checking of words as they are typed. Args: self (Director): An instance of Director. """ for x in range(5): if self._userinput.check_word == self.words[x]._word: self._score.add_points(self.words[x]._points) self.words[x].reset() self._userinput._clear_letters() def _move_words(self): """Handles moving the words around the screen. Args: self (Director): An instance of Director. """ for x in range(len(self.words)): self.words[x].move_next() def _handle_letter_input(self): """Handles adding letters to an array as they are typed. Args: self (Director): An instance of Director. """ if self.input_letter == "": pass elif self.input_letter == "*": self._userinput._clear_letters() else: self._userinput._add_letter(self.input_letter)
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()
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): """Class constructor Args: self (Director): An instance of director """ self._input_service = input_service self._keep_playing = True self._output_service = output_service self._score = Score() self._word = WordActor() self.user_word = Actor() self.user_word._position._y = constants.MAX_Y + 1 def start_game(self): """Controls the loop that executes each step of the game. Args: Self (Director): An instance of Director """ while self._keep_playing == True: self._get_inputs() self._do_updates() self._do_outputs() sleep(constants.FRAME_LENGTH) def _get_inputs(self): """This method excutes the part of the program responsible for gathering the users input. In this game it would be the letters or words being typed by the user. Args: Self (Director): An instance of Director """ user_letter = self._input_service.get_letter() if user_letter == "*": self.check_win() else: self.user_word.set_text(self.user_word._text + user_letter) def _do_updates(self): """Manages the game events that must be executed. In this case it would be managing when a word hits the wall, how many losses the user has and can have, and if they typed a correct word. Args: Self (Director): An instance of Director """ self.check_wall() # self.track_loss() def _do_outputs(self): """Outputs the important game information for each round of play. In this case, that means refreshing the screen, and drawing the necessary words, score, and end game messages. Args: self (Director): An instance of Director. """ self._output_service.clear_screen() self._word.move() self._output_service.clear_screen() # TODO: AS ACTOR AND WORD ARE FINISHED I WILL UPDATE THIS CODE # TO CORRECTLY PASS THE WORDS AND SCORE TO OUTPUT_SERVICE self._output_service.draw_actors(self._word) self._output_service.draw_actor(self._score) self._output_service.draw_actor(self.user_word) self._output_service.flush_buffer() def check_wall(self): """This method will execute the necessary code to check and track if a word has hit the right wall. Args: Self (Director): An instance of Director """ self.hit_wall = 0 for n in range(len(self._word._segments)): if len(self._word._segments[n]._text) + self._word._segments[ n]._position.get_x() > constants.MAX_X: self.hit_wall += 1 self._word.reset(self._word._segments[n]) ##TODO: Change to represent accurate indexes def track_loss(self): """This method will run the necessary code when the end of the game has been reached. Args: Self (director): An instance of Director """ if self.hit_wall >= 5: self._keep_playing = False def check_win(self): """This method will execute the portion of code that will compare the users word to all current words and update the score accordingly. Args: self (Director): An instance of Director word: the word entered by the user. """ self._score.add_points(self._word.compare_words(self.user_word._text)) self.user_word._text = "" self._output_service.flush_buffer() self._output_service.clear_screen()
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: words (words): 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._word = Word() self._input_service = input_service self._keep_playing = True self._output_service = output_service self._score = Score() self._speed = Speed() self._guess = Guess() self._actor = Actor() self.correct = 0 def start_game(self): """Starts the game loop to control the sequence of play. Args: self (Director): an instance of Director. """ self._speed.get_five_words() 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. """ #Get input keyboard guess = self._input_service.get_letter() letters = "" letters += guess x = self._guess.get_guess(letters) # It gives direction to the words direction = self._input_service.get_direction() self._speed.move_words(direction) 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_words() self._speed.always_five_words() self._guess.reset() #Make sure there are 5 words displaying size = self._speed.get_size() if size != 5: self._speed.get_five_words(self) 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._word) self._output_service.draw_actors(self._speed.get_all()) self._output_service.draw_actor(self._score) self._output_service.draw_actor(self._guess) self._output_service.flush_buffer() def compare_words(self): # get the input guess word words_list = self._speed.get_all() guess = self._input_service.get_letter() letters = "" letters += guess guess = self._guess.get_guess(letters) # Compare the guess with our list of words displaying for i in words_list: if guess == i.get_text(): self._speed.remove_words(guess) self._score.add_points(1)