程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> J2ME >> 實現游戲開發中的屏幕滾動功能

實現游戲開發中的屏幕滾動功能

編輯:J2ME
  J2ME游戲開發中使用層的概念中介紹了如何在游戲開發中使用層,其中提到LayerManager的一個概念,VIEw Window,本文將借助這個概念實現屏幕滾動的功能。

    屏幕的移動效果一般我們是通過改變View Window的的位置來實現的,比如你想屏幕向右移動,那麼你要調整View Window的x坐標增加相應的數值,如果想屏幕向左移動,那麼調整View Window的x坐標減少相應的數值。上下移動原理一樣。我們在得到用戶的輸入後就可以對VIEw Window的位置進行調整然後重新繪制屏幕。
 private void input()
    {
        int keyStates = getKeyStates();

        if ((keyStates & LEFT_PRESSED) != 0)
        {
            if (scnX - 1 > 0)
                scnX--;
        }
        if ((keyStates & RIGHT_PRESSED) != 0)
        {
            if (scnX + 1 + 140 < backgroundImage.getWidth())
                scnX++;
        }
       
    }

    // Method to Display Graphics
    private void drawScreen(Graphics g)
    {

        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x0000ff);

        // display all layers
        layerManager.setVIEwWindow(scnX, scnY, 140, 140);
        layerManager.paint(g, 20, 20);

        flushGraphics();
    }
我們只使用一個背景圖片如下:

 

 

 

 

 

由於程序比較簡單,這裡直接給出源代碼不做過多的解釋。

import Javax.microedition.lcdui.*;
import Javax.microedition.lcdui.game.*;

public class ExampleGameCanvas extends GameCanvas implements Runnable
{
    private boolean isPlay; // Game Loop runs when isPlay is true
    private long delay; // To give thread consistency
    private int width; // To hold screen width
    private int height; // To hold screen height
    private int scnX, scnY; // To hold screen starting vIEwpoint

    // Sprites to be used
    Image backgroundImage;
    private Sprite backgroundSprite;

    // Layer Manager
    private LayerManager layerManager;

    // Constructor and initialization
    public ExampleGameCanvas() throws Exception
    {
        super(true);
        width = getWidth();
        height = getHeight();

        scnX = 55;
        scnY = 20;
        delay = 20;

        // Load Images to Sprites
        backgroundImage = Image.createImage("/background.png");
        backgroundSprite = new Sprite(backgroundImage);

        layerManager = new LayerManager();
        layerManager.append(backgroundSprite);

    }

    // Automatically start thread for game loop
    public void start()
    {
        isPlay = true;
        Thread t = new Thread(this);
        t.start();
    }

    public void stop()
    {
        isPlay = false;
    }

    // Main Game Loop
    public void run()
    {
        Graphics g = getGraphics();
        while (isPlay == true)
        {

            input();
            drawScreen(g);
            try
            {
                Thread.sleep(delay);
            } catch (InterruptedException IE)
            {
            }
        }
    }

    // Method to Handle User Inputs
    private void input()
    {
        int keyStates = getKeyStates();

        if ((keyStates & LEFT_PRESSED) != 0)
        {
            if (scnX - 1 > 0)
                scnX--;
        }
        if ((keyStates & RIGHT_PRESSED) != 0)
        {
            if (scnX + 1 + 140 < backgroundImage.getWidth())
                scnX++;
        }
       
    }

    // Method to Display Graphics
    private void drawScreen(Graphics g)
    {

        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0x0000ff);

        // display all layers
        layerManager.setVIEwWindow(scnX, scnY, 140, 140);
        layerManager.paint(g, 20, 20);

        flushGraphics();
    }

}


import Javax.microedition.midlet.*;
import Javax.microedition.lcdui.*;

public class SimpleScrollingLayerManger extends MIDlet
{
    private Display display;

    public void startApp()
    {
        try
        {
            display = Display.getDisplay(this);
            ExampleGameCanvas gameCanvas = new ExampleGameCanvas();
            gameCanvas.start();
            display.setCurrent(gameCanvas);
        } catch (Exception ex)
        {
            System.out.println(ex);
        }
    }

    public Display getDisplay()
    {
        return display;
    }

    public void pauseApp()
    {
    }

    public void destroyApp(boolean unconditional)
    {
        exit();
    }

    public void exit()
    {
        System.gc();
        destroyApp(false);
        notifyDestroyed();
    }
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved