Skip to content

Instantly share code, notes, and snippets.

View EliteIntegrity's full-sized avatar

Game Code School EliteIntegrity

View GitHub Profile
@EliteIntegrity
EliteIntegrity / KotlinInvadersActivity.kt
Last active January 12, 2020 03:20
The completed KotlinInvadersActivity class
package com.gamecodeschool.kotlininvaders
import android.app.Activity
import android.graphics.Point
import android.os.Bundle
class KotlinInvadersActivity : Activity() {
// kotlinInvadersView will be the view of the game
// It will also hold the logic of the game
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Last active March 5, 2019 10:00
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,
@EliteIntegrity
EliteIntegrity / PlayerShip.kt
Created March 5, 2019 09:14
This class represents the ship that the player controls
package com.gamecodeschool.kotlininvaders
import android.content.Context
import android.graphics.Bitmap
import android.graphics.RectF
import android.graphics.BitmapFactory
class PlayerShip(context: Context,
private val screenX: Int,
screenY: Int) {
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 09:21
Adding a new property to the KotlinInvaersView class shown with some context to make it clear where it goes
...
...
// A Canvas and a Paint object
private var canvas: Canvas = Canvas()
private val paint: Paint = Paint()
// The players ship
private var playerShip: PlayerShip = PlayerShip(context, size.x, size.y)
// The score
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 09:26
Update the player's ship each frame by calling its update function from the update function of KotlinInvadersView
private fun update(fps: Long) {
// Update the state of all the game objects
// Move the player's ship
playerShip.update(fps)
}
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 09:29
Here we add the code to draw the player's ship on each frame of animation inside the draw function
...
...
// Choose the brush color for drawing
paint.color = Color.argb(255, 0, 255, 0)
// Draw all the game objects here
// Now draw the player spaceship
canvas.drawBitmap(playerShip.bitmap, playerShip.position.left,
playerShip.position.top
, paint)
@EliteIntegrity
EliteIntegrity / Invader.kt
Created March 5, 2019 09:34
This is the code for the Invader class which will instantiate a whol army of for the player to shoot.
package com.gamecodeschool.kotlininvaders
import android.content.Context
import android.graphics.Bitmap
import android.graphics.RectF
import java.util.*
import android.graphics.BitmapFactory
class Invader(context: Context, row: Int, column: Int, screenX: Int, screenY: Int) {
// How wide, high and spaced out are the invader will be
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 09:40
Add two properties to KotlinInvadersView to represent and control the numbers of the invaders
...
...
// The players ship
private var playerShip: PlayerShip = PlayerShip(context, size.x, size.y)
// Some Invaders
private val invaders = ArrayList<Invader>()
private var numInvaders = 0
// The score
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 09:42
Initialize all the Invader instances in the prepareLevel function
private fun prepareLevel() {
// Here we will initialize the game objects
// Build an army of invaders
Invader.numberOfInvaders = 0
numInvaders = 0
for (column in 0..10) {
for (row in 0..5) {
invaders.add(Invader(context,
row,
column,
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Last active March 5, 2019 12:17
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