Untitled
unknown
java
3 months ago
9.7 kB
7
Indexable
package com.natividad.memorygame; import android.animation.Animator; import android.animation.AnimatorListenerAdapter; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.os.Bundle; import android.os.CountDownTimer; import android.util.Log; import android.view.View; import android.widget.ImageView; import android.widget.TextView; import android.widget.Button; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.ArrayList; import java.util.Collections; public class MainActivity extends AppCompatActivity implements View.OnClickListener { TextView tv_status, tv_timer, tv_score; Button bt_start; private CountDownTimer timerCountdown; private boolean gameStarted = false; private boolean gameCompleted = false; private long timeRemaining; // Store remaining time private final int[] iv_CardIds = { R.id.iv_Card0, R.id.iv_Card1, R.id.iv_Card2, R.id.iv_Card3, R.id.iv_Card4, R.id.iv_Card5, R.id.iv_Card6, R.id.iv_Card7, R.id.iv_Card8, R.id.iv_Card9, R.id.iv_Card10, R.id.iv_Card11, R.id.iv_Card12, R.id.iv_Card13, R.id.iv_Card14, R.id.iv_Card15 }; private final int[] drawableCardIds = { R.drawable.card1, R.drawable.card2, R.drawable.card3, R.drawable.card4, R.drawable.card5, R.drawable.card6, R.drawable.card7, R.drawable.card8 }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_status = findViewById(R.id.tv_status); tv_score = findViewById(R.id.tv_score); tv_timer = findViewById(R.id.tv_timer); bt_start = findViewById(R.id.bt_start); bt_start.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (!gameStarted) { gameStarted = true; setupNewGame(); bt_start.setText("Restart"); } else { setupNewGame(); } } }); disableGame(); } private void setupNewGame() { gameCompleted = false; tv_status.setText("Finish the game before time runs out :)"); if (tv_score != null) { tv_score.setText("Score: 0"); // Reset score UI } if (timerCountdown != null) { timerCountdown.cancel(); } startGameTimer(); ArrayList<Integer> shuffledDrawableIds = new ArrayList<>(); for (int drawableId : drawableCardIds) { shuffledDrawableIds.add(drawableId); shuffledDrawableIds.add(drawableId); } Collections.shuffle(shuffledDrawableIds); for (int i = 0; i < shuffledDrawableIds.size(); i++) { int iv_Id = iv_CardIds[i]; int drawableId = shuffledDrawableIds.get(i); CardInfo cardInfo = new CardInfo(iv_Id, drawableId); ImageView imageView = findViewById(iv_Id); imageView.setImageResource(R.drawable.covered_tile); imageView.setTag(cardInfo); imageView.setOnClickListener(this); } cardInfo1 = null; matchCount = 0; isWaiting = false; } private void startGameTimer() { timerCountdown = new CountDownTimer(30000, 1000) { @Override public void onTick(long millisUntilFinished) { timeRemaining = millisUntilFinished; // Save time left tv_timer.setText("Time Left: " + millisUntilFinished / 1000 + "s"); } @Override public void onFinish() { if (!gameCompleted) { tv_status.setText("Time's up! Try again."); disableGame(); } } }.start(); } private void disableGame() { for (int id : iv_CardIds) { findViewById(id).setOnClickListener(null); } } CardInfo cardInfo1 = null; int matchCount = 0; boolean isWaiting = false; private void coverCard(CardInfo cardInfo) { ImageView imageView = findViewById(cardInfo.getImageViewId()); imageView.setImageResource(R.drawable.covered_tile); cardInfo.setFlipped(false); cardInfo.setMatched(false); } private void calculateScore() { long timeUsed = 30000 - timeRemaining; int timeUsedSeconds = (int) (timeUsed / 1000); int score; if (timeUsedSeconds <= 20) { score = 5; } else if (timeUsedSeconds <= 25) { score = 2; } else { score = 0; } tv_status.setText("Game Over! You scored: " + score + " points."); tv_score.setText("Score: " + score); // Update new score TextView Toast.makeText(this, "You scored: " + score + " points!", Toast.LENGTH_LONG).show(); } @Override public void onClick(View v) { if (!isWaiting) { ImageView imageView = (ImageView) v; CardInfo cardInfo = (CardInfo) imageView.getTag(); Log.d("demo", "onClick: " + cardInfo); if (!cardInfo.isFlipped() && !cardInfo.isMatched()) { flipCard(imageView, cardInfo.getDrawableId(), new Runnable() { @Override public void run() { cardInfo.setFlipped(true); if (cardInfo1 == null) { cardInfo1 = cardInfo; } else { if (cardInfo.getDrawableId() == cardInfo1.getDrawableId()) { matchCount++; cardInfo1.setMatched(true); cardInfo.setMatched(true); tv_status.setText("Match Count: " + matchCount); matchAnimation(imageView, findViewById(cardInfo1.getImageViewId())); if (matchCount == 8) { gameCompleted = true; timerCountdown.cancel(); calculateScore(); // Call the scoring method } cardInfo1 = null; } else { isWaiting = true; mismatchAnimation(imageView, findViewById(cardInfo1.getImageViewId())); imageView.postDelayed(new Runnable() { @Override public void run() { flipCard(imageView, R.drawable.covered_tile, null); flipCard(findViewById(cardInfo1.getImageViewId()), R.drawable.covered_tile, null); cardInfo.setFlipped(false); cardInfo1.setFlipped(false); cardInfo1 = null; isWaiting = false; } }, 100); } } } }); } } } private void flipCard(final ImageView card, final int newImageResId, final Runnable onEnd) { ObjectAnimator flipOut = ObjectAnimator.ofFloat(card, "scaleX", 1f, 0f); ObjectAnimator flipIn = ObjectAnimator.ofFloat(card, "scaleX", 0f, 1f); flipOut.setDuration(200); flipIn.setDuration(200); flipOut.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { card.setImageResource(newImageResId); flipIn.start(); } }); flipIn.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { if (onEnd != null) { onEnd.run(); } } }); flipOut.start(); } private void matchAnimation(View card1, View card2) { AnimatorSet animatorSet = new AnimatorSet(); ObjectAnimator scaleX = ObjectAnimator.ofFloat(card1, "scaleX", 1f, 1.2f, 1f); ObjectAnimator scaleY = ObjectAnimator.ofFloat(card1, "scaleY", 1f, 1.2f, 1f); ObjectAnimator scaleX2 = ObjectAnimator.ofFloat(card2, "scaleX", 1f, 1.2f, 1f); ObjectAnimator scaleY2 = ObjectAnimator.ofFloat(card2, "scaleY", 1f, 1.2f, 1f); animatorSet.playTogether(scaleX, scaleY, scaleX2, scaleY2); animatorSet.setDuration(300); animatorSet.start(); } private void mismatchAnimation(View card1, View card2) { ObjectAnimator shake1 = ObjectAnimator.ofFloat(card1, "translationX", 0, 10, -10, 10, -10, 0); ObjectAnimator shake2 = ObjectAnimator.ofFloat(card2, "translationX", 0, 10, -10, 10, -10, 0); shake1.setDuration(300); shake2.setDuration(300); AnimatorSet animatorSet = new AnimatorSet(); animatorSet.playTogether(shake1, shake2); animatorSet.start(); } }
Editor is loading...
Leave a Comment