Skip to content

Instantly share code, notes, and snippets.

View EliteIntegrity's full-sized avatar

Game Code School EliteIntegrity

View GitHub Profile
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 12:04
Add this code to the end of the pause function to save the players new high score if one was achieved in the current play session
val prefs = context.getSharedPreferences(
"Kotlin Invaders",
Context.MODE_PRIVATE)
val oldHighScore = prefs.getInt("highScore", 0)
if(highScore > oldHighScore) {
val editor = prefs.edit()
editor.putInt(
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 12:01
Loading the high score from the device storage
// To remember the high score
private val prefs: SharedPreferences = context.getSharedPreferences(
"Kotlin Invaders",
Context.MODE_PRIVATE)
private var highScore = prefs.getInt("highScore", 0)
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:53
Call the menacePlayer function from near the end of the run function but still inside the ehile loop.
// Play a sound based on the menace level
if (!paused && ((startFrameTime - lastMenaceTime) > menaceInterval))
menacePlayer()
}// end of while loop
}// end of run function
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:50
Add the menacePlayer function to the KotlinInvadersView class.
private fun menacePlayer() {
if (uhOrOh) {
// Play Uh
soundPlayer.playSound(SoundPlayer.uhID)
} else {
// Play Oh
soundPlayer.playSound(SoundPlayer.ohID)
}
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:42
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) {
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:37
Add this after the call to invader.update and before the code which checks if an invader has bumped into the side of the screen
...
...
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
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:25
Add a property for the SoundPlayer instance to the KotlinInvadersView class.
// For making a noise
private val soundPlayer = SoundPlayer(context)
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:22
Code the onTouchEvent function which enables the player to un-pause the game loop, move the ship and shoot a bullet at the invaders.
// 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-> {
@EliteIntegrity
EliteIntegrity / SoundPlayer.kt
Created March 5, 2019 11:19
This class enables us to easily play a sound from the game loop just by calling the playSound function and passing in the appropriate id for the sound we want to play.
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) {
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:13
This code draws all the bullets for the player and the invaders. Add the code after the code which draws the shelters/bricks, as shown.
...
...
// Draw the bricks if visible
for (brick in bricks) {
if (brick.isVisible) {
canvas.drawRect(brick.position, paint)
}
}
// Draw the players playerBullet if active