美文网首页
谷歌小恐龙游戏简化版java实现

谷歌小恐龙游戏简化版java实现

作者: Trade丿Life | 来源:发表于2020-01-15 09:43 被阅读0次

实现效果

dinasour.JPG

需要准备的图片

cactus01.png
cactus03.png
game_over.png
long1.png
long2.png
map.png
map1.png

滚动背景模块

 package wen.ming.game;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.awt.Image;
import javax.imageio.ImageIO;
/**
 * 滚动背景
 * 
 * @author mingrong
 *
 */
public class BackgroundImage extends GameObject {
    public int x1 = 0;
    public int x2 = 50;
    public int y2 = 25;
    public static final int SPEED = 4;// 滚动速度
    
    public BackgroundImage() {

    }
    /**
     * 滚动
     */
    public void roll() {
         x1 -= SPEED;// 第一幅图片左移
         if (x1 <= -50) {// 如果第一幅图片移出屏幕
             x1 =50;// 回到屏幕右侧
         }
}
}

常量设置模块

package wen.ming.game;

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

恐龙对象的创建

package wen.ming.game;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;

public class Dinasaur extends GameObject {
    boolean  left,up,right,down;
    boolean  live = true;  
    int stepTime = 0;//计时器
    static final int fresh = 10;
    private boolean jumpState = false;// 跳跃状态
    private final int JUMP_HIGHT = 100;// 跳起最大高度
    private final int LOWEST_Y = 220;// 落地最低坐标
    private int jumpValue = 0;// 跳跃的增变量
     
    Image   dinasaurImg  = GameUtil.getImage("images/long1.png");
    Image   dinasaurImg2  = GameUtil.getImage("images/long2.png");
    Image image2;
    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  Dinasaur(Image  img, double x, double y){
            this.img = img;
            this.x = x+3;
            this.y = y+3;
            this.speed = 3;
            this.width = img.getWidth(null) ;
            this.height = img.getHeight(null);
            
        }
        
         public void move() {
                step();// 不断踏步
                if (jumpState) {// 如果正在跳跃
                    if (y >= LOWEST_Y) {// 如果纵坐标大于等于最低点
                        jumpValue = -14;// 增变量为负值
                    }
                    if (y <= LOWEST_Y - JUMP_HIGHT) {// 如果跳过最高点
                        jumpValue = 14;// 增变量为正值
                    }
                    y += jumpValue;// 纵坐标发生变化
                    if (y >= LOWEST_Y) {// 如果再次落地
                        jumpState = false;// 停止跳跃
                    }
                }
            }
         void step() {
                int tmp = stepTime / 100 % 2;
                if (tmp == 1) {
                    image2 = dinasaurImg;
                } else {
                    image2 = dinasaurImg2;
                }

                stepTime += fresh;

            }
         public void jump() {
               
                jumpState = true;// 处于跳跃状态
            }

            public void left() {
                x -= 5;
            }
            public void right() {
                x += 5;
            }
            public void up() {
                y -= 5;
            }
            public void down() {
                y += 5;
            }

        //按下某个键,增加相应的方向
        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;
                }
            }
        
            public  Rectangle   getRect(){
//              return  new Rectangle((int)x+10,(int)y,20,10);
                return  new Rectangle((int)x, (int)y, width, height);
            }
}

游戏物体对象

package wen.ming.game;

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

/**
 * 游戏物体的父类
 * @author MingRong
 *
 */
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+20,(int)y,20,10);
    }
}

返回指定路径文件的图片对象

package wen.ming.game;
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;
    }
}

游戏主界面

package wen.ming.game;
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
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.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author MingRong
 *
 */
public class MyGameFrame  extends  Frame {
     private BufferedImage image;        
     private Graphics2D g2;
   
    Image   planeImg  = GameUtil.getImage("images/long1.png");
    Image   planeImg2  = GameUtil.getImage("images/long2.png");
    Image   bgImg1  = GameUtil.getImage("images/map.png");
    Image   bgImg2  = GameUtil.getImage("images/map1.png");
    Image   bg  = GameUtil.getImage("images/map1.png");
    Image   obstacleImg  = GameUtil.getImage("images/cactus03.png");
    Image   overImg  = GameUtil.getImage("images/game_over.png");
    private Dinasaur dinasaur = new Dinasaur(planeImg, 250, 225);
    private object obj = new object(obstacleImg,250,225);//
    private BackgroundImage background = new BackgroundImage();
    int stepTime = 0;   
    static final int fresh = 10;
    boolean live = true;
    int count = 0;
    @Override
    public void paint(Graphics g) { 
        if (live) {
            obj.randomplace();
            background.roll();
            dinasaur.move();
            Color   c =  g.getColor();
            g.drawImage(bgImg2, (int)background.x1, (int)background.y2, this);
            g.drawImage(obj.obstacle, obj.x, obj.y, this);
            g.drawImage(planeImg, 0, 0, null);
            g.drawImage(dinasaur.image2, (int)dinasaur.x, (int)dinasaur.y, this);
            boolean  peng = dinasaur.getRect().intersects(obj.getRect());
            if (peng) {
                count+=1;
                g.drawImage(overImg, (int)background.x1, (int)background.y2, this);
                System.out.print("ruin");
                if (count ==3) {
                    live = false;
                }
            }
            stepTime += fresh;
            g.setColor(c); 
        }
        }
    /**
     * 刷新贞
     * @author Mingrong
     *
     */
    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) {
//          dinasaur.addDirection(e);
              int code = e.getKeyCode();
               
                if (code == KeyEvent.VK_LEFT) {
                    dinasaur.left();
                }
                if (code == KeyEvent.VK_RIGHT) {
                    dinasaur.right();
                }
                if (code == KeyEvent.VK_UP) {
                    dinasaur.up();
                }
                if (code == KeyEvent.VK_DOWN) {
                    dinasaur.down();
                }
                if (code == KeyEvent.VK_SPACE) {
                    dinasaur.jump();
                }
        }
        
        @Override
        public void keyReleased(KeyEvent e) {

            dinasaur.minusDirection(e);
        }
    }
    
    public  void  launchFrame(){
        this.setTitle("game");
        this.setVisible(true);
        this.setSize(Constant.GAME_WIDTH    , Constant.GAME_HEIGHT);
        this.setLocation(500, 500);
        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        new PaintThread().start();  
        addKeyListener(new KeyMonitor());   
    }
    
    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);
    }
}

障碍物类

package wen.ming.game;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

public class object extends GameObject {
   
    Random r = new Random();
    public int x = 500;
    public int x2 = 50;
    public int y = 225;
    public boolean judge=false;
   
    public static final int SPEED = 11;// 滚动速度
    int temp = r.nextInt(4) + 1;//1-2-3
    Image obstacle;
    Image   obstacleImg  = GameUtil.getImage("images/cactus01.png");
    Image   obstacleImg2  = GameUtil.getImage("images/cactus03.png");
    int stepTime = 0;//计时器
    static final int fresh = 10;
    public  object(Image  img, double x, double y){
        this.img = img;
        this.x = (int)x;
        this.y = (int)y;
        this.speed = 3;
        this.width = img.getWidth(null) ;
        this.height = img.getHeight(null);
        
    }
    
    public object() {
        this.x =500;
        this.width = img.getWidth(null) ;
        this.height = img.getHeight(null);
    }
    public void randomplace() {
          int tmp = stepTime / 100 % 20;
            if (tmp == 1) {
                obstacle = obstacleImg;
            } else {
                obstacle = obstacleImg2;
            }

            stepTime += fresh;
          x -= SPEED;// 第一幅图片左移
          
          if (x <= -500) {// 如果第一幅图片移出屏幕
              x =500;// 回到屏幕右侧
          }
          
    }
    public void moving() {
          
             x -= SPEED;// 第一幅图片左移
           
             if (x <= -500) {// 如果第一幅图片移出屏幕
                 x =500;// 回到屏幕右侧
             }
    }
    public  Rectangle   getRect(){
//      return  new Rectangle((int)x+10,(int)y,20,10);
        return  new Rectangle((int)x, (int)y, width, height);
    }
 }

相关文章

网友评论

      本文标题:谷歌小恐龙游戏简化版java实现

      本文链接:https://www.haomeiwen.com/subject/rxazactx.html