def initialize_blocks(self): #position for blocks is the top left corner #all blocks in the game will be kept in a single array #should have at least 4 gap between the blocks and the bottom of the game assert self.height > self.rows + 4 self.blocks = [] for r in range(self.height): for c in range(self.columns + 2): if (c == 0 or c == self.columns + 1 or r == 0): self.blocks.append(Block(((self.block_size*(c)), self.block_size*(r)), "HARD", self.block_size)) else: if (r <= self.rows): self.blocks.append(Block(((self.block_size*(c)), self.block_size*(r)), "SOFT", self.block_size))
def test_does_intersect(self): #test intersects left ball0 = Ball((0, 2), 3) block0 = Block((1, 0), "HARD", 5) self.assertTrue(ball0.intersects_block(block0)) #test intersects top ball1 = Ball((2, 0), 3) block1 = Block((0, 3), "HARD", 5) self.assertTrue(ball1.intersects_block(block1)) #test intersects bottom ball2 = Ball((2, 8), 3) block2 = Block((0, 2), "HARD", 5) self.assertTrue(ball2.intersects_block(block2)) #test intersects right ball3 = Ball((6, 3), 3) block3 = Block((0, 2), "HARD", 5) self.assertTrue(ball3.intersects_block(block3))
def test_does_not_intersect(self): #test does not intersect left ball0 = Ball((0, 2), 1) block0 = Block((2, 0), "HARD", 5) self.assertFalse(ball0.intersects_block(block0)) #test does not intersect top ball1 = Ball((2, 0), 3) block1 = Block((0, 4), "HARD", 5) self.assertFalse(ball1.intersects_block(block1)) #test does not intersect bottom ball2 = Ball((2, 9), 3) block2 = Block((0, 0), "HARD", 5) self.assertFalse(ball2.intersects_block(block2)) #test does not intersect right ball3 = Ball((9, 3), 3) block3 = Block((0, 2), "HARD", 5) self.assertFalse(ball3.intersects_block(block3))
def test_resolve_on_right(self): ball = Ball((6, 3), 3, Vector(1, 1)) block = Block((0, 2), "HARD", 5) ball.resolve_collision_block(block) self.assertEqual(ball.get_velocity(), Vector(-1, 1)) self.assertEqual(ball.get_center(), (9, 3))
def test_resolve_on_bottom(self): ball = Ball((2, 8), 3, Vector(1, 1)) block = Block((0, 2), "HARD", 5) ball.resolve_collision_block(block) self.assertEqual(ball.get_velocity(), Vector(1, -1)) self.assertEqual(ball.get_center(), (2, 11))