[text] Pokemon game

Viewer

copydownloadembedprintName: Pokemon game
  1. import java.util.Random;
  2. import java.util.Scanner;
  3.  
  4. public class BasicPokemon {
  5.     String name;
  6.     int hitpoints;
  7.     // Constructor to make a BasicPokemon object with a name and hitpoints
  8.     public BasicPokemon(String name1, int hitpoints1) {
  9.         name = name1;
  10.         hitpoints = hitpoints1;
  11.     }
  12.  
  13.     // Get the name of the Pokemon
  14.     public String getName() {
  15.         return name;
  16.     }
  17.     // Set the name of the Pokemon
  18.     public void setName(String name1) {
  19.         name = name1;
  20.     }
  21.     // Gets the current hitpoints of the Pokemon
  22.     public int getHitpoints() {
  23.         return hitpoints;
  24.     }
  25.     // Set the hitpoints of the Pokemon
  26.     public void setHitpoints(int hitpoints1) {
  27.         hitpoints = hitpoints1;
  28.     }
  29.     // Reduce the hitpoints of the Pokemon by a specified amount
  30.     public void takeDamage(int amount) {
  31.         hitpoints -= amount;
  32.     }
  33.     // Attack 
  34.     public void attack(BasicPokemon enemy) {
  35.         Random random = new Random();
  36.         int damage = random.nextInt(6);
  37.  
  38.         if (damage == 0) {
  39.             System.out.println(name + " Attacks and misses!");
  40.             System.out.println("════════════════════════════════════"); //Print damage message and separator
  41.  
  42.         } else {
  43.             System.out.println(name + " Attacks and deals " + damage + " damage!"); 
  44.             System.out.println("════════════════════════════════════"); //Print damage message and separator
  45.  
  46.         }
  47.  
  48.         enemy.takeDamage(damage);
  49.     }
  50. // Check if the Pokemon is still alive
  51.     public boolean isAlive() { 
  52.         if (hitpoints > 0) {
  53.             return true;
  54.         }
  55.         return false;
  56.     }
  57.  
  58.     public static void main(String[] args) {
  59.         BasicPokemon ally = new BasicPokemon("Pikachu", 40);// Ally Pokemon is named Pikachu with 40 hitpoints
  60.         BasicPokemon enemy = new BasicPokemon("Charmander", 40);// Enemy Pokemon is named Charmander with 40 hitpoints
  61.  
  62.  
  63.         //Start game loop
  64.         while (ally.isAlive() && enemy.isAlive()) { //Game continues as long as both Pokemon are alive
  65.  
  66.  
  67.             if (ally.isAlive()) {  // Ally attacks enemy
  68.                 ally.attack(enemy);
  69.             }
  70.  
  71.             if (enemy.isAlive()) { // Enemy attacks ally
  72.                 enemy.attack(ally);
  73.             }
  74.         }
  75.         //Handle game ending here
  76.         if (ally.isAlive()) {                       // Print ally victory message
  77.             System.out.println(ally.getName() + " Won against "    
  78.                     + enemy.getName() + " With "
  79.                     + ally.getHitpoints() + " Hitpoints remaining!");
  80.                     
  81.  
  82.         } else if (enemy.isAlive()) {               // Print enemy victory message
  83.             System.out.println(enemy.getName() + " Won against "
  84.                     + ally.getName() + " With "
  85.                     + enemy.getHitpoints() + " Hitpoints remaining!");
  86.         } else {
  87.             System.out.println("Error"); // Error if both pokemons healths are at 0
  88.         }
  89.  
  90.     }
  91. }

Editor

You can edit this paste and save as new:


File Description
  • Pokemon game
  • Paste Code
  • 26 Apr-2024
  • 3.15 Kb
You can Share it: