梦幻之旅

DEBUG - 天道酬勤

   :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  671 随笔 :: 6 文章 :: 256 评论 :: 0 Trackbacks

周六无聊,花了二个小时写了一个游戏的大体框架.
Game.java

@SuppressWarnings("unused")
public class Game 
{
    
private Scene scene;
    
private GameEngine gameEngine;
    
    
public Game()
    
{
        
this.scene = new Scene();
        
this.gameEngine = new GameEngine();
    }

    
    
public static void main(String[] args)
    
{
        
new Game();
    }

}

GameEngine.java

import java.awt.Graphics2D;
import java.awt.Image;


public class GameEngine implements Runnable
{
    
/** 引擎重画间隔 */
    
private int engineInterval = 30;
    
    
/** 游戏引擎刷新线程 */
    
private Thread refresher;
    
    
/** 游戏主窗口 */
    
private GameFrame gameFrame;
    
    
/** 双缓存作图区 */
    
private Image imageBuffer;
    
    
/** ImageIterm缓存 */
    
public static ImageItermCahe imageItermCache = new ImageItermCahe();
    
    
public GameEngine()
    
{
        
this.refresher = new Thread(this);
        
this.gameFrame = new GameFrame();
        
this.imageBuffer = this.gameFrame.createImage(
                
this.gameFrame.getWidth(), this.gameFrame.getHeight());
        
        
/** 启动游戏引擎 */
        
this.refresher.start();
    }

    
    
private void working()
    
{
        Graphics2D imageBufferGraphics 
= (Graphics2D)(this.imageBuffer.getGraphics());
        imageBufferGraphics.clearRect(
00785360);
        
for(ImageIterm imgIterm : GameEngine.imageItermCache.getImgItermLst())
        
{
            
/** 删除已经删除的图项 */
            
if(GameEngine.imageItermCache.getImgItermMap().get(imgIterm.getId()) == null)
            
{
                GameEngine.imageItermCache.getImgItermLst().remove(imgIterm);
                
continue;
            }

            imageBufferGraphics.drawImage(imgIterm.getImg(),
                    imgIterm.getXTargetTop(),
                    imgIterm.getYTargetTop(),
                    imgIterm.getXTargetBot(),
                    imgIterm.getYTargetBot(),
                    imgIterm.getXSourceTop(),
                    imgIterm.getYSourceTop(),
                    imgIterm.getXSourceBot(),
                    imgIterm.getYSourceBot(), 
null);
        }

        
this.gameFrame.setImageBuffer(this.imageBuffer);
        
this.gameFrame.repaint();
    }

    
    
public void run()
    
{
        
while(true)
        
{
            
this.working();
            
try
            
{
                Thread.sleep(
this.engineInterval);
            }

            
catch(Exception ex)
            
{
                ex.printStackTrace();
            }

        }

    }

    
    
public void setEngineInterval(int engineInterval)
    
{
        
this.engineInterval = engineInterval;
    }

}

GameFrame.java

 

import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JFrame;

@SuppressWarnings(
"serial")
public class GameFrame extends JFrame 
{
    
/** 缓存作图区 */
    
private Image imageBuffer;

    
public GameFrame() 
    
{
        
this.setTitle("游戏作图区");
        
this.setSize(785360);
        
        
/** 定位游戏窗口在屏幕上的位置 */
        
this.setLocation((this.getToolkit().getScreenSize().width - this.getWidth()) / 2,
                (
this.getToolkit().getScreenSize().height - this.getHeight()) / 2);

        
this.setDefaultCloseOperation(3);
        
this.setResizable(false);
        
this.setVisible(true);
    }


    
public void update(Graphics g) 
    
{
        
if (this.imageBuffer != null
        
{
            
this.getGraphics().drawImage(imageBuffer, 00this);
        }

    }

    
    
public void setImageBuffer(Image imageBuffer)
    
{
        
this.imageBuffer = imageBuffer;
    }

}


 还有三个类在下一篇.

posted on 2010-03-21 10:13 HUIKK 阅读(167) 评论(0)  编辑  收藏 所属分类: Java

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


网站导航: