2012年6月12日 星期二

Java Game Development(19)- Run and Movie Loop Methods


import java.awt.*;
import javax.swing.ImageIcon;
import java.awt.Image;


public class apples{
public static void main(String[] args){
apples a = new apples();
a.run();
}
private Animation ani;
private ScreenManager s;
private Image bg;
private static DisplayMode modes1[] = { 設置顯示模式
new DisplayMode(800, 600, 32, 0),
new DisplayMode(800, 600, 24, 0),
new DisplayMode(800, 600, 16, 0),
new DisplayMode(640, 480, 32, 0),
new DisplayMode(640, 480, 24, 0),
new DisplayMode(640, 480, 16, 0),
};
//加載圖像和添加場景
public void loadImages(){
bg = new ImageIcon("C:\\baby.jpg").getImage();
Image face1 = new ImageIcon("C:\\baby1.png").getImage();
Image face2 = new ImageIcon("C:\\baby2.png").getImage();
ani = new Animation();
ani.addScene(face1,250);
ani.addScene(face2,250);
}
//主要的方法(main method)
public void run(){
s = new ScreenManager();
try{
DisplayMode dm = s.findFirstCompatibleMode(modes1);
s.setFullScreen(dm);
loadImages();
movieLoop();
}finally{
s.restoreScreen();
}
}
//播放電影
public void movieLoop(){
long startingTime = System.currentTimeMillis();
long cumTime = startingTime;
while(cumTime - startingTime < 6000){
long timePassed = System.currentTimeMillis() -cumTime;
cumTime += timePassed;
ani.update(timePassed);
//繪製和更新屏幕
Graphics2D g = s.getGraphics();
draw(g);
g.dispose();
s.update();
try{
Thread.sleep(20);
}catch(Exception ex){}
}
}
}

沒有留言:

張貼留言