博客
关于我
超级马力 超级玛丽游戏开发 入门级源码
阅读量:804 次
发布时间:2019-03-25

本文共 3837 字,大约阅读时间需要 12 分钟。

【实例简介】

我停止这个项目有三个原因:

1. 我必须将精力集中在我的主要项目上。这个项目只是一个有趣的副业。

2. 我不愿意为非个人用途滥用受版权保护的材料,你也不应该。

这次参赛的比赛已经结束了。

我发布了这个项目的源代码,因为我收到了大量的诚恳建议

但是我发现自己没有时间去执行这些建议。

该代码(/src/)已经发布到公共域,因此你可以随心所欲地使用它。

关于艺术资源

(/res/)仍然受任天堂的版权保护。因此,我可以确信我们不能对其进行任何使用,建议你直接向任天堂咨询。

此外,如果你想开发一个更大的项目,请考虑使用法律允许的艺术代替传统的艺术创作。

【核心代码】

public class SonarSoundEngine implements Runnable{

private SonarSample silentSample;

private SourceDataLine sdl;

private int rate = 44100;

private ListenerMixer listenerMixer;

private int bufferSize = rate / 100; // 10 ms

private ByteBuffer soundBuffer = ByteBuffer.allocate(bufferSize * 4);

private float[] leftBuf, rightBuf;

private float amplitude = 1;

private float targetAmplitude = 1;

private boolean alive = true;

protected SonarSoundEngine(){} public SonarSoundEngine(int maxChannels) throws LineUnavailableException{

silentSample = new SonarSample(new float[] {0}, 44100);

Mixer mixer = AudioSystem.getMixer(null);

sdl = (SourceDataLine) mixer.getLine(new Line.Info(SourceDataLine.class));

sdl.open(new AudioFormat(rate, 16, 2, true, false), bufferSize * 2 * 2 * 2 * 2 * 2);

soundBuffer.order(ByteOrder.LITTLE_ENDIAN);

sdl.start();

try{

System.out.println("Failed to set the sound volume");

}

listenerMixer = new ListenerMixer(maxChannels);

leftBuf = new float[bufferSize];

rightBuf = new float[bufferSize];

Thread thread = new Thread(this);

thread.setDaemon(true);

thread.setPriority(10);

thread.start();}

public void setListener(SoundListener soundListener){

listenerMixer.setSoundListener(soundListener);

}

public void shutDown(){

alive = false;

}

public SonarSample loadSample(String resourceName){

try{

return SampleLoader.loadSample(resourceName);

} catch (Exception e){

System.out.println("Failed to load sample " + resourceName + ". Using silent sample");

e.printStackTrace();

return silentSample;

}

public void play(SonarSample sample, SoundSource soundSource, float volume, float priority, float rate){

synchronized (listenerMixer) {

listenerMixer.addSoundProducer(new SamplePlayer((SonarSample) sample, rate), soundSource, volume, priority);

}

public void clientTick(float alpha){

synchronized (listenerMixer) {

listenerMixer.update(alpha);

}

public void tick(){

soundBuffer.clear();

// targetAmplitude = (targetAmplitude - 1) * 0.9f 1;

// targetAmplitude = (targetAmplitude - 1) * 0.9f 1;

synchronized (listenerMixer) {

float maxAmplitude = listenerMixer.read(leftBuf, rightBuf, rate);

// if (maxAmplitude > targetAmplitude) targetAmplitude = maxAmplitude;

}

soundBuffer.clear();

float gain = 32000;

for (int i = 0; i < bufferSize; i ) {

// amplitude = (targetAmplitude - amplitude) / rate;

// amplitude = 1;

// float gain = 30000;

int l = (int) (leftBuf[i] * gain);

int r = (int) (rightBuf[i] * gain);

if (l > 32767) l = 32767;

if (r > 32767) r = 32767;

if (l < -32767) l = -32767;

if (r < -32767) r = -32767;

soundBuffer.putShort((short)l);

soundBuffer.putShort((short)r);

sdl.write(soundBuffer.array(), 0, bufferSize * 2 * 2);

转载地址:http://petyk.baihongyu.com/

你可能感兴趣的文章
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>
【Java基础】什么是面向对象?
查看>>
mysql 查询,正数降序排序,负数升序排序
查看>>
MySQL 树形结构 根据指定节点 获取其下属的所有子节点(包含路径上的枝干节点和叶子节点)...
查看>>
mysql 死锁 Deadlock found when trying to get lock; try restarting transaction
查看>>
mysql 死锁(先delete 后insert)日志分析
查看>>
MySQL 死锁了,怎么办?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 深度分页性能急剧下降,该如何优化?
查看>>
MySQL 添加列,修改列,删除列
查看>>