본문 바로가기
Android

안드로이드 동전 던지기 게임 코드

by 봉이로그 2023. 11. 14.
// res/anim/coin_effect.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <rotate
        android:duration="2500"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="7200"
        />

    <translate
        android:duration="1000"
        android:fromYDelta="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toYDelta="-200%"
        />


</set>

 

// res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">

    <Button
        android:id="@+id/toss_button"
        android:layout_width="wrap_content"
        android:layout_height="48dp"
        android:layout_marginTop="587dp"
        android:layout_marginBottom="16dp"
        android:text="Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/coin_image"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toTopOf="@+id/toss_button"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:srcCompat="@drawable/coin_front" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

 

// MainActivity.kt

package com.example.tosscoin

import android.media.MediaPlayer
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.animation.Animation
import android.view.animation.AnimationUtils
import android.widget.Button
import android.widget.ImageView
import java.util.Timer
import kotlin.concurrent.schedule

class MainActivity : AppCompatActivity() {

    var state: Int = 1 // 동전의 앞면


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

        var tossButton = findViewById<Button>(R.id.toss_button);
        var coinImage = findViewById<ImageView>(R.id.coin_image);

        tossButton.text = "Toss"

        tossButton.setOnClickListener {
            var animation : Animation = AnimationUtils.loadAnimation(this, R.anim.coin_effect)
            coinImage.startAnimation(animation)

            var mediaPlayer : MediaPlayer? = MediaPlayer.create(this, R.raw.coin_effect)
            mediaPlayer?.start()

            Timer().schedule(2200) {
                state = (0..1).random()

                if(state == 0) {
                    coinImage.setImageResource(R.drawable.coin_back)
                }else {
                    coinImage.setImageResource(R.drawable.coin_front)
                }
            }
        }
    }
}

 

 

https://www.youtube.com/watch?v=Rn6DDYD0-Xk&t=70s