Skip to content

Instantly share code, notes, and snippets.

@EliteIntegrity
Created March 5, 2019 11:42
Show Gist options
  • Save EliteIntegrity/f2e822e805e4194cb58c0a0ea0ad276f to your computer and use it in GitHub Desktop.
Save EliteIntegrity/f2e822e805e4194cb58c0a0ea0ad276f to your computer and use it in GitHub Desktop.
Add this big chunk of code which handles collision detetion at the very end (but inside of) the update function.
// Has the player's playerBullet
// hit the top of the screen
if (playerBullet.position.bottom < 0) {
playerBullet.isActive =false
}
// Has an invaders playerBullet
// hit the bottom of the screen
for (bullet in invadersBullets) {
if (bullet.position.top > size.y) {
bullet.isActive = false
}
}
// Has the player's playerBullet hit an invader
if (playerBullet.isActive) {
for (invader in invaders) {
if (invader.isVisible) {
if (RectF.intersects(playerBullet.position,
invader.position)) {
invader.isVisible = false
soundPlayer.playSound(
SoundPlayer.invaderExplodeID)
playerBullet.isActive = false
Invader.numberOfInvaders --
score += 10
if(score > highScore){
highScore = score
}
// Has the player cleared the level
//if (score == numInvaders * 10 * waves) {
if (Invader.numberOfInvaders == 0) {
paused = true
lives ++
invaders.clear()
bricks.clear()
invadersBullets.clear()
prepareLevel()
waves ++
break
}
// Don't check any more invaders
break
}
}
}
}
// Has an alien playerBullet hit a shelter brick
for (bullet in invadersBullets) {
if (bullet.isActive) {
for (brick in bricks) {
if (brick.isVisible) {
if (RectF.intersects(bullet.position, brick.position)) {
// A collision has occurred
bullet.isActive = false
brick.isVisible = false
soundPlayer.playSound(SoundPlayer.damageShelterID)
}
}
}
}
}
// Has a player playerBullet hit a shelter brick
if (playerBullet.isActive) {
for (brick in bricks) {
if (brick.isVisible) {
if (RectF.intersects(playerBullet.position, brick.position)) {
// A collision has occurred
playerBullet.isActive = false
brick.isVisible = false
soundPlayer.playSound(SoundPlayer.damageShelterID)
}
}
}
}
// Has an invader playerBullet hit the player ship
for (bullet in invadersBullets) {
if (bullet.isActive) {
if (RectF.intersects(playerShip.position, bullet.position)) {
bullet.isActive = false
lives --
soundPlayer.playSound(SoundPlayer.playerExplodeID)
// Is it game over?
if (lives == 0) {
lost = true
break
}
}
}
}
if (lost) {
paused = true
lives = 3
score = 0
waves = 1
invaders.clear()
bricks.clear()
invadersBullets.clear()
prepareLevel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment