728x90
반응형
해당 소스는 안드로이드 버전이 낮으면 실행 안됩니다.
기능
SharedPreferences를 이용한 세이브 / 로드 | 화면이 꺼지지 않음 |
GIF를 이용한 백그라운드와 터치 이미지 설정 | 터치 좌표 읽기 |
타이머 | 숫자를 줄여서 표현 |
종료(뒤로가기)버튼 터치시 확인창 띄우기 | 온클릭 속성 값을 스위치로 묶음 |
소스 전문
ㅇ XML 및 이미지가 필요하시면 메일을 남겨주세요. ㅇ
ㅇ XML 및 이미지가 필요하시면 메일을 남겨주세요. ㅇ
package com.example.clicker;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
///////////// 기본 변수 선언 구간 /////////////////
int gold = 0; int goldPerSeconds = 1; int goldPerClick = 1; int upgradeGold = 38;
int[] level = new int[10];
/////////////////////////////////////////////////
////////// 효과음 설정 구간 //////////////////////
SoundPool soundPool_click; SoundPool soundPool_upgrade; int soundPlay_click; int soundPlay_upgrade;
/////////////////////////////////////////////////
TextView textViewGold; TextView textViewGoldPerSeconds; TextView textViewGoldPerClick;
TextView textViewGoldPerClickUpgrade; TextView textViewGoldPerSecondsUpgrade;
ImageView imageView_bg; ImageView imageView_coin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//////////////// 화면 꺼지지 않게 설정하기 ///////////////
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
///////////////////////////////////////////////////////
///////////// 화면 터치 효과음 설정 ////////////////////
soundPool_click = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
soundPlay_click = soundPool_click.load(this, R.raw.sound_alter, 1);
///////////////////////////////////////////////////////
////////// 업그레이드 버튼 터치 효과음 설정 //////////////
soundPool_upgrade = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
soundPlay_upgrade = soundPool_upgrade.load(this, R.raw.sound_upgrade, 1);
///////////////////////////////////////////////////////
///////// 업그레이드 레벨 설정 //////////
for(int i = 0; i < 10; i++) {
level[i] = 1;
}
///////////////////////////////////////
///////// 백그라운드 이미지 설정 ( gif 파일 ) ///////////
imageView_bg = findViewById(R.id.imageView_backGround);
Glide.with(this).load(R.raw.img_background).into(imageView_bg);
imageView_coin = findViewById(R.id.imageView_coin);
Glide.with(this).load(R.raw.img_touchcoin).into(imageView_coin);
/////////////// 배경 이미지 추가 (gif)/////////////
imageView_bg.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getX();
float y = event.getY();
imageView_coin.setX(x);
imageView_coin.setY(y-300);
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
if (imageView_coin.getVisibility() == View.GONE) {
imageView_coin.setVisibility(View.VISIBLE);
handler.postDelayed(this, 150);
}
else if (imageView_coin.getVisibility() == View.VISIBLE) {
imageView_coin.setVisibility(View.GONE);
}
}
}, 0);
}
return false;
}
});
//////////////// 텍스트뷰 아이디 설정 //////////////
textViewGold = findViewById(R.id.textViewCurrentGold);
textViewGoldPerSeconds = findViewById(R.id.textViewSecondsGold);
textViewGoldPerClick = findViewById(R.id.textViewTouchGold);
textViewGoldPerClickUpgrade = findViewById(R.id.textViewGoldPerClickUpgrade);
textViewGoldPerSecondsUpgrade = findViewById(R.id.textViewGoldPerSecondsUpgrade);
/////////////////////////////////////////////////
////////////// 저장된 테이터 불러오기 /////////////
checker();
////////////////////////////////////////////////
//////// 타이머 설정 - 게임 실행 1초 이후에 1초당 골드 추가 + 텍스트 설정(변경) /////////
Timer timer = new Timer();
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
gold += goldPerSeconds;
displayGold();
displayGoldPerClick();
displayGoldPerSeconds();
}
};
timer.schedule(timerTask, 1000 , 1000); // 1초마다 골드내역 갱신
///////////////////////////////////////////////////////////////////////////
///////////// 타이머 설정 - 게임 실행 10초 이후에 10초마다 자동 저장 ///////////////////
Timer saveTimer = new Timer();
TimerTask saveTimerTask = new TimerTask() {
@Override
public void run() {
saved();
}
};
saveTimer.schedule(saveTimerTask, 5000, 10000); // 10초마다 저장
///////////////////////////////////////////////////////////////////////////////////
//////////// 버튼 초기 기본 텍스트 설정 ///////////////
displayUpgradeClick();
displayUpgradeSeconds();
///////////////////////////////////////////////////
}
private void displayGoldPerSeconds() {
if(goldPerSeconds < 1000) {
textViewGoldPerSeconds.setText(""+goldPerSeconds);
}
else if(goldPerSeconds >= 1000 && goldPerSeconds <= 999000) {
textViewGoldPerSeconds.setText((float)goldPerSeconds/1000 + "K");
}
else if(goldPerSeconds > 10000 && goldPerSeconds <= 9990000) {
textViewGoldPerSeconds.setText((float)goldPerSeconds/10000 + "M");
}
else if(goldPerSeconds > 100000 && goldPerSeconds <= 99900000) {
textViewGoldPerSeconds.setText((float)goldPerSeconds/100000 + "B");
}
else if(goldPerSeconds > 1000000 && goldPerSeconds <= 999000000) {
textViewGoldPerSeconds.setText((float)goldPerSeconds/1000000 + "T");
}
else if(goldPerSeconds > 10000000 && goldPerSeconds <= 2100000000) {
textViewGoldPerSeconds.setText((float)goldPerSeconds/10000000 + "q");
}
}
private void displayGoldPerClick() {
if(goldPerClick < 1000) {
textViewGoldPerClick.setText(""+goldPerClick);
}
else if(goldPerClick >= 1000 && goldPerClick <= 999000) {
textViewGoldPerClick.setText((float)goldPerClick/1000 + "K");
}
else if(goldPerClick > 10000 && goldPerClick <= 9990000) {
textViewGoldPerClick.setText((float)goldPerClick/10000 + "M");
}
else if(goldPerClick > 100000 && goldPerClick <= 99900000) {
textViewGoldPerClick.setText((float)goldPerClick/100000 + "B");
}
else if(goldPerClick > 1000000 && goldPerClick <= 999000000) {
textViewGoldPerClick.setText((float)goldPerClick/1000000 + "T");
}
else if(goldPerClick > 10000000 && goldPerClick <= 2100000000) {
textViewGoldPerClick.setText((float)goldPerClick/10000000 + "q");
}
}
private void displayGold() {
if(gold < 1000) {
textViewGold.setText(""+gold);
}
else if(gold >= 1000 && gold <= 999000) {
textViewGold.setText((float)gold/1000 + "K");
}
else if(gold > 10000 && gold <= 9990000) {
textViewGold.setText((float)gold/10000 + "M");
}
else if(gold > 100000 && gold <= 99900000) {
textViewGold.setText((float)gold/100000 + "B");
}
else if(gold > 1000000 && gold <= 999000000) {
textViewGold.setText((float)gold/1000000 + "T");
}
else if(gold > 10000000 && gold <= 2100000000) {
textViewGold.setText((float)gold/10000000 + "q");
}
}
/////////////////////// 뒤로가기 버튼 누를시 확인 다이얼로그 창 띄우기 /////////////////////
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("종료하시겠습니까?"); // 다이얼로그 메시지 설정
builder.setPositiveButton("예", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) { // 예 선택시 저장 후 종료
saved();
finish();
}
});
builder.setNegativeButton("아니오", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
} // 아니오 누를시 변화없음
});
builder.setTitle("게임 종료"); // 다이얼로그 타이틀 설정
builder.show(); // 다이얼로그 보여주기
}
////////////////////////////////////////////////////////////////////////////////////////
////////////////////// onClick값 중 onClickGame를 가진 뷰들 처리 /////////////////////////
public void onClickGame(View view) {
switch (view.getId()){
case R.id.imageView_backGround:
gold += goldPerClick;
displayGold();
soundPool_click.play(soundPlay_click, 1f, 1f, 0, 0, 1f);
break;
case R.id.buttonClickUpgrade:
if(gold >= level[0] * upgradeGold) {
gold -= level[0] * upgradeGold;
goldPerClick += 5;
level[0]++;
displayGold();
displayGoldPerClick();
displayUpgradeClick();
soundPool_upgrade.play(soundPlay_upgrade, 1f, 1f, 0, 0, 1f);
}
else {
Toast.makeText(this, "골드가 부족합니다.", Toast.LENGTH_SHORT).show();
}
break;
// onClickGame이 실행된 뷰가 초당 골드 업그레이드 버튼일때
case R.id.buttonSecondsUpgrade:
if(gold >= level[1] * upgradeGold) {
gold -= level[1] * upgradeGold;
goldPerSeconds += 3;
level[1]++;
displayGold();
displayGoldPerSeconds();
displayUpgradeSeconds();
soundPool_upgrade.play(soundPlay_upgrade, 1f, 1f, 0, 0, 1f);
}
else {
Toast.makeText(this, "골드가 부족합니다.", Toast.LENGTH_SHORT).show();
}
break;
case R.id.buttonClick100:
gold += goldPerClick * 100;
displayGold();
soundPool_upgrade.play(soundPlay_upgrade, 1f, 1f, 0, 0, 1f);
break;
case R.id.buttonSeconds100:
gold += goldPerSeconds * 100;
displayGold();
soundPool_upgrade.play(soundPlay_upgrade, 1f, 1f, 0, 0, 1f);
case R.id.button33:
gold = 0;
goldPerSeconds = 1;
goldPerClick = 1;
level[0] = 1;
level[1] = 1;
saved();
finish();
///////////////////////////////////////////////////////////////////////////////////////
}
}
private void displayUpgradeClick() {
if(level[0] * upgradeGold < 1000) {
textViewGoldPerClickUpgrade.setText("" + (level[0] * upgradeGold));
}
else if(level[0] * upgradeGold >= 1000 && level[0] * upgradeGold <= 999000) {
textViewGoldPerClickUpgrade.setText((float)level[0] * upgradeGold / 1000 + "K");
}
else if(level[0] * upgradeGold > 10000 && level[0] * upgradeGold <= 9990000) {
textViewGoldPerClickUpgrade.setText((float)level[0] * upgradeGold / 10000 + "M");
}
else if(level[0] * upgradeGold > 100000 && level[0] * upgradeGold <= 99900000) {
textViewGoldPerClickUpgrade.setText((float)level[0] * upgradeGold / 100000 + "B");
}
else if(level[0] * upgradeGold > 1000000 && level[0] * upgradeGold <= 999000000) {
textViewGoldPerClickUpgrade.setText((float)level[0] * upgradeGold / 1000000+ "T");
}
else if(level[0] * upgradeGold > 10000000 && level[0] * upgradeGold <= 2100000000) {
textViewGoldPerClickUpgrade.setText((float)level[0] * upgradeGold / 10000000+ "q");
}
}
private void displayUpgradeSeconds() {
if(level[1] * upgradeGold < 1000) {
textViewGoldPerSecondsUpgrade.setText("" + (level[1] * upgradeGold));
}
else if(level[1] * upgradeGold >= 1000 && level[1] * upgradeGold <= 999000) {
textViewGoldPerSecondsUpgrade.setText((float)level[1] * upgradeGold / 1000 + "K");
}
else if(level[1] * upgradeGold > 10000 && level[1] * upgradeGold <= 9990000) {
textViewGoldPerSecondsUpgrade.setText((float)level[1] * upgradeGold / 10000 + "M");
}
else if(level[1] * upgradeGold > 100000 && level[1] * upgradeGold <= 99900000) {
textViewGoldPerSecondsUpgrade.setText((float)level[1] * upgradeGold / 100000 + "B");
}
else if(level[1] * upgradeGold > 1000000 && level[1] * upgradeGold <= 999000000) {
textViewGoldPerSecondsUpgrade.setText((float)level[1] * upgradeGold / 1000000 + "T");
}
else if(level[1] * upgradeGold > 10000000 && level[1] * upgradeGold <= 2100000000) {
textViewGoldPerSecondsUpgrade.setText((float)level[1] * upgradeGold / 10000000 + "q");
}
}
////////////////////////////// 저장과 불러오기 //////////////////////////////
public void saved() { // 저장
SharedPreferences sf = getSharedPreferences("Save", MODE_PRIVATE);
SharedPreferences.Editor editor = sf.edit();
editor.putInt("Gold", gold);
editor.putInt("GoldPerSeconds", goldPerSeconds);
editor.putInt("GoldPerClick", goldPerClick);
editor.putInt("level0", level[0]);
editor.putInt("level1", level[1]);
editor.apply();
}
public void checker() { // 불러오기
SharedPreferences sf = getSharedPreferences("Save", MODE_PRIVATE);
gold = sf.getInt("Gold", 1);
goldPerSeconds = sf.getInt("GoldPerSeconds", 1);
goldPerClick = sf.getInt("GoldPerClick", 1);
level[0] = sf.getInt("level0", 1);
level[1] = sf.getInt("level1", 1);
}
}
저작권 문제가 없는 이미지로 구성했던 것으로 기억하는데, 혹시 모르니 이미지 사용에 주의하시기 바랍니다.
728x90
반응형
'안드로이드' 카테고리의 다른 글
do it 안드로이드 도전과제 도전 01 ~ 02 (0) | 2019.11.27 |
---|---|
안드로이드 스튜디오 프로그레스바 사용하기 ( 원형 ) (0) | 2019.11.16 |
안드로이드 스튜디오 버튼을 누르면 이동(인텐트) 2가지 방법 (0) | 2019.10.31 |
안드로이드 스튜디오 간단한 이미지뷰 활용방법 2가지 (0) | 2019.10.31 |
안드로이드 스튜디오 버튼클릭 효과음 + 기본 효과음 제거 2가지 방법 (0) | 2019.10.31 |