【143天】尚学堂高淇Java300集视频精华笔记(93-97)

5集知识点汇总

  1. 静态变量的初始化方法(见代码)

  2. 懒加载应对方法(见代码)

  3. 计时方法(见代码)

最终代码

    package com.test088_097;
    
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
    import java.util.Date;
    
    import com.test084_087_util.GameUtil;
    import com.test084_087_util.MyFrame;
    
    public class PlaneGameFrame extends MyFrame {
        Image bg = GameUtil.getImage("images/bg.jpg");
        Plane p = new Plane("images/plane.png",50,50);
        ArrayList bulletList = new ArrayList();
        Explode bao;
        
        Date startTime;
        Date endTime;
        
        public void paint(Graphics g){
            g.drawImage(bg,0,0,null);
            p.draw(g);
            for(int i=0;i<bulletList.size();i++){
                Bullet b = (Bullet)bulletList.get(i);
                b.draw(g);
                
                //检测跟飞机的碰撞
                boolean peng = b.getRect().intersects(p.getRect());
                if(peng){
                    p.setLive(false);
                    if(bao==null){
                        endTime = new Date();
                        bao = new Explode(p.x,p.y);
                    }
                    bao.draw(g);
                    break;//有一个子弹碰到即爆炸,所以用break
                }
            }
            
            if(!p.isLive()){
                int period =(int)( (endTime.getTime()-startTime.getTime())/1000);
                PrintInfo(g,"时间:" + period + "秒",20,120,260,Color.white);
                
                switch(period/10){
                case 0:
                    PrintInfo(g,"菜鸟",50,100,200,Color.WHITE);
                    break;
                case 2:
                    PrintInfo(g,"小鸟",50,100,200,Color.WHITE);
                    break;
                case 4:
                    PrintInfo(g,"大鸟",50,100,200,Color.WHITE);
                    break;
                }
            }
            
            PrintInfo(g,"分数:100",10,50,50,Color.YELLOW);
        }
        
        public void PrintInfo(Graphics g,String str,int size,int x,int y,Color color){
            Color c = g.getColor();
            g.setColor(color);
            Font f = new Font("宋体",Font.BOLD,size);
            g.setFont(f);
            g.drawString(str, x, y);
            g.setColor(c);
        }
        
        public static void main(String[] args){
            new PlaneGameFrame().launchFrame();
        }
        
        public void launchFrame(){
            super.launchFrame();
            addKeyListener(new KeyMonitor());
            
            //生成一堆子弹
            for(int i=0;i<50;i++){
                Bullet b = new Bullet(); 
                bulletList.add(b);
            }
            startTime = new Date();
            
        }
        
        //定义为内部类,方便的使用外部类的普通属性
        class KeyMonitor extends KeyAdapter{//这个类需要在launchFrame里注册一下才能用
            public void keyPressed(KeyEvent e){
                System.out.println("按下:"+e.getKeyCode());
                p.addDirection(e);    
            }
            
            public void keyReleased(KeyEvent e){
                System.out.println("释放:"+e.getKeyCode());
                p.minusDirection(e);
            }
    
        }
    }
    
    package com.test088_097;
    
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    
    import com.test084_087_util.GameUtil;
    
    public class Plane extends GameObject {
        
        private boolean left,up,right,down;
    
        private boolean live = true;
        
        public void draw(Graphics g){
            if(live){
                g.drawImage(img,(int)x,(int)y,null);
                move();
            }    
        }
    
        public Plane(String imgpath, double x, double y) {
            this.img = GameUtil.getImage(imgpath);
            this.width = img.getWidth(null);
            this.height = img.getHeight(null);
            this.x = x;
            this.y = y;
        } 
        
        public void move(){
            if(left){
                x -= speed;
            }
            if(right){
                x += speed;
            }
            if(up){
                y -= speed;
            }
            if(down){
                y += speed;
            }
            
        }
        
        public void addDirection(KeyEvent e){
            switch(e.getKeyCode()){
            case KeyEvent.VK_LEFT:
                left = true;
                break;
            case KeyEvent.VK_UP:
                up = true;
                break;
            case KeyEvent.VK_RIGHT:
                right = true;
                break;
            case KeyEvent.VK_DOWN:
                down = true;
                break;
            default:
                break;
            }
            move();
            
            
        }
        
        
            public void minusDirection(KeyEvent e){
            switch(e.getKeyCode()){
            case KeyEvent.VK_LEFT:
                left = false;
                break;
            case KeyEvent.VK_UP:
                up = false;
                break;
            case KeyEvent.VK_RIGHT:
                right = false;
                break;
            case KeyEvent.VK_DOWN:
                down = false;
                break;
            default:
                break;
        }
            move();
        
        }
            
        public boolean isLive(){
            return live;
        }
    
        public void setLive(boolean live){
            this.live = live;
        }
        
    }
    
    package com.test088_097;
    
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Rectangle;
    
    import com.test084_087_util.Constant;
    
    public class Bullet extends GameObject {
        
        double degree;   
        
        
        public Bullet(){
            degree = Math.random()*Math.PI*2;
            x = Constant.GAME_WIDTH/2;
            y = Constant.GAME_HEIGHT/2;
            width = 10;
            height = 10;
        }
        
        
        public void draw(Graphics g){
            Color c = g.getColor();
            g.setColor(Color.yellow);
            g.fillOval((int)x,(int)y,width,height);
            
            x += speed*Math.cos(degree);
            y += speed*Math.sin(degree);
            
            if(y>Constant.GAME_HEIGHT-width||y<30){
                degree = -degree;
            }
            if(x<0||x>Constant.GAME_WIDTH-width){
                degree = Math.PI-degree;
            }
            
            g.setColor(c);
            
        }
        
        public Rectangle getRect(){ //获得子弹所在矩形
            return new Rectangle((int)x,(int)y,width,height);
        }
    }
    
    package com.test088_097;
    
    import java.awt.Image;
    import java.awt.Rectangle;
    
    public class GameObject {
        Image img; 
        double x,y;
        int speed=3;
        int width,height;
        
        public Rectangle getRect(){ //获得飞机所在矩形
            return new Rectangle((int)x,(int)y,width,height);
        }
        
        public GameObject(){}
        
        public GameObject(Image img,double x,double y,int speed,int width,int height){
            this.img = img;
            this.x = x;
            this.y = y;
            this.speed = speed;
            this.width = width;
            this.height = height;
        }
        
    }
    
    package com.test088_097;
    
    import java.awt.Graphics;
    import java.awt.Image;
    
    import com.test084_087_util.GameUtil;
    
    public class Explode {
        double x,y;
        static Image[] imgs = new Image[16];
        static{ //静态变量的初始化
            for(int i=0;i<16;i++){
                imgs[i] = GameUtil.getImage("images/explode/e"+(i+1)+".gif");
                imgs[i].getWidth(null);//应对懒加载的办法
            }
        }
        
        int count;
        
        public void draw(Graphics g){
            if(count<=15){
                g.drawImage(imgs[count], (int)x, (int)y, null);
                count++;
            }
            
        }
        
        public Explode(double x,double y){
            this.x = x;
            this.y = y;
        }
        
    }
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值