Created
September 7, 2018 12:20
-
-
Save ssaurel/b5d96658c56a8aec4ddd105d209bc6ed to your computer and use it in GitHub Desktop.
Main Activity of the Reflex Game for the tutorial on the SSaurel's Channel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssaurel.myreflexgame; | |
import android.graphics.Color; | |
import android.os.Bundle; | |
import android.os.Handler; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.view.WindowManager; | |
import android.widget.TextView; | |
import butterknife.BindView; | |
import butterknife.ButterKnife; | |
import butterknife.OnTouch; | |
public class MainActivity extends AppCompatActivity { | |
public static final int BG_COLOR = Color.parseColor("#E8EAF6"); | |
public static final long MIN_DELAY = 1000; // 1 sec | |
public static final long MAX_DELAY = 5000; // 5 sec | |
public static final int NB_TRIES = 5; // 5 tries during our Reflex Game | |
public int currentTry = 1; | |
@BindView(R.id.root) | |
View root; | |
@BindView (R.id.touchArea) | |
View touchArea; | |
@BindView(R.id.targetIv) | |
View targetIv; | |
@BindView(R.id.msgTv) | |
TextView msgTv; | |
private boolean gameStarted, displayed; | |
// we will store total time for NB_TRIES, best time and average time | |
private long totalTime, startTime, bestTime, averageTime; | |
private static Handler HANDLER; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_play); | |
// Keep screen on during the game | |
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); | |
// bind views | |
ButterKnife.bind(this); | |
HANDLER = new Handler(); | |
} | |
@Override | |
protected void onPause() { | |
super.onPause(); | |
if (HANDLER != null) { | |
// cancel all callbacks in on pause state | |
HANDLER.removeCallbacksAndMessages(null); | |
} | |
} | |
@OnTouch(R.id.root) | |
public boolean manageGame() { | |
if (gameStarted) { | |
// game started, we need to manage touch on the screen | |
if (displayed) { | |
// bg in red ==> we need to manage time | |
// delay before user touched the screen | |
long delay = System.currentTimeMillis() - startTime; | |
if (delay < bestTime) { | |
// new best time | |
bestTime = delay; | |
} | |
totalTime += delay; // we add to total time | |
msgTv.setText(getString(R.string.ok_in).replace("#delay#", delay + "")); | |
root.setBackgroundColor(BG_COLOR); // set classical bg color | |
touchArea.setVisibility(View.GONE); | |
targetIv.setVisibility(View.GONE); | |
currentTry++; | |
if (currentTry > NB_TRIES) { | |
// game ended | |
averageTime = totalTime / NB_TRIES; // we calculate average time | |
gameStarted = false; | |
msgTv.setText(getString(R.string.game_ended). | |
replace("#averageTime#", averageTime + ""). | |
replace("#bestTime#", bestTime + "")); | |
} else { | |
// we plan next display randomly for red backgound | |
displayed = false; | |
HANDLER.postDelayed(bgColorTask, randomLongBetween(MIN_DELAY, MAX_DELAY)); | |
} | |
} else { | |
// bg is not in red ==> You lose | |
HANDLER.removeCallbacks(bgColorTask); | |
gameStarted = false; | |
msgTv.setText(R.string.you_lose); | |
} | |
} else { | |
// game not started, we need to start the game | |
// we init variables | |
gameStarted = true; | |
displayed = false; | |
bestTime = Long.MAX_VALUE; | |
totalTime = 0; | |
currentTry = 1; | |
msgTv.setText(R.string.be_ready); | |
// we will change the background color in a random time between MIN_DELAY and MAX_DELAY | |
HANDLER.postDelayed(bgColorTask, randomLongBetween(MIN_DELAY, MAX_DELAY)); | |
} | |
return false; | |
} | |
// we need to create a bg color task runnable instance to change the bg color before user touch the screen as fast as possible | |
private Runnable bgColorTask = new Runnable() { | |
@Override | |
public void run() { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
msgTv.setText(""); | |
root.setBackgroundColor(Color.RED); // bg in RED ==> Touch the screen as fast as you can | |
touchArea.setVisibility(View.VISIBLE); | |
targetIv.setVisibility(View.VISIBLE); | |
displayed = true; // bg in RED is displayed | |
startTime = System.currentTimeMillis(); | |
} | |
}); | |
} | |
}; | |
public static long randomLongBetween(long origin, long bound) { | |
return origin + (long) (Math.random() * (bound - origin)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is deprecated. Use FusedLocation API