84-98_实战项目详细说明

我在成都,你呢?
为了一个人,只身一人来到成都。30小时的站票,在火车上半夜被几次叫醒让我挪一下我的窝,因为到站的人该下车了;就这样一夜做了无数的噩梦!
今天去川大找同学一起自习,刚从川大回来,回来的路上给ta打电话,心想听到她的声音可以减少内心一丝丝的空虚,感觉ta不开心,我努力的逗ta,忘却了自己也不过是一个需要安慰的人;两个人,一阵安静过后,我决定挂电话了,就这样踉踉跄跄一个人骑车回到青旅,心中有太多的苦说不出,想起基友跟我说过,没有什么事是一瓶酒解决不了的,如果有,那就两瓶!
诗人、作家喝着酒能来灵感,写出好的作品;程序员难道就不可以吗?

下面是一个游戏,看了教程之后,自己改写了送给ta的,给他起个名字吧: 《程序员也懂得浪漫》

FrameUtil包

class Constant

package com.zhushen.FrameUtil;
/**
 * 项目的一些配置常量
 * @author zhushen
 *
 */
public class Constant {
    public static final int FRAME_X =100;//窗口初始位置的横坐标
    public static final int FRAME_Y =100;//窗口初始位置的纵坐标
    public static final int FRAME_WIDTH =800;//窗口的宽度
    public static final int FRAME_HEIGHT =600;//窗口的高度
    public static final int PLANE_SPEED =8;//飞机的速度
    public static final int BULLET_SPEED =3;//子弹的速度
    public static final int BULLET_COUNT =10;//子弹的个数
    public static final int FRAME_PADDING =30;//窗口上边框的高度
}

class GameFrame

package com.zhushen.FrameUtil;

import java.awt.Graphics;
import java.awt.Image;
/**
 * 游戏窗口类
 * @author zhushen
 *
 */
public class GameFrame extends MyFrame {
    private static final long serialVersionUID = 1L;
    Image img=GameUtil.getImage("images/mylove.png");
    /**
     * 在窗口里面画元素
     */
    private double x=250,y=250;
    private double degree=Math.PI/3;//[0,2PI]
    private double speed=10;
    public void paint(Graphics g){
        g.drawImage(img, (int)x, (int)y, null);
        if(speed>0){
            speed-=0.05;
        }else{
            speed=0;
        }
        x+=speed*Math.cos(degree);
        y+=speed*Math.sin(degree);
        if(y<50||y>Constant.FRAME_HEIGHT-30){
            degree=-degree;
        }

        if(x>Constant.FRAME_WIDTH-30||x<0){
            degree=Math.PI-degree;  
        }
    }
    public static void main(String[] args) {
        GameFrame gf=new GameFrame();
        gf.launchFrame();
    }
}

class GameUtil

 package com.zhushen.FrameUtil;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;

public class GameUtil {
    private GameUtil(){//工具类的一般构造方法私有 

    }
    /**
     * 根据path加载一张图片,返回Image
     * @param path
     * @return
     */
    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 MyFrame

package com.zhushen.FrameUtil;

import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
/**
 * 简单窗口类
 * @author zhushen
 *
 */
public class MyFrame extends Frame {
    private static final long serialVersionUID = 1L;
    /**
     * 加载窗口
     */
    public void launchFrame(){
        setSize(Constant.FRAME_WIDTH,Constant.FRAME_HEIGHT);
        setLocation(Constant.FRAME_X,Constant.FRAME_Y);
        setVisible(true);
        new PaintThread().start();//启动重画线程

        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e){
                System.exit(0);
            }
        }) ;    
    }
    /**
     * 利用双缓冲技术解决屏幕闪烁问题
     */
    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);
    }
    /**
     * 定义一个重画窗口的线程类,内部类
     * @author zhushen
     *
     */
    class PaintThread extends Thread{
        public void run(){
            while(true){
                repaint();
                try {
                    Thread.sleep(20);//1s画1000/40次
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

planeGame包

class Boom

package com.zhushen.planeGame;

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

import com.zhushen.FrameUtil.GameUtil;

/**
 * 爆炸类
 * @author zhushen
 *
 */
public class Boom {
    double x,y;
    int count;
    static Image[] imgs=new Image[10];
    static {
        for(int i=0;i<10;i++){
            imgs[i]=GameUtil.getImage("images/boom/"+i+".jpg");
            imgs[i].getWidth(null);
        }
    }

    public void draw(Graphics g){
        if(count<10){
            g.drawImage(imgs[count], (int)x, (int)y, null);
            count ++;   
        }   
    }

    public Boom(double x,double y) {
        this.x=x;
        this.y=y;
    }   

}

class Bullet

package com.zhushen.planeGame;

import java.awt.Graphics;
import com.zhushen.FrameUtil.Constant;

public class Bullet extends GameEntity {
    double degree;
    public Bullet(String imgpath){
        super(imgpath);
        degree=Math.random()*Math.PI*2;
        this.speed=Constant.BULLET_SPEED;
        this.x=(Constant.FRAME_WIDTH-this.width)/2;
        this.y=(Constant.FRAME_HEIGHT-this.height)/2;
    }

    public void draw(Graphics g){
        g.drawImage(img, (int)x, (int)y,null);
        x+=speed*Math.cos(degree);
        y+=speed*Math.sin(degree);

        if(y<Constant.FRAME_PADDING||y>Constant.FRAME_HEIGHT-this.height){
            degree=-degree;
        }
        if(x>Constant.FRAME_WIDTH-this.width||x<0){
            degree=Math.PI-degree;  
        }
    }
}

class GameEntity

package com.zhushen.planeGame;

import java.awt.Image;
import java.awt.Rectangle;

import com.zhushen.FrameUtil.GameUtil;

public class GameEntity {
    Image img;//游戏物体的图片
    double x,y;//游戏物体位置的坐标
    double speed;//游戏物体的速度
    int width,height;//宽度和高度
    /**
     * 获得游戏物体所在矩形
     * @return
     */
    public Rectangle getRect(){
        return new Rectangle((int)x,(int)y,width,height);
    }

    public GameEntity(Image img, double x, double y, double speed, int width, int height) {
        super();
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = speed;
        this.width = width;
        this.height = height;
    }

    public GameEntity(String imgpath){
        this.img=GameUtil.getImage(imgpath);
        this.width=img.getWidth(null);
        this.height=img.getHeight(null);
    } 
    public GameEntity(){

    }
}

class Plane

package com.zhushen.planeGame;

import java.awt.Graphics;
import java.awt.event.KeyEvent;

import com.zhushen.FrameUtil.Constant;

public class Plane extends GameEntity {
    boolean left,up,right,down;
    boolean live=true;
    public boolean isLive() {
        return live;
    }
    public void setLive(boolean live) {
        this.live = live;
    }
    public void draw(Graphics g){//画飞机的方法
        if(live){
            g.drawImage(img, (int)x, (int)y,null);
            move();
        }

        if(x<0){
            x=0;
        }
        if(x>Constant.FRAME_WIDTH-this.width){
            x=Constant.FRAME_WIDTH-this.width;
        }
        if(y<Constant.FRAME_PADDING){
            y=Constant.FRAME_PADDING;
        }
        if(y>Constant.FRAME_HEIGHT-this.height){
            y=Constant.FRAME_HEIGHT-this.height;
        }

    }
    /**
     * 飞机的构造方法
     * @param imgpath
     * @param x
     * @param y
     */
    public Plane(String imgpath){
        super(imgpath);
        this.x=(Constant.FRAME_WIDTH-this.width)/2;
        this.y=(Constant.FRAME_HEIGHT-3*this.height);
        this.speed=Constant.PLANE_SPEED;
    } 


    /**
     * 飞机的无参构造方法
     */
    public Plane(){ 
    }

    //控制方向
    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;  
        }
    }
    //通过松开键盘按键减方向
    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;
        }
    }   
}

class PlaneGameFrame

package com.zhushen.planeGame;

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.zhushen.FrameUtil.Constant;
import com.zhushen.FrameUtil.GameUtil;
import com.zhushen.FrameUtil.MyFrame;

public class PlaneGameFrame extends MyFrame {
    Date startTime,endTime;
    private static final long serialVersionUID = 3468223702743833439L;
    Image bg=GameUtil.getImage("images/bg.jpg");
    Plane p=new Plane("images/myLove.png");
    Image first=GameUtil.getImage("images/boom/0.jpg");
    Image second=GameUtil.getImage("images/boom/1.jpg");
    Image third=GameUtil.getImage("images/boom/2.jpg");
    Image fourth=GameUtil.getImage("images/boom/3.jpg");
    Image fifth=GameUtil.getImage("images/boom/4.jpg");
    String str;
    ArrayList bulletList=new ArrayList();//泛型暂时不加
    public void paint(Graphics g){
        g.drawImage(bg,0,0,Constant.FRAME_WIDTH,Constant.FRAME_HEIGHT,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(str==null){
                    endTime=new Date(); 
                    str=new String("baozhao");
                }
                break;  
            }   
        }

        if(!p.isLive()){
            printInfo(g, Color.white, "我亲到你了!!", (int)p.x,(int)p.y, 15);
            double period=(double)(endTime.getTime()-startTime.getTime())/1000;
            //判断打印等级
            if(period<10){
                g.drawImage(first,(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2, null);
                printInfo(g, Color.white, "时间"+period+"秒", (Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+150, 20);
                printInfo(g, Color.white, "嘿嘿嘿!!!",(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+180, 20);
            }else if(period>=10 && period<20){
                g.drawImage(second,(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2, null);
                printInfo(g, Color.white, "时间"+period+"秒", (Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+150, 20);
                printInfo(g, Color.white, "么么哒!!!",(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+180, 20);
            }else if(period>=20 && period<30){
                g.drawImage(third,(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2, null);
                printInfo(g, Color.white, "时间"+period+"秒", (Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+150, 20);
                printInfo(g, Color.white, "爽翻天!!!",(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+180, 20);
           }else if(period>=30 && period<40){
                g.drawImage(fourth,(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2, null);
                printInfo(g, Color.white, "时间"+period+"秒", (Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+150, 20);
                printInfo(g, Color.white, "啪啪啪!!!",(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+180, 20);
           }else {
                g.drawImage(fifth,(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2, null);
                printInfo(g, Color.white, "时间"+period+"秒", (Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+150, 20);
                printInfo(g, Color.white, "玩通关了我的游戏你就是我的人了!哼",(Constant.FRAME_WIDTH-first.getWidth(null))/2, (Constant.FRAME_HEIGHT-first.getHeight(null))/2+180, 20);
           }
        }
    }
    //重写父类的方法
    public void launchFrame(){
        super.launchFrame();
        addKeyListener(new KeyMonitor());//增加键盘的监听
        //加载一堆子弹
        for(int i=0;i<Constant.BULLET_COUNT;i++){
            Bullet b= new Bullet("images/myself.png");
            bulletList.add(b);
        }
        startTime=new Date();
    }

    /**
     * 定义为内部类可以方便的使用外部类的普通属性
     */
    class KeyMonitor extends KeyAdapter{
        @Override
        public void keyPressed(KeyEvent e) {
            p.addDirection(e);  
        }
        @Override
        public void keyReleased(KeyEvent e) {
            p.minusDirection(e);    
        }   
    }
    /**
     * 在屏幕上打印提示信息
     * @param g
     * @param color   字体颜色
     * @param str     打印的目标字符串
     * @param x       横坐标
     * @param y       纵坐标
     * @param size    字体大小
     */
    public void printInfo(Graphics g,Color color,String str,int x,int y,int size){
        Color c=g.getColor();
        g.setColor(color);
        Font f=new Font("正楷",Font.CENTER_BASELINE,size);
        g.setFont(f);
        g.drawString(str,x,y);
        g.setColor(c);  
    }

    public static void main(String[] args) {
        PlaneGameFrame pgf=new PlaneGameFrame();
        pgf.launchFrame();
    }
}

附:

  • images下bg.jpg建议用黑色带花纹的背景
  • mylove.jpg是女生的头像,建议30*30像素
  • myself.jpg是男生的头像,建议10*10像素,裁为圆形

明天是来成都的第一次面试,希望我能好运!我知道上天一定会眷顾那些一直坚持努力的人!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值