Skip to content

Instantly share code, notes, and snippets.

@JBurkeKF
Created Apr 30, 2021
Embed
What would you like to do?
mainBall = Circle(180, 200, 10, fill='red')
l = Label('No hit', 200, 20)
padLeft = Rect(5, 180, 5, 40, fill='black')
padRight = Rect(390, 180, 5, 40, fill='black')
mainBall.speedX = 6
mainBall.speedY = 5
mainBall.currentSpeedX = mainBall.speedX
mainBall.currentSpeedY = mainBall.speedY
padLeft.speed = 6
padLeft.currentSpeed = 0
padRight.speed = 6
padRight.currentSpeed = 0
##################################################
def applyBallCollisionResults(ball, targetX, targetY, hitX, hitY):
ball.centerX = targetX
ball.centerY = targetY
if (hitX):
ball.currentSpeedX = -1 * ball.currentSpeedX
if (hitY):
ball.currentSpeedY = -1 * ball.currentSpeedY
##################################################
def checkBallPadCollision(ball, pad, targetX, targetY):
hitX = False
hitY = False
if (ball.hitsShape(pad)):
if (pad == padLeft and ball.currentSpeedX < 0):
hitX = True
targetX = pad.right + (ball.radius - targetX + pad.right)
elif (pad == padRight and ball.currentSpeedX > 0):
hitX = True
targetX = pad.left - (targetX + ball.radius - pad.left)
if (ball.centerY <= pad.top and ball.currentSpeedY > 0):
hitY = True
targetY = pad.top - (targetY + ball.radius - pad.top)
elif (ball.centerY >= pad.bottom and ball.currentSpeedY < 0):
hitY = True
targetY = pad.bottom + (ball.radius - targetY + pad.bottom)
return targetX, targetY, hitX, hitY
##################################################
def checkBallAreaCollision(ball, targetX, targetY):
hitX = False
hitY = False
if (ball.currentSpeedX > 0):
if (targetX + ball.radius >= 400):
hitX = True
targetX = 400 - (targetX + ball.radius - 400)
elif (ball.currentSpeedX < 0):
if (targetX - ball.radius <= 0):
hitX = True
targetX = -1 * (targetX - ball.radius)
if (ball.currentSpeedY > 0):
if (targetY + ball.radius >= 400):
hitY = True
targetY = 400 - (targetY + ball.radius - 400)
elif (ball.currentSpeedY < 0):
if (targetY - ball.radius <= 0):
hitY = True
targetY = -1 * (targetY - ball.radius)
return targetX, targetY, hitX, hitY
##################################################
def moveBall(ball):
targetX = ball.centerX + ball.currentSpeedX
targetY = ball.centerY + ball.currentSpeedY
return targetX, targetY
##################################################
def movePad(pad):
target = pad.centerY + pad.currentSpeed
if (pad.currentSpeed > 0):
if (target + pad.height * 0.5 >= 390):
target = 390 - pad.height * 0.5
elif (pad.currentSpeed < 0):
if (target - pad.height * 0.5 <= 10):
target = 10 + pad.height * 0.5
pad.centerY = target
##################################################
def onKeyPress(key):
if (key == 'w'):
padLeft.currentSpeed = -1 * padLeft.speed
elif (key == 's'):
padLeft.currentSpeed = padLeft.speed
if (key == 'up'):
padRight.currentSpeed = -1 * padLeft.speed
elif (key == 'down'):
padRight.currentSpeed = padLeft.speed
##################################################
def onKeyRelease(key):
if (key == 'w' or key == 's'):
padLeft.currentSpeed = 0
if (key == 'up' or key == 'down'):
padRight.currentSpeed = 0
##################################################
def onStep():
mainBallTargetX, mainBallTargetY = moveBall(mainBall)
movePad(padLeft)
movePad(padRight)
mainBallHitResultX = False
mainBallHitResultY = False
mainBallTargetX, mainBallTargetY, mainBallHitX, mainBallHitY = checkBallPadCollision(mainBall, padLeft, mainBallTargetX, mainBallTargetY)
mainBallHitResultX = mainBallHitResultX or mainBallHitX
mainBallHitResultY = mainBallHitResultY or mainBallHitY
mainBallTargetX, mainBallTargetY, mainBallHitX, mainBallHitY = checkBallPadCollision(mainBall, padRight, mainBallTargetX, mainBallTargetY)
mainBallHitResultX = mainBallHitResultX or mainBallHitX
mainBallHitResultY = mainBallHitResultY or mainBallHitY
mainBallTargetX, mainBallTargetY, mainBallHitX, mainBallHitY = checkBallAreaCollision(mainBall, mainBallTargetX, mainBallTargetY)
mainBallHitResultX = mainBallHitResultX or mainBallHitX
mainBallHitResultY = mainBallHitResultY or mainBallHitY
applyBallCollisionResults(mainBall, mainBallTargetX, mainBallTargetY, mainBallHitResultX, mainBallHitResultY)
##################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment