Skip to content

Instantly share code, notes, and snippets.

@ssaurel
Created September 7, 2018 12:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ssaurel/b5d96658c56a8aec4ddd105d209bc6ed to your computer and use it in GitHub Desktop.
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
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));
}
}
@jkariscodes
Copy link

This is deprecated. Use FusedLocation API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment