Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.91 KB | None | 0 0
  1. package com.example.mariu.pizzaclicker;
  2.  
  3. import android.content.SharedPreferences;
  4. import android.preference.PreferenceManager;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.widget.Button;
  8. import android.widget.ImageButton;
  9. import android.widget.TextView;
  10. import android.view.View;
  11. import android.widget.Toast;
  12.  
  13. public class MainActivity extends AppCompatActivity {
  14.  
  15. //verdier
  16. public long pizza;
  17. public String pizzaKeyString = "Pizzas";
  18.  
  19. //PPC = Pizza Per Click
  20. public long ppc;
  21. public String ppcKeyString = "ppc";
  22.  
  23. //PPS = Pizza Per Sekund
  24. public long pps;
  25. public String ppsKeyString = "pps";
  26.  
  27. public long shopPPC;
  28. public String shopPPCKeyString = "shopPPC";
  29.  
  30. public long shopPPS;
  31. public String shopPPSKeyString = "shopPPS";
  32.  
  33. //views
  34. public TextView pizzaView;
  35. public TextView ppsAndppcView;
  36. public ImageButton pizzaBtn;
  37. public Button sauceBtn;
  38. public Button cheeseBtn;
  39.  
  40. @Override
  41. protected void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.activity_main);
  44.  
  45. loadPrefs();
  46. initialize();
  47.  
  48. Thread thread = new Thread() {
  49. public void run(){
  50. try{
  51. while(!isInterrupted()){
  52. Thread.sleep(1000);
  53.  
  54. runOnUiThread(new Runnable() {
  55. @Override
  56. public void run() {
  57. pizza += pps;
  58. pizzaView.setText(pizza + " Pizzas");
  59.  
  60. savePrefs(pizzaKeyString, pizza);
  61. }
  62. });
  63.  
  64. }
  65. }catch(InterruptedException ie) {
  66. ie.printStackTrace();
  67. }
  68. }
  69. };
  70. thread.start();
  71. }
  72. //Henter spill-sesjon fra forrige gang.
  73. public void loadPrefs(){
  74. SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  75.  
  76. long pizzaKey = sharedPrefs.getLong(pizzaKeyString, 0);
  77. pizza = pizzaKey;
  78.  
  79. long ppcKey = sharedPrefs.getLong(ppcKeyString, 1);
  80. ppc = ppcKey;
  81.  
  82. long ppsKey = sharedPrefs.getLong(ppsKeyString, 0);
  83. pps = ppsKey;
  84.  
  85. long shopPPCKey = sharedPrefs.getLong(shopPPCKeyString, 10);
  86. shopPPC = shopPPCKey;
  87.  
  88. long shopPPSKey = sharedPrefs.getLong(shopPPSKeyString, 20);
  89. shopPPS = shopPPSKey;
  90.  
  91. }
  92. //Lagrer spill-sesjon.
  93. public void savePrefs(String key, long values){
  94.  
  95. SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
  96. SharedPreferences.Editor editor = sharedPrefs.edit();
  97. editor.putLong(key, values);
  98. editor.commit();
  99.  
  100. }
  101.  
  102. public void initialize(){
  103.  
  104. pizzaView = (TextView) findViewById(R.id.pizzaView);
  105. pizzaView.setText(pizza + " Pizzas");
  106.  
  107. ppsAndppcView = (TextView) findViewById(R.id.ppsAndppcView);
  108. ppsAndppcView.setText(pps + " PPS |"+ ppc + " PPC");
  109.  
  110. pizzaBtn = (ImageButton) findViewById(R.id.pizzaBtn);
  111.  
  112. sauceBtn = (Button) findViewById(R.id.sauceBtn);
  113. sauceBtn.setText("Sauce (1x PPC) Price: " + shopPPC);
  114.  
  115. cheeseBtn = (Button) findViewById(R.id.cheeseBtn);
  116. cheeseBtn.setText("Cheese (1x PPS) Price: " + shopPPS);
  117. }
  118.  
  119. public void pizzaBtn (View view) {
  120. pizza += ppc;
  121. pizzaView.setText(pizza + " Pizzas");
  122.  
  123. savePrefs(pizzaKeyString, pizza);
  124.  
  125. }
  126.  
  127. public void shopPPC (View view) {
  128. if(pizza >= shopPPC){
  129. pizza -= shopPPC;
  130. ppc *= 2;
  131.  
  132. shopPPC *= 1.5;
  133.  
  134. sauceBtn.setText("Sauce (1x PPC) Price: " + shopPPC);
  135. ppsAndppcView.setText(pps + " PPS |"+ ppc + " PPC");
  136. pizzaView.setText(pizza + " Pizzas");
  137.  
  138. savePrefs(shopPPCKeyString, shopPPC);
  139. savePrefs(ppcKeyString, ppc);
  140. savePrefs(pizzaKeyString, pizza);
  141.  
  142. }
  143.  
  144. else{
  145. Toast.makeText(this, "You need more pizzas", Toast.LENGTH_SHORT).show();
  146.  
  147. }
  148. }
  149.  
  150. public void shopPPS (View view) {
  151.  
  152. if(pizza >= shopPPS){
  153. pizza -= shopPPS;
  154. pps += 1;
  155.  
  156. shopPPS *= 1.5;
  157.  
  158. cheeseBtn.setText("Cheese (1x PPS) Price: " + shopPPS);
  159. ppsAndppcView.setText(pps + " PPS |"+ ppc + " PPC");
  160. pizzaView.setText(pizza + " Pizzas");
  161.  
  162. savePrefs(shopPPSKeyString, shopPPS);
  163. savePrefs(ppsKeyString, pps);
  164. savePrefs(pizzaKeyString, pizza);
  165.  
  166. }
  167.  
  168. else{
  169. Toast.makeText(this, "You need more pizzas", Toast.LENGTH_SHORT).show();
  170.  
  171. }
  172.  
  173. }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement