Add original project.
This commit is contained in:
1 parent
2ea0f8441d
commit
6e7114a24d
92 files changed
+3462
No files matched your search
@@ -0,0 +1,171 @@
|
||||
package com.hyperling.apps.games;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
|
||||
import com.google.android.gms.ads.AdView;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
|
||||
private Button btnGameBird, btnGameCow, btnGamePig, btnGameChicken, btnGameCat, btnGameSquirrel, btnOptions, btnHighScores, btnHelp;
|
||||
|
||||
private String TAG, sharedPreferencesKey, adsKey, birdScoreKey, chickenScoreKey;
|
||||
|
||||
private SharedPreferences sharedPreferences;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
TAG = getString(R.string.TAG);
|
||||
|
||||
// Keys
|
||||
sharedPreferencesKey = getString(R.string.sharedPreferencesKey);
|
||||
adsKey = getString(R.string.adsKey);
|
||||
birdScoreKey = getString(R.string.birdScoreKey);
|
||||
chickenScoreKey = getString(R.string.chickenScoreKey);
|
||||
|
||||
// Shared Preferences
|
||||
sharedPreferences = getSharedPreferences(sharedPreferencesKey, MODE_PRIVATE);
|
||||
|
||||
// Initialize buttons
|
||||
btnGameBird = (Button) findViewById(R.id.btnBirdGame);
|
||||
btnGameCow = (Button) findViewById(R.id.btnGameCow);
|
||||
btnGamePig = (Button) findViewById(R.id.btnGamePig);
|
||||
btnGameChicken = (Button) findViewById(R.id.btnGameChicken);
|
||||
btnGameCat = (Button) findViewById(R.id.btnGameCat);
|
||||
btnGameSquirrel = (Button) findViewById(R.id.btnGameSquirrel);
|
||||
btnOptions = (Button) findViewById(R.id.btnOptions);
|
||||
btnHighScores = (Button) findViewById(R.id.btnHighScores);
|
||||
btnHelp = (Button) findViewById(R.id.btnHelp);
|
||||
|
||||
// Set up game buttons
|
||||
btnGameBird.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doGameButton((Button) v);
|
||||
}
|
||||
});
|
||||
btnGameCow.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doGameButton((Button) v);
|
||||
}
|
||||
});
|
||||
btnGamePig.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doGameButton((Button) v);
|
||||
}
|
||||
});
|
||||
btnGameChicken.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doGameButton((Button) v);
|
||||
}
|
||||
});
|
||||
btnGameCat.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doGameButton((Button) v);
|
||||
}
|
||||
});
|
||||
btnGameSquirrel.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doGameButton((Button) v);
|
||||
}
|
||||
});
|
||||
|
||||
// Set up other buttons
|
||||
btnOptions.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doOptionsButton();
|
||||
}
|
||||
});
|
||||
btnHighScores.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
doHighScoresButton();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onResume() {
|
||||
displayAds();
|
||||
|
||||
// Append high scores to button text
|
||||
int score;
|
||||
String newText;
|
||||
|
||||
score = sharedPreferences.getInt(birdScoreKey, 0);
|
||||
Log.d(TAG, "MainActivity.onResume(): birdScore=" + score);
|
||||
if (score > 0) {
|
||||
newText = getString(R.string.btnGameBird) + '\n' + getString(R.string.btnHighScoreSubHeader).concat(" ") + score;
|
||||
Log.d(TAG, "MainActivity.onResume(): newText=" + newText);
|
||||
btnGameBird.setText(newText);
|
||||
}
|
||||
else{
|
||||
btnGameBird.setText(getString(R.string.btnGameBird));
|
||||
}
|
||||
|
||||
score = sharedPreferences.getInt(chickenScoreKey, 0);
|
||||
if (score > 0) {
|
||||
newText = getString(R.string.btnGameChicken) + '\n' + getString(R.string.btnHighScoreSubHeader).concat(" ") + score;
|
||||
Log.d(TAG, "MainActivity.onResume(): newText=" + newText);
|
||||
btnGameChicken.setText(newText);
|
||||
}
|
||||
else{
|
||||
btnGameChicken.setText(getString(R.string.btnGameChicken));
|
||||
}
|
||||
|
||||
super.onResume();
|
||||
}
|
||||
|
||||
// Start the game
|
||||
private void doGameButton(Button b){
|
||||
Log.i(TAG, "MainActivity.doCowGameButton()");
|
||||
|
||||
// Only get the game name, not the high score
|
||||
String gameType, buttonText;
|
||||
buttonText = b.getText().toString();
|
||||
int newLine = buttonText.indexOf('\n');
|
||||
if (newLine > 0) {
|
||||
gameType = buttonText.substring(0, buttonText.indexOf('\n'));
|
||||
}
|
||||
else{
|
||||
gameType = buttonText;
|
||||
}
|
||||
|
||||
// Start the GameActivity
|
||||
Intent game = new Intent(MainActivity.this, GameActivity.class);
|
||||
game.setAction(gameType);
|
||||
startActivity(game);
|
||||
}
|
||||
|
||||
// Go to OptionsActivity
|
||||
private void doOptionsButton(){
|
||||
Log.i(TAG, "MainActivity.doOptionsButton()");
|
||||
|
||||
Intent options = new Intent(MainActivity.this, OptionsActivity.class);
|
||||
startActivity(options);
|
||||
}
|
||||
|
||||
private void displayAds(){
|
||||
AdsHelper.toggleAds(sharedPreferences, adsKey, getApplicationContext(), (AdView) findViewById(R.id.adViewMain));
|
||||
}
|
||||
|
||||
private void doHighScoresButton(){
|
||||
Intent highScores = new Intent(MainActivity.this, HighScoreActivity.class);
|
||||
startActivity(highScores);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user