This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val prefs = context.getSharedPreferences( | |
"Kotlin Invaders", | |
Context.MODE_PRIVATE) | |
val oldHighScore = prefs.getInt("highScore", 0) | |
if(highScore > oldHighScore) { | |
val editor = prefs.edit() | |
editor.putInt( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// To remember the high score | |
private val prefs: SharedPreferences = context.getSharedPreferences( | |
"Kotlin Invaders", | |
Context.MODE_PRIVATE) | |
private var highScore = prefs.getInt("highScore", 0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Play a sound based on the menace level | |
if (!paused && ((startFrameTime - lastMenaceTime) > menaceInterval)) | |
menacePlayer() | |
}// end of while loop | |
}// end of run function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun menacePlayer() { | |
if (uhOrOh) { | |
// Play Uh | |
soundPlayer.playSound(SoundPlayer.uhID) | |
} else { | |
// Play Oh | |
soundPlayer.playSound(SoundPlayer.ohID) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
... | |
invader.update(fps) | |
// Does he want to take a shot? | |
if (invader.takeAim(playerShip.position.left, | |
playerShip.width, | |
waves)) { | |
// If so try and spawn a bullet |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For making a noise | |
private val soundPlayer = SoundPlayer(context) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The SurfaceView class implements onTouchListener | |
// So we can override this method and detect screen touches. | |
override fun onTouchEvent(motionEvent: MotionEvent): Boolean { | |
when (motionEvent.action and MotionEvent.ACTION_MASK) { | |
// Player has touched the screen | |
// Or moved their finger while touching screen | |
MotionEvent.ACTION_POINTER_DOWN, | |
MotionEvent.ACTION_DOWN, | |
MotionEvent.ACTION_MOVE-> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gamecodeschool.kotlininvaders | |
import android.content.Context | |
import android.content.res.AssetFileDescriptor | |
import android.media.AudioManager | |
import android.media.SoundPool | |
import android.util.Log | |
import java.io.IOException | |
class SoundPlayer(context: Context) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
... | |
// Draw the bricks if visible | |
for (brick in bricks) { | |
if (brick.isVisible) { | |
canvas.drawRect(brick.position, paint) | |
} | |
} | |
// Draw the players playerBullet if active |
NewerOlder