Herkese merhaba,

Bu yazımda Android Studio üzerinde Kotlin kullanarak ImageView yardımıyla yakalama oyunu yapacağız. Bunun için xml dosyasına eklememiz gereken nesnelere bakalım:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/imageView2"
            android:layout_toStartOf="@+id/imageView2"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignTop="@+id/imageView3"
            android:layout_centerHorizontal="true"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView4"
            android:layout_toEndOf="@+id/imageView5"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toStartOf="@+id/imageView5"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/imageView5"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignStart="@+id/imageView4"
            android:layout_below="@+id/imageView4"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView8"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView4"
            android:layout_centerHorizontal="true"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <ImageView
            android:id="@+id/imageView9"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView4"
            android:layout_toEndOf="@+id/imageView5"
            app:srcCompat="@mipmap/ic_launcher"
            android:onClick="tiklandi"/>

        <TextView
            android:id="@+id/sayacText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/imageView"
            android:layout_centerHorizontal="true"
            android:text="Sayac : " />

        <TextView
            android:id="@+id/skorText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/imageView7"
            android:layout_centerHorizontal="true"
            android:text="Skor :" />
    </RelativeLayout>
</android.support.constraint.ConstraintLayout>

Şimdi gelelim Kotlin dosyamıza:

İlk olarak her zaman ki gibi tanımlamalarla başlayacağız.

lateinit var image1 : ImageView
lateinit var image2 : ImageView
lateinit var image3 : ImageView
lateinit var image4 : ImageView
lateinit var image5 : ImageView
lateinit var image6 : ImageView
lateinit var image7 : ImageView
lateinit var image8 : ImageView
lateinit var image9 : ImageView
lateinit var sayacText : TextView
lateinit var skorText : TextView
var sayac : Long = 20
var skor : Int = 0
private lateinit var timer: CountDownTimer
var bitti : Boolean = false

Şimdi ImageView ve TextView’lerin ID’lerini tanımlayalım.

image1 = findViewById(R.id.imageView)
image2 = findViewById(R.id.imageView2)
image3 = findViewById(R.id.imageView3)
image4 = findViewById(R.id.imageView4)
image5 = findViewById(R.id.imageView5)
image6 = findViewById(R.id.imageView6)
image7 = findViewById(R.id.imageView7)
image8 = findViewById(R.id.imageView8)
image9 = findViewById(R.id.imageView9)
sayacText = findViewById(R.id.sayacText)
skorText = findViewById(R.id.skorText)

İlk açılışta hiçbir ImageView’in gözükmemesi için hepsini Invisible yapalım.

image1.visibility = View.INVISIBLE
image2.visibility = View.INVISIBLE
image3.visibility = View.INVISIBLE
image4.visibility = View.INVISIBLE
image5.visibility = View.INVISIBLE
image6.visibility = View.INVISIBLE
image7.visibility = View.INVISIBLE
image8.visibility = View.INVISIBLE
image9.visibility = View.INVISIBLE

Gelelim sayaç kısmına:

timer = object : CountDownTimer(sayac * 1000, 1000) {
    override fun onFinish(){
        bitti = true
    }
    override fun onTick(millisUntilFinished: Long) {
        sayac = millisUntilFinished / 1000
        sayacText.setText(sayac.toString())
        var rastgelesayi : Int = rand(1,10)
        if (rastgelesayi == 1){
            image1.visibility = View.VISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.INVISIBLE
        } else if (rastgelesayi == 2){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.VISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.INVISIBLE
        } else if (rastgelesayi == 3){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.VISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.INVISIBLE
        }else if (rastgelesayi == 4){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.VISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.INVISIBLE
        }else if (rastgelesayi == 5){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.VISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.INVISIBLE
        }else if (rastgelesayi == 6){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.VISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.INVISIBLE
        }else if (rastgelesayi == 7){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.VISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.INVISIBLE
        }else if (rastgelesayi == 8){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.VISIBLE
            image9.visibility = View.INVISIBLE
        }else if (rastgelesayi == 9){
            image1.visibility = View.INVISIBLE
            image2.visibility = View.INVISIBLE
            image3.visibility = View.INVISIBLE
            image4.visibility = View.INVISIBLE
            image5.visibility = View.INVISIBLE
            image6.visibility = View.INVISIBLE
            image7.visibility = View.INVISIBLE
            image8.visibility = View.INVISIBLE
            image9.visibility = View.VISIBLE
        }
    }
}.start()

Burada yaptığımız şey aslında çok basit. Sayaç her çalıştığında rastgele bir sayı üretecek ve o sayıya denk gelen ImageView’i Visible yapacak. En son tıklama olayına geçiyoruz:

fun tiklandi(view : View){
    if (!bitti) {
        skor++
        skorText.setText("Skor : " + skor.toString())
    }
}

ImageView’lere tıklandığı zaman skor’u 1 arttıracak ve TextView içerisine skoru yazacak.

Gelelim kodun tamamına :

package com.can.ders

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.os.CountDownTimer
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import java.util.*

class MainActivity : AppCompatActivity() {

    lateinit var image1 : ImageView
    lateinit var image2 : ImageView
    lateinit var image3 : ImageView
    lateinit var image4 : ImageView
    lateinit var image5 : ImageView
    lateinit var image6 : ImageView
    lateinit var image7 : ImageView
    lateinit var image8 : ImageView
    lateinit var image9 : ImageView
    lateinit var sayacText : TextView
    lateinit var skorText : TextView
    var sayac : Long = 20
    var skor : Int = 0
    private lateinit var timer: CountDownTimer
    var bitti : Boolean = false

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        image1 = findViewById(R.id.imageView)
        image2 = findViewById(R.id.imageView2)
        image3 = findViewById(R.id.imageView3)
        image4 = findViewById(R.id.imageView4)
        image5 = findViewById(R.id.imageView5)
        image6 = findViewById(R.id.imageView6)
        image7 = findViewById(R.id.imageView7)
        image8 = findViewById(R.id.imageView8)
        image9 = findViewById(R.id.imageView9)
        sayacText = findViewById(R.id.sayacText)
        skorText = findViewById(R.id.skorText)

        image1.visibility = View.INVISIBLE
        image2.visibility = View.INVISIBLE
        image3.visibility = View.INVISIBLE
        image4.visibility = View.INVISIBLE
        image5.visibility = View.INVISIBLE
        image6.visibility = View.INVISIBLE
        image7.visibility = View.INVISIBLE
        image8.visibility = View.INVISIBLE
        image9.visibility = View.INVISIBLE

        timer = object : CountDownTimer(sayac * 1000, 1000) {
            override fun onFinish(){
                bitti = true
            }
            override fun onTick(millisUntilFinished: Long) {
                sayac = millisUntilFinished / 1000
                sayacText.setText(sayac.toString())
                var rastgelesayi : Int = rand(1,10)
                if (rastgelesayi == 1){
                    image1.visibility = View.VISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.INVISIBLE
                } else if (rastgelesayi == 2){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.VISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.INVISIBLE
                } else if (rastgelesayi == 3){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.VISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.INVISIBLE
                }else if (rastgelesayi == 4){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.VISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.INVISIBLE
                }else if (rastgelesayi == 5){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.VISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.INVISIBLE
                }else if (rastgelesayi == 6){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.VISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.INVISIBLE
                }else if (rastgelesayi == 7){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.VISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.INVISIBLE
                }else if (rastgelesayi == 8){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.VISIBLE
                    image9.visibility = View.INVISIBLE
                }else if (rastgelesayi == 9){
                    image1.visibility = View.INVISIBLE
                    image2.visibility = View.INVISIBLE
                    image3.visibility = View.INVISIBLE
                    image4.visibility = View.INVISIBLE
                    image5.visibility = View.INVISIBLE
                    image6.visibility = View.INVISIBLE
                    image7.visibility = View.INVISIBLE
                    image8.visibility = View.INVISIBLE
                    image9.visibility = View.VISIBLE
                }
            }
        }.start()

    }

    fun rand(from : Int, to: Int) : Int{
        val random = Random()
        return random.nextInt(to-from) + from
    }

    fun tiklandi(view : View){
        if (!bitti) {
            skor++
            skorText.setText("Skor : " + skor.toString())
        }
    }
}

Bir sonraki yazımda görüşmek üzere…