Skip to content

Instantly share code, notes, and snippets.

@EliteIntegrity
Last active March 5, 2019 12:17
Show Gist options
  • Save EliteIntegrity/223e350263343d864a28707793b63cff to your computer and use it in GitHub Desktop.
Save EliteIntegrity/223e350263343d864a28707793b63cff to your computer and use it in GitHub Desktop.
Adding code to the update function to control the movement of the invaders including when they drop down a row and head in the opposite direction.
private fun update(fps: Long) {
// Update the state of all the game objects
// Move the player's ship
playerShip.update(fps)
// Did an invader bump into the side of the screen
var bumped = false
// Has the player lost
var lost = false
// Update all the invaders if visible
for (invader in invaders) {
if (invader.isVisible) {
// Move the next invader
invader.update(fps)
// If that move caused them to bump
// the screen change bumped to true
if (invader.position.left > size.x - invader.width
|| invader.position.left < 0) {
bumped = true
}
}
}
// Did an invader bump into the edge of the screen
if (bumped) {
// Move all the invaders down and change direction
for (invader in invaders) {
invader.dropDownAndReverse(waves)
// Have the invaders landed
if (invader.position.bottom >= size.y && invader.isVisible) {
lost = true;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment