├── Chapter01
├── HelloWorldActivity.kt
├── MainActivity.kt
├── README.txt
├── content_hello_world.xml
└── content_main.xml
├── Chapter02
├── HelloWorldActivity.kt
├── MainActivity.kt
├── content_hello_world.xml
└── content_main.xml
├── Chapter03
└── README.txt
├── Chapter04
└── LayoutExploration
│ ├── java
│ └── MainActivity.kt
│ └── layout
│ ├── activity_main.xml
│ ├── main_menu.xml
│ └── my_table_layout.xml
├── Chapter05
└── CardViewLayout
│ ├── drawable
│ ├── image_1.png
│ ├── image_2.png
│ └── image_3.png
│ ├── java
│ └── MainActivity.kt
│ ├── layout
│ ├── card_contents_1.xml
│ ├── card_contents_2.xml
│ ├── card_contents_3.xml
│ └── main_layout.xml
│ └── values
│ └── dimens.xml
├── Chapter06
└── Lifecycle Demo
│ └── MainActivity.kt
├── Chapter07
└── MainActivity.kt
├── Chapter08
├── Loops Demo
│ └── MainActivity.kt
└── When Demo
│ └── MainActivity.kt
├── Chapter09
└── MainActivity.kt
├── Chapter10
├── Basic Classes
│ ├── Carrier.kt
│ ├── Destroyer.kt
│ ├── MainActivity.kt
│ └── ShipYard.kt
└── Chapter Example Classes
│ ├── AccessTester.kt
│ ├── Book.kt
│ ├── MainActivity.kt
│ ├── Meeting.kt
│ ├── SatelliteController.kt
│ └── Soldier.kt
├── Chapter11
├── Basic Classes with Inheritance
│ ├── Carrier.kt
│ ├── Destroyer.kt
│ ├── MainActivity.kt
│ ├── Ship.kt
│ └── ShipYard.kt
└── Chapter examples
│ ├── MainActivity.kt
│ ├── Paratrooper.kt
│ ├── Sniper.kt
│ ├── Soldier.kt
│ └── SpecialForces.kt
├── Chapter12
└── Kotlin meet UI
│ ├── MainActivity.kt
│ └── activity_main.xml
├── Chapter13
└── Widget Exploration
│ ├── MainActivity.kt
│ └── exploration_layout.xml
├── Chapter14
├── Dialog Demo
│ ├── MainActivity.kt
│ ├── MyDialog.kt
│ └── activity_main.xml
└── Note to self
│ ├── java
│ ├── DialogNewNote.kt
│ ├── DialogShowNote.kt
│ ├── MainActivity.kt
│ └── Note.kt
│ ├── layout
│ ├── activity_main.xml
│ ├── content_main.xml
│ ├── dialog_new_note.xml
│ └── dialog_show_note.xml
│ └── values
│ └── strings.xml
├── Chapter15
├── Dynamic Array Example
│ └── MainActivity.kt
└── Simple Array Example
│ └── MainActivity.kt
├── Chapter16
└── Note to self
│ ├── java
│ ├── DialogNewNote.kt
│ ├── DialogShowNote.kt
│ ├── MainActivity.kt
│ ├── Note.kt
│ └── NoteAdapter.kt
│ ├── layout
│ ├── activity_main.xml
│ ├── content_main.xml
│ ├── dialog_new_note.xml
│ ├── dialog_show_note.xml
│ └── listitem.xml
│ └── values
│ └── strings.xml
├── Chapter17
└── Note to self
│ ├── java
│ ├── DialogNewNote.kt
│ ├── DialogShowNote.kt
│ ├── JSONSerializer.kt
│ ├── MainActivity.kt
│ ├── Note.kt
│ ├── NoteAdapter.kt
│ └── SettingsActivity.kt
│ ├── layout
│ ├── activity_main.xml
│ ├── activity_settings.xml
│ ├── content_main.xml
│ ├── dialog_new_note.xml
│ ├── dialog_show_note.xml
│ └── listitem.xml
│ └── values
│ └── strings.xml
├── Chapter18
├── java
│ ├── DialogNewNote.kt
│ ├── DialogShowNote.kt
│ └── readme.txt
└── res
│ ├── strings.xml
│ ├── values-de
│ └── strings.xml
│ └── values-es
│ └── strings.xml
├── Chapter19
├── MainActivity.kt
└── anim
│ ├── bounce.xml
│ ├── fade_in.xml
│ ├── fade_in_out.xml
│ ├── fade_out.xml
│ ├── flash.xml
│ ├── left_right.xml
│ ├── right_left.xml
│ ├── rotate_left.xml
│ ├── rotate_right.xml
│ ├── top_bot.xml
│ ├── zoom_in.xml
│ └── zoom_out.xml
├── Chapter20
├── Bitmap Manipulation
│ ├── MainActivity.kt
│ └── drawable
│ │ └── bob.png
└── Canvas Demo
│ └── MainActivity.kt
├── Chapter21
├── LiveDrawingView.kt
├── MainActivity.kt
├── Particle.kt
└── ParticleSystem.kt
├── Chapter22
├── LiveDrawingView.kt
├── MainActivity.kt
├── Particle.kt
└── ParticleSystem.kt
├── Chapter23
└── Sound Demo
│ ├── assets
│ ├── fx1.ogg
│ ├── fx2.ogg
│ └── fx3.ogg
│ ├── java
│ └── MainActivity.kt
│ ├── layout
│ ├── activity_main.xml
│ └── content_main.xml
│ └── values
│ └── strings.xml
├── Chapter24
├── Device Detection
│ ├── MainActivity.kt
│ ├── layout-land
│ │ └── activity_main.xml
│ └── layout
│ │ └── activity_main.xml
└── Simple Fragment
│ ├── java
│ ├── MainActivity.kt
│ └── SimpleFragment.kt
│ └── layout
│ ├── activity_main.xml
│ └── fragment_layout.xml
├── Chapter25
├── Fragment Pager
│ ├── java
│ │ ├── MainActivity.kt
│ │ └── SimpleFragment.kt
│ └── layout
│ │ ├── activity_main.xml
│ │ └── fragment_layout.xml
└── Image Pager
│ ├── drawable
│ ├── ic_launcher_background.xml
│ ├── image1.png
│ ├── image2.png
│ ├── image3.png
│ ├── image4.png
│ ├── image5.png
│ └── image6.png
│ ├── java
│ ├── ImagePagerAdapter.kt
│ └── MainActivity.kt
│ └── layout
│ ├── activity_main.xml
│ └── pager_item.xml
├── Chapter26
├── java
│ ├── DeleteFragment.kt
│ ├── InsertFragment.kt
│ ├── MainActivity.kt
│ ├── ResultsFragment.kt
│ └── SearchFragment.kt
├── layout
│ ├── activity_main.xml
│ ├── app_bar_main.xml
│ ├── content_delete.xml
│ ├── content_insert.xml
│ ├── content_main.xml
│ ├── content_results.xml
│ ├── content_search.xml
│ └── nav_header_main.xml
└── menu
│ ├── activity_main_drawer.xml
│ └── main.xml
├── Chapter27
├── java
│ ├── DataManager.kt
│ ├── DeleteFragment.kt
│ ├── InsertFragment.kt
│ ├── MainActivity.kt
│ ├── ResultsFragment.kt
│ └── SearchFragment.kt
└── layout
│ ├── activity_main.xml
│ ├── app_bar_main.xml
│ ├── content_delete.xml
│ ├── content_insert.xml
│ ├── content_main.xml
│ ├── content_results.xml
│ ├── content_search.xml
│ └── nav_header_main.xml
├── LICENSE
└── README.md
/Chapter01/HelloWorldActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.helloworld
2 |
3 | import android.os.Bundle
4 | import com.google.android.material.snackbar.Snackbar
5 | import androidx.appcompat.app.AppCompatActivity
6 | import android.view.Menu
7 | import android.view.MenuItem
8 |
9 | import kotlinx.android.synthetic.main.activity_hello_world.*
10 |
11 | class HelloWorldActivity : AppCompatActivity() {
12 |
13 | override fun onCreate(savedInstanceState: Bundle?) {
14 | super.onCreate(savedInstanceState)
15 | setContentView(R.layout.activity_hello_world)
16 | setSupportActionBar(toolbar)
17 |
18 | fab.setOnClickListener { view ->
19 | Snackbar.make(view, "Replace with your own action",
20 | Snackbar.LENGTH_LONG)
21 | .setAction("Action", null).show()
22 | }
23 | }
24 |
25 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
26 | // Inflate the menu; this adds items to the action bar if it is present.
27 | menuInflater.inflate(R.menu.menu_hello_world, menu)
28 | return true
29 | }
30 |
31 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
32 | // Handle action bar item clicks here. The action bar will
33 | // automatically handle clicks on the Home/Up button, so long
34 | // as you specify a parent activity in AndroidManifest.xml.
35 | return when (item.itemId) {
36 | R.id.action_settings -> true
37 | else -> super.onOptionsItemSelected(item)
38 | }
39 | }
40 | }
41 |
42 |
--------------------------------------------------------------------------------
/Chapter01/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.helloworld
2 |
3 | import android.os.Bundle
4 | import com.google.android.material.snackbar.Snackbar
5 | import androidx.appcompat.app.AppCompatActivity
6 | import android.view.Menu
7 | import android.view.MenuItem
8 |
9 | import kotlinx.android.synthetic.main.activity_hello_world.*
10 |
11 | class MainActivity : AppCompatActivity() {
12 |
13 | override fun onCreate(savedInstanceState: Bundle?) {
14 | super.onCreate(savedInstanceState)
15 | setContentView(R.layout.activity_main)
16 | setSupportActionBar(toolbar)
17 |
18 | fab.setOnClickListener { view ->
19 | Snackbar.make(view, "Replace with your own action",
20 | Snackbar.LENGTH_LONG)
21 | .setAction("Action", null).show()
22 | }
23 | }
24 |
25 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
26 | // Inflate the menu; this adds items to the action bar if it is present.
27 | menuInflater.inflate(R.menu.menu_hello_world, menu)
28 | return true
29 | }
30 |
31 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
32 | // Handle action bar item clicks here. The action bar will
33 | // automatically handle clicks on the Home/Up button, so long
34 | // as you specify a parent activity in AndroidManifest.xml.
35 | return when (item.itemId) {
36 | R.id.action_settings -> true
37 | else -> super.onOptionsItemSelected(item)
38 | }
39 | }
40 | }
41 |
42 |
--------------------------------------------------------------------------------
/Chapter01/README.txt:
--------------------------------------------------------------------------------
1 | Copy & pasting this code will not work. It is provided here for reference only
--------------------------------------------------------------------------------
/Chapter01/content_hello_world.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
19 |
20 |
--------------------------------------------------------------------------------
/Chapter01/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
19 |
20 |
--------------------------------------------------------------------------------
/Chapter02/HelloWorldActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.helloworld
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import com.google.android.material.snackbar.Snackbar
6 | import androidx.appcompat.app.AppCompatActivity
7 | import android.view.Menu
8 | import android.view.MenuItem
9 | import android.view.View
10 | import android.widget.Toast
11 |
12 | import kotlinx.android.synthetic.main.activity_hello_world.*
13 |
14 | class HelloWorldActivity : AppCompatActivity() {
15 |
16 | override fun onCreate(savedInstanceState: Bundle?) {
17 | super.onCreate(savedInstanceState)
18 | setContentView(R.layout.activity_hello_world)
19 | setSupportActionBar(toolbar)
20 |
21 | fab.setOnClickListener { view ->
22 | Snackbar.make(view, "Replace with your own action",
23 | Snackbar.LENGTH_LONG)
24 | .setAction("Action", null).show()
25 | }
26 |
27 | // Your code goes here
28 | Toast.makeText(this, "Can you see me?",
29 | Toast.LENGTH_SHORT).show()
30 |
31 | Log.i("info", "Done creating the app")
32 |
33 | }
34 |
35 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
36 | // Inflate the menu; this adds items to the action bar if it is present.
37 | menuInflater.inflate(R.menu.menu_hello_world, menu)
38 | return true
39 | }
40 |
41 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
42 | // Handle action bar item clicks here. The action bar will
43 | // automatically handle clicks on the Home/Up button, so long
44 | // as you specify a parent activity in AndroidManifest.xml.
45 | return when (item.itemId) {
46 | R.id.action_settings -> true
47 | else -> super.onOptionsItemSelected(item)
48 | }
49 | }
50 |
51 | fun topClick(v: View) {
52 | Toast.makeText(this, "Top button clicked",
53 | Toast.LENGTH_SHORT).show()
54 |
55 | Log.i("info", "The user clicked the top button")
56 | }
57 |
58 | fun bottomClick(v: View) {
59 | Toast.makeText(this, "Bottom button clicked",
60 | Toast.LENGTH_SHORT).show()
61 |
62 | Log.i("info", "The user clicked the bottom button")
63 | }
64 |
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/Chapter02/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.helloworld
2 |
3 | import android.os.Bundle
4 | import android.util.Log
5 | import com.google.android.material.snackbar.Snackbar
6 | import androidx.appcompat.app.AppCompatActivity
7 | import android.view.Menu
8 | import android.view.MenuItem
9 | import android.view.View
10 | import android.widget.Toast
11 |
12 | import kotlinx.android.synthetic.main.activity_hello_world.*
13 |
14 | class MainActivity : AppCompatActivity() {
15 |
16 | override fun onCreate(savedInstanceState: Bundle?) {
17 | super.onCreate(savedInstanceState)
18 | setContentView(R.layout.activity_main)
19 | setSupportActionBar(toolbar)
20 |
21 | fab.setOnClickListener { view ->
22 | Snackbar.make(view, "Replace with your own action",
23 | Snackbar.LENGTH_LONG)
24 | .setAction("Action", null).show()
25 | }
26 |
27 | // Your code goes here
28 | Toast.makeText(this, "Can you see me?",
29 | Toast.LENGTH_SHORT).show()
30 |
31 | Log.i("info", "Done creating the app")
32 |
33 | }
34 |
35 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
36 | // Inflate the menu; this adds items to the action bar if it is present.
37 | menuInflater.inflate(R.menu.menu_hello_world, menu)
38 | return true
39 | }
40 |
41 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
42 | // Handle action bar item clicks here. The action bar will
43 | // automatically handle clicks on the Home/Up button, so long
44 | // as you specify a parent activity in AndroidManifest.xml.
45 | return when (item.itemId) {
46 | R.id.action_settings -> true
47 | else -> super.onOptionsItemSelected(item)
48 | }
49 | }
50 |
51 | fun topClick(v: View) {
52 | Toast.makeText(this, "Top button clicked",
53 | Toast.LENGTH_SHORT).show()
54 |
55 | Log.i("info", "The user clicked the top button")
56 | }
57 |
58 | fun bottomClick(v: View) {
59 | Toast.makeText(this, "Bottom button clicked",
60 | Toast.LENGTH_SHORT).show()
61 |
62 | Log.i("info", "The user clicked the bottom button")
63 | }
64 |
65 | }
66 |
67 |
--------------------------------------------------------------------------------
/Chapter02/content_hello_world.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
20 |
21 |
31 |
32 |
33 |
42 |
43 |
--------------------------------------------------------------------------------
/Chapter02/content_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
20 |
21 |
31 |
32 |
33 |
42 |
43 |
--------------------------------------------------------------------------------
/Chapter03/README.txt:
--------------------------------------------------------------------------------
1 | All the code in chapter 3 is auto-generated in Android Studio
--------------------------------------------------------------------------------
/Chapter04/LayoutExploration/java/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.exploringlayouts
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.view.View
6 |
7 | class MainActivity : AppCompatActivity() {
8 |
9 | override fun onCreate(savedInstanceState: Bundle?) {
10 | super.onCreate(savedInstanceState)
11 | setContentView(R.layout.main_menu);
12 | }
13 |
14 | fun loadConstraintLayout(v: View) {
15 | setContentView(R.layout.activity_main)
16 | }
17 |
18 | fun loadTableLayout(v: View) {
19 | setContentView(R.layout.my_table_layout)
20 | }
21 |
22 | fun loadMenuLayout(v: View) {
23 | setContentView(R.layout.main_menu)
24 | }
25 |
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter04/LayoutExploration/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
11 |
26 |
27 |
40 |
41 |
59 |
60 |
--------------------------------------------------------------------------------
/Chapter04/LayoutExploration/layout/main_menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
13 |
14 |
23 |
24 |
28 |
29 |
37 |
38 |
45 |
46 |
47 |
51 |
52 |
60 |
61 |
68 |
69 |
70 |
77 |
78 |
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/drawable/image_1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Android-Programming-with-Kotlin-for-Beginners/82edaf072bfa00675fd58fda7e04e6ad0f5a0448/Chapter05/CardViewLayout/drawable/image_1.png
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/drawable/image_2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Android-Programming-with-Kotlin-for-Beginners/82edaf072bfa00675fd58fda7e04e6ad0f5a0448/Chapter05/CardViewLayout/drawable/image_2.png
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/drawable/image_3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/Android-Programming-with-Kotlin-for-Beginners/82edaf072bfa00675fd58fda7e04e6ad0f5a0448/Chapter05/CardViewLayout/drawable/image_3.png
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/java/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.cardviewlayout
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 |
6 | class MainActivity : AppCompatActivity() {
7 |
8 | override fun onCreate(savedInstanceState: Bundle?) {
9 | super.onCreate(savedInstanceState)
10 | setContentView(R.layout.main_layout);
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/layout/card_contents_1.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
24 |
25 |
32 |
33 |
41 |
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/layout/card_contents_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
24 |
25 |
32 |
33 |
41 |
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/layout/card_contents_3.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
24 |
25 |
32 |
33 |
41 |
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/layout/main_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
14 |
15 |
22 |
23 |
24 |
25 |
26 |
27 |
34 |
35 |
36 |
37 |
38 |
39 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
--------------------------------------------------------------------------------
/Chapter05/CardViewLayout/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 10dp
5 |
6 |
--------------------------------------------------------------------------------
/Chapter06/Lifecycle Demo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.lifecycledemo
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.util.Log
6 | import android.widget.Toast
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | override fun onCreate(savedInstanceState: Bundle?) {
11 | super.onCreate(savedInstanceState)
12 | setContentView(R.layout.activity_main)
13 |
14 | Toast.makeText(this, "In onCreate",
15 | Toast.LENGTH_SHORT).show()
16 |
17 | Log.i("info", "In onCreate")
18 |
19 | }
20 |
21 | override fun onStart() {
22 | // First call the “official” version of this method
23 | super.onStart();
24 |
25 | Toast.makeText(this, "In onStart",
26 | Toast.LENGTH_SHORT).show();
27 |
28 | Log.i("info", "In onStart");
29 | }
30 |
31 | override fun onResume() {
32 | // First call the "official” version of this method
33 | super.onResume();
34 |
35 | Toast.makeText(this, "In onResume",
36 | Toast.LENGTH_SHORT).show();
37 |
38 | Log.i("info", "In onResume");
39 | }
40 |
41 | override fun onPause() {
42 | // First call the "official” version of this method
43 | super.onPause();
44 |
45 | Toast.makeText(this, "In onPause",
46 | Toast.LENGTH_SHORT).show();
47 |
48 | Log.i("info", "In onPause");
49 | }
50 |
51 | override fun onStop() {
52 | // First call the "official" version of this method
53 | super.onStop();
54 |
55 | Toast.makeText(this, "In onStop",
56 | Toast.LENGTH_SHORT).show();
57 |
58 | Log.i("info", "In onStop");
59 | }
60 |
61 | override fun onDestroy() {
62 | // First call the "official” version of this method
63 | super.onDestroy();
64 |
65 | Toast.makeText(this, "In onDestroy",
66 | Toast.LENGTH_SHORT).show();
67 |
68 | Log.i("info", "In onDestroy");
69 | }
70 |
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/Chapter07/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.expressyourself
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.util.Log
6 | import android.widget.Toast
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | override fun onCreate(savedInstanceState: Bundle?) {
11 | super.onCreate(savedInstanceState)
12 | setContentView(R.layout.activity_main)
13 |
14 | val name = "Nolan Bushnell"
15 | val yearOfBirth = 1943
16 | var currentYear = 2019
17 | var age: Int
18 |
19 | age = currentYear - yearOfBirth
20 | Log.i("info", "$age")
21 | currentYear ++;
22 | Log.i("info","$name was born in $yearOfBirth and is $age years old. Next year he will be ${currentYear - yearOfBirth} years old)")
23 |
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/Chapter08/Loops Demo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.loopsdemo
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.util.Log
6 |
7 | class MainActivity : AppCompatActivity() {
8 |
9 | override fun onCreate(savedInstanceState: Bundle?) {
10 | super.onCreate(savedInstanceState)
11 |
12 | var y = 10
13 | do {
14 | y++
15 | Log.i("In the do block and y=","$y")
16 | }
17 | while(y < 10)
18 |
19 | // We could do this...
20 | // val list = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
21 | // It is much quicker to do this...
22 | val list = 1..10
23 | for (i in list)
24 | Log.i("Looping through list","Current value is $i")
25 |
26 |
27 |
28 |
29 | var countDown = 10
30 | while(countDown > 0){
31 |
32 | if(countDown == 5)break
33 |
34 | Log.i("countDown =","$countDown")
35 | countDown --
36 | }
37 |
38 | var countUp = 0
39 | while(countUp < 10){
40 | countUp++
41 |
42 | if(countUp > 5)continue
43 |
44 | Log.i("Inside loop","countUp = $countUp")
45 | }
46 |
47 | Log.i("Outside loop","countUp = $countUp")
48 |
49 |
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter08/When Demo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.whendemo
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.util.Log
6 |
7 | class MainActivity : AppCompatActivity() {
8 |
9 | override fun onCreate(savedInstanceState: Bundle?) {
10 | super.onCreate(savedInstanceState)
11 | setContentView(R.layout.activity_main)
12 |
13 |
14 | val time = 9
15 |
16 | val amOrPM = if (time < 12) {
17 | "am"
18 | } else {
19 | "pm"
20 | }
21 |
22 | Log.i("It is ", amOrPM)
23 |
24 | val weight = 30
25 | val instruction = "Put this bag in the ${if (weight >= 25) "hold" else "cabin"}"
26 | Log.i("instruction is ", instruction)
27 |
28 | var isComingOverTheBridge: Boolean
29 | var enemyTroops: Int
30 | var friendlyTroops: Int
31 | var wavingWhiteFlag: Boolean
32 |
33 | isComingOverTheBridge = true;
34 | enemyTroops = 10;
35 | friendlyTroops = 5;
36 | wavingWhiteFlag = false;
37 |
38 |
39 | if (!wavingWhiteFlag) {
40 |
41 | // not surrendering so check everything else
42 |
43 | if (isComingOverTheBridge && friendlyTroops >= enemyTroops) {
44 |
45 | // shoot them
46 | } else if (isComingOverTheBridge && friendlyTroops <
47 | enemyTroops) {
48 |
49 | // blow the bridge
50 |
51 | }
52 |
53 | } else {
54 |
55 | // this is the else for our first if
56 | // Take prisoners
57 |
58 | }
59 |
60 | // Holding position
61 |
62 | // Change the value that rating is initialized to between 1 and 5
63 | val rating:Int = 4
64 | when (rating) {
65 | 1 -> Log.i("Oh dear! Rating = ", "$rating stars")
66 | 2 -> Log.i("Not good! Rating = ", "$rating stars")
67 | 3 -> Log.i("Not bad! Rating = ", "$rating stars")
68 | 4 -> Log.i("This is good! Rating = ", "$rating stars")
69 | 5 -> Log.i("Amazing! Rating = ", "$rating stars")
70 |
71 | else -> {
72 | print("Error - Invalid rating")
73 | Log.i("Error:", "$rating is not a valid rating")
74 | }
75 | }
76 |
77 | // Enter an ocean, river or breed of dog
78 | val name:String = "Nile"
79 | when (name) {
80 | "Atlantic","Pacific", "Arctic" ->
81 | Log.i("Found:", "$name is an ocean")
82 |
83 | "Thames","Nile", "Mississippi" ->
84 | Log.i("Found:", "$name is a river")
85 |
86 | "Labrador","Beagle", "Jack Russel" ->
87 | Log.i("Found:", "$name is a dog")
88 |
89 | else -> {
90 | Log.i("Not found:", "$name is not in database")
91 | }
92 | }
93 |
94 |
95 |
96 | }
97 |
98 |
99 |
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/Chapter09/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.functiondemo
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.util.Log
6 |
7 | class MainActivity : AppCompatActivity() {
8 |
9 | override fun onCreate(savedInstanceState: Bundle?) {
10 | super.onCreate(savedInstanceState)
11 |
12 | val firstName = "Gabe"
13 | val secondName = "Newell"
14 |
15 | // Call function using literal String
16 | printName("Sid","Meier")
17 |
18 | // Call using String variables
19 | printName(firstName, secondName)
20 |
21 | // If a circle has a radius of 3
22 | // What is the area
23 | printAreaCircle(3f)
24 |
25 | // Returned value is discarded
26 | getSum(10, 10)
27 |
28 | // Returned value is stored in answer
29 | val answer = getSum(10, 10)
30 |
31 | // Print out the returned value
32 | Log.i("Returned value =","${getSum(10, 10)}")
33 |
34 | // Use the returned value in another function call
35 | printAreaCircle(getSum(1,2).toFloat())
36 |
37 | Log.i("Returned area =","${getAreaCircle(3f)}")
38 | Log.i("Returned name =","${getName("Alan","Turing")}")
39 |
40 | printUpTo3(1)
41 | printUpTo3(2)
42 | printUpTo3(3)
43 | printUpTo3(4)
44 |
45 | orderProduct(product ="Beer")
46 | orderProduct(true, product ="Porsche")
47 | orderProduct(true, product ="Barbie (Jet-Set Edition)", postalService = "Next Day")
48 | orderProduct(false, product ="Flat-pack bookcase", postalService = "Carrier Pigeon")
49 | }
50 |
51 | fun printName(first: String, second:String){
52 | Log.i("Joined Name =","$first $second")
53 | }
54 |
55 | fun printAreaCircle(radius: Float){
56 | Log.i("Area =","${3.14 * (radius *radius)}")
57 | }
58 |
59 | fun getSum(a: Int, b: Int): Int {
60 | return a + b
61 | }
62 |
63 | fun getAreaCircle(radius: Float): Float{
64 | return 3.14f * (radius * radius)
65 | }
66 |
67 | fun getName(first: String, second: String): String{
68 | return "$first $second"
69 | }
70 |
71 | fun printUpTo3(aNumber: Int){ // No return type!
72 | if(aNumber > 3){
73 | Log.i("aNumber is","TOO BIG! - Didn't you read my name")
74 | return // Going back to the calling code
75 | }
76 |
77 | Log.i("aNumber is","$aNumber")
78 | }
79 |
80 | fun orderProduct(giftWrap: Boolean = false,
81 | product: String,
82 | postalService: String = "Standard") {
83 |
84 | var details: String =""
85 |
86 | if (giftWrap) {
87 | details += "Gift wrapped "
88 | }
89 |
90 | details += "$product "
91 | details += "by $postalService postage"
92 |
93 | Log.i("Product details",details)
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/Chapter10/Basic Classes/Carrier.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | import android.util.Log
4 |
5 | class Carrier (name: String){
6 | // What is the name of this ship
7 | var name: String = ""
8 | private set
9 |
10 | // What type of ship is it
11 | // Always a destroyer
12 | val type = "Carrier"
13 |
14 | // How much the ship can take before sinking
15 | private var hullIntegrity = 100
16 |
17 | // How many shots left in the arsenal
18 | var attacksRemaining = 1
19 | // Cannot be directly set externally
20 | private set
21 |
22 | // No external access whatsoever
23 | private var attackPower = 120
24 |
25 | // Has the ship been sunk
26 | private var sunk = false
27 |
28 | // This code runs as the instance is being initialized
29 | init {
30 | // So we can use the name parameter
31 | this.name = "$type $name"
32 | }
33 |
34 | // Anything call use this function
35 | fun takeDamage(damageTaken: Int) {
36 | if (!sunk) {
37 | hullIntegrity -= damageTaken
38 | Log.d("$name damage taken =","$damageTaken")
39 | Log.d("$name hull integrity =","$hullIntegrity")
40 |
41 | if (hullIntegrity <= 0) {
42 | Log.d("Carrier", "$name has been sunk")
43 | sunk = true
44 | }
45 | } else {
46 | // Already sunk
47 | Log.d("Error", "Ship does not exist")
48 | }
49 | }
50 |
51 | // Return a different value dependent
52 | // on the when expression
53 | fun launchAerialAttack() :Int {
54 | // Let the calling code no how much damage to do
55 | return if (attacksRemaining > 0) {
56 | attacksRemaining--
57 | attackPower
58 | }else{
59 | 0
60 | }
61 | }
62 |
63 | fun serviceShip() {
64 | attacksRemaining = 20
65 | hullIntegrity = 200
66 | }
67 | }
--------------------------------------------------------------------------------
/Chapter10/Basic Classes/Destroyer.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | import android.util.Log
4 |
5 | class Destroyer(name: String) {
6 | // What is the name of this ship
7 | var name: String = ""
8 | private set
9 |
10 | // What type of ship is it
11 | // Always a destroyer
12 | val type = "Destroyer"
13 |
14 | // How much the ship can take before sinking
15 | private var hullIntegrity = 200
16 |
17 | // How many shots left in the arsenal
18 | var ammo = 1
19 | // Cannot be directly set externally
20 | private set
21 |
22 | // No external access whatsoever
23 | private var shotPower = 60
24 |
25 | // Has the ship been sunk
26 | private var sunk = false
27 |
28 | // This code runs as the instance is being initialized
29 | init {
30 | // So we can use the name parameter
31 | this.name = "$type $name"
32 | }
33 |
34 | // Anything can use this function
35 | fun takeDamage(damageTaken: Int) {
36 | if (!sunk) {
37 | hullIntegrity -= damageTaken
38 | Log.d("$name damage taken =","$damageTaken")
39 | Log.d("$name hull integrity =","$hullIntegrity")
40 |
41 | if (hullIntegrity <= 0) {
42 | Log.d("Destroyer", "$name has been sunk")
43 | sunk = true
44 | }
45 | } else {
46 | // Already sunk
47 | Log.d("Error", "Ship does not exist")
48 | }
49 | }
50 |
51 | // Return a different value dependent
52 | // on the when expression
53 | fun shootShell():Int {
54 | // Let the calling code no how much damage to do
55 | return if (ammo > 0) {
56 | ammo--
57 | shotPower
58 | }else{
59 | 0
60 | }
61 | }
62 |
63 | fun serviceShip() {
64 | ammo = 10
65 | hullIntegrity = 100
66 | }
67 |
68 | }
--------------------------------------------------------------------------------
/Chapter10/Basic Classes/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.util.Log
6 |
7 | class MainActivity : AppCompatActivity() {
8 |
9 | override fun onCreate(savedInstanceState: Bundle?) {
10 | super.onCreate(savedInstanceState)
11 | setContentView(R.layout.activity_main)
12 |
13 | val friendlyDestroyer = Destroyer("Invincible")
14 | val friendlyCarrier = Carrier("Indomitable")
15 |
16 | val enemyDestroyer = Destroyer("Grey Death")
17 | val enemyCarrier = Carrier("Big Grey Death")
18 |
19 | val friendlyShipyard = ShipYard()
20 |
21 | // A small battle
22 | friendlyDestroyer.takeDamage(enemyDestroyer.shootShell())
23 | friendlyDestroyer.takeDamage(enemyCarrier.launchAerialAttack())
24 | enemyCarrier.takeDamage(friendlyCarrier.launchAerialAttack())
25 | enemyCarrier.takeDamage(friendlyDestroyer.shootShell())
26 |
27 | // Take stock of the supplies situation
28 | Log.d("${friendlyDestroyer.name} ammo = ",
29 | "${friendlyDestroyer.ammo}")
30 |
31 | Log.d("${friendlyCarrier.name} attacks = ",
32 | "${friendlyCarrier.attacksRemaining}")
33 |
34 | // Dock at the shipyard
35 | friendlyShipyard.serviceCarrier(friendlyCarrier)
36 | friendlyShipyard.serviceDestroyer(friendlyDestroyer)
37 |
38 | // Take stock of the supplies situation
39 | Log.d("${friendlyDestroyer.name} ammo = ",
40 | "${friendlyDestroyer.ammo}")
41 |
42 | Log.d("${friendlyCarrier.name} attacks = ",
43 | "${friendlyCarrier.attacksRemaining}")
44 |
45 |
46 | // Finish off the enemy
47 | enemyDestroyer.takeDamage(friendlyDestroyer.shootShell())
48 | enemyDestroyer.takeDamage(friendlyCarrier.launchAerialAttack())
49 | enemyDestroyer.takeDamage(friendlyDestroyer.shootShell())
50 |
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/Chapter10/Basic Classes/ShipYard.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | class ShipYard {
4 |
5 | fun serviceDestroyer(destroyer: Destroyer){
6 | destroyer.serviceShip()
7 | }
8 |
9 | fun serviceCarrier(carrier: Carrier){
10 | carrier.serviceShip()
11 | }
12 | }
--------------------------------------------------------------------------------
/Chapter10/Chapter Example Classes/AccessTester.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | class AccessTester {
4 |
5 |
6 |
7 |
8 | }
--------------------------------------------------------------------------------
/Chapter10/Chapter Example Classes/Book.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | class Book(val title: String, var copiesSold: Int) {
4 | // Here we put our code as normal
5 | // But title and copiesSold are already
6 | // declared and initialized
7 | }
--------------------------------------------------------------------------------
/Chapter10/Chapter Example Classes/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.util.Log
6 |
7 |
8 |
9 | class MainActivity : AppCompatActivity() {
10 |
11 | override fun onCreate(savedInstanceState: Bundle?) {
12 |
13 | super.onCreate(savedInstanceState)
14 |
15 | // First declare an instance of Soldier called soldier1
16 | val soldier1 = Soldier()
17 | soldier1.getStatus()
18 |
19 | // Now access and print each of the variables
20 | Log.i("Name =","${soldier1.name}")
21 | Log.i("Rank =","${soldier1.rank}")
22 | Log.i("Missing =","${soldier1.missing}")
23 |
24 | // Mission to rescue Private Ryan succeeds
25 | soldier1.missing = false;
26 |
27 | // Ryan behaved impeccably
28 | soldier1.rank = "Private First Class"
29 |
30 | // Now access and print each of the variables
31 | Log.i("Name =","${soldier1.name}")
32 | Log.i("Rank =","${soldier1.rank}")
33 | Log.i("Missing =","${soldier1.missing}")
34 |
35 | // In onCreate or some other function/class from our app
36 | // Create a new instance of the Soldier class
37 | val soldier = Soldier()
38 | // Access the value of bullets
39 | Log.i("bullets = ","${soldier.bullets}")// Code in getter will execute
40 | soldier.bullets--
41 | Log.i("bullets =","${soldier.bullets}")// Code in setter will execute
42 |
43 |
44 | // Create a soldier
45 | val strongSoldier = Soldier()
46 | // Print out the totalWeight value
47 | Log.i("totalWeight =","${strongSoldier.totalWeight}")
48 |
49 | // Change the value of packWeight
50 | strongSoldier.packWeight = 300
51 | // Print out the totalWeight value again
52 | Log.i("totalWeight =","${strongSoldier.totalWeight}")
53 |
54 |
55 |
56 | var satelliteController = SatelliteController()
57 |
58 |
59 |
60 |
61 |
62 |
63 | // This still doesn't work which is what we want
64 | // satelliteController.gpsCoordinates = "1.2345, 5.6789"
65 |
66 | // But this will print the gpsCoordinates
67 | Log.i("Coords=","$satelliteController.gpsCoordinates")
68 |
69 |
70 | // Instantiate a Book using the primary constructor
71 | val book = Book("Animal Farm", 20000000)
72 |
73 |
74 | // Book two meetings
75 | // First when we don't yet know the time
76 | val meeting = Meeting("Thursday", "Bob")
77 |
78 | // And secondly when we do know the time
79 | val anotherMeeting = Meeting("Wednesday","Dave","3 PM")
80 |
81 |
82 | }
83 | }
84 |
--------------------------------------------------------------------------------
/Chapter10/Chapter Example Classes/Meeting.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | // Perhaps the user of the class
4 | // doesn't know the time as it
5 | // is yet to be confirmed
6 | class Meeting(val day: String, val person: String) {
7 | var time: String = "To be decided"
8 | // The user of the class can
9 | // supply the day, time and person
10 | // of a meeting
11 | constructor(day: String, person: String, time: String)
12 | :this(day, person){
13 |
14 | // "this" refers to the current instance
15 | this.time = time
16 | // time (the property) now equals time
17 | // that was passed in as a parameter
18 | }
19 | }
--------------------------------------------------------------------------------
/Chapter10/Chapter Example Classes/SatelliteController.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | class SatelliteController {
4 | var gpsCoordinates = "51.331958,0.029057"
5 | private set
6 |
7 | private var bigProblem = false
8 |
9 | private fun dropOutOfTheSky() {
10 | }
11 |
12 | private fun doDiagnostics() {
13 | // etc
14 | }
15 |
16 |
17 | private fun recalibrateSensors(){
18 | // etc
19 | }
20 |
21 | fun updateCoordinates(){
22 | // Recalculate coordinates and update
23 | // the gpsCoordinates property
24 | gpsCoordinates = "21.123456, 2.654321"
25 |
26 | // user can now access the new coordinates
27 | // but still can't change them
28 | }
29 |
30 | fun runMaintenance(){
31 | if(bigProblem){
32 | dropOutOfTheSky()
33 | }
34 | else{
35 | doDiagnostics()
36 | recalibrateSensors()
37 | }
38 |
39 | }
40 | }
--------------------------------------------------------------------------------
/Chapter10/Chapter Example Classes/Soldier.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasses
2 |
3 | import android.util.Log
4 |
5 | class Soldier{
6 |
7 | // members
8 | var name = "Ryan"
9 | var rank = "Private"
10 | var missing = true
11 |
12 | var bullets = 100
13 | get() {
14 | Log.i("Getter being used","Value = $field")
15 | return field
16 | }
17 | set(value) {
18 | field = if (value < 0) 0 else value
19 | Log.i("Setter being used","New value = $field")
20 | }
21 |
22 | var packWeight = 150
23 | val gunWeight = 30
24 | var totalWeight = packWeight + gunWeight
25 | get() = packWeight + gunWeight
26 |
27 |
28 | // member function
29 | fun getStatus() {
30 | var status = "$rank $name"
31 | if(missing){
32 | status = "$status is missing!"
33 | }else{
34 | status = "$status ready for duty."
35 | }
36 |
37 | Log.i("Status",status)
38 | }
39 |
40 |
41 | }
--------------------------------------------------------------------------------
/Chapter11/Basic Classes with Inheritance/Carrier.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasseswithinheritance
2 |
3 | class Carrier (name: String): Ship(
4 | name,
5 | "Carrier",
6 | 20,
7 | 100){
8 |
9 | // No external access whatsoever
10 | private var attackPower = 120
11 |
12 | override fun attack(): Int {
13 | // Let the calling code no how much damage to do
14 | return if (attacksRemaining > 0) {
15 | attacksRemaining--
16 | attackPower
17 | }else{
18 | 0
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/Chapter11/Basic Classes with Inheritance/Destroyer.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasseswithinheritance
2 |
3 | class Destroyer(name: String): Ship(
4 | name,
5 | "Destroyer",
6 | 10,
7 | 200) {
8 |
9 | // No external access whatsoever
10 | private var shotPower = 60
11 |
12 | override fun attack():Int {
13 | // Let the calling code no how much damage to do
14 | return if (attacksRemaining > 0) {
15 | attacksRemaining--
16 | shotPower
17 | }else{
18 | 0
19 | }
20 | }
21 | }
--------------------------------------------------------------------------------
/Chapter11/Basic Classes with Inheritance/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasseswithinheritance
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 |
6 | class MainActivity : AppCompatActivity() {
7 |
8 | override fun onCreate(savedInstanceState: Bundle?) {
9 | super.onCreate(savedInstanceState)
10 | setContentView(R.layout.activity_main)
11 |
12 | val friendlyDestroyer = Destroyer("Invincible")
13 | val friendlyCarrier = Carrier("Indomitable")
14 |
15 | val enemyDestroyer = Destroyer("Grey Death")
16 | val enemyCarrier = Carrier("Big Grey Death")
17 |
18 | val friendlyShipyard = ShipYard()
19 |
20 | // A small battle
21 | friendlyDestroyer.takeDamage(enemyDestroyer.attack())
22 | friendlyDestroyer.takeDamage(enemyCarrier.attack())
23 | enemyCarrier.takeDamage(friendlyCarrier.attack())
24 | enemyCarrier.takeDamage(friendlyDestroyer.attack())
25 |
26 | // Take stock of the supplies situation
27 | friendlyDestroyer.showStats()
28 | friendlyCarrier.showStats()
29 |
30 | // Dock at the shipyard
31 | friendlyShipyard.serviceShip(friendlyCarrier)
32 | friendlyShipyard.serviceShip(friendlyDestroyer)
33 |
34 | // Take stock of the supplies situation
35 | friendlyDestroyer.showStats()
36 | friendlyCarrier.showStats()
37 |
38 | // Finish off the enemy
39 | enemyDestroyer.takeDamage(friendlyDestroyer.attack())
40 | enemyDestroyer.takeDamage(friendlyCarrier.attack())
41 | enemyDestroyer.takeDamage(friendlyDestroyer.attack())
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter11/Basic Classes with Inheritance/Ship.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasseswithinheritance
2 |
3 | import android.util.Log
4 |
5 | abstract class Ship(
6 | val name: String,
7 | private var type: String,
8 | private val maxAttacks: Int,
9 | private val maxHullIntegrity: Int) {
10 |
11 | // The stats that all ships have
12 | private var sunk = false
13 | private var hullIntegrity: Int
14 | protected var attacksRemaining: Int
15 |
16 | init{
17 | hullIntegrity = this.maxHullIntegrity
18 | attacksRemaining = 1
19 | }
20 |
21 | // Anything can use this function
22 | fun takeDamage(damageTaken: Int) {
23 | if (!sunk) {
24 | hullIntegrity -= damageTaken
25 | Log.i("$name damage taken =","$damageTaken")
26 | Log.i("$name hull integrity =","$hullIntegrity")
27 |
28 | if (hullIntegrity <= 0) {
29 | Log.i(type, "$name has been sunk")
30 | sunk = true
31 | }
32 | } else {
33 | // Already sunk
34 | Log.i("Error", "Ship does not exist")
35 | }
36 | }
37 |
38 | fun serviceShip() {
39 | attacksRemaining = maxAttacks
40 | hullIntegrity = maxHullIntegrity
41 | }
42 |
43 | fun showStats(){
44 | Log.i("$type $name",
45 | "Attacks:$attacksRemaining - Hull:$hullIntegrity")
46 | }
47 |
48 | abstract fun attack(): Int
49 |
50 | }
--------------------------------------------------------------------------------
/Chapter11/Basic Classes with Inheritance/ShipYard.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.basicclasseswithinheritance
2 |
3 | import android.util.Log
4 |
5 | class ShipYard {
6 | fun serviceShip(shipToBeServiced: Ship){
7 | shipToBeServiced.serviceShip()
8 | Log.i("Servicing","${shipToBeServiced.name}")
9 | }
10 | }
--------------------------------------------------------------------------------
/Chapter11/Chapter examples/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.inheritanceexamples
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 |
6 | class MainActivity : AppCompatActivity() {
7 |
8 | override fun onCreate(savedInstanceState: Bundle?) {
9 | super.onCreate(savedInstanceState)
10 |
11 | val soldier = Soldier()
12 | soldier.shoot()
13 |
14 | val specialForces = SpecialForces()
15 | specialForces.shoot()
16 | specialForces.SneakUpOnEnemy()
17 |
18 | val paratrooper = Paratrooper()
19 | paratrooper.shoot()
20 | paratrooper.jumpOutOfPlane()
21 |
22 | val sniper = Sniper()
23 | sniper.getIntoPosition()
24 | sniper.shoot()
25 | sniper.shoot()
26 | sniper.shoot()
27 | sniper.shoot()
28 |
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/Chapter11/Chapter examples/Paratrooper.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.inheritanceexamples
2 |
3 | import android.util.Log
4 |
5 | class Paratrooper: Soldier() {
6 | fun jumpOutOfPlane() {
7 | Log.i("Action", "Jump out of plane")
8 | }
9 | }
--------------------------------------------------------------------------------
/Chapter11/Chapter examples/Sniper.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.inheritanceexamples
2 |
3 | import android.util.Log
4 |
5 | class Sniper: Soldier(){
6 | // He forget to bring enough ammo
7 | var sniperAmmo = 3
8 |
9 | override fun shoot(){
10 | when (sniperAmmo > 0) {
11 | true -> {
12 | Log.i("Action", "Steady… Adjust for wind… Bang.")
13 | sniperAmmo--;
14 | }
15 | false -> super.shoot()
16 | }
17 | }
18 |
19 | fun getIntoPosition(){
20 | Log.i("Action","Preparing line of sight to target")
21 | }
22 | }
--------------------------------------------------------------------------------
/Chapter11/Chapter examples/Soldier.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.inheritanceexamples
2 |
3 | import android.util.Log
4 |
5 | open class Soldier() {
6 |
7 | open fun shoot () {
8 | Log.i("Action","Bang bang bang")
9 | }
10 | }
--------------------------------------------------------------------------------
/Chapter11/Chapter examples/SpecialForces.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.inheritanceexamples
2 |
3 | import android.util.Log
4 |
5 | class SpecialForces: Soldier(){
6 | fun SneakUpOnEnemy(){
7 | Log.i("Action","Sneaking up on enemy")
8 | }
9 | }
--------------------------------------------------------------------------------
/Chapter12/Kotlin meet UI/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.kotlinmeetui
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.view.View
6 | import kotlinx.android.synthetic.main.activity_main.*
7 |
8 |
9 | class MainActivity : AppCompatActivity(), View.OnClickListener {
10 | // An int variable to hold a value
11 | private var value = 0
12 |
13 |
14 | override fun onClick(v: View) {
15 | // A local variable to use later
16 | val size: Float
17 |
18 | when (v.id) {
19 | R.id.btnAdd -> {
20 | value++
21 | txtValue.text = "$value"
22 | }
23 |
24 | R.id.btnTake -> {
25 | value--
26 | txtValue.text = "$value"
27 | }
28 |
29 | R.id.btnReset -> {
30 | value = 0
31 | txtValue.text = "$value"
32 | }
33 |
34 | R.id.btnGrow -> {
35 | size = txtValue.textScaleX
36 | txtValue.textScaleX = size + 1
37 | }
38 |
39 | R.id.btnShrink -> {
40 | size = txtValue.textScaleX
41 | txtValue.textScaleX = size - 1
42 | }
43 |
44 | R.id.btnHide -> if (txtValue.visibility == View.VISIBLE) {
45 | // Currently visible so hide it
46 | txtValue.visibility = View.INVISIBLE
47 |
48 | // Change text on the button
49 | btnHide.text = "SHOW"
50 |
51 | } else {
52 | // Currently hidden so show it
53 | txtValue.visibility = View.VISIBLE
54 |
55 | // Change text on the button
56 | btnHide.text = "HIDE"
57 | }
58 | }
59 | }
60 |
61 | override fun onCreate(savedInstanceState: Bundle?) {
62 | super.onCreate(savedInstanceState)
63 | setContentView(R.layout.activity_main)
64 |
65 | // Listen for all the button clicks
66 | btnAdd.setOnClickListener(this)
67 | btnTake.setOnClickListener(this)
68 | txtValue.setOnClickListener(this)
69 | btnGrow.setOnClickListener(this)
70 | btnShrink.setOnClickListener(this)
71 | btnReset.setOnClickListener(this)
72 | btnHide.setOnClickListener(this)
73 |
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/Chapter12/Kotlin meet UI/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
19 |
20 |
29 |
30 |
39 |
40 |
49 |
50 |
59 |
60 |
69 |
70 |
79 |
80 |
--------------------------------------------------------------------------------
/Chapter14/Dialog Demo/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.gamecodeschool.dialogdemo
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 | import android.widget.Button
6 |
7 |
8 | class MainActivity : AppCompatActivity() {
9 |
10 | override fun onCreate(savedInstanceState: Bundle?) {
11 | super.onCreate(savedInstanceState)
12 | setContentView(R.layout.activity_main)
13 |
14 | val button = findViewById