基本信息
源码名称:超级马力 超级玛丽游戏开发 入门级源码,仅供参考学习
源码大小:0.26M
文件格式:.zip
开发语言:Java
更新时间:2012-12-18
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

 

I stopped working on this project for three reasons;
1) I had to focus on my main project. This was just a fun side project.
2) I didn't feel comfortable abusing copyrighted material for non-personal use, and neither should you.
3) The competition this was an entry for ended.

I'm releasing the source code for this project since I've gotten quite a bit of email with really
good suggestions on how to make this better, but I don't have the time to implement them myself.

The code (/src/) is released as public domain, so you can do with it what you wish.
The art (/res/) is still copyright nintendo, so it's almost certainly NOT ok to do anything at all with it. Ask nintendo.

And, please, if you're going to make a bigger project out of this, please consider replacing the art with legal art.

 


About the code:

The code is basically undocumented, but should be readable anyway as it's fairly clean. The main entry points are
AppletLauncher and FrameLauncher. The main game is in MarioComponent.

"sonar" is the base of a software sound engine I've been working on. It's pretty nice, but there's a few bugs in it
(mostly timing based). It can easilly be ripped out and reused in another project.

The level editor isn't used for anything more than changing the behavior of blocks anymore. But I think there's still
code in there somewhere for loading a level instead of generating it, so if you want to reintroduce static levels,
you've got a nice base for a level editor there.

The game DOES support scrolling in the Y directions right out of the box! However, I didn't think it fit the retro feeling,
so I made all levels only one screen tall. ;)

The sprite package and class should be renamed into "entity" or "mobile" or something..



 

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
        {
/*            FloatControl volumeControl = (FloatControl) sdl.getControl(FloatControl.Type.MASTER_GAIN);
            volumeControl.setValue(volumeControl.getMaximum());*/
        }
        catch (IllegalArgumentException e)
        {
            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);
    }

    public void run()
    {
        while (alive)
        {
            tick();
        }
    }
}