2016.10.24

2016-10-24  本文已影响0人  VikingOldYoung

总结今天的学习

主要利用图形界面的基础知识设计小游戏

加入双缓冲避免了游戏屏幕闪烁

    private Image offScreenImage=null;
        public void update(Graphics g){
        if(offScreenImage == null )
            offScreenImage= this.createImage(Constant.FRAME_WIDTH,Constant.FRAME_HEIGHT);       
        Graphics gOff =offScreenImage.getGraphics();        
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }

物体沿任意角度飞行,用弧度计算路径来处理的
例如

y = Speed*sin(degree);

导入图片,他弄了个工具类

public class GameUtil {
    
    private GameUtil(){}; //工具类通常把构造器私有了!
    
    public static Image getImage(String path){
        
        URL u=GameUtil.class.getClassLoader().getResource(path);
                //这据我估计应该是读取地址
        BufferedImage img=null;
        
        try {
            img=ImageIO.read(u);
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return img;
    }   
}

还有个键盘监听

    class KeyListener extends KeyAdapter{

        @Override
        public void keyPressed(KeyEvent e) {
            // 键盘按下
            plane.anxia(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            // 键盘弹起
            plane.shifang(e);
        }
        
    }

public void launchFrame() {
        // TODO Auto-generated method stub
        super.launchFrame();
        addKeyListener(new KeyListener());
}

还有个容器的利用,之所以用容器,应该是新建bullet时的b再给下一个并不会影响去找到上一个!哈哈

ArrayList bulletList=new ArrayList();//建一个存放子弹的容器,方便画图

    public void paint(Graphics g) {
        g.drawImage(bg, 0, 0, null);
        plane.draw(g);
        
        for(int i=0;i<bulletList.size();i++){
            Bullet b=(Bullet)bulletList.get(i);//挨个新建,很方便
            b.draw(g);//挨个画
        }
    }

顺便说说今天下午笔试都没过,没有挫败,坚定。我觉得,不断地学习加强自己才是正道。加油,成为你想要成为的人!梦想和理想总是有交集的!

上一篇 下一篇

猜你喜欢

热点阅读