Skip to content

Instantly share code, notes, and snippets.

@EliteIntegrity
Last active March 5, 2019 10:00
Show Gist options
  • Save EliteIntegrity/145921b2c6b1fced0d213ff97ce58422 to your computer and use it in GitHub Desktop.
Save EliteIntegrity/145921b2c6b1fced0d213ff97ce58422 to your computer and use it in GitHub Desktop.
This is the KorlinInvadersView class, with a working thread that can be started and stopped from KotlinInvadersActivity. It just draws the score to the screen. As we code the classes that represent the game objects we will add code here to update and draw them. Note that the class inherits from SurfaceView so it has an instance of Holder to lock…
package com.gamecodeschool.kotlininvaders
import android.content.Context
import android.content.SharedPreferences
import android.graphics.*
import android.view.SurfaceView
import android.util.Log
import android.view.MotionEvent
class KotlinInvadersView(context: Context,
private val size: Point)
: SurfaceView(context),
Runnable {
// This is our thread
private val gameThread = Thread(this)
// A boolean which we will set and unset
private var playing = false
// Game is paused at the start
private var paused = true
// A Canvas and a Paint object
private var canvas: Canvas = Canvas()
private val paint: Paint = Paint()
// The score
private var score = 0
// The wave number
private var waves = 1
// Lives
private var lives = 3
private var highScore = 0
// How menacing should the sound be?
private var menaceInterval: Long = 1000
// Which menace sound should play next
private var uhOrOh: Boolean = false
// When did we last play a menacing sound
private var lastMenaceTime = System.currentTimeMillis()
private fun prepareLevel() {
// Here we will initialize the game objects
}
override fun run() {
// This variable tracks the game frame rate
var fps: Long = 0
while (playing) {
// Capture the current time
val startFrameTime = System.currentTimeMillis()
// Update the frame
if (!paused) {
update(fps)
}
// Draw the frame
draw()
// Calculate the fps rate this frame
val timeThisFrame = System.currentTimeMillis() - startFrameTime
if (timeThisFrame >= 1) {
fps = 1000 / timeThisFrame
}
}
}
private fun update(fps: Long) {
// Update the state of all the game objects
}
private fun draw() {
// Make sure our drawing surface is valid or the game will crash
if (holder.surface.isValid) {
// Lock the canvas ready to draw
canvas = holder.lockCanvas()
// Draw the background color
canvas.drawColor(Color.argb(255, 0, 0, 0))
// Choose the brush color for drawing
paint.color = Color.argb(255, 0, 255, 0)
// Draw all the game objects here
// Draw the score and remaining lives
// Change the brush color
paint.color = Color.argb(255, 255, 255, 255)
paint.textSize = 70f
canvas.drawText("Score: $score Lives: $lives Wave: " +
"$waves HI: $highScore", 20f, 75f, paint)
// Draw everything to the screen
holder.unlockCanvasAndPost(canvas)
}
}
// If SpaceInvadersActivity is paused/stopped
// then shut down our thread.
fun pause() {
playing = false
try {
gameThread.join()
} catch (e: InterruptedException) {
Log.e("Error:", "joining thread")
}
}
// If SpaceInvadersActivity is started then
// start our thread.
fun resume() {
playing = true
prepareLevel()
gameThread.start()
}
// The SurfaceView class implements onTouchListener
// So we can override this method and detect screen touches.
override fun onTouchEvent(motionEvent: MotionEvent): Boolean {
return true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment