def test_eat_prize(self): root = Tk() board = self.create_test_board(5, 5, Point(1, 1), [Prize(2, 1)]) board.pacman.set_new_direction(Point(1, 0)) board.update_board() board.update_board() self.assertEqual(board.field[2][1], None) self.assertEqual(board.pacman.score, 200)
def respawn(self): # запускать в отдельном потоке self.direction = Point(0, 0) self.location = Point(7, 7) time.sleep(7) self.direction = Point(0, -1) self.speed = self.speed / 2 time.sleep(1) self.speed = self.speed * 2
def test_long_movement(self): board = self.create_test_board(5, 5, Point(1, 1)) board.pacman.set_new_direction(Point(1, 0)) board.update_board() self.assertEqual(board.pacman.location, Point(2, 1)) board.update_board() self.assertEqual(board.pacman.location, Point(3, 1)) board.update_board() self.assertEqual(board.pacman.location, Point(4, 1))
def move(self, speed, direction): new_location = Point(self.location.x + direction.x * speed, self.location.y + direction.y * speed) if self._movement_marker and self.location.distance(self._movement_marker[0]) +\ self._movement_marker[0].distance(new_location) == self.location.distance(new_location): new_location = self._movement_marker[0] + Point( self._movement_marker[1].x * speed, self._movement_marker[1].y * speed) self.direction = self._movement_marker[1] self._movement_marker = None self.location = make_location_in_borders(new_location)
def __init__(self, x, y, board, speed, sprite="./sprites/ghost_red.png"): self.sprite = ImageTk.PhotoImage(file=sprite) self.scared_sprite = ImageTk.PhotoImage( file="./sprites/ghost_scared.png") self.location = Point(x, y) self.speed = speed self.board = board self.direction = Point(0, 0) self._movement_marker = None self.last_teleported = 0 self.destination = Point(14, 1)
def test_dont_eat_food(self): root = Tk() food = Food(2, 1) p_food = PowerFood(3, 1) board = self.create_test_board(5, 5, Point(1, 1), [food, p_food]) board.pacman = Pacman(-1, -1, board, 1) board.ghost.move(1, Point(1, 0)) board.ghost.move(1, Point(1, 0)) board.update_board() self.assertEqual(board.field[2][1], food) board.update_board() self.assertEqual(board.field[3][1], p_food)
def test_eat_food(self): root = Tk() board = self.create_test_board(5, 5, Point( 1, 1), [Food(2, 1), PowerFood(3, 1)]) board.pacman.set_new_direction(Point(1, 0)) board.update_board() board.update_board() self.assertEqual(board.field[2][1], None) self.assertEqual(board.pacman.score, 10) board.update_board() self.assertEqual(board.field[3][1], None) self.assertEqual(board.pacman.score, 110) self.assertEqual(board.pacman.super_power, 1)
def test_movement(self): board = self.create_test_board(5, 5, Point(3, 3)) board.ghost.move(1, Point(1, 0)) self.assertEqual(board.ghost.location, Point(4, 3)) board.ghost.move(1, Point(0, 1)) self.assertEqual(board.ghost.location, Point(4, 4)) board.ghost.move(1, Point(-1, 0)) self.assertEqual(board.ghost.location, Point(3, 4)) board.ghost.move(1, Point(0, -1)) self.assertEqual(board.ghost.location, Point(3, 3))
def _configure_sprites(self): self.sprites = {Point(1, 0): [ImageTk.PhotoImage(file="./sprites/pacman_1.png"), ImageTk.PhotoImage(file="./sprites/pacman_2.png"), ImageTk.PhotoImage(file="./sprites/pacman_3.png")], Point(0, 1): [ImageTk.PhotoImage(Image.open("./sprites/pacman_1.png").rotate(270)), ImageTk.PhotoImage(Image.open("./sprites/pacman_2.png").rotate(270)), ImageTk.PhotoImage(Image.open("./sprites/pacman_3.png").rotate(270))], Point(-1, 0): [ImageTk.PhotoImage(Image.open("./sprites/pacman_1.png").rotate(180)), ImageTk.PhotoImage(Image.open("./sprites/pacman_2.png").rotate(180)), ImageTk.PhotoImage(Image.open("./sprites/pacman_3.png").rotate(180))], Point(0, -1): [ImageTk.PhotoImage(Image.open("./sprites/pacman_1.png").rotate(90)), ImageTk.PhotoImage(Image.open("./sprites/pacman_2.png").rotate(90)), ImageTk.PhotoImage(Image.open("./sprites/pacman_3.png").rotate(90))] }
def decide_direction(self): if self.direction.is_opposite(self.saved_direction): self.saved_direction = Point(0, 0) return self.direction if self.location != round(self.location): return self.last_direction new_location = round(make_location_in_borders(self.location + self.direction)) if not isinstance(self.board.field[new_location.x][new_location.y], Wall): self.saved_direction = Point(0, 0) return self.direction new_location = round(make_location_in_borders(self.location + self.saved_direction)) if not isinstance(self.board.field[new_location.x][new_location.y], Wall): return self.saved_direction self.saved_direction = Point(0, 0) return self.saved_direction
def __init__(self, x, y, board, speed): self.respawn_location = Point(x, y) self.location = self.respawn_location self.saved_direction = Point(0, 0) self.last_direction = Point(1, 0) self.direction = Point(0, 0) self._directions = [] self.sprites = {} self.speed = speed self.lives = 3 self.board = board self.score = 0 self.super_power = 0 self.last_teleported = 0 self._configure_sprites()
def _get_prefered_directions(self): if self.board.game_state == 'frightened': directions = [Point(1, 0), Point(-1, 0), Point(0, 1), Point(0, -1)] random.shuffle(directions) return directions else: self._update_destination() directions = [Point(1, 0), Point(0, 1), Point(-1, 0), Point(0, -1)] course = self.location - self.destination directions.sort(key=lambda x: (x + course).length()) return directions
def action_when_collided_with(self, obj): if isinstance(obj, Food): winsound.PlaySound("./audio/pacman_chomp.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.score += 10 self.board.food_left -= 1 elif isinstance(obj, PowerFood): winsound.PlaySound("./audio/pacman_eatfruit.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.score += 100 self.super_power += 1 Timer(5.0, self._reset_super_power).start() elif isinstance(obj, game_logic.ghost.Ghost): if not self.super_power: winsound.PlaySound("./audio/pacman_death.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.location = Point(-1, -1) self.lives -= 1 self.saved_direction = Point(0, 0) self.direction = Point(0, 0) Timer(1.0, self.respawn).start() elif isinstance(obj, Prize): winsound.PlaySound("./audio/pacman_eatfruit.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.score += 200
def test_walls_stop_movement(self): board = self.create_test_board(5, 5, Point(1, 1), [Wall(3, 1), Wall(2, 4)]) board.pacman.set_new_direction(Point(1, 0)) board.update_board() self.assertEqual(board.pacman.location, Point(2, 1)) board.update_board() self.assertEqual(board.pacman.location, Point(2, 1)) board.pacman.set_new_direction(Point(0, 1)) board.update_board() self.assertEqual(board.pacman.location, Point(2, 2)) board.update_board() self.assertEqual(board.pacman.location, Point(2, 3)) board.update_board() self.assertEqual(board.pacman.location, Point(2, 3))
def set_direction_down(self): self.set_new_direction(Point(0, 1))
class Ghost: def __init__(self, x, y, board, speed, sprite="./sprites/ghost_red.png"): self.sprite = ImageTk.PhotoImage(file=sprite) self.scared_sprite = ImageTk.PhotoImage( file="./sprites/ghost_scared.png") self.location = Point(x, y) self.speed = speed self.board = board self.direction = Point(0, 0) self._movement_marker = None self.last_teleported = 0 self.destination = Point(14, 1) def move(self, speed, direction): new_location = Point(self.location.x + direction.x * speed, self.location.y + direction.y * speed) if self._movement_marker and self.location.distance(self._movement_marker[0]) +\ self._movement_marker[0].distance(new_location) == self.location.distance(new_location): new_location = self._movement_marker[0] + Point( self._movement_marker[1].x * speed, self._movement_marker[1].y * speed) self.direction = self._movement_marker[1] self._movement_marker = None self.location = make_location_in_borders(new_location) def _get_prefered_directions(self): if self.board.game_state == 'frightened': directions = [Point(1, 0), Point(-1, 0), Point(0, 1), Point(0, -1)] random.shuffle(directions) return directions else: self._update_destination() directions = [Point(1, 0), Point(0, 1), Point(-1, 0), Point(0, -1)] course = self.location - self.destination directions.sort(key=lambda x: (x + course).length()) return directions def _make_marker(self): location = round(self.location) directions = self._get_prefered_directions() for direction in directions: if direction.is_opposite(self.direction): continue new_location = round(make_location_in_borders(location + direction)) if not isinstance(self.board.field[new_location.x][new_location.y], Wall): self._movement_marker = (location, direction) break def _update_destination(self): self.destination = self.board.pacman.location def _account_portals(self, dest): # возвращает расположение портала входа, если ближе пройти по нему straight_distance = self.location.distance(dest) for portal in self.board.portals: if straight_distance > self.location.distance( portal.location) + portal.exit_location.distance(dest): return portal.location return dest def update_position(self): self._make_marker() if self.board.pacman.super_power: self.move(self.speed / 2, self.direction) else: self.move(self.speed, self.direction) def get_sprite(self): if self.board.pacman.super_power: return self.scared_sprite else: return self.sprite def action_when_collided_with(self, obj): if isinstance(obj, Pacman): if obj.super_power: threading.Thread(target=self.respawn).start() winsound.PlaySound("./audio/pacman_eatghost.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) obj.score += 200 def respawn(self): # запускать в отдельном потоке self.direction = Point(0, 0) self.location = Point(7, 7) time.sleep(7) self.direction = Point(0, -1) self.speed = self.speed / 2 time.sleep(1) self.speed = self.speed * 2
def make_location_in_borders(location): return Point((location.x + 0.5) % BOARD_WIDTH - 0.5, (location.y + 0.5) % BOARD_HEIGHT - 0.5)
def set_direction_stop(self): self.saved_direction = Point(0, 0) self.direction = Point(0, 0)
class Pacman: def __init__(self, x, y, board, speed): self.respawn_location = Point(x, y) self.location = self.respawn_location self.saved_direction = Point(0, 0) self.last_direction = Point(1, 0) self.direction = Point(0, 0) self._directions = [] self.sprites = {} self.speed = speed self.lives = 3 self.board = board self.score = 0 self.super_power = 0 self.last_teleported = 0 self._configure_sprites() def action_when_collided_with(self, obj): if isinstance(obj, Food): winsound.PlaySound("./audio/pacman_chomp.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.score += 10 self.board.food_left -= 1 elif isinstance(obj, PowerFood): winsound.PlaySound("./audio/pacman_eatfruit.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.score += 100 self.super_power += 1 Timer(5.0, self._reset_super_power).start() elif isinstance(obj, game_logic.ghost.Ghost): if not self.super_power: winsound.PlaySound("./audio/pacman_death.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.location = Point(-1, -1) self.lives -= 1 self.saved_direction = Point(0, 0) self.direction = Point(0, 0) Timer(1.0, self.respawn).start() elif isinstance(obj, Prize): winsound.PlaySound("./audio/pacman_eatfruit.wav", winsound.SND_FILENAME | winsound.SND_ASYNC) self.score += 200 def move(self, direction): new_location = Point(self.location.x + direction.x * self.speed, self.location.y + direction.y * self.speed) self.location = make_location_in_borders(new_location) def decide_direction(self): if self.direction.is_opposite(self.saved_direction): self.saved_direction = Point(0, 0) return self.direction if self.location != round(self.location): return self.last_direction new_location = round(make_location_in_borders(self.location + self.direction)) if not isinstance(self.board.field[new_location.x][new_location.y], Wall): self.saved_direction = Point(0, 0) return self.direction new_location = round(make_location_in_borders(self.location + self.saved_direction)) if not isinstance(self.board.field[new_location.x][new_location.y], Wall): return self.saved_direction self.saved_direction = Point(0, 0) return self.saved_direction def update_position(self): if self.location != Point(-1, -1): new_dir = self.decide_direction() if new_dir != Point(0, 0): self.last_direction = new_dir self.move(new_dir) def get_sprite(self): return self.sprites[self.last_direction][round((self.location.x + self.location.y) * 10) % 3] def set_new_direction(self, new_direction): if new_direction == self.direction: return self.saved_direction = self.direction self.direction = new_direction def set_direction_up(self): self.set_new_direction(Point(0, -1)) def set_direction_down(self): self.set_new_direction(Point(0, 1)) def set_direction_left(self): self.set_new_direction(Point(-1, 0)) def set_direction_right(self): self.set_new_direction(Point(1, 0)) def set_direction_stop(self): self.saved_direction = Point(0, 0) self.direction = Point(0, 0) def respawn(self): self.location = self.respawn_location def _reset_super_power(self): self.super_power -= 1 def _configure_sprites(self): self.sprites = {Point(1, 0): [ImageTk.PhotoImage(file="./sprites/pacman_1.png"), ImageTk.PhotoImage(file="./sprites/pacman_2.png"), ImageTk.PhotoImage(file="./sprites/pacman_3.png")], Point(0, 1): [ImageTk.PhotoImage(Image.open("./sprites/pacman_1.png").rotate(270)), ImageTk.PhotoImage(Image.open("./sprites/pacman_2.png").rotate(270)), ImageTk.PhotoImage(Image.open("./sprites/pacman_3.png").rotate(270))], Point(-1, 0): [ImageTk.PhotoImage(Image.open("./sprites/pacman_1.png").rotate(180)), ImageTk.PhotoImage(Image.open("./sprites/pacman_2.png").rotate(180)), ImageTk.PhotoImage(Image.open("./sprites/pacman_3.png").rotate(180))], Point(0, -1): [ImageTk.PhotoImage(Image.open("./sprites/pacman_1.png").rotate(90)), ImageTk.PhotoImage(Image.open("./sprites/pacman_2.png").rotate(90)), ImageTk.PhotoImage(Image.open("./sprites/pacman_3.png").rotate(90))] }
def move(self, direction): new_location = Point(self.location.x + direction.x * self.speed, self.location.y + direction.y * self.speed) self.location = make_location_in_borders(new_location)
def __init__(self, x, y): self.location = Point(x, y)
def __init__(self, x, y): self.location = Point(x, y) s_names = ["./sprites/prize_apple.png", "./sprites/prize_cherry.png", "./sprites/prize_srawberry.png"] self.sprite = ImageTk.PhotoImage(file=random.choice(s_names))
def update_position(self): if self.location != Point(-1, -1): new_dir = self.decide_direction() if new_dir != Point(0, 0): self.last_direction = new_dir self.move(new_dir)
def set_direction_up(self): self.set_new_direction(Point(0, -1))
def set_direction_left(self): self.set_new_direction(Point(-1, 0))
def set_direction_right(self): self.set_new_direction(Point(1, 0))
def __init__(self, x, y): self.location = Point(x, y) self.sprite = ImageTk.PhotoImage(file="./sprites/power_food.png")
def __init__(self, x, y, exit_x, exit_y): self.location = Point(x, y) self.exit_location = Point(exit_x, exit_y) self.sprite = tkinter.PhotoImage(file="./sprites/loop_portal.gif", format="gif -index 0")