Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.trafficlight;
- import java.util.ArrayList;
- import java.util.Collections;
- import android.annotation.TargetApi;
- import android.app.Activity;
- import android.graphics.Color;
- import android.graphics.drawable.ColorDrawable;
- import android.os.Build;
- import android.os.Bundle;
- import android.os.CountDownTimer;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MainActivity extends Activity implements OnClickListener{
- TextView topLeft;
- TextView topRight;
- TextView bottomLeft;
- TextView bottomRight;
- Button startButton;
- int random;
- int counterAmountCorrectTiles;
- TextView counter;
- TextView timer;
- private CountDownTimer countDownTimer;
- private long startTime;
- private final long interval = 1;
- ArrayList<Integer> randomList = new ArrayList<Integer>();
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- topLeft = (TextView) findViewById(R.id.topLeft);
- topRight = (TextView) findViewById(R.id.topRight);
- bottomLeft = (TextView) findViewById(R.id.bottomLeft);
- bottomRight = (TextView) findViewById(R.id.bottomRight);
- counter = (TextView) findViewById(R.id.counter);
- timer = (TextView) findViewById(R.id.timer);
- startButton = (Button) findViewById(R.id.startButton);
- startButton.setOnClickListener(this);
- topLeft.setOnClickListener(this);
- topRight.setOnClickListener(this);
- bottomLeft.setOnClickListener(this);
- bottomRight.setOnClickListener(this);
- }
- private void MessageBox(String Message)
- {
- Toast.makeText(getApplicationContext(), Message,
- Toast.LENGTH_LONG).show();
- }
- @TargetApi(Build.VERSION_CODES.HONEYCOMB) private void Counter()
- {
- IncreaseLevel();
- counterAmountCorrectTiles++;
- counter.setText("Amount: " + String.valueOf(counterAmountCorrectTiles));
- }
- @TargetApi(Build.VERSION_CODES.HONEYCOMB) public void onClick(View v) {
- switch (v.getId())
- {
- case R.id.startButton:
- startTime = 2000;
- countDownTimer = new MyCountDownTimer(startTime, interval);
- ChangeTile();
- counterAmountCorrectTiles = 0;
- countDownTimer.start();
- break;
- case R.id.bottomLeft:
- ColorDrawable BottomLeftColorDrawable = (ColorDrawable)bottomLeft.getBackground();
- int colorIdBottomLeft = BottomLeftColorDrawable.getColor();
- if (colorIdBottomLeft != Color.parseColor("#B8B8B8"))
- {
- ChangeTile();
- Counter();
- countDownTimer.start();
- }
- else
- {
- GameOver();
- }
- break;
- case R.id.bottomRight:
- ColorDrawable BottomRightColorDrawable = (ColorDrawable)bottomRight.getBackground();
- int colorIdBottomRight = BottomRightColorDrawable.getColor();
- if (colorIdBottomRight != Color.parseColor("#B8B8B8"))
- {
- ChangeTile();
- Counter();
- countDownTimer.start();
- }
- else
- {
- GameOver();
- }
- break;
- case R.id.topLeft:
- ColorDrawable TopLeftColorDrawable = (ColorDrawable)topLeft.getBackground();
- int colorIdTopLeft = TopLeftColorDrawable.getColor();
- if (colorIdTopLeft != Color.parseColor("#B8B8B8"))
- {
- ChangeTile();
- Counter();
- countDownTimer.start();
- }
- else
- {
- GameOver();
- }
- break;
- case R.id.topRight:
- ColorDrawable TopRightColorDrawable = (ColorDrawable)topRight.getBackground();
- int colorIdTopRight = TopRightColorDrawable.getColor();
- if (colorIdTopRight != Color.parseColor("#B8B8B8"))
- {
- ChangeTile();
- Counter();
- countDownTimer.start();
- }
- else
- {
- GameOver();
- }
- break;
- }
- }
- private void GameOver()
- {
- MessageBox("Game Over!");
- countDownTimer.cancel();
- timer.setText("");
- topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- }
- private void UniqueRandomizer()
- {
- for(int i=0;i<4;i++)
- {
- randomList.add(i);
- }
- Collections.shuffle(randomList);
- random = randomList.get(0);
- }
- public void IncreaseLevel()
- {
- switch (counterAmountCorrectTiles)
- {
- case 5: startTime = 1500;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 10: startTime = 1250;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 15: startTime = 1000;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 20: startTime = 750;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 25: startTime = 500;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 30: startTime = 350;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 35: startTime = 250;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 40: startTime = 150;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- case 45: startTime = 100;
- countDownTimer.cancel();
- countDownTimer = new MyCountDownTimer(startTime, interval);
- break;
- }
- }
- private void ChangeTile()
- {
- UniqueRandomizer();
- switch (random)
- {
- case 0:
- topLeft.setBackgroundColor(0xffff00ff);
- topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- break;
- case 1:
- topRight.setBackgroundColor(0xffff0000);
- topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- break;
- case 2:
- bottomLeft.setBackgroundColor(0xffffff00);
- topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- break;
- case 3:
- bottomRight.setBackgroundColor(Color.parseColor("#636161"));
- topRight.setBackgroundColor(Color.parseColor("#B8B8B8"));
- topLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- bottomLeft.setBackgroundColor(Color.parseColor("#B8B8B8"));
- break;
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public class MyCountDownTimer extends CountDownTimer {
- public MyCountDownTimer(long startTime, long interval) {
- super(startTime, interval);
- }
- @Override
- public void onFinish() {
- GameOver();
- }
- @Override
- public void onTick(long millisUntilFinished) {
- timer.setText("Timer: " + millisUntilFinished);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement