Игра “Русская рулетка”. Часть 2

MainActivity

import android.annotation.TargetApi;
import android.app.Activity;
import android.media.AudioAttributes;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.util.Random;

public class MainActivity extends Activity {
private SoundPool sounds;
private int sound_shot;
private int sound_shot_false;
private int sound_baraban;
private ImageView blood_image;
private int on_shot = 3;
private int max_number = 10;
private int random = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createSoundPool();
loadSounds();
init();

}
protected void createSoundPool() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
createNewSoundPool();
} else {
createOldSoundPool();
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void createNewSoundPool(){
AudioAttributes attributes = new AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_GAME)
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.build();
sounds = new SoundPool.Builder()
.setAudioAttributes(attributes)
.build();
}

@SuppressWarnings("deprecation")
protected void createOldSoundPool(){
sounds = new SoundPool(5, AudioManager.STREAM_MUSIC,0);
}
private void loadSounds()
{
sound_shot = sounds.load(this,R.raw.revolver_shot, 1);
sound_shot_false = sounds.load(this,R.raw.gun_false, 1);
sound_baraban = sounds.load(this,R.raw.revolver_baraban, 1);

}

public void onShot(View view)
{
if(random == on_shot )
{
sounds.play(sound_shot,1.0f,1.0f,1,0,1);
blood_image.setVisibility(View.VISIBLE);
}
else
{
sounds.play(sound_shot_false,1.0f,1.0f,1,0,1);
}

}

public void onBaraban(View view)
{
sounds.play(sound_baraban,1.0f,1.0f,1,0,1);
blood_image.setVisibility(View.GONE);
random = new Random().nextInt(max_number);
//Log.d("MainActivity","Random number " + random);

}
private void init()
{
blood_image = findViewById(R.id.image_blood);

}

}

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">

<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/gun"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/gun"

/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onShot"
android:text="Shot"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.905"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="56dp"
android:layout_marginRight="56dp"
android:onClick="onBaraban"
android:text="Baraban"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.991"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView"
app:layout_constraintVertical_bias="0.192" />

<ImageView
android:id="@+id/image_blood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/blood"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="@+id/imageView"
app:layout_constraintEnd_toEndOf="@+id/imageView"
app:layout_constraintStart_toStartOf="@+id/imageView"
app:layout_constraintTop_toTopOf="@+id/imageView" />

</androidx.constraintlayout.widget.ConstraintLayout>

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *