arsalandywriter.com

Creating a Simple Android Game: A Family Adventure

Written on

Chapter 1: The Joy of Coding with Kids

It's essential to foster new skills in our children. My son is currently enrolled in a programming course tailored for kids at a nearby facility. While he's learning to navigate Scratch, he expressed a desire to create something for Android devices. This sparked the idea for our project.

We collaborated on a straightforward two-player counting game aimed at reaching the number 17. Each player can add 1, 2, or 3 to the total on their turn, and the first to hit 17 emerges victorious.

The game features three buttons corresponding to the numbers one, two, and three. Additionally, it includes a turn indicator, a reset button, and a small display that shows the player's distance from the target. Each time a button is pressed, the background color changes, adding a fun twist.

It's a simple yet engaging game that my daughter also enjoyed. My son, however, is eager to develop a game with "characters," which will be our next challenge.

The first video titled "PROGRAMMING for kids: Basic Concepts Part 1" provides a great introduction to programming for children, offering foundational concepts that can inspire young minds.

Chapter 2: Coding the Game

In our game, we utilize the following code to create a basic Android app. Here’s a brief overview of how the game mechanics operate:

import android.os.Bundle

import androidx.activity.ComponentActivity

import androidx.activity.compose.setContent

import androidx.compose.foundation.background

import androidx.compose.foundation.layout.Arrangement

import androidx.compose.foundation.layout.Column

import androidx.compose.foundation.layout.Row

import androidx.compose.foundation.layout.Spacer

import androidx.compose.foundation.layout.fillMaxSize

import androidx.compose.foundation.layout.height

import androidx.compose.foundation.layout.padding

import androidx.compose.material3.Button

import androidx.compose.material3.MaterialTheme

import androidx.compose.material3.Surface

import androidx.compose.material3.Text

import androidx.compose.runtime.Composable

import androidx.compose.runtime.getValue

import androidx.compose.runtime.mutableIntStateOf

import androidx.compose.runtime.mutableStateOf

import androidx.compose.runtime.remember

import androidx.compose.runtime.setValue

import androidx.compose.ui.Alignment

import androidx.compose.ui.Modifier

import androidx.compose.ui.graphics.Color

import androidx.compose.ui.unit.dp

import phinneas.and.dad.ui.theme.PhinneasGaloreTheme

class MainActivity : ComponentActivity() {

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

setContent {

PhinneasGaloreTheme {

Surface(

modifier = Modifier.fillMaxSize(),

) {

BubbleClickGame()

}

}

}

}

}

@Composable

fun BubbleClickGame() {

val goal = 17

var count by remember { mutableIntStateOf(0) }

var player1 by remember { mutableStateOf(true) }

val colors = Backgrounds().colorList

var backgroundColor by remember { mutableStateOf(colors.random()) }

Column(

modifier = Modifier

.padding(20.dp)

.fillMaxSize()

.background(backgroundColor),

horizontalAlignment = Alignment.CenterHorizontally,

verticalArrangement = Arrangement.Center,

) {

Row(modifier = Modifier.weight(0.1f)) {

Text(

text = if (count < goal) {

if (!player1) "Player Two" else "Player One"

} else {

"You Win"

},

style = MaterialTheme.typography.displayLarge

)

}

Row(modifier = Modifier.weight(0.1f)) {

Text(

text = if (count >= goal) "" else count.toString(),

style = MaterialTheme.typography.displayLarge,

)

}

Row(modifier = Modifier.weight(0.7f)) {

Column {

Button(onClick = {

count++

player1 = !player1

backgroundColor = colors.random()

}) {

Text(text = "Add One")

}

Spacer(modifier = Modifier.height(10.dp))

Button(onClick = {

count += 2

player1 = !player1

backgroundColor = colors.random()

}) {

Text(text = "Add Two")

}

Spacer(modifier = Modifier.height(10.dp))

Button(onClick = {

count += 3

player1 = !player1

backgroundColor = colors.random()

}) {

Text(text = "Add Three")

}

Spacer(modifier = Modifier.height(10.dp))

}

}

Row(modifier = Modifier.weight(0.1f)) {

Text(

text = "${goal - count} away",

style = MaterialTheme.typography.bodyLarge

)

}

Row(modifier = Modifier.weight(0.1f)) {

Button(onClick = {

count = 0

player1 = true

backgroundColor = colors.random()

}) {

Text(text = "Reset")

}

Spacer(modifier = Modifier.height(10.dp))

}

}

}

class Backgrounds {

var colorList: List<Color> = listOf(

Color.Black,

Color.Blue,

Color.Gray,

Color.DarkGray

)

}

The code is not flawless and can be simplified in numerous ways. However, its primary goal is to introduce programming concepts to an eight-year-old. Keeping the code straightforward is essential during the initial learning phase.

The second video titled "Coding in Python Part 1" serves as an excellent resource, guiding beginners through the fundamentals of Python programming, making it accessible for young learners.

Thank you for taking the time to read this. If you found this enjoyable, you might also like my page on Medium.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Empowering Parents: 7 Insightful Quotes from My New Book

Explore seven key quotes from my book on A.I., Robotics, and Coding, designed to help parents navigate the digital landscape for their children.

The Fed's Inaction Fuels Inflationary Mindset

The Fed's pause in interest rate hikes exacerbates inflation concerns, embedding it into consumer psychology.

No Limits: Embracing Life's Opportunities at Any Age

Discover how to overcome limitations and pursue your dreams at any stage of life.