⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pingucanvas.java

📁 j2me_nokia_flash打企鹅手机版
💻 JAVA
字号:
import javax.microedition.lcdui.*;

// Referenced classes of package ys.pingu:
//            PinguSprites, PinguMIDlet, PinguScores

public class PinguCanvas extends Canvas
    implements CommandListener, Runnable
{

    public static int IMAGE_WIDTH;
    public static int IMAGE_HEIGHT;
    public static Graphics GC;
    public static int GAMESTATE;
    public static int GAMECOUNT;
    public static int HIGHSCORE;
    public static int SCORE;
    public static long TT = 0L;
    public static final int STATE_YETIWAITING = 0;
    public static final int STATE_PINGUDANCING = 1;
    public static final int STATE_PINGUJUMPING = 2;
    public static final int STATE_PITCH = 3;
    public static final int STATE_PINGUFLYING = 4;
    public static final int STATE_SCOREACCEPT = 5;
    private static final Font GAME_FONT = Font.getFont(0, 1, 0);
    private static final int ANCHOR = 20;
    private static final int MILLIS_PER_TICK = 40;
    private static final int GAMECOUNT_MAX = 5;
    private volatile Thread animationThread;
    private final Command backCommand = new Command("Back", 2, 1);
    private PinguSprites sprites;
    private PinguMIDlet parent;
    private Image currentFrame;
    private boolean keyHit;
    private boolean ignoreKeyHit;

    public PinguCanvas(PinguMIDlet pingumidlet)
    {
        animationThread = null;
        IMAGE_WIDTH = getWidth();
        IMAGE_HEIGHT = getHeight();
        parent = pingumidlet;
        if(!isDoubleBuffered())
        {
            currentFrame = Image.createImage(getWidth(), getHeight());
            GC = currentFrame.getGraphics();
            GC.setFont(GAME_FONT);
        }
        sprites = new PinguSprites();
        GAMECOUNT = 0;
        HIGHSCORE = 0;
        SCORE = 0;
        setCommandListener(this);
        addCommand(backCommand);
    }

    public void init()
    {
        GAMESTATE = 0;
        GAMECOUNT = 0;
        HIGHSCORE = 0;
        SCORE = 0;
        keyHit = false;
        ignoreKeyHit = true;
        sprites.init();
    }

    public void continueGame()
    {
        ignoreKeyHit = true;
    }

    public synchronized void start()
    {
        animationThread = new Thread(this);
        animationThread.start();
    }

    public synchronized void stop()
    {
        animationThread = null;
    }

    public void run()
    {
        Thread thread = Thread.currentThread();
        try
        {
            while(thread == animationThread) 
            {
                long l = System.currentTimeMillis();
                if(isShown())
                {
                    tick();
                    repaint();
                }
                long l1 = System.currentTimeMillis() - l;
                if(l1 < 40L)
                {
                    synchronized(this)
                    {
                        wait(40L - l1);
                    }
                } else
                {
                    Thread _tmp = thread;
                    Thread.yield();
                }
            }
        }
        catch(InterruptedException interruptedexception) { }
    }

    public void commandAction(Command command, Displayable displayable)
    {
        if(command == backCommand)
        {
            stop();
            parent.pinguCanvasMenu();
        }
    }

    protected void keyReleased(int i)
    {
        if(ignoreKeyHit)
        {
            ignoreKeyHit = false;
            return;
        } else
        {
            keyHit = true;
            return;
        }
    }

    protected void paint(Graphics g)
    {
        GC = currentFrame != null ? currentFrame.getGraphics() : g;
        GC.setFont(GAME_FONT);
        long l = System.currentTimeMillis();
        sprites.draw();
        TT = System.currentTimeMillis() - l;
        if(currentFrame != null)
            g.drawImage(currentFrame, 0, 0, 20);
    }

    private void tick()
    {
        if(keyHit && GAMESTATE == 0)
        {
            keyHit = false;
            GAMESTATE = 1;
        } else
        if(GAMESTATE != 1)
            if(GAMESTATE == 2)
            {
                GAMESTATE = 3;
                sprites.setHitable();
            } else
            if(keyHit && GAMESTATE == 3)
            {
                keyHit = false;
                sprites.hit();
            } else
            if(GAMESTATE != 4 && keyHit && GAMESTATE == 5)
            {
                keyHit = false;
                GAMECOUNT++;
                HIGHSCORE += SCORE;
                if(GAMECOUNT < 5)
                {
                    GAMESTATE = 0;
                    SCORE = 0;
                    sprites.init();
                } else
                {
                    int i = HIGHSCORE;
                    init();
                    if(PinguScores.isHighScore(i))
                    {
                        stop();
                        parent.pinguCanvasEnterHighScore(i);
                    }
                }
            }
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -