Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2014
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.37 KB | None | 0 0
  1. package com.example.trafficlight;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collections;
  5. import android.annotation.TargetApi;
  6. import android.app.Activity;
  7. import android.graphics.Color;
  8. import android.graphics.drawable.ColorDrawable;
  9. import android.os.Build;
  10. import android.os.Bundle;
  11. import android.os.CountDownTimer;
  12. import android.view.Menu;
  13. import android.view.MenuItem;
  14. import android.view.View;
  15. import android.view.View.OnClickListener;
  16. import android.widget.Button;
  17. import android.widget.TextView;
  18. import android.widget.Toast;
  19.  
  20.  
  21. public class MainActivity extends Activity implements OnClickListener{
  22.    
  23.     TextView topLeft;
  24.     TextView topRight;
  25.     TextView bottomLeft;
  26.     TextView bottomRight;
  27.     Button startButton;
  28.     int random;
  29.     int counterAmountCorrectTiles;
  30.     TextView counter;
  31.     TextView timer;
  32.     private CountDownTimer countDownTimer;
  33.     private long startTime;
  34.    
  35.     private final long interval = 1;
  36.     ArrayList<Integer> randomList = new ArrayList<Integer>();
  37.  
  38.  
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.        
  42.         super.onCreate(savedInstanceState);
  43.         setContentView(R.layout.activity_main);
  44.        
  45.         topLeft = (TextView) findViewById(R.id.topLeft);
  46.         topRight = (TextView) findViewById(R.id.topRight);
  47.         bottomLeft = (TextView) findViewById(R.id.bottomLeft);
  48.         bottomRight = (TextView) findViewById(R.id.bottomRight);
  49.        
  50.         counter = (TextView) findViewById(R.id.counter);
  51.         timer = (TextView) findViewById(R.id.timer);
  52.        
  53.         startButton = (Button) findViewById(R.id.startButton);
  54.         startButton.setOnClickListener(this);
  55.        
  56.         topLeft.setOnClickListener(this);
  57.         topRight.setOnClickListener(this);
  58.         bottomLeft.setOnClickListener(this);
  59.         bottomRight.setOnClickListener(this);
  60.        
  61.  
  62.     }
  63.    
  64.     private void MessageBox(String Message)
  65.     {
  66.         Toast.makeText(getApplicationContext(), Message,
  67.                    Toast.LENGTH_LONG).show();
  68.     }
  69.    
  70.     @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void Counter()
  71.     {  
  72.         IncreaseLevel();
  73.         counterAmountCorrectTiles++;
  74.         counter.setText("Amount: " + String.valueOf(counterAmountCorrectTiles));
  75.     }
  76.    
  77.     @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void onClick(View v) {
  78.        
  79.         switch (v.getId())
  80.         {
  81.         case R.id.startButton:
  82.            
  83.                 startTime = 2000;
  84.                 countDownTimer = new MyCountDownTimer(startTime, interval);
  85.            
  86.                 ChangeTile();
  87.                
  88.                 counterAmountCorrectTiles = 0;
  89.                 countDownTimer.start();
  90.  
  91.             break;
  92.            
  93.         case R.id.bottomLeft:
  94.            
  95.                 ColorDrawable BottomLeftColorDrawable = (ColorDrawable)bottomLeft.getBackground();
  96.                 int colorIdBottomLeft = BottomLeftColorDrawable.getColor();
  97.                
  98.                 if (colorIdBottomLeft != Color.parseColor("#B8B8B8"))
  99.                 {
  100.                     ChangeTile();
  101.                     Counter();
  102.                     countDownTimer.start();
  103.                 }
  104.                 else
  105.                 {
  106.                     GameOver();
  107.                 }
  108.                
  109.                
  110.             break;
  111.        
  112.         case R.id.bottomRight:
  113.            
  114.                 ColorDrawable BottomRightColorDrawable = (ColorDrawable)bottomRight.getBackground();
  115.                 int colorIdBottomRight = BottomRightColorDrawable.getColor();
  116.                
  117.                 if (colorIdBottomRight != Color.parseColor("#B8B8B8"))
  118.                 {
  119.                     ChangeTile();
  120.                     Counter();
  121.                     countDownTimer.start();
  122.                 }
  123.                 else
  124.                 {
  125.                     GameOver();
  126.                 }
  127.                
  128.             break;
  129.            
  130.         case R.id.topLeft:
  131.  
  132.                 ColorDrawable TopLeftColorDrawable = (ColorDrawable)topLeft.getBackground();
  133.                 int colorIdTopLeft = TopLeftColorDrawable.getColor();
  134.                
  135.                 if (colorIdTopLeft != Color.parseColor("#B8B8B8"))
  136.                 {
  137.                     ChangeTile();
  138.                     Counter();
  139.                     countDownTimer.start();
  140.                 }
  141.                 else
  142.                 {
  143.                     GameOver();
  144.                 }
  145.            
  146.             break;
  147.            
  148.         case R.id.topRight:
  149.            
  150.                 ColorDrawable TopRightColorDrawable = (ColorDrawable)topRight.getBackground();
  151.                 int colorIdTopRight = TopRightColorDrawable.getColor();
  152.                
  153.                 if (colorIdTopRight != Color.parseColor("#B8B8B8"))
  154.                 {
  155.                     ChangeTile();
  156.                     Counter();
  157.                     countDownTimer.start();
  158.                 }
  159.                 else
  160.                 {
  161.                     GameOver();
  162.                 }
  163.            
  164.             break;
  165.         }
  166.     }
  167.    
  168.    private void GameOver()
  169.    {
  170.         MessageBox("Game Over!");
  171.        
  172.         countDownTimer.cancel();
  173.         timer.setText("");
  174.        
  175.         topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  176.         topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  177.         bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  178.         bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  179.    }
  180.    
  181.    private void UniqueRandomizer()
  182.    {
  183.        for(int i=0;i<4;i++)
  184.         {
  185.         randomList.add(i);
  186.         }
  187.         Collections.shuffle(randomList);
  188.        
  189.         random = randomList.get(0);
  190.    }
  191.    
  192.    public void IncreaseLevel()
  193.    {
  194.        switch (counterAmountCorrectTiles)
  195.        {
  196.        case 5: startTime = 1500;
  197.                 countDownTimer.cancel();
  198.                 countDownTimer = new MyCountDownTimer(startTime, interval);
  199.        break;
  200.                 case 10: startTime = 1250;
  201.                 countDownTimer.cancel();
  202.                 countDownTimer = new MyCountDownTimer(startTime, interval);
  203.        break;
  204.                case 15: startTime = 1000;
  205.                countDownTimer.cancel();
  206.                countDownTimer = new MyCountDownTimer(startTime, interval);
  207.        break;
  208.                case 20: startTime = 750;
  209.                countDownTimer.cancel();
  210.                countDownTimer = new MyCountDownTimer(startTime, interval);
  211.        break;
  212.                case 25: startTime = 500;
  213.                countDownTimer.cancel();
  214.                countDownTimer = new MyCountDownTimer(startTime, interval);
  215.        break;
  216.                case 30: startTime = 350;
  217.                countDownTimer.cancel();
  218.                countDownTimer = new MyCountDownTimer(startTime, interval);
  219.        break;
  220.                case 35: startTime = 250;
  221.                countDownTimer.cancel();
  222.                countDownTimer = new MyCountDownTimer(startTime, interval);
  223.        break;
  224.                case 40: startTime = 150;
  225.                countDownTimer.cancel();
  226.                countDownTimer = new MyCountDownTimer(startTime, interval);
  227.        break;
  228.                case 45: startTime = 100;
  229.                countDownTimer.cancel();
  230.                countDownTimer = new MyCountDownTimer(startTime, interval);
  231.        break;
  232.        }
  233.    }
  234.    
  235.     private void ChangeTile()
  236.     {  
  237.         UniqueRandomizer();
  238.        
  239.         switch (random)
  240.         {
  241.         case 0:
  242.                 topLeft.setBackgroundColor(0xffff00ff);
  243.                 topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  244.                 bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  245.                 bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  246.         break;
  247.        
  248.         case 1:
  249.                 topRight.setBackgroundColor(0xffff0000);
  250.                 topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  251.                 bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  252.                 bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  253.         break;
  254.  
  255.         case 2:
  256.                bottomLeft.setBackgroundColor(0xffffff00);
  257.                topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  258.                topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  259.                bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  260.         break;
  261.  
  262.         case 3:
  263.                bottomRight.setBackgroundColor(Color.parseColor("#636161"));
  264.                topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
  265.                topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  266.                bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
  267.         break;
  268.         }
  269.        
  270.     }
  271.  
  272.     @Override
  273.     public boolean onCreateOptionsMenu(Menu menu) {
  274.         // Inflate the menu; this adds items to the action bar if it is present.
  275.         getMenuInflater().inflate(R.menu.main, menu);
  276.         return true;
  277.     }
  278.  
  279.     @Override
  280.     public boolean onOptionsItemSelected(MenuItem item) {
  281.         // Handle action bar item clicks here. The action bar will
  282.         // automatically handle clicks on the Home/Up button, so long
  283.         // as you specify a parent activity in AndroidManifest.xml.
  284.         int id = item.getItemId();
  285.         if (id == R.id.action_settings) {
  286.             return true;
  287.         }
  288.         return super.onOptionsItemSelected(item);
  289.     }
  290.    
  291.     public class MyCountDownTimer extends CountDownTimer {
  292.           public MyCountDownTimer(long startTime, long interval) {
  293.            super(startTime, interval);
  294.           }
  295.  
  296.           @Override
  297.           public void onFinish() {
  298.              GameOver();
  299.           }
  300.  
  301.           @Override
  302.           public void onTick(long millisUntilFinished) {
  303.               timer.setText("Timer: " + millisUntilFinished);
  304.           }
  305.          }
  306. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement