如何在我的游戏中添加声音字节?

时间:2016-08-05 18:10:48

标签: java swing audio

所以,我有一个有点像复古僵尸生存游戏的工作游戏。我只有2个僵尸,还有一把手枪作为武器。我正在寻找一种方法来释放keylistener空间时向Jpanel添加声音字节。我已经看过问题" java生成声音"但发现信息没有帮助,因为我更喜欢使用自己的声音字节文件。

这里是部分代码,我将添加音频样本(我知道我的编程风格非常混乱,抱歉:|)

  public void keyReleased(KeyEvent u){
    int key10=u.getKeyCode();
    if (key10==KeyEvent.VK_SPACE){
        shoot=1;
    }


}

以下是完整代码:

package game;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.Random;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.Timer;

public class game extends JPanel implements ActionListener {
private int key6;
private int key3;
private int key1;
private int key2;
private int key4;
private int moveleft;
private int moveright;
private int moveup;
private int movedown;
private boolean medkitalive;
private int setx;
private int sety;
private int randx;
private int randy;
private int medx;
private int medy;
private int randx2;
private int randy2;
private int randx3;
private int randy3;
private Image enemy2;
private int kills=0;
private int health=100;
private int restart=0;
private boolean enemyalive;
private boolean enemy2alive;
private boolean enemy3alive;
private int enemiesalive=0;
private int enemiesallowed=0;
private int timetester=0;
private int released=0;
private int released1=0;
private int released2=0;
private int released3=0;
private int shoot=0;
private int movebullet;
private int shootang=0;
private Timer timer;
private Timer timer1;
private int Delay=10;
private final int B_width=1000;
private final int B_height=600;
private Image background;
private Image player;
private Image playerright;
private Image playerleft;
private Image playerup;
private boolean ingame=true;
private final int ALL_DOTS=1000;
private int xvel;
private int yvel;
private Image enemy;
private int key5;
 private final int x[] = new int[ALL_DOTS];
    private final int y[] = new int[ALL_DOTS];
    private Image bullet;
    private Image blood;
    private Image medkit;
    private Image bullet2;
    public game(){
    addKeyListener(new Tadapter1());
    addKeyListener(new Tadapter());
    addKeyListener(new Tadapter2());
    addKeyListener(new Tadapter4());
    addKeyListener(new Tadapter5());
    addKeyListener(new Tadapter6());
    setFocusable(true);
    setPreferredSize(new Dimension(B_width,B_height));
    setBackground(Color.black);
    loadimages();
    initgame();

}

private void loadimages() {
    ImageIcon iia= new ImageIcon("background.jpg");
    background= iia.getImage();

    ImageIcon iib= new ImageIcon("player.png");
    player= iib.getImage();

    ImageIcon iic=new ImageIcon("bullet.png");
    bullet=iic.getImage();

    ImageIcon iid=new ImageIcon("enemy.png");
    enemy=iid.getImage();

    ImageIcon iie=new ImageIcon("blood.jpg");
    blood=iie.getImage();

    ImageIcon iif=new ImageIcon("playerleft.png");
    playerleft=iif.getImage();

    ImageIcon iig=new ImageIcon("playerright.png");
    playerright=iig.getImage();

    ImageIcon iih=new ImageIcon("playerup.png");
    playerup=iih.getImage();

    ImageIcon iii=new ImageIcon("bullet2.png");
    bullet2=iii.getImage();

    ImageIcon iij=new ImageIcon("medkit.png");
    medkit=iij.getImage();

    ImageIcon iik=new ImageIcon("enemy2.png");
    enemy2=iik.getImage();

}






public void doDrawing(Graphics g){

    if (health<=0){g.setFont(new Font("Helvetica", Font.BOLD, 60 ));
        g.setColor(Color.GREEN);
        g.drawImage(blood,0,0,this);
        health=0;
        g.drawString("Game Over, press enter to restart", 10, 300);
        if (restart==1){
            ingame=true;
            x[0]=500;
            y[0]=500;
            kills=0;
            health=100;

        }
    }
    if (ingame){
        if (health>0){

        restart=0;
        Random rand=new Random();
        Random rand2=new Random();
        Random rand3= new Random();
        timetester++;
        if (timetester%150==1&&enemiesalive==0){
        enemyalive=true;
         randx=rand.nextInt(800)+100;
         randy=rand.nextInt(500)+100;
         if (randx-x[0]<100||randy-y[0]<100){
                randx=rand2.nextInt(800)+100;
                randy=rand2.nextInt(500)+100;
            }
         if (randx-x[0]<100||randy-y[0]<100){
                randx=rand2.nextInt(800)+100;
                randy=rand2.nextInt(500)+100;
            }
         if (randx-x[0]<100||randy-y[0]<100){
                randx=rand2.nextInt(800)+100;
                randy=rand2.nextInt(500)+100;
            }
         if (randx-x[0]<100||randy-y[0]<100){
                randx=rand2.nextInt(800)+100;
                randy=rand2.nextInt(500)+100;
            }
         if (randx-x[0]<100||randy-y[0]<100){
                randx=rand2.nextInt(800)+100;
                randy=rand2.nextInt(500)+100;
            }

         enemiesalive=1;

        }
        if (timetester%2000==1&&medkitalive==false){
            medkitalive=true;
            medx=rand3.nextInt(800)+100;
            medy=rand3.nextInt(500)+100;
        }

        if (timetester%150==1&&enemy2alive==false&&kills>=10){
            enemy2alive=true;
            randx2=rand2.nextInt(800)+100;
            randy2=rand2.nextInt(500)+100;
            if (randx2-x[0]<100||randy2-y[0]<100){
                randx2=rand2.nextInt(800)+100;
                randy2=rand2.nextInt(500)+100;
            }
            if (randx2-x[0]<100||randy2-y[0]<100){
                randx2=rand2.nextInt(800)+100;
                randy2=rand2.nextInt(500)+100;
            }
            if (randx2-x[0]<100||randy2-y[0]<100){
                randx2=rand2.nextInt(800)+100;
                randy2=rand2.nextInt(500)+100;
            }
            if (randx2-x[0]<100||randy2-y[0]<100){
                randx2=rand2.nextInt(800)+100;
                randy2=rand2.nextInt(500)+100;
            }
            if (randx2-x[0]<100||randy2-y[0]<100){
                randx2=rand2.nextInt(800)+100;
                randy2=rand2.nextInt(500)+100;
            }

        }
        if (x[0]-randx>0&&shoot==0){
            randx++;
        }
        if (x[0]-randx<0&&shoot==0){
            randx--;
        }
        if (y[0]-randy<0&&shoot==0){
            randy--;
        }
        if (y[0]-randy>0&&shoot==0){
            randy++;
        }
        if (x[0]-randx2>0&&shoot==0){
            randx2++; if (kills>10){
                randx2+=((kills-10)/10);
            }if (kills>=30){
                randx2-=-1+((kills-10)/10);
            }
        }
        if (x[0]-randx2<0&&shoot==0){
            randx2--;if (kills>10){
                randx2-=((kills-10)/10);
            }if (kills>=30){
                randx2+=-1+((kills-10)/10);
            }
        }
        if (y[0]-randy2<0&&shoot==0){
            randy2--;if (kills>10){
                randy2-=((kills-10)/10);
            }if (kills>=30){
                randy2+=-1+((kills-10)/10);
            }
        }
        if (y[0]-randy2>0&&shoot==0){
            randy2++;if (kills>10){
                randy2+=((kills-10)/10);
            }if (kills>=30){
                randy2-=-1+((kills-10)/10);
            }
        }
        if (x[0]-randx>0&&shoot==1&&timetester%5==1){
            randx++;
        }
        if (x[0]-randx<0&&shoot==1&&timetester%5==1){
            randx--;
        }
        if (y[0]-randy<0&&shoot==1&&timetester%5==1){
            randy--;
        }
        if (y[0]-randy>0&&shoot==1&&timetester%5==1){
            randy++;
        }
        if (x[0]-randx2>0&&shoot==1&&timetester%5==1){
            randx2++;if (kills>10){
                randx2+=((kills-10)/10);
            }if (kills>=30){
                randy2-=-1+((kills-10)/10);
            }
        }
        if (x[0]-randx2<0&&shoot==1&&timetester%5==1){
            randx2--;if (kills>10){
                randx2-=((kills-10)/10);
            }
            if (kills>=30){
                randx2+=-1+((kills-10)/10);
            }
        }
        if (y[0]-randy2<0&&shoot==1&&timetester%5==1){
            randy2--;if (kills>10){
                randy2-=((kills-10)/10);
            }
            if (kills>=30){
                randy2+=-1+((kills-10)/10);
            }
        }
        if (y[0]-randy2>0&&shoot==1&&timetester%5==1){
            randy2++;if (kills>10){
                randy2+=((kills-10)/10);
            }
            if (kills>=30){
                randy2-=-1+((kills-10)/10);
            }
        }
        if (!enemy2alive){
            randx2=-100;
            randy2=-100;
        }
        if(!enemyalive){
            randx=-100;
            randy=-100;
        }
        g.drawImage(background, 0, 0, this);
        if (shootang==3){
        g.drawImage(player,x[0],y[0],this);}
        if (shootang==0){
            g.drawImage(playerleft,x[0],y[0],this);}
        }
        if (shootang==1){
        g.drawImage(playerup,x[0],y[0],this);}
        if (shootang==2){
            g.drawImage(playerright,x[0],y[0],this);}
        if (enemyalive){
        g.drawImage(enemy,randx, randy, this);}

        if (enemy2alive){
            g.drawImage(enemy2, randx2, randy2, this);

        }
        if (medkitalive==true){
            g.drawImage(medkit, medx, medy, this);
        }
        Rectangle player=new Rectangle(x[0],y[0],25,25);
        Rectangle enemy2r=new Rectangle(randx2,randy2,25,25);
        Rectangle enemy=new Rectangle(randx,randy,25,25);
        g.drawRect(randx, randy, 25, 25);
        g.drawRect(randx2, randy2, 25, 25);
        Rectangle medkit=new Rectangle(medx,medy,25,25);

        if (player.intersects(medkit)&&medkitalive==true){
            health+=10;
            medkitalive=false;
        }
        g.setFont(new Font("Helvetica", Font.BOLD, 60 ));
        g.setColor(Color.RED);
        String Health = Integer.toString(health);
        g.drawString("Health:  "+Health ,1, 50);



        g.setFont(new Font("Helvetica", Font.BOLD, 60 ));
        g.setColor(Color.RED);
        String Kills = Integer.toString(kills);
        g.drawString("Kills:  "+Kills ,600, 50);
        if (enemy2r.intersects(player)&&enemy2alive==true){

            if(timetester%5==1){
                health--;
            }


        }
        if (enemy.intersects(player)&&enemyalive==true){


            if(timetester%5==1){
                health--;
            }



        }
        if (moveleft==0&&moveright==0&&moveup==0&&movedown==0){
            setx=x[0];
            sety=y[0];
        }

        if (shoot==0){
            movebullet=0;
        }

        if (shoot==1){
            System.out.print(movebullet+"...");

            if (shootang==0&&moveright==0&&moveup==0&&movedown==0||moveleft==1){
                moveleft=1;
                g.drawImage(bullet, x[0]+movebullet, y[0]+10, this);
                Rectangle bullet= new Rectangle(x[0]+movebullet,y[0]+10,5,5);
                movebullet-=2;
                if (bullet.intersects(enemy)){

                    enemiesalive=0;
                    if (enemyalive==true){
                        kills++;
                    }
                    enemyalive=false;

                    movebullet=0;moveleft=0;
                    shoot=0;
                }
                if (bullet.intersects(enemy2r)){
                    if (enemy2alive==true){
                        kills++;
                    }
                    enemy2alive=false;

                    movebullet=0;moveleft=0;
                    shoot=0;
                }


                if (movebullet<-400){movebullet=0;moveleft=0;
                    shoot=0;
                }
            }
            if (shootang==1&&moveleft==0&&moveright==0&&movedown==0||moveup==1){
                moveup=1;
                g.drawImage(bullet2, x[0]+10, y[0]+movebullet, this);
                Rectangle bullet= new Rectangle(x[0]+10,y[0]+movebullet,5,5);

                movebullet-=2;
                if (bullet.intersects(enemy)){

                    enemiesalive=0;
                    if (enemyalive==true){
                        kills++;
                    }
                    enemyalive=false;

                    moveup=0;
                    movebullet=0;
                    shoot=0;
                }
                if (bullet.intersects(enemy2r)){
                    if (enemy2alive==true){
                        kills++;
                    }
                    enemy2alive=false;

                    moveup=0;
                    movebullet=0;
                    shoot=0;
                }

                if (movebullet<-400){moveup=0;
                    movebullet=0;
                    shoot=0;
                }
            }
            if (shootang==3&&moveleft==0&&moveright==0&&moveup==0||movedown==1){movedown=1;
                g.drawImage(bullet2, x[0]+5, y[0]+movebullet, this);
                Rectangle bullet= new Rectangle(x[0]+5,y[0]+movebullet,5,5);

                movebullet+=2;
                if (bullet.intersects(enemy)){
                    if (enemyalive==true){
                        kills++;
                    }
                    enemiesalive=0;
                    enemyalive=false;

                    movedown=0;
                    movebullet=0;
                    shoot=0;
                }
                if (bullet.intersects(enemy2r)){
                    if (enemy2alive==true){
                        kills++;
                    }
                    enemy2alive=false;
                    movedown=0;
                    movebullet=0;
                    shoot=0;
                }



                if (movebullet>400){movedown=0;
                    movebullet=0;
                    shoot=0;
                }
            }
            if (shootang==2&&moveleft==0&&moveup==0&&movedown==0||moveright==1){moveright=1;
                g.drawImage(bullet, x[0]+movebullet, y[0]+10, this);

                Rectangle bullet= new Rectangle(x[0]+movebullet,y[0]+10,5,5);
                movebullet+=2;
                if (bullet.intersects(enemy)){
                    enemiesalive=0;
                    if (enemyalive==true){
                        kills++;
                    }
                    enemyalive=false;

                    moveright=0;
                    movebullet=0;
                    shoot=0;
                }
                if (bullet.intersects(enemy2r)){
                    if (enemy2alive==true){
                        kills++;
                    }
                    enemy2alive=false;

                    moveright=0;
                    movebullet=0;
                    shoot=0;
                }

                if (movebullet>400){moveright=0;
                    movebullet=0;
                    shoot=0;
                }
            }
        }
    }
}


@Override
public void paintComponent(Graphics g){
    super.paintComponent(g);
    doDrawing(g);




}
private void initgame() {

    timer= new Timer(Delay,this);
    timer.start();  

}

public void movex(){

    x[0]+=xvel;
}
public void movey(){
    y[0]+=yvel;
}

@Override
public void actionPerformed(ActionEvent e) {
    if (health<=0){
        ingame=false;
        repaint();

    }
    if (health>0){
    if (ingame){
        if (shoot==0){
        movex();
        movey();}
        if (shoot==1){
            timer.setDelay(0);
            }
            if (shoot==0){
            timer.setDelay(10);
            }
    }
    repaint();}
    if (health<=0){
        ingame=false;
    }
}
public class Tadapter extends KeyAdapter{

    @Override
    public void keyPressed(KeyEvent f){

        int key3=f.getKeyCode();
        if (key3==KeyEvent.VK_A){
            xvel=-3;
            released3=1;
            shootang=0;

        }}





    public void keyReleased(KeyEvent f){
        int key4=f.getKeyCode();
        if (key4==KeyEvent.VK_A){
            released3=0;
        }
        if (key4==KeyEvent.VK_A&&released1!=1){
            xvel=0;
        }
        if (key4==KeyEvent.VK_A&&released1==1){
            xvel=3;
        }

    }


}







public class Tadapter1 extends KeyAdapter{
@Override
public void keyPressed(KeyEvent e){

    int key1=e.getKeyCode();
    if (key1==KeyEvent.VK_W){
        yvel=-3;
        released2=1;
        shootang=1;

    }
}
public void keyReleased(KeyEvent e){
    int key2=e.getKeyCode();
    if (key2==KeyEvent.VK_W){
        released2=0;

    }
    if (key2==KeyEvent.VK_W&&released!=1){
        yvel=0;
    }
    if (key2==KeyEvent.VK_W&&released==1){
        yvel=3;
    }

}


}

public class Tadapter2 extends KeyAdapter{
@Override
public void keyPressed(KeyEvent h){
    key5=h.getKeyCode();
    if (key5==KeyEvent.VK_D){
        xvel=3;
        released1=1;
        shootang=2;
    }
}
public void keyReleased(KeyEvent h){
    key6=h.getKeyCode();
    if (key6==KeyEvent.VK_D&&released3==1){
        xvel=-3;


    }
    if (key6==KeyEvent.VK_D&&released3!=1){
        xvel=0;

    }
    if (key6==KeyEvent.VK_D){

        released1=0;
    }
}
}

public class Tadapter4 extends KeyAdapter{
@Override
public void keyPressed(KeyEvent j){
    int key7=j.getKeyCode();
    if (key7==KeyEvent.VK_S){
        yvel=3;
        released=1;
        shootang=3;
    }
}
public void keyReleased(KeyEvent j){
    int key8=j.getKeyCode();
    if (key8==KeyEvent.VK_S){

        released=0;
    }
    if (key8==KeyEvent.VK_S&&released2==1){
        yvel=-3;
    }
    if (key8==KeyEvent.VK_S&&released2!=1){
        yvel=0;
    }
}
}


public class Tadapter5 extends KeyAdapter{
@Override
public void keyPressed(KeyEvent u){
    int key9=u.getKeyCode();
    if (key9==KeyEvent.VK_SPACE){

    }
}
public void keyReleased(KeyEvent u){
    int key10=u.getKeyCode();
    if (key10==KeyEvent.VK_SPACE){
        shoot=1;
    }


}
}

public class Tadapter6 extends KeyAdapter{
@Override
public void keyPressed(KeyEvent p){
    int key9=p.getKeyCode();
    if (key9==KeyEvent.VK_ENTER){

    }
}
public void keyReleased(KeyEvent p){
    int key10=p.getKeyCode();
    if (key10==KeyEvent.VK_ENTER){
        restart=1;
    }


}
}

}

此外,哪些类型的文件可以在Jpanel中使用?

1 个答案:

答案 0 :(得分:0)

许多方式 - 已在此处回答How to play .wav files with java

他们还讨论其他格式。