Screen Elements;
TextView shows game score
Rest of the screen is divided into 4 equal colored rectangle views
Goal
Per second any of those 4 rectangle views will change its color to gray
the gray color will remain for 1 whole second
after 1 second the gray rectangle will revert to its original color and again one random rectangle will turn gray
player needs to tap on the rectangles that are grey during that 1 second, so that there is at least 1 tap per second
Conditions
if user taps on the rectangle view while gray then score moves up by 1+
if user cannot tap the view within 1 second, game over
if user taps on a view that is not gray, also game over
In short, the objective is to tap the gray rectangle within a second. If it is missed or a colored box is tapped the game gets over.
I have tried to code this like below, and I have used ALPHABETS in place of colors and have used X instead of GRAY,
Please help me understand what am I doing wrong?
Any help is appreciated.
Thanks!
public class MainActivity extends AppCompatActivity {
Button ba, bb, bc, bd, startgame;
TextView score;
GameCountDownTimer gameCountDownTimer;
// stores button index, original color
int greyButton[] = new int[2];
int clicked = 99;
boolean firstrun=true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startgame.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
gameCountDownTimer = new GameCountDownTimer(3000, 1000);
gameCountDownTimer.start();
}
});
}
public class GameCountDownTimer extends CountDownTimer {
public GameCountDownTimer(long millisInFuture, long countDownInterval) {
super(millisInFuture, countDownInterval);
}
@Override
public void onTick(long millisUntilFinished) {
int progress = (int) (millisUntilFinished/1000);
score.setText(String.valueOf(progress));
}
@Override
public void onFinish() {
if(firstrun) {
randomize();
firstrun=false;
}
else{
// only continue if player taps on the correct button
// or clicked == greyButton[0]
if(clicked==greyButton[0]){
randomize();
this.start();
} else {
score.setText("GAME OVER!");
this.cancel();
}
}
}
}
public void randomize(){
// if grayButton has a value, revert to original color
if(greyButton != null){
revertToOriginalColor(greyButton[0]);
}
int nextGrayButton = new Random().nextInt(4);
setGrayButton(nextGrayButton);
}
public void revertToOriginalColor(int index){
}
public void setGrayButton(int index){
}
}
EDIT: Solved
Added the following portion in my onCreate()
and registered all the 4 buttons to the overriden onClick, after implementing View.OnClickListener to my activity class.
// initialize the CountDownTimer
timer = new CountDownTimer(1000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
//updating the time
}
@Override
public void onFinish() {
gameOver = true;
this.cancel();
simpleAlert(score);
}
};
I would suggest using Rxjava. Add following dependencies in your app level build.gradle file
implementation "io.reactivex.rxjava2:rxjava:2.1.9"
implementation "io.reactivex.rxjava2:rxandroid:2.0.1"
implementation "com.jakewharton.rxbinding2:rxbinding:2.0.0"
Add following code in your class.
RxView.clicks(my_grid_layout)
.map(new Function<Object, Integer>() {
@Override
public Integer apply(Object o) throws Exception {
return 1;
}
})
.buffer(1, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new Observer<List<Integer>>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(List<Integer> integers) {
Log.e(TAG, "onNext: " + integers.size() + " taps received!");
int number_of_clicks= integers.size();
}
@Override
public void onError(Throwable e) {
Log.e(TAG, "onError: " + e.getMessage());
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete");
}
});
If you want to restrict to Using only android libraries
private int count = 0;
private Timer myTimer;
relativeLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
count= count+1;
}
});
myTimer = new Timer();
myTimer.schedule(new TimerTask() {
@Override
public void run() {
Log.d("TAG",String.valueOf(count));
count=0;
}
}, 0, 1000);
}