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.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 |
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.SharedPreferences | |
import android.graphics.* | |
import android.view.SurfaceView | |
import android.util.Log | |
import android.view.MotionEvent | |
class KotlinInvadersView(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
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) { |
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
... | |
... | |
// 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 |
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 update(fps: Long) { | |
// Update the state of all the game objects | |
// Move the player's ship | |
playerShip.update(fps) | |
} |
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
... | |
... | |
// 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) |
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.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 |
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 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 |
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 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, |
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 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 |
OlderNewer