def reflect(self, initialVector: Vector): normalComponent: float = initialVector.dot(self.normal) if normalComponent < 0: normalComponentVector = self.normal.scale(normalComponent) reflector = normalComponentVector.scale(-2) else: reflector = Vector(0, 0) return initialVector + reflector
def paddleBounce(ball: Ball, paddle: Paddle): """ Calculates a return angle for a ball colliding with the paddle. Rebound angle is determined by where the ball strikes the paddle. """ paddleCOM = paddle.y + int(paddle.height * 0.5) speed = ball.getVelocity().getMagnitude() newSpeed = int(speed) + random.randint(-25, 50) newSpeed = max(newSpeed, 100) offset = -(paddleCOM - ball.y) * 2 / paddle.height reboundAngle = offset * math.pi / 3 newVelocity = Vector.fromPolarCoOrds(-newSpeed, -reboundAngle) ball.setVelocity(newVelocity)
def getBlocks(level: dict) -> list: initialPointer: Vector = Vector(600, 50) blockDimensions: tuple = (25, 100) gap: int = 25 generatorX = Vector(blockDimensions[0] + gap, 0).scale(-1) generatorY = Vector(0, blockDimensions[1] + gap) blocks: list = [] print(level["rows"]) for xIndex, row in enumerate(level["rows"]): for yIndex, cell in enumerate(row): positionPointer = initialPointer + generatorX.scale(xIndex) + generatorY.scale(yIndex) if cell: blocks.append(Block(positionPointer, blockDimensions)) positionPointer = positionPointer + generatorY print(positionPointer) return blocks
def getCentre(self) -> Vector: return self.position + Vector(self.dimensions[0] // 2, self.dimensions[1] // 2)
def getDimensionsVector(self) -> Vector: return Vector(self.dimensions[0], self.dimensions[1])
def getVelocity(self) -> Vector: return Vector(self.vx, self.vy)
def getPosition(self) -> Vector: return Vector(self.x, self.y)
def updateBall(ball: Ball, gamestate: GameState): """ Updates x and y position of the ball based on original positions combined with time differential. Detects collision with paddle or walls and reverses travel direction. Increments Score Value for display on score board. Destroys the ball if it travels off screen right. """ now = current_time() paddle = gamestate.paddle height = gamestate.height border = gamestate.border scrValue = gamestate.scoreValue blocks = gamestate.blocks if ball.timeOfLastUpdate is None: timeSinceLastUpdate = 0.0 else: timeSinceLastUpdate = timeSince(ball.timeOfLastUpdate) ball.timeOfLastUpdate = now nextPosition = getNextPosition(ball, timeSinceLastUpdate) hitboxes = [] for block in blocks: hitboxes.append(block.getHitBox()) blockIndex = ball.getHitBox().collidelist(hitboxes) if blockIndex >= 0: block = blocks[blockIndex] positionDelta: Vector = ball.getPosition().diff(block.getCentre()) theta = math.acos(positionDelta.normalise().x) verticalDistance = block.getDimensionsVector().getMagnitude( ) // 2 * math.sin(theta) verticalCollision = abs(verticalDistance) > block.getHeight() // 2 if verticalCollision: normal = Vector(0, positionDelta.y).normalise() else: normal = Vector(positionDelta.x, 0).normalise() wallBounce(ball, normal) gamestate.blocks.remove(block) scrValue += 2 paddleCollision = ball.getHitBox().colliderect(paddle.getHitBox()) if paddleCollision: paddleBounce(ball, paddle) hitBackWall = nextPosition.x < border + ball.RADIUS if hitBackWall: scrValue += 1 wallBounce(ball, Vector(1, 0)) increaseX(ball) hitTopWall = nextPosition.y < border + ball.RADIUS if hitTopWall: scrValue += 1 wallBounce(ball, Vector(0, 1)) hitBottomWall = nextPosition.y > height - border - ball.RADIUS if hitBottomWall: scrValue += 1 wallBounce(ball, Vector(0, -1)) print(ball.getPosition()) move(ball, timeSinceLastUpdate) return scrValue
def __init__(self, normal: Vector) -> None: self.normal = normal.normalise()