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 11:07
Add the code to update the bullets, each frame, in the update function. Add the call just before the code that checks wether an invader bumped into the edge of the screen.
...
...
// Update the players playerBullet if active
if (playerBullet.isActive) {
playerBullet.update(fps)
}
// Update all the invaders bullets if active
for (bullet in invadersBullets) {
if (bullet.isActive) {
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:03
Initialize the invader's bullets in the prepareLevel function just after we initialized the shelters
...
...
// Initialize the invadersBullets array
for (i in 0 until maxInvaderBullets) {
invadersBullets.add(Bullet(size.y))
}
...
...
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 11:00
Declare some properties for the bullets for the player and the invaders
...
...
// The player's shelters are built from bricks
private val bricks = ArrayList<DefenceBrick>()
private var numBricks: Int = 0
// The player's playerBullet
// much faster and half the length
// compared to invader's bullet
private var playerBullet = Bullet(size.y, 1200f, 40f)
@EliteIntegrity
EliteIntegrity / Bullet.kt
Created March 5, 2019 10:55
This is the Buulet class. The player will reuse a single Bullet instance and the Invaders will reuse an ArrayList of instances between them.
package com.gamecodeschool.kotlininvaders
import android.graphics.RectF
class Bullet(screenY: Int,
private val speed: Float = 350f,
heightModifier: Float = 20f) {
val position = RectF()
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 10:49
Add the code which draws the bricks in the draw function. Add it just after the code which draws the invaders.
// Draw the bricks if visible
for (brick in bricks) {
if (brick.isVisible) {
canvas.drawRect(brick.position, paint)
}
}
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 10:46
Add the code to initialize the bricks in the prepareLevel function. Add it just after the code which initializes the invaders.
// Build the shelters
numBricks = 0
for (shelterNumber in 0..4) {
for (column in 0..18) {
for (row in 0..8) {
bricks.add(DefenceBrick(row,
column,
shelterNumber,
size.x,
size.y))
@EliteIntegrity
EliteIntegrity / KotlinInvadersView.kt
Created March 5, 2019 10:43
Add two properties for the bricks
...
...
// Some Invaders
private val invaders = ArrayList<Invader>()
private var numInvaders = 0
// The player's shelters are built from bricks
private val bricks = ArrayList<DefenceBrick>()
private var numBricks: Int = 0
@EliteIntegrity
EliteIntegrity / DefenceBrick.kt
Created March 5, 2019 10:18
This is the class which we will use dozens of instances to create destructable barriers for the player to hide behind
package com.gamecodeschool.kotlininvaders
import android.graphics.RectF
class DefenceBrick(row: Int, column: Int, shelterNumber: Int, screenX: Int, screenY: Int) {
var isVisible = true
private val width = screenX / 180
@EliteIntegrity
EliteIntegrity / AndroidManifest.xml
Created March 5, 2019 10:13
The complete file contents for context
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gamecodeschool.kotlininvaders">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
@EliteIntegrity
EliteIntegrity / AndroidManifest.xml
Created March 5, 2019 10:11
This is the code to add to the manifest to make the game fullscreen and lock it to the landscape orientation
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:screenOrientation="landscape"