Пример #1
0
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)
    cur = 0
    for i in range(0, ds.WIDTH // ds.SPACING + 1, ds.SPACING):
        ds.eat(i, ds.TH)
        assert len(ds.top_row) == ds.WIDTH // ds.SPACING
        cur += 1
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)
    ds.eat(150, 150)
    assert ds.top_row[2].x == 225
    assert len(ds.top_row) == 8
    assert ds.left_col[2].y == 225
    assert len(ds.left_col) == 8
Пример #3
0
def test_eat():
    """ Test eat method """
    ds = Dots(600, 600, 150, 450, 150, 450)
    for x_coordinate in range(150, 451, 150):
        for y_coordinate in range(600):
            ds.eat(x_coordinate, y_coordinate)
    for y_coordinate in range(150, 451, 150):
        for x_coordinate in range(600):
            ds.eat(x_coordinate, y_coordinate)
    assert ds.dots_left() == 0
Пример #4
0
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)
    # Testing the top row
    dots_to_eat = []
    assert len(ds.top_row) == ds.WIDTH // ds.SPACING + 1
    for i in range(len(ds.top_row)):
        dots_to_eat.append(ds.top_row[i])
    ds.eat(dots_to_eat)
    assert len(ds.top_row) == 0
    # Testing the bottom row
    assert len(ds.bottom_row) == ds.WIDTH // ds.SPACING + 1
    dots_to_eat = []
    for i in range(ds.WIDTH // ds.SPACING + 1):
        dots_to_eat.append(ds.bottom_row[0])
        ds.eat(dots_to_eat)
        assert len(ds.bottom_row) == ds.WIDTH // ds.SPACING + 1 - 1 * (i + 1)
    # Testing the left column
    dots_to_eat = []
    assert len(ds.left_col) == ds.HEIGHT // ds.SPACING + 1
    for i in range(len(ds.left_col)):
        dots_to_eat.append(ds.left_col[0])
    ds.eat(dots_to_eat)
    assert len(ds.top_row) == 0
    # Testing the right column
    assert len(ds.right_col) == ds.HEIGHT // ds.SPACING + 1
    dots_to_eat = []
    for i in range(ds.HEIGHT // ds.SPACING + 1):
        dots_to_eat.append(ds.right_col[0])
        ds.eat(dots_to_eat)
        assert len(ds.right_col) == ds.HEIGHT // ds.SPACING + 1 - 1 * (i + 1)
Пример #5
0
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)

    ds.eat(56, 150)
    # pacman is able to eat the dot on (75, 150) and (0, 150)
    print(len(ds.top_row))
    for dot in ds.top_row:
        assert not (dot.x == 75 and dot.y == 150)
        assert not (dot.x == 0 and dot.y == 150)

    ds.eat(450, 500)
    # pacman is able to eat the dot on (450, 525) and (450, 450)
    for dot in ds.right_col:
        assert not (dot.x == 450 and dot.y == 525)
        assert not (dot.x == 450 and dot.y == 450)

    ds.eat(150, 570)
    # pacman is able to eat the dot on (150, 600)
    for dot in ds.left_col:
        assert not (dot.x == 150 and dot.y == 600)

    ds.eat(20, 450)
    # pacman is able to eat the dot on (0, 450)
    for dot in ds.bottom_row:
        assert not (dot.x == 0 and dot.y == 450)
Пример #6
0
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)
    # firstly, if the exactly dot in the top row, the distance between
    # Pacman and dot is within eat distance(50).Then execute eat method,
    # check the same dot is not in the top row anymore
    for dot in ds.top_row:
        if dot.x == 300 and dot.y == 150:
            ds.eat(305, 150)
            assert dot not in ds.top_row

    # firstly, if the exactly dot in the bottom_row, the distance between
    # Pacman and dot is more than eat distance(50).Then execute eat method,
    # check the same dot is still in the bottom_row
    for dot in ds.bottom_row:
        if dot.x == 525 and dot.y == 450:
            ds.eat(625, 450)
            assert dot in ds.bottom_row

    # firstly, if the exactly dot in the left_col in the bottom,
    # the distance between Pacman and dot is more than 550.
    # Then execute eat method,check the same dot is not in the left_col anymore
    for dot in ds.left_col:
        if dot.x == 150 and dot.y == 600:
            ds.eat(150, 50)
            assert dot not in ds.left_col

    # firstly, if the exactly dot in the right_col in the middle,
    # the distance between Pacman and dot is more than 50.
    # Then execute eat method,check the same dot is not in the left_col
    for dot in ds.right_col:
        if dot.x == 450 and dot.y == 300:
            ds.eat(443, 300)
            assert dot not in ds.right_col
Пример #7
0
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)
    ds.eat(70, 150)
    for dots in ds.top_row:
        assert dots.x != 75
    ds.eat(70, 450)
    for dots in ds.bottom_row:
        assert dots.x != 75
    ds.eat(150, 70)
    for dots in ds.left_col:
        assert dots.y != 75
    ds.eat(450, 70)
    for dots in ds.right_col:
        assert dots.y != 75
Пример #8
0
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)
    ds.eat(100, 150)
    assert len(ds.top_row) == 8
    ds.eat(150, 200)
    assert len(ds.left_col) == 8
    ds.eat(150, 150)
    assert len(ds.top_row) == 7
    assert len(ds.left_col) == 8
Пример #9
0
class Maze:
    """Draws the maze and handles interaction between Pacman and dots"""
    def __init__(self, WIDTH, HEIGHT, LEFT_VERT, RIGHT_VERT, TOP_HORIZ,
                 BOTTOM_HORIZ, game_controller):
        self.LEFT_VERT = LEFT_VERT
        self.RIGHT_VERT = RIGHT_VERT
        self.TOP_HORIZ = TOP_HORIZ
        self.BOTTOM_HORIZ = BOTTOM_HORIZ
        self.WIDTH = WIDTH
        self.HEIGHT = HEIGHT
        self.gc = game_controller
        self.dots = Dots(WIDTH, HEIGHT, LEFT_VERT, RIGHT_VERT, TOP_HORIZ,
                         BOTTOM_HORIZ)

    def eat_dots(self, x, y):
        """Calls the Dots class method eat and passes Pac-Man's x and y to
        it"""
        self.dots.eat(x, y)

    def update(self):
        """Make necessary per-frame updates"""
        # Check whether the dots are all eaten
        if self.dots.dots_left() == 0:
            self.gc.player_wins = True

    def display(self):
        """Display the maze"""
        self.update()

        # Display the dots
        self.dots.display()

        # Draw the maze walls
        stroke(0.0, 0.0, 10)
        strokeWeight(5)
        fill(0)
        rectMode(CORNER)

        clearance = 60
        overdraw = 20  # Start drawing offscreen
        t = -(overdraw)
        l = -(overdraw)
        border = 20
        big_rad = 30
        small_rad = 17

        # Upper left
        t = -(overdraw)
        l = -(overdraw)
        w = self.LEFT_VERT - clearance + overdraw
        h = self.TOP_HORIZ - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l, t, w - border, h - border, small_rad)

        # Upper middle
        t = -(overdraw)
        l = self.LEFT_VERT + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t, w - border * 2, h - border, small_rad)

        # Upper right
        l = self.RIGHT_VERT + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t, w - border * 2, h - border, small_rad)

        # Middle left
        t = self.TOP_HORIZ + clearance
        l = -(overdraw)
        w = self.LEFT_VERT - clearance + overdraw
        h = (self.BOTTOM_HORIZ - clearance) - (self.TOP_HORIZ + clearance)
        rect(l, t, w, h, big_rad)
        rect(l, t + border, w - border, h - border * 2, small_rad)

        # Middle middle
        l = self.LEFT_VERT + clearance
        t = self.TOP_HORIZ + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border * 2, small_rad)

        # Middle right
        l = self.RIGHT_VERT + clearance
        t = self.TOP_HORIZ + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border * 2, small_rad)

        # Lower left
        w = self.LEFT_VERT - clearance + overdraw
        h = self.TOP_HORIZ - clearance + overdraw
        l = -(overdraw)
        t = self.BOTTOM_HORIZ + clearance
        rect(l, t, w, h, big_rad)
        rect(l, t + border, w - border, h - border, small_rad)

        # Lower middle
        l = self.LEFT_VERT + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border, small_rad)

        # Lower right
        l = self.RIGHT_VERT + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border, small_rad)
Пример #10
0
def test_eat():
    """Tests the number of dots that will remain after
    Pacman reaches certain x,y coordinates"""
    ds = Dots(600, 600, 150, 450, 150, 450)

    assert len(ds.total) == 36
    ds.eat(0, ds.TH)
    assert len(ds.total) == 35
    ds.eat(0, ds.BH)
    assert len(ds.total) == 34
    ds.eat(ds.LV, 150)
    assert len(ds.total) == 32
    ds.eat(ds.RV, 225)
    assert len(ds.total) == 31
    ds.eat(ds.RV, 200)
    assert len(ds.total) == 31
    ds.eat(ds.RV, 49)
    assert len(ds.total) == 30
Пример #11
0
def test_eat():
    """Test the eat method of Dots class"""
    # PAC-MAN at BOTTOM ROW TESTS:

    # Test eating a dot at the end of bottom row
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 600
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.bottom_row[i].y == ds.BH
        SPACING_multiple += 1

    # Test eating a dot at the end of bottom row
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 585
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.bottom_row[i].y == ds.BH
        SPACING_multiple += 1

    # Test eating a dot at the beginning of bottom row
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 0
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.bottom_row[i].y == ds.BH
        SPACING_multiple += 1

    # Test eating a dot at the beginning of bottom row
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 37
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.bottom_row[i].y == ds.BH
        SPACING_multiple += 1

    # Test eating a dot at the bottom left intersection
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the bottom left intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 161
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the bottom right intersection
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the bottom right intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 443
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the bottom row
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 375
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1

    # Test eating a dot at the bottom row
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 365
    y = 450
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1

    # PAC-MAN at TOP ROW TESTS:

    # Test eating a dot at the end of top row
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 600
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.top_row[i].y == ds.TH
        SPACING_multiple += 1

    # Test eating a dot at the end of top row
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 585
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.top_row[i].y == ds.TH
        SPACING_multiple += 1

    # Test eating a dot at the beginning of top row
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 0
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.top_row[i].y == ds.TH
        SPACING_multiple += 1

    # Test eating a dot at the beginning of top row
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 37
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * SPACING_multiple
        assert ds.top_row[i].y == ds.TH
        SPACING_multiple += 1

    # Test eating a dot at the top left intersection
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the top left intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 161
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the top right intersection
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the top right intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 443
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the top row
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 375
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1

    # Test eating a dot at the top row
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 365
    y = 150
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1

    # PAC-MAN at LEFT COLUMN TESTS:

    # Test eating a dot at the end of left column
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 600
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the end of left column
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 585
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the beginning of left column
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 0
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the beginning of left column
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 37
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the top left intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 140
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1

    # Test eating a dot at the bottom left intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 428
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1

    # Test eating a dot at the left column
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 375
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the left column
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 150
    y = 365
    for i in range(len(ds.left_col)):
        assert ds.left_col[i].x == ds.LV
        assert ds.left_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.left_col[dot_i].x == ds.LV
        assert ds.left_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # PAC-MAN at RIGHT COLUMN TESTS:

    # Test eating a dot at the end of right column
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 600
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the end of right column
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 585
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the beginning of right column
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 0
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the beginning of right column
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 37
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    SPACING_multiple = 1
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * SPACING_multiple
        SPACING_multiple += 1

    # Test eating a dot at the top right intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 140
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    for i in range(len(ds.top_row)):
        assert ds.top_row[i].x == ds.SPACING * i
        assert ds.top_row[i].y == ds.TH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 3, 4, 5, 6, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.top_row[dot_i].x == ds.SPACING * i
        assert ds.top_row[dot_i].y == ds.TH
        dot_i += 1

    # Test eating a dot at the bottom right intersection
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 428
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    for i in range(len(ds.bottom_row)):
        assert ds.bottom_row[i].x == ds.SPACING * i
        assert ds.bottom_row[i].y == ds.BH
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 5, 7, 8]:
        assert ds.bottom_row[dot_i].x == ds.SPACING * i
        assert ds.bottom_row[dot_i].y == ds.BH
        dot_i += 1

    # Test eating a dot at the right column
    # (exactly at dot location)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 375
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1

    # Test eating a dot at the right column
    # (within Pac-Man eating range)
    ds = Dots(600, 600, 150, 450, 150, 450)
    x = 450
    y = 365
    for i in range(len(ds.right_col)):
        assert ds.right_col[i].x == ds.RV
        assert ds.right_col[i].y == ds.SPACING * i
    ds.eat(x, y)
    dot_i = 0
    for i in [0, 1, 2, 3, 4, 6, 7, 8]:
        assert ds.right_col[dot_i].x == ds.RV
        assert ds.right_col[dot_i].y == ds.SPACING * i
        dot_i += 1
Пример #12
0
class Maze:
    """Draws the maze and handles interaction between Pacman and dots"""
    def __init__(self, WIDTH, HEIGHT, LEFT_VERT, RIGHT_VERT, TOP_HORIZ,
                 BOTTOM_HORIZ, game_controller):
        self.LEFT_VERT = LEFT_VERT
        self.RIGHT_VERT = RIGHT_VERT
        self.TOP_HORIZ = TOP_HORIZ
        self.BOTTOM_HORIZ = BOTTOM_HORIZ
        self.WIDTH = WIDTH
        self.HEIGHT = HEIGHT
        self.gc = game_controller
        self.dots = Dots(WIDTH, HEIGHT, LEFT_VERT, RIGHT_VERT, TOP_HORIZ,
                         BOTTOM_HORIZ)

    # TODO:
    # PROBLEM 3: implement dot eating
    # BEGIN CODE CHANGES
    def eat_dots(self, x, y, direction):
        """Given two integers representing the x, y coordinates of Pacman
        and another integer representing its direction, storethe dots that
        will be eaten in a list.
        Integer Integer Integer -> None"""
        dots_to_eat = []
        for dot in self.dots.top_row:
            if self.within_reach(x, y, dot, direction):
                dots_to_eat.append(dot)
        for dot in self.dots.bottom_row:
            if self.within_reach(x, y, dot, direction):
                dots_to_eat.append(dot)
        for dot in self.dots.left_col:
            if self.within_reach(x, y, dot, direction):
                dots_to_eat.append(dot)
        for dot in self.dots.right_col:
            if self.within_reach(x, y, dot, direction):
                dots_to_eat.append(dot)
        if dots_to_eat:
            self.dots.eat(dots_to_eat)

    def within_reach(self, x, y, dot_object, direction):
        """Given two integer representing the x and y coordinates, and a
        dot object, and an integer, return a Boolean value.
        Integer Integer Dot Integer -> Boolean"""
        if dot_object.x == x:
            dis = dot_object.y - y
            if abs(dis) <= self.dots.EAT_DIST and dis * direction >= 0 \
                    or abs(dis) + self.dots.EAT_DIST >= \
                    self.HEIGHT + self.dots.radian and dis * direction < 0:
                return True
        elif dot_object.y == y:
            dis = dot_object.x - x
            if abs(dis) <= self.dots.EAT_DIST and dis * direction >= 0 \
                    or abs(dis) + self.dots.EAT_DIST >= \
                    self.WIDTH + self.dots.radian and dis * direction < 0:
                return True
        else:
            return False

    # END CODE CHANGES

    def update(self):
        """Make necessary per-frame updates"""
        # Check whether the dots are all eaten
        if self.dots.dots_left() == 0:
            self.gc.player_wins = True

    def display(self):
        """Display the maze"""
        self.update()

        # Display the dots
        self.dots.display()

        # Draw the maze walls
        stroke(0.0, 0.0, 10)
        strokeWeight(5)
        fill(0)
        rectMode(CORNER)

        clearance = 60
        overdraw = 20  # Start drawing offscreen
        t = -(overdraw)
        l = -(overdraw)
        border = 20
        big_rad = 30
        small_rad = 17

        # Upper left
        t = -(overdraw)
        l = -(overdraw)
        w = self.LEFT_VERT - clearance + overdraw
        h = self.TOP_HORIZ - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l, t, w - border, h - border, small_rad)

        # Upper middle
        t = -(overdraw)
        l = self.LEFT_VERT + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t, w - border * 2, h - border, small_rad)

        # Upper right
        l = self.RIGHT_VERT + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t, w - border * 2, h - border, small_rad)

        # Middle left
        t = self.TOP_HORIZ + clearance
        l = -(overdraw)
        w = self.LEFT_VERT - clearance + overdraw
        h = (self.BOTTOM_HORIZ - clearance) - (self.TOP_HORIZ + clearance)
        rect(l, t, w, h, big_rad)
        rect(l, t + border, w - border, h - border * 2, small_rad)

        # Middle middle
        l = self.LEFT_VERT + clearance
        t = self.TOP_HORIZ + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border * 2, small_rad)

        # Middle right
        l = self.RIGHT_VERT + clearance
        t = self.TOP_HORIZ + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border * 2, small_rad)

        # Lower left
        w = self.LEFT_VERT - clearance + overdraw
        h = self.TOP_HORIZ - clearance + overdraw
        l = -(overdraw)
        t = self.BOTTOM_HORIZ + clearance
        rect(l, t, w, h, big_rad)
        rect(l, t + border, w - border, h - border, small_rad)

        # Lower middle
        l = self.LEFT_VERT + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border, small_rad)

        # Lower right
        l = self.RIGHT_VERT + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border * 2, h - border, small_rad)
Пример #13
0
class Maze:
    """Draws the maze and handles interaction between Pacman and dots"""
    def __init__(self, WIDTH, HEIGHT,
                 LEFT_VERT, RIGHT_VERT,
                 TOP_HORIZ, BOTTOM_HORIZ,
                 game_controller):
        self.LEFT_VERT = LEFT_VERT
        self.RIGHT_VERT = RIGHT_VERT
        self.TOP_HORIZ = TOP_HORIZ
        self.BOTTOM_HORIZ = BOTTOM_HORIZ
        self.WIDTH = WIDTH
        self.HEIGHT = HEIGHT
        self.gc = game_controller
        self.dots = Dots(WIDTH, HEIGHT,
                         LEFT_VERT, RIGHT_VERT,
                         TOP_HORIZ, BOTTOM_HORIZ)

    # TODO:
    # PROBLEM 3: implement dot eating
    # BEGIN CODE CHANGES
    def eat_dots(self, pacx, pacy, pac_width, pac_height):
        # You might want/need to pass arguments here.
        """Based on pacman's location, eat dots.
        Also cnosider corner case: when pacman
        moves off the edge of the view area,
        then pacman should eat one more dots on the opposite side"""
        self.dots.eat(pacx, pacy)
        if pacx > self.WIDTH - pac_width/2:
            self.dots.eat(0, pacy)
        if pacx < pac_width/2:
            self.dots.eat(self.WIDTH, pacy)
        if pacy > self.HEIGHT - pac_height/2:
            self.dots.eat(pacx, 0)
        if pacy < pac_height/2:
            self.dots.eat(pacx, self.HEIGHT)
    # END CODE CHANGES

    def update(self):
        """Make necessary per-frame updates"""
        # Check whether the dots are all eaten
        if self.dots.dots_left() == 0:
            self.gc.player_wins = True

    def display(self):
        """Display the maze"""
        self.update()

        # Display the dots
        self.dots.display()

        # Draw the maze walls
        stroke(0.0, 0.0, 10)
        strokeWeight(5)
        fill(0)
        rectMode(CORNER)

        clearance = 60
        overdraw = 20  # Start drawing offscreen
        t = -(overdraw)
        l = -(overdraw)
        border = 20
        big_rad = 30
        small_rad = 17

        # Upper left
        t = -(overdraw)
        l = -(overdraw)
        w = self.LEFT_VERT - clearance + overdraw
        h = self.TOP_HORIZ - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l, t, w - border, h - border, small_rad)

        # Upper middle
        t = -(overdraw)
        l = self.LEFT_VERT + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t, w - border*2, h - border, small_rad)

        # Upper right
        l = self.RIGHT_VERT + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t, w - border*2, h - border, small_rad)

        # Middle left
        t = self.TOP_HORIZ + clearance
        l = -(overdraw)
        w = self.LEFT_VERT - clearance + overdraw
        h = (self.BOTTOM_HORIZ - clearance) - (self.TOP_HORIZ + clearance)
        rect(l, t, w, h, big_rad)
        rect(l, t + border, w - border, h - border*2, small_rad)

        # Middle middle
        l = self.LEFT_VERT + clearance
        t = self.TOP_HORIZ + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border*2, h - border*2, small_rad)

        # Middle right
        l = self.RIGHT_VERT + clearance
        t = self.TOP_HORIZ + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border*2, h - border*2, small_rad)

        # Lower left
        w = self.LEFT_VERT - clearance + overdraw
        h = self.TOP_HORIZ - clearance + overdraw
        l = -(overdraw)
        t = self.BOTTOM_HORIZ + clearance
        rect(l, t, w, h, big_rad)
        rect(l, t + border, w - border, h - border, small_rad)

        # Lower middle
        l = self.LEFT_VERT + clearance
        w = (self.RIGHT_VERT - clearance) - (self.LEFT_VERT + clearance)
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border*2, h - border, small_rad)

        # Lower right
        l = self.RIGHT_VERT + clearance
        w = self.RIGHT_VERT - clearance + overdraw
        rect(l, t, w, h, big_rad)
        rect(l + border, t + border, w - border*2, h - border, small_rad)
Пример #14
0
def test_eat():
    ds = Dots(600, 600, 150, 450, 150, 450)
    for x in range(600):
        ds.eat(x, 450)
    assert len(ds.bottom_row) == 0