def start_game(self): """Starts the game loop to control the sequence of play.""" while self.keep_playing == True: self._cue_action("input") self._cue_action("update") self._cue_action("output") self.keep_playing = self._script["update"][1].checkGameOver() sleep(constants.FRAME_LENGTH) self.gameWon = self._script["update"][1].getWinCondition( ) #Checks if all bricks are destroyed if self.gameWon == True: self.endingText = "You won!" #If all bricks gone, game is won. else: self.endingText = "Game Over! Try Again" #if not, game over. self._cast = {} #deletes all displayed objects x = int((constants.MAX_X / 2) - 5) y = int((constants.MAX_Y / 2) - 1) position = Point(x, y) endingScreen = Actor() #creates new object to display - Ending Screen endingScreen.set_text(self.endingText) endingScreen.set_position(position) self._cast["endingScreen"] = [endingScreen] self._cue_action("output") #displays the text object sleep(10) #Delays game end so text can be read.
def add_word(self, word, position, velocity): """ This method will add a new word to the array storing all the words """ words = Actor() words.set_text(word) words.set_position(position) words.set_velocity(velocity) self._list_of_words.append(words)
def add_words(self, text, position, velocity): """Adds a new segment to the speed using the given text, position and velocity. Args: self (speed): An instance of speed. text (string): The segment's text. position (Point): The segment's position. velocity (Point): The segment's velocity. """ segment = Actor() segment.set_text(str(text)) segment.set_position(position) segment.set_velocity(velocity) self._word_list.append(segment)
def _add_segment(self, position, velocity): """Adds a new segment to the snake using the given text, position and velocity. Args: self (Snake): An instance of snake. text (string): The segment's text. position (Point): The segment's position. velocity (Point): The segment's velocity. """ segment = Actor() segment.set_text(self._choose_word()) segment.set_position(position) segment.set_velocity(velocity) self._segments.append(segment)
def main(screen): # create the cast {key: tag, value: list} cast = {} # create paddle x = int(constants.MAX_X / 2) y = int(constants.MAX_Y - 2) position = Point(x, y) paddle = Actor() paddle.set_text("===========") paddle.set_position(position) cast["paddle"] = [paddle] # create bricks cast["brick"] = [] for x in range(5, 75): for y in range(2, 6): position = Point(x, y) brick = Actor() brick.set_text("*") brick.set_position(position) cast["brick"].append(brick) #create ball x = int(constants.MAX_X / 2) y = int(constants.MAX_Y / 2) position = Point(x, y) velocity = Point(1, -1) ball = Actor() ball.set_text("@") ball.set_position(position) ball.set_velocity(velocity) cast["ball"] = [ball] position = Point(0, 0) score = Actor() score.set_text(f"Score: ") score.set_position(position) score.set_velocity(position) cast["score"] = [score] # create the script {key: tag, value: list} script = {} input_service = InputService(screen) output_service = OutputService(screen) control_actors_action = ControlActorsAction(input_service) move_actors_action = MoveActorsAction() handle_collisions_acition = HandleCollisionsAction() draw_actors_action = DrawActorsAction(output_service) script["input"] = [control_actors_action] script["update"] = [move_actors_action, handle_collisions_acition] script["output"] = [draw_actors_action] # start the game director = Director(cast, script) director.start_game()
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()
def _add_segment(self, text, position, velocity): segment = Actor() segment.set_text(text) segment.set_position(position) segment.set_velocity(velocity) self.word.append(segment)