Touching Game

XML File:
<?xml version="1.0"  
encoding="utf-8"?>
 <LinearLayout 
 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 xmlns:tools="http://schemas.android.com/tools"     
android:layout_width="match_parent" 
 android:layout_height="match_parent"     
android:orientation="vertical"     
android:gravity="center"     
tools:context=".MainActivity">
    <TextView 
 android:id="@+id/score"         
android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_marginLeft="110dp" 
 />

    <ImageView         
android:id="@+id/img" 
 android:layout_width="wrap_content"         
 android:layout_height="wrap_content" 
 android:src="@mipmap/ic_launcher"         
android:scaleX=".5"         
android:scaleY=".5"/>
    <ImageView 
 android:id="@+id/food" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:src="@mipmap/ic_launcher_round"         
android:scaleX=".5"         
android:scaleY=".5"        />


    <Button 
 android:layout_marginTop="200dp" 
 android:id="@+id/up" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="UP" />
    <LinearLayout         
android:layout_width="wrap_content"         
android:layout_height="wrap_content">

    <Button 
 android:id="@+id/left"         
android:layout_marginRight="0dp" 
 android:layout_width="wrap_content"         
android:layout_height="wrap_content"         
android:text="Left" />

        <Button 
 android:id="@+id/size"             
android:layout_width="wrap_content"             
android:layout_height="wrap_content"             
android:layout_marginLeft="50dp" 
 android:layout_marginRight="40dp" 
 android:text="Size" />
    <Button        android:id="@+id/right" 
 android:layout_marginLeft="10dp" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="right"         
/>
    </LinearLayout>
    <Button         
android:id="@+id/down" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"         
android:text="down" />


    <TextView        android:id="@+id/txt" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" />

</LinearLayout>
 
 
 
 
 
 
 
 
Java Code: 
 
 
package com.example.dell.munna;

import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ZoomControls;

public class MainActivity extends AppCompatActivity {

    int t=0;
    private TextView txt,score;
    private ImageView img,food;
    private Button left,right,up,down,size;

    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        food = (ImageView) findViewById(R.id.food);
        img = (ImageView) findViewById(R.id.img);
        left = (Button) findViewById(R.id.left);
        right = (Button) findViewById(R.id.right);
        up = (Button) findViewById(R.id.up);
        down = (Button) findViewById(R.id.down);
        size = (Button) findViewById(R.id.size);
        txt = (TextView) findViewById(R.id.txt);
        score = (TextView) findViewById(R.id.score);

        img.setX(0);
        img.setY(0);
        food.setX(0);
        food.setY(0);

        right.setOnClickListener(new View.OnClickListener() {
            float x;
            float y;

            @Override            public void onClick(View v) {
                x = img.getX();
                y = img.getY();

                float a;
                float b;
                b = food.getY();
                a = food.getX();

                if(((x+20)>=a&&(x-20)<=a)&&((y+20)>=b&&(y-20)<=b)){
                    t++;
                    score.setText("Score"+t);
                    food.setX((x+1028)%599);
                    food.setY((y+560)%599);
                }
                //txt.setText(x + " " + y + " " + a +" " + b + " ");                x+=20;
                img.setX(x);
                img.setY(y);
                txt.setText(x + " " + y + " " + a +" " + b + " ");
            }
        });

        left.setOnClickListener(new View.OnClickListener() {
            float x;
            float y;

            @Override            public void onClick(View v) {
                x = img.getX();
                y = img.getY();

                float a;
                float b;
                b = food.getY();
                a = food.getX();

                if(((x+20)>=a&&(x-20)<=a)&&((y+20)>=b&&(y-20)<=b)){
                    t++;
                    score.setText("Score"+t);
                    food.setX((x+1028)%599);
                    food.setY((y+560)%599);
                }
                   // txt.setText(x + " " + y + " " + a +" " + b + " ");                x-=20;
                img.setX(x);
                img.setY(y);
                txt.setText(x + " " + y + " " + a +" " + b + " ");
            }
        });

        up.setOnClickListener(new View.OnClickListener() {
            float x;
            float y;
            @Override            public void onClick(View v) {

                x = img.getX();
                y = img.getY();


                float a;
                float b;
                b = food.getY();
                a = food.getX();
                if(((x+20)>=a&&(x-20)<=a)&&((y+20)>=b&&(y-20)<=b)){
                    t++;
                    score.setText("Score"+t);
                    food.setX((x+1028)%599);
                    food.setY((y+560)%599);
                }
                txt.setText(x + " " + y + " " + a +" " + b + " ");

                y-=20;
                img.setX(x);
                img.setY(y);
                txt.setText(x + " " + y + " " + a +" " + b + " ");
            }
        });


        down.setOnClickListener(new View.OnClickListener() {
            float x;
            float y;

            @Override            public void onClick(View v) {
                x = img.getX();
                y = img.getY();
                float a;
                float b;
                b = food.getY();
                a = food.getX();

                if(((x+20)>=a&&(x-20)<=a)&&((y+20)>=b&&(y-20)<=b)){
                    t++;
                    score.setText("Score"+t);
                    food.setX((x+1028)%599);
                    food.setY((y+560)%599);

                }

                y+=20;
                img.setX(x);
                img.setY(y);
                txt.setText(x + " " + y + " " + a +" " + b + " ");
            }
        });

        size.setOnClickListener(new View.OnClickListener() {
            float x,y;
            @Override            public void onClick(View v) {
                x=img.getScaleX();
                y=img.getScaleY();
                img.setScaleY(y+1);
                img.setScaleX(x+1);

                txt.setText(x + " " + y);
            }
        });

    }
}

No comments