Untitled

 avatar
unknown
plain_text
2 months ago
2.3 kB
2
Indexable
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class KnifeGameActivity extends Activity {

    private int playerHP = 100;
    private int opponentHP = 100;
    private int currentLevel = 1;
    private TextView statusText;
    private Button attackButton1, attackButton2, attackButton3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_knife_game);

        statusText = findViewById(R.id.statusText);
        attackButton1 = findViewById(R.id.attackButton1);
        attackButton2 = findViewById(R.id.attackButton2);
        attackButton3 = findViewById(R.id.attackButton3);

        updateStatus();

        attackButton1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attack(10);
            }
        });

        attackButton2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attack(20);
            }
        });

        attackButton3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                attack(30);
            }
        });
    }

    private void attack(int damage) {
        opponentHP -= damage;
        if (opponentHP <= 0) {
            currentLevel++;
            opponentHP = 100; // Reset opponent HP for next level
            if (currentLevel > 500) {
                // Game completed
                statusText.setText("You have completed the game!");
                return;
            }
        }
        playerAttack();
        updateStatus();
    }

    private void playerAttack() {
        playerHP -= (int) (Math.random() * 20); // Random damage to player
        if (playerHP <= 0) {
            statusText.setText("Game Over! You have been defeated.");
        }
    }

    private void updateStatus() {
        statusText.setText("Level: " + currentLevel + "\nYour HP: " + playerHP + "\nOpponent HP: " + opponentHP);
    }
}
Leave a Comment