标签:闪烁 i++ NPU gif source isa 背景 音频 extend
http://www.cnblogs.com/wkfg/p/7063081.html
加速道具,增加炸弹携带数量,增加炸弹威力
无敌南瓜-吃到后获得5秒的无敌效果
生命泡泡-吃到该道具后生命值加1
本组课题:泡泡堂游戏
本人任务:主要编写Player类以及PlayerAttribute类,添加部分道具以及游戏背景音乐以及部分地图
private static final long serialVersionUID = 1L;
/**玩家键盘控制参数,初始均为false*/
private boolean bL = false, bU = false, bD = false, bR = false;
/**玩家当前生命值,初始4*/
public int live=4;
/**玩家是否存活*/
public boolean isalive=true;
/**玩家当前速度,初始10*/
public int speed=10;
/**玩家速度限制最大值*/
public int maxspeed=20;
/**玩家同时可投炸弹数,初始1*/
public int bombnum=1;
/**玩家可投炸弹数限制最大值*/
public int maxbombnum=6;
/**现存的炸弹数*/
int bombexist=0;
/**玩家当前炸弹威力,初始1*/
public int power=1;
/**玩家炸弹威力限制最大值*/
public int maxpower=5;
/**人物方向转动图片*/
String pathU;
String pathD;
String pathL;
String pathR;
public Player(String pathU,String pathD,String pathL,String pathR,int x,int y,int index)//初始化玩家信息
{
this.pathU=pathU;
this.pathD=pathD;
this.pathL=pathL;
this.pathR=pathR;//四个存储人物各个方向的图片
imgPath=pathD;
this.index=index;//标记是1P还是2P玩家
x*=80;
y*=80;
this.x=x;
this.y=y;//人物的坐标
this.lastX=x;
this.lastY=y;//人物的上一坐标
this.setBounds(this.x,this.y,80,80);//设置人物大小
setIcon(pathD,this);//设置人物图片
}
public enum Direction {
//分别代表上下左右和停止
L, D, U, R, STOP
}
通过一个有参方法进行。该函数通过对人物的坐标更新赋值,从而进行人物的移动,部分代码如下
public void moveStep(int n)
{
int l;
int loop=80;
for(l=0;l<=loop;l+=speed)
{
switch(n)
{
case 1:
this.setLocation(x-l,y);
break;
case 2:
this.setLocation(x,y-l);
break;
case 3:
this.setLocation(x+l,y);
break;
case 4:
this.setLocation(x,y+l);
break;
default:
break;
}
//如果遇到碰撞物,则不移动
if(meetbox())
{
break;
}
if(l<80)
{
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//如果循环后最后移动值不为80,则修复。强制用户每次只能移动一格的距离
if(l!=loop)
{
l=80;
switch(n)
{
case 1:
this.setLocation(x-l,y);
break;
case 2:
this.setLocation(x,y-l);
break;
case 3:
this.setLocation(x+l,y);
break;
case 4:
this.setLocation(x,y+l);
break;
default:
break;
}
}
}
public void move() {
//定义上一位置
this.lastX = x;
//定义上一位置
this.lastY = y;
//在不遇到(碰撞物、边界、炸弹)的情况下,每40μs移动speed个像素,一共移动80像素(地图上一个格子的宽度)
switch (dir) {
//向左移动
case L:
if(!invincible)
{
setIcon(pathL);
}
moveStep(1);
break;
//向下移动
case U:
if(!invincible)
{
setIcon(pathU);
}
moveStep(2);
break;
//向右移动
case R:
if(!invincible)
{
setIcon(pathR);
}
moveStep(3);
break;
//向下移动
case D:
if(!invincible)
{
setIcon(pathD);
}
moveStep(4);
break;
case STOP:
//停止
break;
default:
break;
}
//更新x,y坐标值
this.x=this.getX();
this.y=this.getY();
}
boolean meetbox()
{
int x = 1120;
int y = 880;
if(this.getX()<0||this.getX()>x||this.getY()<0||this.getY()>y)
{
return true;
}
int w = 15;
int h = 12;
for(int i=0;i<w;i++)
{
for(int j=0;j<h;j++)
{
Box temp=GameFrame.thismap.getBoxbyLocation(i,j);
if(temp.getRect().intersects(this.getRect())&&temp.isExist)
{
if(!temp.isdestroyshowT)
//遇到箱子
{
return true;
}
}
if(temp.isExistBomb&&!temp.isExistPlayer&&temp.getRect().intersects(this.getRect()))
//遇到炸弹
{
return true;
}
}
}
return false;
}
class InvincibleThread2 extends Thread
{
@Override
public void run(){
//无敌时间开始
invincible=true;
int loop = 50;
//获得5秒的闪烁无敌时间
for(int i=0;i<loop;i++)
{
//每0.1秒闪烁一个循环,一共闪烁50次
setIcon("images/default.png");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
setIcon("images/invinciblePlayer.png");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
setIcon(imgPath);
invincible=false;
}
}
/**玩家被炸到(受伤)*/
public void beInjured()
{
//生命值-1
this.live--;
//设置玩家属性面板的生命值
this.pla.setLabel("生命值:"+this.live,this.pla.live);
//如果死了(生命=0)
if(live==0)
{
//玩家死亡
this.isalive=false;
//玩家属性面板头像更换
setIcon("images/player"+this.index+"DIED.png",this.pla.photo);
//玩家图片更换
setIcon("images/player"+this.index+"DIED.png",this);
//提示消息
JOptionPane.showMessageDialog(this,"玩家"+this.index+"阵亡!","GameOver",2);
//总人数减1
GameFrame.NumofAlive--;
//如果总存活数为1,则游戏结束
if(GameFrame.NumofAlive==1)
{
JOptionPane.showMessageDialog(this,"游戏结束!单机确认退出~","GameOver",2);
//显示主界面
System.exit(0);
}
}
else
{
//定义玩家无敌的线程
Thread th=new InvincibleThread1();
th.start();
}
}
public void plusspeed()
{
//如果当前速度小于最高速度
if(myP.speed<myP.maxspeed)
{
myP.speed+=2;
//更新速度数据
setLabel("速度:"+myP.speed,speed);
}
}
public void plusbombnum()
{
//如果当前炸弹数小于最高炸弹数
if(myP.bombnum<myP.maxbombnum)
{
//炸弹数加一
myP.bombnum++;
//更新炸弹数据
setLabel("泡泡数:"+myP.bombnum,bombnum);
}
}
public void pluspower()
{
if(myP.power<myP.maxpower)
//如果当前威力小于最高威力
{
//威力加一
myP.power++;
//更新威力数据
setLabel("威力:"+myP.power,power);
}
}
public void pluslive()
{
//生命加一
myP.live++;
//刷新生命值数据
setLabel("生命值:"+myP.live,live);
}
public class Music extends Thread
{
public volatile boolean flag = true;
private String fileName;
private final int EXTERNAL_BUFFER_SIZE = 524288;
public Music(int n) {
switch (n)
{
case 1:
this.fileName="gameBgm.wav";
break;
case 2:
this.fileName="Music.wav";
break;
case 3:
this.fileName="MUsic2.wav";
break;
default :
break;
}
}
@Override
public void run() {
//1 获取你要播放的音乐文件
File soundFile = new File(fileName);
if (!soundFile.exists()) {
System.err.println("Wave file not found:" + fileName);
return;
}
while (flag){
//2、定义一个AudioInputStream用于接收输入的音频数据
AudioInputStream audioInputStream = null;
try {
//3、使用AudioSystem来获取音频的音频输入流(处理(抛出)异常)
audioInputStream = AudioSystem.getAudioInputStream(soundFile);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
//4、使用AudioFormat来获取AudioInputStream的格式
AudioFormat format = audioInputStream.getFormat();
//5、一个源数据行
SourceDataLine auline = null;
//6、获取受数据行支持的音频格式DataLine.info
DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);
try {
//7、获取与上面类型相匹配的行 写到源数据行里
auline = (SourceDataLine) AudioSystem.getLine(info);
auline.open(format);
} catch (LineUnavailableException e) {
e.printStackTrace();
return;
} catch (Exception e) {
e.printStackTrace();
return;
}
//9 允许某个数据行执行数据i/o
auline.start();
//10、写数据
int nBytesRead = 0;
//设置字节数组大小
byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];
try {
//11、从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中
while (nBytesRead != -1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0) {
//12、读取了之后将数据写入混频器,开始播放
auline.write(abData, 0, nBytesRead);
}
}
} catch (IOException e) {
e.printStackTrace();
return;
} finally {
//关闭
auline.drain();
auline.close();
}
}
总结步骤:
1 获取你要播放的音乐文件
2、定义一个AudioInputStream用于接收输入的音频数据
3、使用AudioSystem来获取音频的音频输入流(处理(抛出)异常)
4、使用AudioFormat来获取AudioInputStream的格式
5、创建一个源数据行
6、获取受数据行支持的音频格式 DataLine.info 如果采用.getSourceDataLine()方法可以省略)
7、获取与上面类型相匹配的行 写到源数据行里 二选一
8、打开具有指定格式的行,这样可以使行获得资源并进行操作
9、允许某个数据行执行数据i/o
10、写数据
11、从音频流读取指定的最大数量的数据字节,并将其放入给定的字节数组中。
12、读取哪个数组
13、读取了之后将数据写入混频器,开始播放
标签:闪烁 i++ NPU gif source isa 背景 音频 extend
原文地址:https://www.cnblogs.com/seerking/p/12173351.html