How can I make a separate class for a fire ball in my code?

Jozef Brudnicki

My code currently works, with that said I would like to make a separate class for the fire ball. I don't know how to do this and i cant find any examples I am just looking for help on deriving a separate class so that my main code is not so large. Here is code:

import java.awt.image.BufferedImage;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.*;
import java.awt.event.KeyEvent;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import java.awt.Image;
import java.io.*;
import javax.swing.JOptionPane;
import javax.imageio.*;
import java.awt.Rectangle;
public class Main extends JPanel implements ActionListener, KeyListener{
    ImageIcon images[], imagesL[],standingImage[],punchImg[],
            fireImg[],kickImg[],jumpImg[],slamImg[];
    int x = 100, y = 530;
    int count;
    int velX,velY;
    int velF;
    int firey,firex;
    int gravity = 20;
    int totalImages =3, currentImage = 0, animationDelay = 80,
            numSlam = 3,currentSlam = 0, numberImages = 1,
            currentStand = 0,
            numPunch = 3, currentPunch = 0;

    int currentFire = 0, numFire =1,totalImagesL =3,
            currentImageL =     0,currentKick = 0, numKick=2,currentJump = 0,
            numJump = 1;
    int height, totalHeight = -120;
    int  velEnemy = 5;
    int counter;
    int enemyX = 310;
    Timer animationTimer;
    boolean isMovingLeft;
    boolean isKick;
    boolean isFire = false, fireIs = false,isMovingRight = false,
            isNotMoving = true, isPunch = false, startFire = false, isJump = false,
            isSlam = false;
    boolean  enemyIsDead = false;
    Image fireball;
    Image back;
    Image title;
    Image alien;
    long lastJumptime = 0;
    long lastrJumptime = 0;
    long currentTime;
    long lastFiretime = 0;
    private final int FRAME_DELAY = 50;
    public Main() {
        setFocusable(true); // make your panel focusable
        addKeyListener(this); // register the key listener
        setFocusTraversalKeysEnabled(false);
        //WALKING/RUNNING RIGHT//
        images = new ImageIcon[totalImages];
        images[2] = new ImageIcon("standing.png");
        images[1] = new ImageIcon("midWay.png");
        images[0] = new ImageIcon("running.png");
        //WALKING/RUNNING LEFT//
        imagesL = new ImageIcon[totalImagesL];
        imagesL[2] = new ImageIcon("standingL.png");
        imagesL[1] = new ImageIcon("midWayL.png");
        imagesL[0] = new ImageIcon("runningL.png");
        ///STANDING STILL
        standingImage = new ImageIcon[numberImages];
        standingImage[0] = new ImageIcon("standing.png");
        //PUNCH/////
        punchImg = new ImageIcon[numPunch];
        punchImg[0] = new ImageIcon("midPunch.png");
        punchImg[1] = new ImageIcon("punchFull.png");
        punchImg[2] = new ImageIcon("midPunch.png");
        //COMMENCE FIREBALL//
        fireImg = new ImageIcon[numFire];
        fireImg[0] = new ImageIcon("startFire.png");
        //COMMENCE KICK//
        kickImg = new ImageIcon[numKick];
        kickImg[0]= new ImageIcon("startKick.png");
        kickImg[1]= new ImageIcon("finishKick.png");
        //JUMPING//
        jumpImg = new ImageIcon[numJump];
        jumpImg[0] = new ImageIcon("ready.png");
        //TITLE THINGY///
        ImageIcon title = new ImageIcon("gokuTitle.png");
        //SLAM THINGY//
        slamImg = new ImageIcon[numSlam];
        slamImg[0] = new ImageIcon("startSlam.png");
        slamImg[1] = new ImageIcon("midSlam.png");
        slamImg[2] = new ImageIcon("endSlam.png");
        startAnimation();
    }

    public void paintComponent(Graphics g) {
        //SOUND FOR THIS STUFF//
        Sound[] soundSelection = new Sound[4];
        soundSelection[0]= new Sound("Fire Spell.wav");
        soundSelection[1] = new Sound("kick effect.wav");
        soundSelection[2] = new Sound("fire ball troll.wav");
        soundSelection[3] = new Sound("punch sound effect.wav");
        //END STUFF FOR SOUND//
        super.paintComponent(g);
        Graphics g2 = (Graphics) g;
        try{
            back = ImageIO.read(new File("backGround.png"));
            title = ImageIO.read(new File("gokuTitle.png"));
        }catch (IOException e) {
        }
        g2.drawImage(back, 0, 0, null);
        g2.drawImage(title, 0, 0, null);
        if (isMovingRight == true){//if moving right
            if (images[currentImage].getImageLoadStatus() == MediaTracker.COMPLETE) {
                Image imgRun =  images[currentImage].getImage();
                g2.drawImage(imgRun, x, y, null);
                currentImage = (currentImage + 1) % totalImages;
            }
        }
        if (isMovingLeft == true){//if moving left
            if (imagesL[currentImageL].getImageLoadStatus() == MediaTracker.COMPLETE)
            {
                Image imgRunL =  imagesL[currentImageL].getImage();
                g2.drawImage(imgRunL, x, y, null);
                currentImageL = (currentImageL + 1) % totalImagesL;
            }
        }
        if (isNotMoving == true){//IF not moving
            if (standingImage[currentStand].getImageLoadStatus() == MediaTracker.COMPLETE) {
                Image imgStand = standingImage[currentStand].getImage();
                g2.drawImage(imgStand, x, y, null);
                currentStand = (currentStand + 1) % numberImages;
            }
        }
        if (fireIs== true){
            //Do a seperate class for fireball
            isFire = true;
            if (fireImg[currentFire].getImageLoadStatus() == MediaTracker.COMPLETE) {
                Image  fireMe = fireImg[currentFire].getImage();
                g2.drawImage(fireMe, x, y, null);
                currentFire = (currentFire+ 1) % numFire;
                soundSelection[0].play();
                soundSelection[2].play();
            }
            try{
                fireball = ImageIO.read(new File("fireball.png"));
            }catch (IOException e) {
            }
        }
        if(isFire ==true){
            fireIs = false;
            g2.drawImage(fireball, firex + 80, firey+30, null);
        }
        if(isPunch == true){
            if (punchImg[currentPunch].getImageLoadStatus() == MediaTracker.COMPLETE) {
                Image punchMe = punchImg[currentPunch].getImage();
                g2.drawImage(punchMe, x, y, null);
                currentPunch = (currentPunch+ 1) % numPunch;
                soundSelection[3].play();
            }
        }
        if(isKick == true){
            if (kickImg[currentKick].getImageLoadStatus() == MediaTracker.COMPLETE) {
                Image kickMe = kickImg[currentKick].getImage();
                g2.drawImage(kickMe, x, y, null);
                currentKick = (currentKick+ 1) % numKick;
                soundSelection[1].play();
            }
        }
        if (isJump == true){
            if (jumpImg[currentJump].getImageLoadStatus() == MediaTracker.COMPLETE) {
                Image jumpUp = jumpImg[currentJump].getImage();
                g2.drawImage(jumpUp,x, y+10, null);
                currentJump = (currentJump+ 1) % numJump;
            }
        }
        if (isSlam == true){
            if (slamImg[currentSlam].getImageLoadStatus() == MediaTracker.COMPLETE) {
                Image slamUp = slamImg[currentSlam].getImage();
                g2.drawImage(slamUp,x, y+10, null);
                currentSlam = (currentSlam+ 1) % numSlam;
            }
        }
        //ENEMY THAT DOES NOT WANT TO WORK//

        //draw rect here//
        Rectangle player = new Rectangle(x,y, 60,100);
        Rectangle enemies = new Rectangle(enemyX,530, 60,100);
        /////
        if (player.intersects(enemies)){
            enemyIsDead = true;
        }
        if (enemyIsDead == false){
            try{
                alien = ImageIO.read(new File("Alien1.png"));
            }catch (IOException e) {
            }
            g2.drawImage(alien, enemyX, 530, null);
        }


    }

    public void actionPerformed(ActionEvent e) {
        repaint();
        try {
            Thread.sleep(FRAME_DELAY);
        } catch (InterruptedException exception) {
            throw new RuntimeException(exception);
        }
        x+=velX;
        height += velY;
        enemyX+=velEnemy;
        if (height >= totalHeight){
            y+=velY;
        }else{
            y+=gravity;
        }
        firex += velF;
        if (y < 2){
            y = 3;
        }
        if (y >530){
            y = 531;
        }
    }
    public void right(){
        velX = 20;
    }
    public void left(){
        velX = -20;
    }
    public void fire(){
        velF = 30;
    }
    public void jump(){
        velY = -30;
    }
    public void notMovingMethod(){
        isMovingRight = false;
        isNotMoving = true;
        isPunch = false;
        isMovingLeft = false;
        isKick  = false;
        isJump = false;
        isSlam = false;
    }
    public void everythingFalse(){
        isPunch = false;
        isNotMoving = false;
        isMovingRight = false;
        isMovingLeft = false;
        isKick  = false;
        isJump = false;
        isSlam = false;
    }
    public void keyPressed(KeyEvent e) {
        int code = e.getKeyCode();
        //FIND OUT ABOUT COUNTER FOR ENEMIES NEARBY,LIKE IF AN ENEMY IS NEAR YOU FOR 
        2 SEC YOU LOSE HEALTH
        if(code == KeyEvent.VK_Z){
            currentTime=System.currentTimeMillis();
            if (currentTime >= lastFiretime + 5000){
                firex = x;
                firey = y;
                fire();
                fireIs = true;
                everythingFalse();
                lastFiretime=System.currentTimeMillis();
            }
        }
        if (code == KeyEvent.VK_P){
            System.out.println("Paused");
            stopAnimation();
        }
        if (code == KeyEvent.VK_A){
            left();
            everythingFalse();
            isMovingLeft = true;

        }

        if (code == KeyEvent.VK_F){
            everythingFalse();
            isPunch = true;
        }

        if(code == KeyEvent.VK_D){
            right();
            everythingFalse();
            isMovingRight = true;
        }
        if(code == KeyEvent.VK_Q){
            everythingFalse();
            isKick  = true;
        }
        if(code == KeyEvent.VK_W){
            currentTime=System.currentTimeMillis();
            if (currentTime >= lastJumptime + 1000){
                jump();
                everythingFalse();
                isJump = true;
                lastJumptime=System.currentTimeMillis();
            }
        }
        if (code == KeyEvent.VK_E){
            everythingFalse();
            isSlam = true;
        }
    }
    public void keyTyped(KeyEvent e){}
    public void keyReleased(KeyEvent e){
        int code = e.getKeyCode();

        if(code == KeyEvent.VK_D){
            notMovingMethod();
        }
        if (code == KeyEvent.VK_P){
            startAnimation();
        }
        if (code == KeyEvent.VK_A){
            notMovingMethod();
        }
        if(code == KeyEvent.VK_Q){
            notMovingMethod();
        }
        if (code == KeyEvent.VK_F){
            notMovingMethod();
        }
        if (code == KeyEvent.VK_E){
            notMovingMethod();
        }
        if(code == KeyEvent.VK_Z){
            notMovingMethod();
        }
        if(code == KeyEvent.VK_W){
            currentTime=System.currentTimeMillis();
            if (currentTime >= lastrJumptime + 1000){
                notMovingMethod();
                height = 0;
            }
            lastrJumptime = System.currentTimeMillis();
        }
        velX = 0;
    }
    public void startAnimation() {
        if (animationTimer == null) {
            currentImage = 0;
            currentImageL = 0;
            currentStand = 0;
            currentPunch=0;
            currentFire = 0;
            currentKick = 0;
            currentJump = 0;
            currentSlam = 0;
            animationTimer = new Timer(animationDelay, this);
            animationTimer.start();
        } else if (!animationTimer.isRunning())
            animationTimer.restart();
    }

    public void stopAnimation() {
        animationTimer.stop();
    }
}
nhouser9

Well I'm very glad you are asking how to use separate classes, because based on the code you posted you need them very badly. Not just for the fireball. For the sound, for the animations, for the player, for the enemy. Every class should have only one responsibility, which is something your code does not do but which would make it much more legible and maintainable.

To answer your question, create a new .java file with your new class, something like:

public class Fireball {
    public Fireball() {
        //do whatever you need to do in the constructor
    }

    //you can declare other methods of fireballs here
}

And then include it in your main class with an import statement.

More info on the Single Responsibility Principle: https://en.wikipedia.org/wiki/Single_responsibility_principle

A basic overview of classes - the best place to start if you are just deciding to use different classes for the first time: https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java: How can I make a ball behave as a bouncing and elastic ball?

How can I make a ball jump wherever it is?

How can i separate my networking code from my ViewController

How come I can't make a Material AppBar a separate class?

How can I separate my ArrayList to different java class?

How can I separate my variables to another class?

How can I separate runs of my TensorFlow code in TensorBoard?

How can I separate my typescript code into multiple files?

How can I move code to create a list into a separate class and then call it

How can I make VS Code format my Python code?

Kotlin, how can I make my code shorter in this spaghetti code?

How can I make a ball move up or down in JavaFX?

How can I roll a Ball in unity in the direction my cam is looking?

How can i add new position to my ball - Kivy

How do I make a protocol that my generic class can conform to?

How can I make my class pretty printable in Python?

How to make a super class I can call to handle my downloads

How can i make my Networking class generic in Swift?

How can I make class structures out of my Json request?

How can I make my functions to be part of Class?

How can i make my custom AppCompatActivity class for some function?

How can I make my class instance serializable for multiprocessing in Python?

How can I refactor my kotlin code and make it more clear

How can I make my code lose the white space at the beginning?

How can I make my D3 code cleaner

How can I make my JavaScript code more DRY?

How can I make my generics code compatible with this method signature?

How can I make my pandas code more efficient?

How can I make my javascript code permanent?