Skip to content

Instantly share code, notes, and snippets.

@emiremen
Last active Sep 8, 2022
Embed
What would you like to do?
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