This site requires JavaScript, please enable it in your browser!
Greenfoot back
ThatAsianPixel
ThatAsianPixel wrote ...

2020/3/11

I don't know why it won't print... please assist

ThatAsianPixel ThatAsianPixel

2020/3/11

#
I am currently in the process of finishing my game for my school project. However, when I try to get what the user inputs, no matter what they put, it terminates and ends the game. I want it so that if you press "fight", it lists the 4 moves so that the user can input which one they want. However, no matter what I input, it says "Battle Terminated" and sends me back to the FrontPage. Please assist. Current class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
 * Write a description of class Blastoise here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Blastoise extends Actor
{
    private String name;
    private String type;
    private double health;
    private int time;
    /**
     * Act - do whatever the Blastoise wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        time++;
        returnHealth();
        Attack();
    }
    
    public Blastoise()
    {
        name = "Blastoise";
        type = "Water";
        health = 362;
    }
    
    public void Attack()
    {
        double random = (Math.random()*0.85)+0.15;
        double damage = 0;
        double modifier = 0;
        Moves m1 = new Moves("Skull Bash",130,100,"Normal","Physical");
        Moves m2 = new Moves("Hydro Pump",110,80,"Water","Special");
        Moves m3 = new Moves("Ice Beam",90,100,"Ice","Special");
        Moves m4 = new Moves("Weather Ball",50,100,"Normal","Special");
        Moveset ms = new Moveset(m1,m2,m3,m4);
        
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter: Fight or Run");
        String response = scan.nextLine();
        if(response.toLowerCase()=="fight")
        {
             System.out.println("Choose a move: ");
             for(int i = 0; i < 4;i++)
             {
                 ArrayList<Moves> x = ms.getMoveset();
                 System.out.println("- " + x.get(i).getName());
             }
             if(scan.nextLine().toLowerCase() == "skull bash")
             {
                 String enemyType = " ";
                 int enemyDefense = 0;
                 int power = m1.getPower();
                 Actor enemy = BattlePage.getCPU();
                 //Start of Outside Code: https://www.greenfoot.org/topics/63103/0#bottom
                 ArrayList<Integer> enemyStats = null;
                 if(enemy instanceof Blastoise)
                 {
                     enemyDefense = 328;
                     enemyType = ((Blastoise)enemy).getType();
                     if (enemyType.toLowerCase() == "water")
                     {
                         modifier = random;
                     }
                 }
                 if(enemy instanceof Charizard)
                 {
                     enemyDefense = 280;
                     enemyType = ((Charizard)enemy).getType();
                     if (enemyType.toLowerCase() == "fire")
                     {
                         modifier = random; 
                     }
                 }
                 if(enemy instanceof Venusaur)
                 {
                     enemyDefense = 291;
                     enemyType = ((Venusaur)enemy).getType();
                     if (enemyType.toLowerCase() == "grass")
                     {
                         modifier = random; 
                     }
                 }
                 //End of Outside Code
                 damage = ((42*power*(291/enemyDefense)/50)+2)* modifier;
                 setHealth(damage);
             }
        }
        else
        {
            System.out.println("Battle Forfeited");
            Greenfoot.setWorld(new FrontPage());
        }
    }
    
    public String getType()
    {
        return type;
    }
    
    public double getHealth()
    {
        return health;
    }
    
    public void setHealth(double damage)
    {
        health = health - damage;
    }
    public String getName()
    {
        return name;
    }
    
    public void returnHealth()
    {
        System.out.println(getName() + "'s Health: " + getHealth());
    }
}
FrontPage Class if needed:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FrontPage extends World
{

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public FrontPage()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        setBackground("TitleScreen.jpg");
        
        Actor startButton = new startButton();
        addObject(startButton, 215, 300);
        
        Actor helpButton = new helpButton();
        addObject(helpButton, 360, 300);
    }
}
danpost danpost

2020/3/11

#
I do not think you can use Scanner(System.in) with greenfoot. Use Greenfoot.ask to get user input.
ThatAsianPixel ThatAsianPixel

2020/3/11

#
danpost wrote...
I do not think you can use Scanner(System.in) with greenfoot. Use Greenfoot.ask to get user input.
Wait, can I not import java.util.*? Because I'm using the input-output window as well.
danpost danpost

2020/3/11

#
ThatAsianPixel wrote...
Wait, can I not import java.util.*? Because I'm using the input-output window as well.
Sure, you can import it. However, it is System.in that greenfoot uses to get keystrokes and presses in the Greenfoot class. So you cannot specifically use it.
ThatAsianPixel ThatAsianPixel

2020/3/11

#
Alright thanks. I need one more thing; so I want to get the player's Pokemon HP and the CPU's Pokemon HP. The Pokemon are assigned to selectedPlayer and selectedCPU in the BattlePage class. What can I replace Player in the HP class with to get what I'm trying to do? If you're not sure where Player is, its near the bottom, bolded. BattlePage:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class BattlePage here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class BattlePage extends World
{
    private static Actor selectedPlayer;
    private static Actor selectedCPU;
    /**
     * Constructor for objects of class BattlePage.
     * 
     */
    public BattlePage()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        setBackground("battle_background.jpg");
        
        addObject(selectedCPU,500,100);
        addObject(selectedPlayer,150,275);
    }
    
    public static Actor getPlayer()
    {
        return selectedPlayer; 
    }
    public static Actor getCPU()
    {
        return selectedCPU;
    }
    
    public static void setPlayer(Actor player)
    {
        selectedPlayer = player;
    }
    public static void setCPU(Actor CPU)
    {
        selectedCPU = CPU;
    }
}
StatBars Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The StatBar class let's you display any stat with a little amount of code required.
 * 
 * @author Zamoht
 * @version January 2014
 */
public class StatBars extends Actor
{
    public static enum Bar{RECT, ROUND, SMOOTH};
    private Bar newBar;
    private GreenfootImage frame,maxBar;
    private Color color;
    private int fontSize,letterW,stringW, target;
    private int max1;
    private double value,temp, speed;
    public void act()
    {
        if (target > value)
        {
            value+=speed*((double)max1/1000.0);
            if (target < value)
            {
                value = target;
            }
        }
        else if (target < value)
        {
            value-=speed*((double)max1/1000.0);
            if (target > value)
            {
                value = target;
            }
        }

        if (value != temp)
        {
            updateImage();
        }
    }

    public StatBars(Color color, int W, int H, Bar style, int max, int cur, int newMax)
    {
        newBar = style;
        int edge = getEdge(W,H);
        if(cur > max)
            {
                cur = max;
            }
        frame = new GreenfootImage(W,H);
        frame.setColor(Color.GRAY);
        frame.fillOval(0,0,edge,H);
        frame.fillRect(edge/2,1,W-edge,H);
        frame.fillOval(W-edge,0,edge,H);

        maxBar = new GreenfootImage(W,H);
        setColor(color,edge,false);

        setMaxHP(max, true);
        this.value = cur;
        target = (int)value;
        speed = 5;
        max1 = newMax;
        updateImage();
    }

    private int getEdge(int W, int H)
    {
        int edge;
        switch (newBar)
        {
            case RECT:
            edge = 0;
            break;

            case ROUND:
            edge = H;
            break;

            case SMOOTH:
            edge = H/2;
            break;

            default:
            edge = 0;
            break;
        }
        return edge;
    }

    public void setColor(Color color)
    {
        setColor(color, true);
    }
    
    private void setColor(Color color, int edge, boolean update)
    {
        if (maxBar == null || color.equals(this.color))
        {
            return;
        }
        this.color = color;
        int width = maxBar.getWidth();
        int height = maxBar.getHeight();
        maxBar.clear();
        maxBar.setColor(color);
        maxBar.fillOval(0, 0, edge, height);
        maxBar.fillRect(edge/2, 1, width-edge, height);
        maxBar.fillOval(width-edge, 0, edge, height);
        if (update)updateImage();
    }

    public void setColor(Color color, boolean update)
    {
        int width;
        int height;
        if (maxBar != null)
        {
            width = maxBar.getWidth();
            height = maxBar.getHeight();
        }
        else
        {
            return;
        }
        setColor(color, getEdge(width, height), update);
    }
    public void updateColor()
    {
        //Used by subclasses to update the color of the bar
    }

    private void updateImage(){
        updateColor();
        GreenfootImage img = new GreenfootImage(frame);
        double ratio = (double)value/(double)max1;
        int W = (int)Math.round(ratio*frame.getWidth());
        if(W > 0)
        {
            GreenfootImage part = new GreenfootImage(W, frame.getHeight());
            part.drawImage(maxBar,0,0);
            img.drawImage(part,0,0);
        }
        GreenfootImage label = new GreenfootImage(""+(int)value+"/"+max1, fontSize, Color.BLACK, null);
        int modifier = 0;
        if(label.getHeight()%2 == 0 || (label.getHeight()%2 == 1 && img.getHeight()%2 == 1))
            modifier = 1;
        img.drawImage(label,
            img.getWidth()/2 - stringW + letterW * (getDigits(max1) - getDigits((int)value)),
            img.getHeight()/2 - label.getHeight()/2 + modifier);
        setImage(img);
        temp = value;
    }

    private int getDigits(int value)
    {
        int digits = 0;
        do
        {
            digits++;
            value/=10;
        }
        while (value > 0);
        return digits;
    }

    public void setMaxHP(int max1, boolean fixFont)
    {
        this.max1 = max1;
        if(fixFont)
        {
            int W = frame.getWidth();
            int H = frame.getHeight();
            int digits = getDigits(max1);
            fontSize = 1;
            GreenfootImage label = new GreenfootImage("0",fontSize,Color.BLACK,null);
            while(label.getHeight() < H-H/5 && label.getWidth()*(digits*2+1) < W-W/5)
            {
                letterW = label.getWidth();
                fontSize++;
                label = new GreenfootImage("0",fontSize,Color.BLACK,null);
            }
            fontSize--;
            label = new GreenfootImage(""+max1+"/"+max1,fontSize,Color.BLACK, null);
            stringW = label.getWidth()/2;
        }
        updateImage();
    }
   
    public void setSpeed(double speed)
    {
        this.speed = speed;
    }
    
    public int getMax1()
    {
        return max1;
    }
    public void changeMax1(int val){
        max1 = val;
    }
    public int getValue()
    {
        return (int)value;
    }
    public void add(int val)
    {
        target = val;
        if (target > max1) target = max1;
        if (target < 0) target = 0;
        updateImage();
    }
}
HP Class:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class HP here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HP extends StatBars
{
    private static int maxHP;
    
    public HP(int length, int height){
        super(new Color(0,255,0),length,height,Bar.ROUND,[b]Player[/b].HP,[b]Player[/b].liveHP, maxHP);
        setSpeed(5000);
      
    }
    /**
     * Act - do whatever the HP wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       super.act();
    }
    public void updateColor()
    {
        if (getValue() <= getMax1()/4)
        {
            setColor(Color.RED);
        }
        else if (getValue() < getMax1()/2)
        {
            setColor(Color.YELLOW);
        }
        else
        {
            setColor(Color.GREEN);
        }
    }
}
danpost danpost

2020/3/12

#
Q: From what class does your Blastoise class extend?
ThatAsianPixel ThatAsianPixel

2020/3/12

#
danpost wrote...
Q: From what class does your Blastoise class extend?
Blastoise extends from Actor, as do the Charizard and Venusaur classes as well.
danpost danpost

2020/3/12

#
ThatAsianPixel wrote...
Blastoise extends from Actor, as do the Charizard and Venusaur classes as well.
It would be best to have those classes extending a more general class (which in turn extends Actor). Let us call the class Cpu. Then, remove all shared fields and methods in those classes and put them in the Cpu class. That way you can declare selectedCPU as type Cpu and you will not have to determine which sub-type is currently assigned to the field. You can do similarly with the selectedPlayer field. Make sure you also change the return type of the get methods for those fields.
Blazer Blazer

2020/3/15

#
You could, alternatively, have a general class that extends Actor, from which Blastoise, Charizard, and Venusaur extend from. Then add instances of those classes to a Cpu class and a Player class. Doing so, subclasses of that general class can inherit methods from that general class. You could also override a method by using the naming the method with the same name. (subclasses are classes that extend another class. This other class is called a superclass, in case you didn't know :D) Less messier when implementing changes to your code
You need to login to post a reply.