Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Guangjun Jia
FlappyBird
Commits
e2cb276d
Commit
e2cb276d
authored
Jul 12, 2020
by
kingyu
Committed by
kingyuluk
Jul 12, 2020
Browse files
feat: 更换音频播放方式
BREAKING CHANGE: 使用AudioClip类的方法播放连续的短音频可能会导致线程冲突使游戏卡顿,改用sun.audio类的AudioPlayer方法播放音频
parent
4e8f8dab
Changes
37
Hide whitespace changes
Inline
Side-by-side
sources/img/down_5.png
→
re
sources/img/down_5.png
View file @
e2cb276d
File moved
sources/img/down_6.png
→
re
sources/img/down_6.png
View file @
e2cb276d
File moved
sources/img/down_7.png
→
re
sources/img/down_7.png
View file @
e2cb276d
File moved
sources/img/over.png
→
re
sources/img/over.png
View file @
e2cb276d
File moved
sources/img/pipe.png
→
re
sources/img/pipe.png
View file @
e2cb276d
File moved
sources/img/pipe_bottom.png
→
re
sources/img/pipe_bottom.png
View file @
e2cb276d
File moved
sources/img/pipe_top.png
→
re
sources/img/pipe_top.png
View file @
e2cb276d
File moved
sources/img/score.png
→
re
sources/img/score.png
View file @
e2cb276d
File moved
sources/img/start.png
→
re
sources/img/start.png
View file @
e2cb276d
File moved
sources/img/title.png
→
re
sources/img/title.png
View file @
e2cb276d
File moved
sources/img/up.png
→
re
sources/img/up.png
View file @
e2cb276d
File moved
sources/wav/crash.wav
→
re
sources/wav/crash.wav
View file @
e2cb276d
File moved
sources/wav/fly.wav
→
re
sources/wav/fly.wav
View file @
e2cb276d
File moved
sources/wav/score.wav
→
re
sources/wav/score.wav
View file @
e2cb276d
File moved
src/com/bird/main/GameFrame.java
View file @
e2cb276d
package
com.bird.main
;
import
com.bird.util.MusicUtil
;
import
static
com
.
bird
.
util
.
Constant
.
FRAME_HEIGHT
;
import
static
com
.
bird
.
util
.
Constant
.
FRAME_WIDTH
;
import
static
com
.
bird
.
util
.
Constant
.
FRAME_X
;
...
...
@@ -119,7 +117,6 @@ public class GameFrame extends Frame implements Runnable {
foreground
=
new
GameForeground
();
ready
=
new
GameReady
();
bird
=
new
Bird
();
MusicUtil
.
load
();
setGameState
(
STATE_READY
);
// 启动用于刷新窗口的线程
...
...
src/com/bird/util/Constant.java
View file @
e2cb276d
...
...
@@ -22,36 +22,37 @@ public class Constant {
public
static
final
int
FRAME_Y
=
100
;
// 图像资源路径
public
static
final
String
BG_IMG_PATH
=
"sources/img/background.png"
;
// 背景图片
public
static
final
String
BG_IMG_PATH
=
"
re
sources/img/background.png"
;
// 背景图片
public
static
final
int
HOVER_MOVING_SCORE
=
4
;
//出现移动管道的分数
// 小鸟图片
// 小鸟图片
public
static
final
String
[][]
BIRDS_IMG_PATH
=
{
{
"sources/img/0.png"
,
"sources/img/1.png"
,
"sources/img/2.png"
,
"sources/img/3.png"
,
"sources/img/4.png"
,
"sources/img/5.png"
,
"sources/img/6.png"
,
"sources/img/7.png"
},
{
"sources/img/up.png"
,
"sources/img/up.png"
,
"sources/img/up.png"
,
"sources/img/up.png"
,
"sources/img/up.png"
,
"sources/img/up.png"
,
"sources/img/up.png"
,
"sources/img/up.png"
},
{
"sources/img/down_0.png"
,
"sources/img/down_1.png"
,
"sources/img/down_2.png"
,
"sources/img/down_3.png"
,
"sources/img/down_4.png"
,
"sources/img/down_5.png"
,
"sources/img/down_6.png"
,
"sources/img/down_7.png"
},
{
"sources/img/dead.png"
,
"sources/img/dead.png"
,
"sources/img/dead.png"
,
"sources/img/dead.png"
,
"sources/img/dead.png"
,
"sources/img/dead.png"
,
"sources/img/dead.png"
,
"sources/img/dead.png"
,
}
};
{
"resources/img/0.png"
,
"resources/img/1.png"
,
"resources/img/2.png"
,
"resources/img/3.png"
,
"resources/img/4.png"
,
"resources/img/5.png"
,
"resources/img/6.png"
,
"resources/img/7.png"
},
{
"resources/img/up.png"
,
"resources/img/up.png"
,
"resources/img/up.png"
,
"resources/img/up.png"
,
"resources/img/up.png"
,
"resources/img/up.png"
,
"resources/img/up.png"
,
"resources/img/up.png"
},
{
"resources/img/down_0.png"
,
"resources/img/down_1.png"
,
"resources/img/down_2.png"
,
"resources/img/down_3.png"
,
"resources/img/down_4.png"
,
"resources/img/down_5.png"
,
"resources/img/down_6.png"
,
"resources/img/down_7.png"
},
{
"resources/img/dead.png"
,
"resources/img/dead.png"
,
"resources/img/dead.png"
,
"resources/img/dead.png"
,
"resources/img/dead.png"
,
"resources/img/dead.png"
,
"resources/img/dead.png"
,
"resources/img/dead.png"
,
}
};
// 云朵图片
public
static
final
String
[]
CLOUDS_IMG_PATH
=
{
"sources/img/cloud_0.png"
,
"sources/img/cloud_1.png"
};
public
static
final
String
[]
CLOUDS_IMG_PATH
=
{
"
re
sources/img/cloud_0.png"
,
"
re
sources/img/cloud_1.png"
};
// 水管图片
public
static
final
String
[]
PIPE_IMG_PATH
=
{
"sources/img/pipe.png"
,
"sources/img/pipe_top.png"
,
"sources/img/pipe_bottom.png"
};
public
static
final
String
[]
PIPE_IMG_PATH
=
{
"
re
sources/img/pipe.png"
,
"
re
sources/img/pipe_top.png"
,
"
re
sources/img/pipe_bottom.png"
};
public
static
final
String
TITLE_IMG_PATH
=
"sources/img/title.png"
;
public
static
final
String
NOTICE_IMG_PATH
=
"sources/img/start.png"
;
public
static
final
String
SCORE_IMG_PATH
=
"sources/img/score.png"
;
public
static
final
String
OVER_IMG_PATH
=
"sources/img/over.png"
;
public
static
final
String
AGAIN_IMG_PATH
=
"sources/img/again.png"
;
public
static
final
String
TITLE_IMG_PATH
=
"
re
sources/img/title.png"
;
public
static
final
String
NOTICE_IMG_PATH
=
"
re
sources/img/start.png"
;
public
static
final
String
SCORE_IMG_PATH
=
"
re
sources/img/score.png"
;
public
static
final
String
OVER_IMG_PATH
=
"
re
sources/img/over.png"
;
public
static
final
String
AGAIN_IMG_PATH
=
"
re
sources/img/again.png"
;
public
static
final
String
SCORE_FILE_PATH
=
"sources/score"
;
// 分数文件路径
public
static
final
String
SCORE_FILE_PATH
=
"
re
sources/score"
;
// 分数文件路径
// 游戏背景色
public
static
final
Color
BG_COLOR
=
new
Color
(
0x4bc4cf
);
...
...
src/com/bird/util/MusicUtil.java
View file @
e2cb276d
package
com.bird.util
;
import
java.applet.Applet
;
import
java.applet.AudioClip
;
import
java.io.File
;
import
java.net.MalformedURLException
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
sun.audio.AudioPlayer
;
import
sun.audio.AudioStream
;
/**
* 音乐工具类
*
*
* @author Kingyu wav音频:JDK提供的类可直接解码 mp3音频:JDK没有提供支持,需要使用第三方的工具包
*
*
*/
public
class
MusicUtil
{
private
static
AudioClip
fly
;
private
static
AudioClip
crash
;
private
static
AudioClip
score
;
// 装载音乐资源
@SuppressWarnings
(
"deprecation"
)
public
static
void
load
()
{
try
{
fly
=
Applet
.
newAudioClip
(
new
File
(
"sources/wav/fly.wav"
).
toURL
());
crash
=
Applet
.
newAudioClip
(
new
File
(
"sources/wav/crash.wav"
).
toURL
());
score
=
Applet
.
newAudioClip
(
new
File
(
"sources/wav/score.wav"
).
toURL
());
}
catch
(
MalformedURLException
e
)
{
e
.
printStackTrace
();
}
}
private
static
AudioStream
fly
;
private
static
AudioStream
crash
;
private
static
AudioStream
score
;
private
static
InputStream
flyIn
;
private
static
InputStream
crashIn
;
private
static
InputStream
scoreIn
;
//wav播放
public
static
void
playFly
()
{
fly
.
play
();
try
{
// create an audiostream from the inputstream
flyIn
=
new
FileInputStream
(
"resources/wav/fly.wav"
);
fly
=
new
AudioStream
(
flyIn
);
}
catch
(
FileNotFoundException
fnfe
)
{
}
catch
(
IOException
ioe
)
{
}
AudioPlayer
.
player
.
start
(
fly
);
}
public
static
void
playCrash
()
{
crash
.
play
();
try
{
// create an audiostream from the inputstream
crashIn
=
new
FileInputStream
(
"resources/wav/crash.wav"
);
crash
=
new
AudioStream
(
crashIn
);
}
catch
(
FileNotFoundException
fnfe
)
{
}
catch
(
IOException
ioe
)
{
}
AudioPlayer
.
player
.
start
(
crash
);
}
public
static
void
playScore
()
{
score
.
play
();
try
{
// create an audiostream from the inputstream
scoreIn
=
new
FileInputStream
(
"resources/wav/score.wav"
);
score
=
new
AudioStream
(
scoreIn
);
}
catch
(
FileNotFoundException
fnfe
)
{
}
catch
(
IOException
ioe
)
{
}
AudioPlayer
.
player
.
start
(
score
);
}
}
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment