Java小遊戲

這是一個飛機躲避子彈的小遊戲,其中有許多幹貨java

這是蒟蒻我第二次作,請各位大佬多多指教shell

目錄數組

1.遊戲主窗口的建立dom

2.圖形繪製_文本繪製_顏色改變_圖像對象的加載ide

3.線程內部類實現動畫工具

4.遊戲物體根類的實現動畫

5.面向對象思想重構飛機類設計this

6.鍵盤控制遊戲物體原理spa

7.面向對象重構飛機類的鍵盤控制代碼.net

8.炮彈類設計_任意角度飛行

9.容器或數組產生多發炮彈

10.雙緩衝解決閃爍問題_矩形檢測原理

11.炮彈和飛機的碰撞檢測_飛機死掉

12.爆炸類_圖片數組輪播處理

13.099_主窗口畫出爆炸類

14.100_飛機死亡和計時功能

 

//建立類


public class Constant {
    public static final int GAME_WIDTH = 500;
    public static final  int  GAME_HEIGHT = 500;
}

 

當飛機與子彈發生碰撞時的爆炸效果的實現

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

/*
 * 爆炸類
 */
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;
    }
}

 

遊戲物體

package cn.sxt.game;

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

/**
 * 遊戲物體的父類
 * @author VictorDW
 *
 */
public class GameObject {
      Image  img;
      double  x,y;
       int   speed;
      int  width, height;
    
    public  void  drawSelf(Graphics  g){
        g.drawImage(img, (int)x,(int) y, null);
    }

    public GameObject(Image img, double x, double y, int 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 GameObject(Image img, double x, double y) {
        super();
        this.img = img;
        this.x = x;
        this.y = y;
    }
    
    public GameObject() {
    }
    
    /**
     * 返回物體所在的矩形。便於後續的碰撞檢測
     * @return
     */
    public  Rectangle   getRect(){
        return  new Rectangle((int)x, (int)y, width, height);
    }
    
    
}

 

 

 
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() {
    } 
 
    
    /**
     * 返回指定路徑文件的圖片對象
     * @param path
     * @return
     */
    public static Image getImage(String path) {
        BufferedImage bi = null;
        try {
            URL u = GameUtil.class.getClassLoader().getResource(path);
            bi = ImageIO.read(u);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bi;
    }
}

 

飛機

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

public class Plane  extends GameObject {
    boolean  left,up,right,down;
    
    boolean  live = true;  
    
    public  void  drawSelf(Graphics  g){
        if(live){
                g.drawImage(img, (int)x,(int) y, null);
                
                if(left){
                    x -=speed;
                }
                if(right){
                    x += speed;
                }
                if(up){
                    y -=speed;    //y = y-speed;
                }
                if(down){
                    y += speed;
            }
        }else{
            
        }
        
        
        
    }
    
    public  Plane(Image  img, double x, double y){
        this.img = img;
        this.x = x;
        this.y = y;
        this.speed = 5;
        this.width = img.getWidth(null) ;
        this.height = img.getHeight(null);
        
    }
    
    //按下某個鍵,增長相應的方向
    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;
        }
    }
    
    //按下某個鍵,取消相應的方向
        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;
            }
        }
    
    
}

 

炮彈

import java.awt.Color;
import java.awt.Graphics;

/**
 * 炮彈類
 * 
 *
 */
public class Shell   extends  GameObject {
    
    double  degree;
    
    public  Shell(){
        x = 200;
        y = 200;
        width=10;
        height = 10;
        speed = 3;
        degree = Math.random()*Math.PI*2;
    }
    
    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(x<0||x>Constant.GAME_WIDTH-width){
            degree  = Math.PI - degree;
        }
        
        if(y<30||y>Constant.GAME_HEIGHT-height){
            degree  = - degree;
        }
        
        
        
        
        g.setColor(c);
    }
    
}

 

主類

import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Date;

/**
 * 飛機遊戲的主窗口
 * @author VictorDW
 *
 */
public class MyGameFrame  extends  Frame {
    
    Image   planeImg  = GameUtil.getImage("images/plane.png");
    Image   bg  = GameUtil.getImage("images/bg.jpg");
    
    Plane   plane = new Plane(planeImg,250,250);
    Shell[]   shells = new Shell[30];
    
    Explode   bao ;
    Date  startTime = new Date();
    Date  endTime;
    int period;   //遊戲持續的時間
    
    @Override
    public void paint(Graphics g) {        //自動被調用。  g至關於一隻畫筆
        Color   c =  g.getColor();
        g.drawImage(bg, 0, 0, null);
        
        plane.drawSelf(g);  //畫飛機
        
        //畫出全部的炮彈
        for(int i=0;i<shells.length;i++){
            shells[i].draw(g);
            
            //飛機和炮彈的碰撞檢測!!!
            boolean  peng = shells[i].getRect().intersects(plane.getRect());
            if(peng){
                plane.live = false;
                if(bao ==null){
                    bao  = new Explode(plane.x, plane.y);
                    
                    endTime = new Date();
                    period = (int)((endTime.getTime()-startTime.getTime())/1000);
                }
                bao.draw(g);
            }
            
            //計時功能,給出提示
            if(!plane.live){
                g.setColor(Color.red);
                Font   f  =  new Font("宋體", Font.BOLD, 50);
                g.setFont(f);
                g.drawString("時間:"+period+"秒", (int)plane.x, (int)plane.y);
            }
            
        }
        
        g.setColor(c);
    }
    
    
    //幫助咱們反覆的重畫窗口!
    class  PaintThread  extends  Thread  {
        @Override
        public void run() {
            while(true){
                repaint();        //重畫
                
                try {
                    Thread.sleep(40);       //1s=1000ms
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }        
            }
        }
        
    }
    
    //定義鍵盤監聽的內部類
    class   KeyMonitor extends  KeyAdapter  {

        @Override
        public void keyPressed(KeyEvent e) {
            plane.addDirection(e);
        }

        @Override
        public void keyReleased(KeyEvent e) {
            plane.minusDirection(e);
        }
        
        
    }
    
    
    /**
     * 初始化窗口
     */
    public  void  launchFrame(){
        this.setTitle("躲避byYD");
        this.setVisible(true);
        this.setSize(Constant.GAME_WIDTH    , Constant.GAME_HEIGHT);
        this.setLocation(300, 300);
        
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        
        new PaintThread().start();    //啓動重畫窗口的線程
        addKeyListener(new KeyMonitor());   //給窗口增長鍵盤的監聽
        
        
        //初始化50個炮彈
        for(int i=0;i<shells.length;i++){
            shells[i] = new Shell();
        }
        
    }
    
    public static void main(String[] args) {
        MyGameFrame  f = new MyGameFrame();
        f.launchFrame();
    }
    
    private Image offScreenImage = null;
     
    public void update(Graphics g) {
        if(offScreenImage == null)
            offScreenImage = this.createImage(Constant.GAME_WIDTH,Constant.GAME_HEIGHT);//這是遊戲窗口的寬度和高度
         
        Graphics gOff = offScreenImage.getGraphics();
        paint(gOff);
        g.drawImage(offScreenImage, 0, 0, null);
    }
    
}

 

如下是背景與模型,能夠本身修改與更換

背景圖

 

飛機模型

 

 

爆炸效果

相關文章
相關標籤/搜索