Beispiel #1
0
    def testEquals(self):
        """ check to see how the rect uses __eq__ 
        """
        r1 = Rect(1, 2, 3, 4)
        r2 = Rect(10, 20, 30, 40)
        r3 = (10, 20, 30, 40)
        r4 = Rect(10, 20, 30, 40)

        class foo(Rect):
            def __eq__(self, other):
                return id(self) == id(other)

            def __ne__(self, other):
                return id(self) != id(other)

        class foo2(Rect):
            pass

        r5 = foo(10, 20, 30, 40)
        r6 = foo2(10, 20, 30, 40)

        self.assertNotEqual(r5, r2)

        # because we define equality differently for this subclass.
        self.assertEqual(r6, r2)

        rect_list = [r1, r2, r3, r4, r6]

        # see if we can remove 4 of these.
        rect_list.remove(r2)
        rect_list.remove(r2)
        rect_list.remove(r2)
        rect_list.remove(r2)
        self.assertRaises(ValueError, rect_list.remove, r2)
Beispiel #2
0
 def drawColourBox(self, screen, selected, colour, xpos, ypos):
     if (selected):
         # If cursor is on this position (-1) then highlight current colour
         # Higlight with black and white so it will contrast with either colour
         screen.draw.filled_rect(Rect((xpos,ypos),(24,24)), (0,0,0))
         screen.draw.filled_rect(Rect((xpos-2,ypos-2),(24,24)), (255,255,255))
     screen.draw.filled_rect(Rect((xpos,ypos),(20,20)), colour )
Beispiel #3
0
    def test_unionall_ip(self):
        r1 = Rect(0, 0, 1, 1)
        r2 = Rect(-2, -2, 1, 1)
        r3 = Rect(2, 2, 1, 1)

        r1.unionall_ip([r2, r3])
        self.assertEqual(Rect(-2, -2, 5, 5), r1)
Beispiel #4
0
    def test_collidedictall(self):

        # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidedictall:

        # Rect.collidedictall(dict): return [(key, value), ...]
        # test if all rectangles in a dictionary intersect
        #
        # Returns a list of all the key and value pairs that intersect with
        # the Rect. If no collisions are found an empty dictionary is
        # returned.
        #
        # Rect objects are not hashable and cannot be used as keys in a
        # dictionary, only as values.

        r = Rect(1, 1, 10, 10)

        r2 = Rect(1, 1, 10, 10)
        r3 = Rect(5, 5, 10, 10)
        r4 = Rect(10, 10, 10, 10)
        r5 = Rect(50, 50, 10, 10)

        rects_values = 1
        d = {2: r2}
        l = r.collidedictall(d, rects_values)
        self.assertEqual(l, [(2, r2)])

        d2 = {2: r2, 3: r3, 4: r4, 5: r5}
        l2 = r.collidedictall(d2, rects_values)
        self.assertEqual(l2, [(2, r2), (3, r3), (4, r4)])
Beispiel #5
0
 def test_constructor_from_rect_object(self):
     "Build a rect from an object with a rect attribute which is an object"
     class Obj: pass
     obj = Obj()
     obj.rect = Rect(1, 2, 3, 4)
     r = Rect(obj)
     self.assertEqual(r, Rect(1, 2, 3, 4))
Beispiel #6
0
 def test_not_hashable(self):
     r1 = Rect(1, 2, 3, 4)
     r2 = Rect(r1)
     self.assertEqual(r1, r2)
     self.assertIsNot(r1, r2)
     with self.assertRaises(TypeError):
         self.assertEqual({r1}, {r2})
Beispiel #7
0
 def test_move(self):
     r = Rect(1, 2, 3, 4)
     move_x = 10
     move_y = 20
     r2 = r.move(move_x, move_y)
     expected_r2 = Rect(r.left + move_x, r.top + move_y, r.width, r.height)
     self.assertEqual(expected_r2, r2)
Beispiel #8
0
 def test_normalize(self):
     r = Rect(1, 2, -3, -6)
     r2 = Rect(r)
     r2.normalize()
     self.assertTrue(r2.width >= 0)
     self.assertTrue(r2.height >= 0)
     self.assertEqual((abs(r.width), abs(r.height)), r2.size)
     self.assertEqual((-2, -4), r2.topleft)
Beispiel #9
0
 def test_clamp(self):
     r = Rect(10, 10, 10, 10)
     c = Rect(19, 12, 5, 5).clamp(r)
     self.assertEqual(c.right, r.right)
     self.assertEqual(c.top, 12)
     c = Rect(1, 2, 3, 4).clamp(r)
     self.assertEqual(c.topleft, r.topleft)
     c = Rect(5, 500, 22, 33).clamp(r)
     self.assertEqual(c.center, r.center)
Beispiel #10
0
 def test_constructor_from_rect_callable(self):
     """Build a rect from an object with a rect attribute which
     is called and returns a tuple
     """
     class Obj:
         def rect(self):
             return 1, 2, 3, 4
     obj = Obj()
     r = Rect(obj)
     self.assertEqual(r, Rect(1, 2, 3, 4))
Beispiel #11
0
    def test_top(self):
        """Changing the top attribute moves the rect and does not change
           the rect's width
        """
        r = Rect(1, 2, 3, 4)
        new_top = 10

        r.top = new_top
        self.assertEqual(Rect(1, new_top, 3, 4), r)
        self.assertEqual(new_top, r.top)
Beispiel #12
0
    def test_left(self):
        """Changing the left attribute moves the rect and does not change
           the rect's width
        """
        r = Rect(1, 2, 3, 4)
        new_left = 10

        r.left = new_left
        self.assertEqual(new_left, r.left)
        self.assertEqual(Rect(new_left, 2, 3, 4), r)
Beispiel #13
0
 def test_constructor_from_rect_indirect_tuple(self):
     """Build a rect from an object with a rect attribute which
     is an object which has a rect attribute which is a tuple
     """
     class Obj: pass
     obj = Obj()
     obj1 = Obj()
     obj1.rect = Rect(1, 2, 3, 4)
     obj.rect = obj1
     r = Rect(obj)
     self.assertEqual(r, Rect(1, 2, 3, 4))
Beispiel #14
0
    def test_inflate_ip__smaller(self):
        "The inflate method inflates around the center of the rectangle"
        r = Rect(2, 4, 6, 8)
        r2 = Rect(r)
        r2.inflate_ip(-4, -6)

        self.assertEqual(r.center, r2.center)
        self.assertEqual(r.left + 2, r2.left)
        self.assertEqual(r.top + 3, r2.top)
        self.assertEqual(r.right - 2, r2.right)
        self.assertEqual(r.bottom - 3, r2.bottom)
        self.assertEqual(r.width - 4, r2.width)
        self.assertEqual(r.height - 6, r2.height)
Beispiel #15
0
 def setUp(self):
     # the Alien should be 66 x 92 px
     self.actor = Actor('alien', pos=(100, 150), anchor=('left', 'top'))
     self.separate_rect = Rect((0, 20), (20, 300))
     self.overlapping_rect = Rect((120, 100), (100, 100))
     self.enclosed_rect = Rect((110, 160), (10, 10))
     self.enclosing_rect = Rect((0, 0), (500, 500))
Beispiel #16
0
    def draw_playing_menu(self):
        # 绘制游戏中菜单

        # 背景
        screen.blit("playing_bg_pku", (0, 0))
        # 金币
        screen.blit("money", (225, 620))
        screen.draw.text(str(self.money),
                         topright=(350, 640),
                         color=(0, 0, 0),
                         fontsize=50)
        # 生命
        screen.blit("lives", (50, 90))
        screen.draw.text(str(self.lives),
                         topright=(130, 100),
                         color=(0, 0, 0),
                         fontsize=50)
        # 按钮
        self.back_button.draw()
        self.pause_button.draw()
        self.start_button.draw()
        self.next_button.draw()
        # 塔的价格
        screen.blit("little_money", (500, 650))
        screen.draw.text("50",
                         topright=(575, 650),
                         color=(0, 0, 0),
                         fontsize=30)
        screen.blit("little_money", (675, 650))
        screen.draw.text("100",
                         topright=(750, 650),
                         color=(0, 0, 0),
                         fontsize=30)
        screen.blit("little_money", (875, 650))
        screen.draw.text("200",
                         topright=(950, 650),
                         color=(0, 0, 0),
                         fontsize=30)
        # 塔的购买按钮
        self.buy_tower1.draw()
        self.buy_tower2.draw()
        self.buy_tower3.draw()
        # 游戏进度条
        rect = Rect((25, 475), (150, 10))
        screen.draw.filled_rect(rect, (0, 255, 0))
        rect = Rect((25, 475),
                    (self.enemy_count / (self.level * 10) * 150, 10))
        screen.draw.filled_rect(rect, (255, 0, 0))
        screen.blit("virus2_2",
                    (self.enemy_count / (self.level * 10) * 150, 465))
Beispiel #17
0
 def test_clip(self):
     r1 = Rect(1, 2, 3, 4)
     self.assertEqual(Rect(1, 2, 2, 2), r1.clip(Rect(0, 0, 3, 4)))
     self.assertEqual(Rect(2, 2, 2, 4), r1.clip(Rect(2, 2, 10, 20)))
     self.assertEqual(Rect(2, 3, 1, 2), r1.clip(Rect(2, 3, 1, 2)))
     self.assertEqual((0, 0), r1.clip(20, 30, 5, 6).size)
     self.assertEqual(r1, r1.clip(Rect(r1)),
                      "r1 does not clip an identical rect to itself")
Beispiel #18
0
 def test_constructor_from_tuple(self):
     "Build a Rect from a 4-item tuple"
     r = Rect((1, 2, 3, 4))
     self.assertEqual(r.x, 1)
     self.assertEqual(r.y, 2)
     self.assertEqual(r.w, 3)
     self.assertEqual(r.h, 4)
Beispiel #19
0
    def health_bar(self):
        # 绘制血量条

        if self.type == 5:
            bar_len = 60  # 彩蛋中角色的图片更大
        else:
            bar_len = 50

        # 绘制总血量
        rect = Rect((self.x - bar_len / 2, self.y - bar_len), (bar_len, 10))
        screen.draw.filled_rect(rect, (255, 0, 0))

        # 绘制剩余血量
        remain_len = self.health / self.health_list[self.type - 1] * bar_len
        rect = Rect((self.x - bar_len / 2, self.y - bar_len), (remain_len, 10))
        screen.draw.filled_rect(rect, (0, 255, 0))
Beispiel #20
0
 def test_filled_rect(self):
     yellow = (255, 255, 0)
     """We can draw a filled rectangle."""
     self.screen.draw.filled_rect(rect=Rect((0, 0), (100, 100)),
                                  color=yellow)
     self.assertImagesAlmostEqual(self.screen.surface,
                                  images.expected_filled_rect)
Beispiel #21
0
 def test_float_instance(self):
     "Create an instance with floating-point co-ordinates"
     r = Rect(1.2, 3.4, 5.6, 7.8)
     self.assertEqual(r.x, 1.2)
     self.assertEqual(r.y, 3.4)
     self.assertEqual(r.w, 5.6)
     self.assertEqual(r.h, 7.8)
Beispiel #22
0
    def test_size(self):
        "Changing the size resizes the rect from the top-left corner"
        r = Rect(1, 2, 3, 4)
        new_size = (10, 20)
        old_topleft = r.topleft

        r.size = new_size
        self.assertEqual(new_size, r.size)
        self.assertEqual(old_topleft, r.topleft)
Beispiel #23
0
    def test_collidelist(self):

        # __doc__ (as of 2008-08-02) for pygame.rect.Rect.collidelist:

        # Rect.collidelist(list): return index
        # test if one rectangle in a list intersects
        #
        # Test whether the rectangle collides with any in a sequence of
        # rectangles. The index of the first collision found is returned. If
        # no collisions are found an index of -1 is returned.

        r = Rect(1, 1, 10, 10)
        l = [Rect(50, 50, 1, 1), Rect(5, 5, 10, 10), Rect(15, 15, 1, 1)]

        self.assertEqual(r.collidelist(l), 1)

        f = [Rect(50, 50, 1, 1), (100, 100, 4, 4)]
        self.assertEqual(r.collidelist(f), -1)
Beispiel #24
0
    def test_topleft(self):
        """Changing the topleft attribute moves the rect and does not change
           the rect's size
        """
        r = Rect(1, 2, 3, 4)
        new_topleft = (r.left + 20, r.top + 30)
        old_size = r.size

        r.topleft = new_topleft
        self.assertEqual(new_topleft, r.topleft)
        self.assertEqual(old_size, r.size)
Beispiel #25
0
    def test_height(self):
        "Changing the height resizes the rect from the top-left corner"
        r = Rect(1, 2, 3, 4)
        new_height = 10
        old_topleft = r.topleft
        old_width = r.width

        r.height = new_height
        self.assertEqual(new_height, r.height)
        self.assertEqual(old_width, r.width)
        self.assertEqual(old_topleft, r.topleft)
Beispiel #26
0
    def test_fit(self):

        # __doc__ (as of 2008-08-02) for pygame.rect.Rect.fit:

        # Rect.fit(Rect): return Rect
        # resize and move a rectangle with aspect ratio
        #
        # Returns a new rectangle that is moved and resized to fit another.
        # The aspect ratio of the original Rect is preserved, so the new
        # rectangle may be smaller than the target in either width or height.

        r = Rect(10, 10, 30, 30)

        r2 = Rect(30, 30, 15, 10)

        f = r.fit(r2)
        self.assertTrue(r2.contains(f))

        f2 = r2.fit(r)
        self.assertTrue(r.contains(f2))
Beispiel #27
0
    def test_inflate__larger(self):
        "The inflate method inflates around the center of the rectangle"
        r = Rect(2, 4, 6, 8)
        r2 = r.inflate(4, 6)

        self.assertEqual(r.center, r2.center)
        self.assertEqual(r.left - 2, r2.left)
        self.assertEqual(r.top - 3, r2.top)
        self.assertEqual(r.right + 2, r2.right)
        self.assertEqual(r.bottom + 3, r2.bottom)
        self.assertEqual(r.width + 4, r2.width)
        self.assertEqual(r.height + 6, r2.height)
Beispiel #28
0
 def draw_background(self, screen=None):
     if screen is not None:
         px_width = self.width * self.cell_width
         px_height = self.height * self.cell_height
         height = max(self.controles_height, px_height + self.margin[1] * 2)
         screen.draw.filled_rect(
             Rect((0, 0), (px_width + 2 * self.margin[0], self.margin[1])),
             self.border_color)
         screen.draw.filled_rect(
             Rect((0, self.margin[1]), (self.margin[0], px_height)),
             self.border_color)
         screen.draw.filled_rect(
             Rect((0, px_height + self.margin[1]),
                  (px_width + 2 * self.margin[0], self.margin[1])),
             self.border_color)
         screen.draw.filled_rect(
             Rect((px_width + self.margin[0], self.margin[1]),
                  (self.margin[0], px_height)), self.border_color)
         screen.draw.filled_rect(
             Rect((px_width + 2 * self.margin[0], 0),
                  (self.controles_width, height)), self.background_color)
         if height > px_height + 2 * self.margin[1]:
             diff = height - px_height + 2 * self.margin[1] - 1
             screen.draw.filled_rect(
                 Rect((0, px_height + 2 * self.margin[1] + 1),
                      (px_width + 2 * self.margin[0], diff)),
                 self.background_color)
Beispiel #29
0
    def test_bottom(self):
        """Changing the bottom attribute moves the rect and does not change
           the rect's height
        """
        r = Rect(1, 2, 3, 4)
        new_bottom = r.bottom + 20
        expected_top = r.top + 20
        old_height = r.height

        r.bottom = new_bottom
        self.assertEqual(new_bottom, r.bottom)
        self.assertEqual(expected_top, r.top)
        self.assertEqual(old_height, r.height)
Beispiel #30
0
    def test_right(self):
        """Changing the right attribute moves the rect and does not change
           the rect's width
        """
        r = Rect(1, 2, 3, 4)
        new_right = r.right + 20
        expected_left = r.left + 20
        old_width = r.width

        r.right = new_right
        self.assertEqual(new_right, r.right)
        self.assertEqual(expected_left, r.left)
        self.assertEqual(old_width, r.width)