Skip to content

Instantly share code, notes, and snippets.

@emiremen
Last active August 23, 2023 11:01
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emiremen/36ec663a293063b3ae1884411c41f336 to your computer and use it in GitHub Desktop.
Save emiremen/36ec663a293063b3ae1884411c41f336 to your computer and use it in GitHub Desktop.
Android Studio Catch Kenny oyunu
public class MainActivity extends AppCompatActivity {
ImageView myImg;
TextView textView, textView2;
int score;
int screenX, screenY;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Bu kısmı internetten araştırıp buldum
//Ekran yükseklik ve genişliğini buluyoruz
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
screenX = size.x;
screenY = size.y;
score = 0;
myImg = findViewById(R.id.imageView);
textView = findViewById(R.id.textView);
textView2 = findViewById(R.id.textView2);
startGame();
}
public void startGame(){
textView2.setText("Score: " + (score = 0));
new CountDownTimer(10000,600){
public void onTick(long milisaniye){
textView.setText("Time: " + milisaniye / 1000);
float rndmX = new Random().nextInt(screenX - myImg.getMeasuredWidth());
float rndmY = new Random().nextInt(screenY - 2 * myImg.getMeasuredHeight());
myImg.setX(rndmX);
myImg.setY(rndmY);
}
public void onFinish(){
textView.setText("Time is Finish!");
myImg.setVisibility(View.INVISIBLE);
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setTitle("Restart");
alert.setMessage("Do you want try again?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
startGame();
myImg.setVisibility(View.VISIBLE);
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this,"Game Over.", Toast.LENGTH_LONG);
}
});
alert.show();
}
}.start();
}
public void imgClick(View view){
score++;
textView2.setText("Score: " + score);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment