def execute(self, cast): """Executes the action using the given actors. Args: cast (dict): The game actors {key: tag, value: list}. """ ball = cast["ball"][0] # there's only one paddle = cast["paddle"][0] # there's only one bricks = cast["brick"] for brick in bricks: if ball.get_position().equals(brick.get_position()): bricks.remove(brick) ball.set_velocity(Point.reverse_y(ball.get_velocity())) if ball.get_position().get_y() == paddle.get_position().get_y(): if ball.get_position().get_x() >= paddle.get_position().get_x( ) and ball.get_position().get_x() <= ( paddle.get_position().get_x() + 11): ball.set_velocity(Point.reverse_y(ball.get_velocity())) if ball.get_position().get_y() == 0: ball.set_velocity(Point.reverse_y(ball.get_velocity())) if ball.get_position().get_x() == 0 or ball.get_position().get_x( ) == constants.MAX_X: ball.set_velocity(Point.reverse_x(ball.get_velocity())) if ball.get_position().get_y() == constants.MAX_Y: quit()
def execute(self, cast): """Executes the action using given actors. Args: cast(dict): The game actors {key:tag,value:list}""" player1 = cast["cycles"][0][0] velocity = player1.get_velocity() direction = self._input_service.get_direction().scale( constants.CYCLE_SPEED) if self._input_service._keys == []: # if direction == Point(0,0): direction = velocity player1.set_velocity(direction) player2 = cast["cycles"][1][0] change = random.randint(0, 20) if change == 0: newdir = Point(constants.CYCLE_SPEED, 0) elif change == 1: newdir = Point(0, constants.CYCLE_SPEED) elif change == 2: newdir = Point(-constants.CYCLE_SPEED, 0) elif change == 3: newdir = Point(0, -constants.CYCLE_SPEED) else: newdir = player2.get_velocity() player2.set_velocity(newdir)
def __init__(self): """The class constructor.""" self.brick = Brick self._description = "" self._text = "" self._position = Point(0, 0) self._velocity = Point(0, 0)
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()
def __init__(self, screen): """The class constructor.""" self._screen = screen self._keys = {} self._keys[97] = Point(-2, 0) # a self._keys[0x01] = Point(-2, 0) # <- self._keys[100] = Point(2, 0) # d self._keys[0x02] = Point(2, 0) # ->
def __init__(self, screen): """The class constructor.""" self._screen = screen self._keys = {} # self._keys[119] = Point(0, -1) # w # self._keys[115] = Point(0, 1) # s self._keys[97] = Point(-1, 0) # a self._keys[100] = Point(1, 0) # d
def _prepare_trail(self): x = int(constants.MAX_X / 2) y = int(constants.MAX_Y / 2) for n in range(20): image = constants.CYCLE_IMAGE if n == 0 else constants.TRAIL_IMAGE position = Point(x - n, y) velocity = Point(1, 0) self._add_trail(image, position, velocity)
def __init__(self, screen): """The class constructor.""" self._screen = screen self._keys = {} self._keys[119] = Point(0, 0) # w self._keys[115] = Point(0, 0) # s self._keys[97] = Point(-5, 0) # a self._keys[100] = Point(5, 0) # d
def main(): cast = {} cast["cycles"] = [] cycle_x = 0 for _ in range(constants.NUM_CYCLES): trail = [] cycle_x += round(constants.MAX_X / (constants.NUM_CYCLES + 1)) position = Point(cycle_x, constants.CYCLE_Y) velocity = Point(0, constants.CYCLE_SPEED) cycle = Cycle(position, velocity, constants.CYCLE_IMAGE) trail.append(cycle) for j in range(1, constants.TRAIL_LENGTH + 1): if j == 1: k = constants.CYCLE_IMG_SCALE else: k = constants.TRAIL_IMG_SCALE position = Point(cycle_x, constants.CYCLE_Y - (j * k)) velocity = cycle.get_velocity() segment = Segment(position, velocity, constants.TRAIL_IMAGE) trail.append(segment) cast["cycles"].append(trail) obstacles = [] while len(obstacles) <= constants.NUM_OBST: x = random.randint(0 + constants.OBST_WIDTH, constants.MAX_X - constants.OBST_WIDTH) y = random.randint(0 + constants.OBST_HEIGHT, constants.MAX_Y - constants.OBST_HEIGHT) obstacle = Obstacle(x, y) for trail in cast["cycles"]: cycle = trail[0] if not obstacle.collides_with_sprite(cycle): if obstacles: for other in obstacles: if not obstacle.collides_with_sprite(other): obstacles.append(obstacle) else: obstacles.append(obstacle) cast["obstacles"] = obstacles script = {} input_service = InputService() output_service = OutputService() control_actors_action = ControlActorsAction(input_service) move_actors_action = MoveActorsAction() handles_collisions_action = HandleCollisionsAction() draw_actors_action = DrawActorsAction(output_service) script["input"] = [control_actors_action] script["update"] = [move_actors_action, handles_collisions_action] script["output"] = [draw_actors_action] director = Director(cast, script, input_service) director.setup() arcade.run()
def new_word(self): """ This will add a new word to the list of words """ word = (random.choice(constants.LIBRARY)) position = Point(random.randint(0, constants.MAX_X), random.randint(0, constants.MAX_Y / 2)) velocity = Point(0, 1) self.add_word(word, position, velocity)
def __init__(self): """The class constructor. Args: self (Actor): an instance of Actor. """ self._text = "" self._position = Point(0, 0) self._velocity = Point(0, 0)
def always_five_words(self): list_words = self.get_all_words() if len(self._word_list) < 5: rand = random.choice(list_words) self.all_used.append(rand) text = rand position = Point(30, 20) velocity = Point(random.randint(0, 1), random.randint(0, 1)) self.add_words(text, position, velocity)
def main(): cast = {} cast["cycles"] = [] cast["obstacles"] = arcade.SpriteList() cycle_x = 0 for i in range(constants.NUM_CYCLES): trail = [] cycle_x += round(constants.MAX_X/(constants.NUM_CYCLES + 1)) position = Point(cycle_x, constants.CYCLE_Y) velocity = Point(0,constants.CYCLE_SPEED) if i == 0: img = constants.CYCLE_IMAGE trail_img = constants.CYCLE_TRAIL bck = constants.CYCLE_DOWN lft = constants.CYCLE_LEFT rgt = constants.CYCLE_RIGHT opponent = "Blue Cycle" else: img = constants.AI_IMAGE trail_img = constants.AI_TRAIL bck = constants.AI_DOWN lft = constants.AI_LEFT rgt = constants.AI_RIGHT opponent = "Yellow Cycle" cycle = Cycle(position,velocity,img,opponent) cycle.set_trail(trail_img) cycle.store_img(bck,'bck') cycle.store_img(lft,'lft') cycle.store_img(rgt,'rgt') trail.append(cycle) light_trail = arcade.SpriteList() trail.append(light_trail) cast["cycles"].append(trail) cast["obstacles"].append(cycle) script = {} input_service = InputService() output_service = OutputService() control_actors_action = ControlActorsAction(input_service) move_actors_action = MoveActorsAction() handles_collisions_action = HandleCollisionsAction() draw_actors_action = DrawActorsAction(output_service) script["input"] = [control_actors_action] script["update"] = [move_actors_action, handles_collisions_action] script["output"] = [draw_actors_action] window = arcade.Window(constants.MAX_X, constants.MAX_Y, "TRON") start_view = Instructions(cast,script,input_service) window.show_view(start_view) arcade.run()
def prepare_words(self): """ This method will create the original starting words for the game """ for x in range(constants.STARTING_WORDS): word = (random.choice(constants.LIBRARY)) position = Point(random.randint(0, constants.MAX_X), random.randint(0, constants.MAX_Y / 2)) velocity = Point(0, 1) self.add_word(word, position, velocity)
def test_validate_point(self): sample_row = 2 sample_columns = 4 sample_alive_points = [] obj = Board(sample_row, sample_columns, sample_alive_points) self.assertTrue(obj.validate_point(Point(1, 3))) self.assertFalse(obj.validate_point(Point(1, 5)))
def test_get_count_of_alive_neighbours_of_cell(self): sample_row = 2 sample_columns = 4 sample_alive_points = [Point(0, 0), Point(1, 1)] obj = Board(sample_row, sample_columns, sample_alive_points) test_point = Point(0, 0) expected_alive_neighbours = 1 self.assertEqual(obj.get_count_of_alive_neighbours_of_cell(test_point), expected_alive_neighbours)
def get_direction(self): """Gets the selected direction for the given player. Returns: Point: The selected direction. """ direction = Point(0, 0) event = self._screen.get_event() if isinstance(event, KeyboardEvent): if event.key_code == 27: sys.exit() direction = self._keys.get(event.key_code, Point(0, 0)) return direction
def _prepare_body(self): """Prepares the snake body by adding segments. Args: self (Snake): an instance of Snake. """ x = 0 y = 2 for n in range(constants.STARTING_WORDS): position = Point(x, y + 2 * n) velocity = Point(1, 0) self._add_segment(position, velocity)
def test_initial_state_of_board(self): sample_row = 3 sample_columns = 3 sample_points = [Point(0, 0), Point(0, 2)] obj = Board(sample_row, sample_columns, sample_points) for i in range(len(sample_points)): actual_status = obj.board[sample_points[i].X()][ sample_points[i].Y()].get_state() expected_status = Status.ALIVE self.assertEqual(expected_status, actual_status)
def execute(self, cast): """Executes the action using the given actors. Args: cast (dict): The game actors {key: tag, value: list}. """ direction = self._input_service.get_direction() paddle = cast["paddle"][0] # there's only one in the cast paddle.set_velocity(direction) if paddle.get_position().get_x() < 0: paddle.set_position(Point(0, 19)) if paddle.get_position().get_x() > constants.MAX_X - 11: paddle.set_position(Point(constants.MAX_X - 11, 19))
def _prepare(self): """Prepares the Words. Args: self (Words): an instance of Words. """ for n in range(constants.SNAKE_LENGTH): x = random.radint(1, constants.MAX_X - 10) y = random.randint(1, constants.MAX_Y - 10) text = self._get_word() position = Point(x, y) velocity = Point(0, constants,SPEED) self._add_word(text, position, velocity)
def __init__(self, screen): """The class constructor. Args: self (InputService): An instance of InputService. """ self._screen = screen self._directions = {} self._directions[119] = Point(0, -1) # UP self._directions[97] = Point(-1, 0) # DOWN self._directions[100] = Point(1, 0) # RIGHT self._directions[115] = Point(0, 1) # LEFT self._current = self._directions[100]
def _prepare_word(self): """Prepares the word body by inputting its position Args: self (word): an instance of Word. """ self._generate_words_in_list() x = int(constants.MAX_X / 2) y = int(constants.MAX_Y / 2) for n in range(len(self._words_displayed)): for letter in self._words_displayed[n]: text = letter position = Point(x - n, y) velocity = Point(1, 0) self._add_segment(text, position, velocity)
def get_direction(self): """Gets direction for given actor. Returns: direction(Point): the selected direction.""" x = 0 y = 0 if arcade.key.LEFT in self._keys: x = -1 # direction = Point(0, 1) # return direction elif arcade.key.RIGHT in self._keys: x = 1 # direction = Point(1, 0) # return direction if arcade.key.UP in self._keys: y = 1 # direction = Point(0, -1) # return direction elif arcade.key.DOWN in self._keys: y = -1 # direction = Point(-1, 0) # return direction """Implement logic so cycle doesn't run over self w direction switch""" direction = Point(x, y) return direction
def get_direction(self): """Gets direction for given actor. Returns: direction(Point): the selected direction.""" x = 0 y = 0 if arcade.key.LEFT in self._keys: x = -1 # direction = Point(0, 1) # return direction elif arcade.key.RIGHT in self._keys: x = 1 # direction = Point(1, 0) # return direction if arcade.key.UP in self._keys: y = 1 # direction = Point(0, -1) # return direction elif arcade.key.DOWN in self._keys: y = -1 # direction = Point(-1, 0) # return direction direction = Point(x, y) return direction
def execute(self, cast): """Executes the action using the given actors. Args: cast (dict): The game actors {key: tag, value: list}. """ self.paddle = cast["paddle"][0] # there's only one self.ball = cast["ball"][0] # there's only one self.bricks = cast["brick"] self.value = cast["score"][0] position = self.ball.get_position() x = position.get_x() y = position.get_y() if x == 1 or x == constants.MAX_X - 1: self._handle_wall() for symbol in range(len(self.paddle.get_text())): paddle_position = self.paddle.get_position() paddle_position = paddle_position.add(Point(symbol, -1)) if self.ball.get_position().equals(paddle_position): self._handle_paddle() elif y == constants.MAX_Y - 1: self._handle_floor() if y == 1: self._handle_ceiling() for symbol in range(len(self.bricks) - 1): brick = self.bricks[symbol] if self.ball.get_position().equals(brick.get_position()): self._handle_brick(symbol)
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 get_five_words(self): list_words = self.get_all_words() for i in range(5): # Create a random number for x and y random_x = random.randint(1, 50) random_y = random.randint(1, 20) x = int(constants.MAX_X - random_x) y = int(constants.MAX_Y - random_y) rand = random.choice(list_words) self.all_used.append(rand) text = rand position = Point(x, y) velocity = Point(random.randint(-1, 1), random.randint(-1, 1)) self.add_words(text, position, velocity)
def _prepare_trail(self): """Prepares the trail to follow behind the cycle. Args: self(Cycle): an instance of Cycle.""" x = self.center_x y = self.center_y velocity = Point(0, 0) position = Point(x, y) self._add_trail_bit(position, velocity, constants.CYCLE_IMAGE) for k in range(1, constants.TRAIL_LENGTH + 1): if k == 1: n = constants.CYCLE_IMG_SCALE else: n = constants.TRAIL_IMG_SCALE position = Point(x, y - (k * n)) self._add_trail_bit(position, velocity, constants.TRAIL_IMAGE)
def reset(self): """Resets the word by moving it to a random position within the boundaries of the screen and reassigning the points to a random number. Args: self (word): an instance of word. """ position = Point(0, 20) self.set_position(position)