Skip to content

Instantly share code, notes, and snippets.

@IvanShafran
Created January 3, 2022 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IvanShafran/86dc2ecfba6fa6577a7621c8eca7ed8a to your computer and use it in GitHub Desktop.
Save IvanShafran/86dc2ecfba6fa6577a7621c8eca7ed8a to your computer and use it in GitHub Desktop.
// Move the cake off the screen right away
private fun initializeCake() {
cakeSize = height / 8
moveCakeToStartPoint()
}
private fun moveCakeToStartPoint() {
// Choose a random position
cakeRect.left = width + width * Random.nextFloat()
cakeRect.right = cakeRect.left + cakeSize
// Choose a random line
val isTopLine = Random.nextBoolean()
cakeRect.top = getObjectYTopForLine(cakeSize, isTopLine).toFloat()
cakeRect.bottom = cakeRect.top + cakeSize
}
// Mpve the cake by time
private fun moveCake() {
val currentTime = System.currentTimeMillis()
val deltaTime = currentTime - previousTimestamp
val deltaX = cakeSpeed * width * deltaTime
cakeRect.left -= deltaX
cakeRect.right = cakeRect.left + cakeSize
previousTimestamp = currentTime
}
// If the cake and the player intersect each other then count a point
private fun checkPlayerCaughtCake() {
if (RectF.intersects(playerRect, cakeRect)) {
score += if (flags.isLeftEyeOpen && flags.isRightEyeOpen) 1 else 2
moveCakeToStartPoint()
}
}
// If the player misses the cake then move the cake off the screen again
private fun checkCakeIsOutOfScreenStart() {
if (cakeRect.right < 0) {
moveCakeToStartPoint()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment