2016-11-14 8 views
1

私のコードは現在動作していますが、私は火球のための別のクラスを作りたいと言っています。私はこれを行う方法がわからないし、私の主なコードがそれほど大きくないように別のクラスを派生させるための助けを求めている例を見つけることはできません。ここにコードがあります:私のコードで火球用に別のクラスを作るにはどうすればいいですか?

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(); 
    } 
} 
+1

あなたをインデントすべきですコード – SLaks

+0

ここでMVC(モデル、ビュー、コントローラ)を使用してチェックアウトしてください。http://www.oracle.com/technetwork/articles/javase/index-142890.html最初はコードの大部分をやり直さなければならないかもしれませんが、複数のオブジェクト(Controller、Fireball、Kickなど)に分割すると、長期的に役立ちます。クラスの作成に関するヘルプは、https://docs.oracle.com/javase/tutorial/java/javaOO/classes.htmlを参照してください。 – Brydenr

答えて

0

私はあなたが投稿したコードに基づいて非常にひどく必要なので、別のクラスを使用する方法を尋ねていることを非常にうれしく思っています。火の玉だけでなくサウンド用、アニメーション用、プレイヤー用、敵用。すべてのクラスには責任が1つあります。これはコードではできないものですが、はるかに読みやすく保守的です。

は、あなたの質問に答えると、新しいクラスで何か新しい.javaファイルを作成するには:

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

    //you can declare other methods of fireballs here 
} 

をそしてimportステートメントを使用して、あなたのメインクラスに含めます。シングル責任原則に

さらに詳しい情報:https://en.wikipedia.org/wiki/Single_responsibility_principle

クラスの基本的な概要 - あなただけの最初の時間に別のクラスを使用することを決定している場合を開始するのに最適な場所:https://docs.oracle.com/javase/tutorial/java/javaOO/classes.html

+0

私はあなたが言ったことを試みましたが、実際にメインプログラムにそれを描いて絵を描くのは混乱しています。たとえば、キャラクター/ファイアボール(特にkeyListener)を移動するためにピクチャを更新する方法はありますか?また、すべてのメソッドを別のクラスに移動する方法は?また、これまでのおかげで、ありがとう。 –

+0

火球で始まり、それを自分のクラスに移動します。そのクラスに火球の画像と火球を描くロジックを入れます。火球を描画するロジックを行うdrawSelf(Graphics g)のようなメソッドを作ります。そのメソッドをFireballクラス内に作成します。次にメインクラスからそのメソッドを呼び出します。また、これが助けてくれればupvote =]を覚えておいてください – nhouser9

0

Javaの最上位クラスは別々のファイルで定義されており、インポートできます。

クラス内にクラスを埋め込むこともできます(例:

public class Main { 
    public static class Fireball { 
     // your code here 
    } 
} 

しかし、あなたのユースケースではそうするのは適切ではないようです。

関連する問題