Android: Mini game Funny Marathon
Giải phóng thời gian, khai phóng năng lực
Video hướng dẫn:
Bài viết hướng dẫn:
File activity_main.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF"> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:id="@+id/rgChoose"> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rbCat" android:layout_marginBottom="35dp"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rbDog" android:layout_marginBottom="35dp"/> <RadioButton android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/rbDragon" android:layout_marginBottom="25dp"/> </RadioGroup> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sb1" android:layout_alignParentBottom="true" android:thumb="@drawable/cat" android:layout_marginTop="10dp" android:layout_marginStart="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="150dp" style="@android:style/Widget.SeekBar" android:layout_toRightOf="@id/rgChoose" android:layout_marginLeft="10dp" android:layout_toEndOf="@id/rgChoose"/> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sb2" android:layout_alignParentBottom="true" android:thumb="@drawable/dog" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginStart="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="80dp" style="@android:style/Widget.SeekBar" android:layout_toRightOf="@id/rgChoose" android:layout_toEndOf="@id/rgChoose"/> <SeekBar android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/sb3" android:layout_alignParentBottom="true" android:thumb="@drawable/dragon" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginStart="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="10dp" style="@android:style/Widget.SeekBar" android:layout_toEndOf="@id/rgChoose" android:layout_toRightOf="@id/rgChoose"/> <ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/button" android:id="@+id/ibStart" android:layout_centerHorizontal="true" android:layout_marginTop="60dp" android:background="@android:color/transparent" android:contentDescription="@string/button_start"/> <TextView android:text="@string/funny_marathon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView3" android:layout_centerHorizontal="true" android:textSize="30sp" android:textStyle="bold" android:textColor="#4CAF50"/> <TextView android:text="@string/score" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/txtScore" android:textColor="#E91E63" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:layout_marginStart="10dp" android:layout_marginRight="10dp" android:layout_marginEnd="10dp" android:layout_marginBottom="10dp" android:textStyle="bold" android:textSize="24sp"/> </RelativeLayout>
File strings.xml:
<resources> <string name="app_name">FunnyMarathonV1Study</string> <string name="score">1000</string> <string name="funny_marathon">Funny marathon</string> <string name="button_start">Button Start</string> </resources>
File MainActivity.java:
package v1study.com.funnymarathonv1study; import android.os.CountDownTimer; import android.view.View; import android.widget.*; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import java.util.Random; public class MainActivity extends AppCompatActivity { SeekBar sb1, sb2, sb3; ImageButton ibStart; TextView txtScore; RadioGroup rgChoose; int score; RadioButton rbCat, rbDog, rbDragon; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); score = 1000; getViews(); txtScore.setText(score + ""); sb1.setEnabled(false); sb2.setEnabled(false); sb3.setEnabled(false); final CountDownTimer countDownTimer = new CountDownTimer(20000, 400) { @Override public void onTick(long millisUntilFinished) { Random random = new Random(); int r = 8; String won; if (sb1.getProgress() >= sb1.getMax()) { won = "The cat WON!"; score += (rgChoose.getCheckedRadioButtonId() == R.id.rbCat) ? 100 : -100; win(this, won); } if (sb2.getProgress() >= sb2.getMax()) { won = "The dog WON!"; score += (rgChoose.getCheckedRadioButtonId() == R.id.rbDog) ? 100 : -100; win(this, won); } if (sb3.getProgress() >= sb3.getMax()) { won = "The dragon WON!"; score += (rgChoose.getCheckedRadioButtonId() == R.id.rbDragon) ? 100 : -100; win(this, won); } sb1.setProgress(sb1.getProgress() + random.nextInt(r)); sb2.setProgress(sb2.getProgress() + random.nextInt(r)); sb3.setProgress(sb3.getProgress() + random.nextInt(r)); } @Override public void onFinish() { } }; ibStart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (rbCat.isChecked() == false && rbDog.isChecked() == false && rbDragon.isChecked() == false) { countDownTimer.cancel(); Toast.makeText(MainActivity.this, "Please choose one animal!", 2000).show(); return; } if (score <= 0) { countDownTimer.cancel(); Toast.makeText(MainActivity.this, "You have not enough scores to play!", 2000).show(); return; } rbCat.setEnabled(false); rbDog.setEnabled(false); rbDragon.setEnabled(false); ibStart.setVisibility(View.INVISIBLE); countDownTimer.cancel(); sb1.setProgress(0); sb2.setProgress(0); sb3.setProgress(0); countDownTimer.start(); } }); } void enable() { rbCat.setEnabled(true); rbDog.setEnabled(true); rbDragon.setEnabled(true); } void win(CountDownTimer countDownTimer, String won) { enable(); countDownTimer.cancel(); ibStart.setVisibility(View.VISIBLE); Toast.makeText(MainActivity.this, won, 3000).show(); txtScore.setText(score + ""); } void getViews() { sb1 = findViewById(R.id.sb1); sb2 = findViewById(R.id.sb2); sb3 = findViewById(R.id.sb3); ibStart = findViewById(R.id.ibStart); txtScore = findViewById(R.id.txtScore); rgChoose = findViewById(R.id.rgChoose); rbCat = findViewById(R.id.rbCat); rbDog = findViewById(R.id.rbDog); rbDragon = findViewById(R.id.rbDragon); } }
Giải phóng thời gian, khai phóng năng lực