<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 0,  comments - 6,  trackbacks - 0

    The Basic Window  基本窗口

    Our basic window is going to be created and maintained by a central class, Game. The following sections cover the initial sections of code in the main class.

    譯:Game類實現(xiàn)基本窗口的創(chuàng)建和維護。下面的章節(jié)包括主窗口類的的初始化代碼:

    Game Entry Point

    游戲入口點

    In java our entry point is "public static void main(String arg[])". This is where the application starts when its run. From here we're going to create an instance of our main class which will start everything else running. Game will be a subclass of Canvas, since it will be the main element displaying the graphics. Note, that it needs to be a subclass of Canvas since its one of the only components that supports using accelerated graphics.

    譯:Java,啟動應用程序的點是public static void main(String arg[])”。在這里我們要創(chuàng)建一個主類的對象實例使整個程序運行起來。Game類是 Canvas類的子類,主要用于把游戲圖形在界面上顯示出來。請注意,它必須是一個Canvas(畫布,Canvas 組件表示屏幕上一個空白矩形區(qū)域,應用程序可以在該區(qū)域內(nèi)繪圖,或者可以從該區(qū)域捕獲用戶的輸入事件。,因為它是唯一支持圖形加速的組件

    public static void main(String argv[]) {

    Game g = new Game();

            g.gameLoop();

    }

    Creating the window

    創(chuàng)建窗口

    First we need to create our window and configure its contents. We're going to fix our resolution to 800x600. However, since the window may have decoration the content must be set to 800x600 and we must rely on pack() (shown a little later) to actually size the window appropriately.

    譯:首先,我們需要創(chuàng)建并配置我們的窗口。我們將設(shè)置屏幕分辨率為800*600。 然而因為窗口包含的一些內(nèi)容必須被設(shè)置成800*600的分辨率,所以必須依靠pack()方法(稍后介紹,調(diào)整此窗口的大小,以適合其子組件的首選大小和布局)來合適地設(shè)置窗口的實際大小。

    // create a frame to contain our game

    //創(chuàng)建一個 frame 包含我們的游戲

    JFrame container = new JFrame("Space Invaders 101");

    // get hold the content of the frame and set up the 

    // resolution of the game

    //獲得frame(窗口)的內(nèi)容面板。設(shè)置游戲分辨率

    JPanel panel = (JPanel) container.getContentPane();

    panel.setPreferredSize(new Dimension(800,600));

    panel.setLayout(null);

    // setup our canvas size and put it into the content of the frame

    //設(shè)置畫布尺寸并將其添加到frame(窗口)的內(nèi)容面板

    setBounds(0,0,800,600);

    panel.add(this);

    Since the canvas we're working with is going to be actively redrawn (i.e. accelerated graphics) we need to prevent Java AWT attempting to redraw our surface. Finally, we get the window to resolve its size, prevent the user resizing it and make it visible.

    譯:由于畫布會被頻繁的重繪(即加速圖形),我們需要阻止Java AWT重繪我們的界面。最后,我們設(shè)置好窗口的大小,并且不允許用戶重置窗口大小,然后顯示此窗口。

    // Tell AWT not to bother repainting our canvas since we're

    // going to do that our self in accelerated mode

    setIgnoreRepaint(true);//設(shè)置是否應該忽略從操作系統(tǒng)接受的繪制消息。

    // finally make the window visible  //最后顯示該窗口

    container.pack();

    container.setResizable(false);

    container.setVisible(true); //根據(jù)參數(shù)的值顯示或隱藏此 Window

    Accelerated Graphics  圖形加速

    To manage our accelerated graphics canvas we're going to rely on a class provided from the JDK. The BufferStrategy is named that because its a strategy for managing buffers, or rather the swapping of buffers. This supports us using page flipping and accelerated graphics.

    譯:我們需要依靠JDK提供的BufferStrategy 類在 canvas (畫布)上使用緩沖區(qū)管理策略,如稱為翻頁的緩沖區(qū)交換策略,來支持圖形加速

    Creating a BufferStrategy couldn't be simpler. We simply ask the Canvas to do it for us. The only thing that needs to be specified is how many buffers to use to manage the screen, in this case we're going to use just 2.

    譯:構(gòu)造一個BufferStrategy本身不是一件簡單的事情。我們可以簡單地調(diào)用Canvas 的方法來創(chuàng)建。唯一需要指定的是需要多少緩沖區(qū)用來管理屏幕,我們將只使用2緩沖區(qū)。

    // create the buffering strategy which will allow AWT

    // to manage our accelerated graphics 創(chuàng)建緩沖策略,讓AWT管理我們的圖形加速

    createBufferStrategy(2);

    strategy = getBufferStrategy();

    The Game Loop  游戲循環(huán)

    The game loop is a remarkably important part of any game. Client side games tend to be single threaded. This helps prevent a whole bunch of complexities synchronising game updates and game drawing. Generally this results in more stable and maintainble code. There are good arguments to use threading, however for the purposes of this tutorial we're not going to consider it.

    譯:游戲循環(huán)是任何游戲顯著的重要組成部分。客戶端游戲往往是單線程的。這有助于避免大量復雜同步游戲更新和游戲圖形繪制。通常來說,這樣的代碼更加穩(wěn)定和易于維護有很多好處促使我們使用線程,然而,出于本教程的目的,我們將不討論它了。

    The normal game loop runs something like this:

    · Work out how long its been since we last looped

    · Process any input from the user/player

    · Move everything based on the time since last loop

    · Draw everything on screen

    · Swap the buffers over to make the new image visible

    · Wait for a set period

    譯:一般的游戲循環(huán)是這樣運行的:

    · 計算出從游戲重新開始運行已經(jīng)多長時間了

    · 處理用戶/玩家的任何輸入

    · 基于上一次循環(huán)結(jié)束的時間移動所有游戲?qū)嶓w

    · 在屏幕上繪制游戲?qū)嶓w

    · 交換緩沖區(qū),加速新圖片顯示

    · 等待一個規(guī)定的時間

    At this stage we're just interested in getting the screen swap and timing done, so we add a function called gameLoop that does this:

    譯:在這個階段,我們僅僅對屏幕切換(重繪)逝去時間感興趣,所以我們添加一個gameLoop函數(shù)來實現(xiàn)

    long lastLoopTime = System.currentTimeMillis(); // 上一次循環(huán)的時間

    while (gameRunning) {

    // work out how long its been since the last update, this

    // will be used to calculate how far the entities should

    // move this loop 計算從上一次更新到現(xiàn)在已經(jīng)過去多長時間了,

             //逝去時間值將用于計算本次循環(huán)所有實體對象應該移動多遠

    long delta = System.currentTimeMillis() - lastLoopTime;

    lastLoopTime = System.currentTimeMillis();

    // Get hold of a graphics context for the accelerated 

    // surface and blank it out 獲得一個加速圖形上下文,并讓它的表面空著

            Graphics2D g = (Graphics2D) strategy.getDrawGraphics();

    g.setColor(Color.black);

    g.fillRect(0,0,800,600);

    // finally, we've completed drawing so clear up the graphics

    // and flip the buffer over 最后,我們已經(jīng)完成繪圖,因此釋放此圖形的上下文以及它使用的所有系統(tǒng)資源,翻轉(zhuǎn)緩沖區(qū)(將顯示指針指向緩沖區(qū),此緩沖區(qū)變?yōu)轱@示區(qū),舊的顯示區(qū)變?yōu)榫彌_區(qū)),顯示圖形

    g.dispose();

    strategy.show();

    // finally pause for a bit. Note: this should run us at about

    // 100 fps but on windows this might vary each loop due to

    // a bad implementation of timer  最好,暫停一會兒。注意:大約是100幀,但在windows下面會有所不同,因為定時器實現(xiàn)不同。

    try { Thread.sleep(10); } catch (Exception e) {}

    }

    Finally we call gameLoop from the bottom of the constructor of Game. This just starts the game loop running around. If you've been following this code you should be able to run what you currently have and a window should be displayed with an accelerated canvas that shows a black screen.

    譯:最后,我們在Game類的構(gòu)造器函數(shù)中調(diào)用 gameLoop 方法,啟動游戲。如果你已經(jīng)寫好這些代碼了,你可以運行它們,會彈出一個采用了硬件加速的黑色屏幕窗口。

    學軟件開發(fā),到蜂鳥科技!
    超強的師資力量 、完善的課程體系 、超低的培訓價格 、真實的企業(yè)項目。

    網(wǎng)址:www.ntcsoft.com 
    電話:0371-63839606 
    鄭州軟件開發(fā)興趣小組群:38236716

    posted on 2010-11-25 23:43 whistler 閱讀(276) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    留言簿(2)

    我參與的團隊

    文章檔案(22)

    搜索

    •  

    最新評論

    主站蜘蛛池模板: 亚洲导航深夜福利| 老司机福利在线免费观看| 国产精品无码免费播放| 亚洲AV无码专区在线观看成人| 亚洲人妻av伦理| 亚洲最大免费视频网| 久久亚洲中文无码咪咪爱| 国产gv天堂亚洲国产gv刚刚碰| 久久午夜夜伦鲁鲁片免费无码影视| 日韩亚洲人成在线| 精品亚洲综合在线第一区| 国产va精品免费观看| 黄视频在线观看免费| 日韩亚洲国产综合高清| 久久国产亚洲精品麻豆| 全免费a级毛片免费看不卡| 免费国产成人18在线观看| 亚洲av成人片在线观看| 亚洲高清在线视频| 四虎永久成人免费| 84pao强力永久免费高清| 一级黄色片免费观看| 亚洲欧洲另类春色校园网站| 在线观看午夜亚洲一区| 永久久久免费浮力影院| 99精品视频在线视频免费观看| 在线视频亚洲一区| 亚洲冬月枫中文字幕在线看| 在线亚洲午夜理论AV大片| 午夜国产大片免费观看| 免费观看激色视频网站(性色) | 精品亚洲成在人线AV无码| 亚洲一区二区视频在线观看| 性感美女视频在线观看免费精品| 久久久久成人片免费观看蜜芽| 黄色三级三级免费看| 亚洲乱码无人区卡1卡2卡3| 亚洲精品网站在线观看你懂的| 最新亚洲成av人免费看| 国产公开免费人成视频| 成全视频免费高清|