How to build and Android triangles color GAME
Comment transferer de l'argent dans un compte en banque aux USA sans payer les frais de 45$ exiger par les Banques Haitiennes ou tout autre banque: MoneyOnline
Watch how it's work on youtube: https://www.youtube.com/watch?v=k4YgGIpRs7k
This is the JAVA class that will execute your code:
Jeux.JAVA (copy and paste the source code below in you java class, you can use the same name(Jeux.java) if you want to:
package jbej90.automation.test.consht; // replace with your package name
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
import static android.support.v4.view.ViewCompat.setRotation;
public class Jeux extends AppCompatActivity {
ImageView iv_button, iv_arrow;
TextView tv_points;
ProgressBar progressBar ;
Handler handler;
Runnable runnable;
Random r;
private final static int STATE_BLUE= 1;
private final static int STATE_RED = 2;
private final static int STATE_YELLOW = 3;
private final static int STATE_GREEN = 4;
int buttonState = STATE_BLUE;
int arrowState = STATE_BLUE;
int currentTime = 4000;
int startTime = 4000;
int currentPoints = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.jeux); // the layout xml that content xml of the game
iv_button = (ImageView) findViewById(R.id.iv_button);
iv_arrow = (ImageView) findViewById(R.id.iv_arrow);
tv_points = (TextView) findViewById(R.id.tv_points);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(startTime);
progressBar.setProgress(startTime);
tv_points.setText("Points: " + currentPoints);
r = new Random();
arrowState = r.nextInt(4 )+ 1;
setArrowImage(arrowState);
iv_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setButtonImage(setButtonPosition(buttonState));
}
});
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
currentTime = currentTime -100;
progressBar.setProgress(currentTime);
if (currentTime > 0){
handler.postDelayed(runnable, 100);
}
else{
if (buttonState == arrowState){
currentPoints = currentPoints + 1;
tv_points.setText("Points: " + currentPoints);
startTime = startTime -100;
if (startTime < 1000){
startTime = 2000;
}
progressBar.setMax(startTime);
currentTime = startTime;
progressBar.setProgress(currentTime);
arrowState = r.nextInt(4) + 1;
setArrowImage(arrowState);
handler.postDelayed(runnable, 100);
}
else{
iv_button.setEnabled(false);
Toast.makeText(Jeux.this, "Game Over", Toast.LENGTH_SHORT).show();
}
}
}
};
handler.postDelayed(runnable, 100);
}
private void setArrowImage(int state ){
switch (state){
case STATE_BLUE:
iv_arrow.setImageResource(R.drawable.ic_blue);
arrowState = STATE_BLUE;
break;
case STATE_RED:
iv_arrow.setImageResource(R.drawable.ic_red);
arrowState = STATE_RED;
break;
case STATE_YELLOW:
iv_arrow.setImageResource(R.drawable.ic_yellow);
arrowState = STATE_YELLOW;
break;
case STATE_GREEN:
iv_arrow.setImageResource(R.drawable.ic_green);
arrowState = STATE_GREEN;
break;
}
}
private void setRotation(final ImageView image, final int drawable){
RotateAnimation rotateAnimation = new RotateAnimation(0, 90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f );
rotateAnimation.setDuration(100);
rotateAnimation.setInterpolator(new LinearInterpolator());
rotateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
image.setImageResource( drawable);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
image.startAnimation(rotateAnimation);
}
private int setButtonPosition(int position){
position = position +1;
if (position == 5){
position = 1;
}
return position;
}
private void setButtonImage(int state){
switch (state){
case STATE_BLUE:
setRotation(iv_button, R.drawable.ic_button_blue);
buttonState = STATE_BLUE;
break;
case STATE_RED:
setRotation(iv_button, R.drawable.ic_button_red);
buttonState = STATE_RED;
break;
case STATE_YELLOW:
setRotation(iv_button, R.drawable.ic_button_yellow);
buttonState = STATE_YELLOW;
break;
case STATE_GREEN:
setRotation(iv_button, R.drawable.ic_button_green);
buttonState = STATE_GREEN;
break;
}
}
}
jeux.xml (this is the layout that content points textview and progress bar showing up below triangles)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:padding="20dp"
android:layout_height="match_parent"
tools:context=".Jeux">
<TextView
android:text="Point: 0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/tv_points"
/>
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_below="@+id/tv_points"
android:layout_centerHorizontal="true"
android:id="@+id/progressBar"/>
<include layout="@layout/layout_buttons"
android:layout_width="match_parent"
android:layout_marginTop="20dp"
android:layout_below="@id/progressBar"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
layout_button.xml ( this is the layout that content all 4 triangles images )
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView android:layout_width="match_parent"
android:scaleType="centerInside"
android:layout_weight="1"
android:src="@drawable/ic_blue"
android:id="@+id/iv_arrow"
android:layout_height="match_parent"/>
<ImageView android:layout_width="match_parent"
android:scaleType="centerInside"
android:layout_weight="1"
android:src="@drawable/ic_button_blue"
android:id="@+id/iv_button"
android:layout_height="match_parent"/>
</LinearLayout>
NB: Finally you need one JAVA class( Jeux.java)
Two XML layout:
1) jeux.xml and
2) layout_buttons
3) Image of triangles are below or clic here to download the folder:https://drive.google.com/drive/folders/1pruX_YBMPrj-aCZjoIdbMS130drVY6JF?usp=sharing








Commentaires
Enregistrer un commentaire