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类实现基本窗口的创建和维护。下面的章节包括主窗口类的的初始化代码:

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[])”。在这里我们要创建一个主类的对象实例使整个程序运行起来。Game类是 Canvas类的子类,主要用于把游戏图形在界面上显示出来。请注意,它必须是一个Canvas(画布,Canvas 组件表示屏幕上一个空白矩形区域,应用程序可以在该区域内绘图,或者可以从该区域捕获用户的输入事件。,因为它是唯一支持图形加速的组件

public static void main(String argv[]) {

Game g = new Game();

        g.gameLoop();

}

Creating the window

创建窗口

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.

译:首先,我们需要创建并配置我们的窗口。我们将设置屏幕分辨率为800*600。 然而因为窗口包含的一些内容必须被设置成800*600的分辨率,所以必须依靠pack()方法(稍后介绍,调整此窗口的大小,以适合其子组件的首选大小和布局)来合适地设置窗口的实际大小。

// create a frame to contain our game

//创建一个 frame 包含我们的游戏

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

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

// resolution of the game

//获得frame(窗口)的内容面板。设置游戏分辨率

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

//设置画布尺寸并将其添加到frame(窗口)的内容面板

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重绘我们的界面。最后,我们设置好窗口的大小,并且不允许用户重置窗口大小,然后显示此窗口。

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

// going to do that our self in accelerated mode

setIgnoreRepaint(true);//设置是否应该忽略从操作系统接受的绘制消息。

// finally make the window visible  //最后显示该窗口

container.pack();

container.setResizable(false);

container.setVisible(true); //根据参数的值显示或隐藏此 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 (画布)上使用缓冲区管理策略,如称为翻页的缓冲区交换策略,来支持图形加速

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.

译:构造一个BufferStrategy本身不是一件简单的事情。我们可以简单地调用Canvas 的方法来创建。唯一需要指定的是需要多少缓冲区用来管理屏幕,我们将只使用2缓冲区。

// create the buffering strategy which will allow AWT

// to manage our accelerated graphics 创建缓冲策略,让AWT管理我们的图形加速

createBufferStrategy(2);

strategy = getBufferStrategy();

The Game Loop  游戏循环

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.

译:游戏循环是任何游戏显着的重要组成部分。客户端游戏往往是单线程的。这有助于避免大量复杂同步游戏更新和游戏图形绘制。通常来说,这样的代码更加稳定和易于维护有很多好处促使我们使用线程,然而,出于本教程的目的,我们将不讨论它了。

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

译:一般的游戏循环是这样运行的:

· 计算出从游戏重新开始运行已经多长时间了

· 处理用户/玩家的任何输入

· 基于上一次循环结束的时间移动所有游戏实体

· 在屏幕上绘制游戏实体

· 交换缓冲区,加速新图片显示

· 等待一个规定的时间

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函数来实现

long lastLoopTime = System.currentTimeMillis(); // 上一次循环的时间

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 计算从上一次更新到现在已经过去多长时间了,

         //逝去时间值将用于计算本次循环所有实体对象应该移动多远

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 最后,我们已经完成绘图,因此释放此图形的上下文以及它使用的所有系统资源,翻转缓冲区(将显示指针指向缓冲区,此缓冲区变为显示区,旧的显示区变为缓冲区),显示图形

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下面会有所不同,因为定时器实现不同。

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类的构造器函数中调用 gameLoop 方法,启动游戏。如果你已经写好这些代码了,你可以运行它们,会弹出一个采用了硬件加速的黑色屏幕窗口。

学软件开发,到蜂鸟科技!
超强的师资力量 、完善的课程体系 、超低的培训价格 、真实的企业项目。

网址:www.ntcsoft.com 
电话:0371-63839606 
郑州软件开发兴趣小组群:38236716

posted on 2010-11-25 23:43 whistler 阅读(261) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

留言簿(2)

我参与的团队

文章档案(22)

搜索

  •  

最新评论