吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 8273|回复: 13
收起左侧

[Java 转载] 【分享】 chrome恐龙小游戏 java

  [复制链接]
leiyanfu 发表于 2019-5-3 14:24
本帖最后由 leiyanfu 于 2019-5-3 14:27 编辑

最近学习java也有一段时间了 前些天打开网站老是出出现小恐龙所以就萌生了想模仿着写一个 无奈一些东西我还没学到 比如java的窗口 键盘事件的监听啊 什么的
所以就去网上找了一下  哎 还真有 果断下载 idea打开阅读一帆 尝试着自己写一写还不错  我就把源码分享给大家   
1.jpg QQ截图20190503142359.jpg
[Java] 纯文本查看 复制代码
package tn;

import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

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

public class BackgroundImage {

    BufferedImage image;//主图片
    BufferedImage image1, image2,image_over,image_yun;//轮播图片
    Graphics2D g2;//美工
    int x1, x2;//两个图片的坐标
    int x_yun=800,y_yun=50;
    int x_over=240,y_over=50;
    static final int SPEED = 3;//速度

    public BackgroundImage() {

        try {
            image1 = ImageIO.read(new File("image/map.png"));
            image2 = ImageIO.read(new File("image/map1.png"));
           image_over = ImageIO.read(new File("image/game_over.png"));
           image_yun = ImageIO.read(new File("image/yun.png"));

        } catch (Exception e) {
            e.printStackTrace();
        }
        image = new BufferedImage(734, 286, BufferedImage.TYPE_INT_BGR);
        g2 = image.createGraphics();
        x1 = 0;
        x2 = 734;
        g2.drawImage(image1, x1, 0, null);
        g2.drawImage(image2, x2, 0, null);


    }

    //背景滚动
    public void roll() {
        x1 -= SPEED;
        x2 -= SPEED;

        if (x1 <= -734) {
            x1 = 734;
        }
        if (x2 <= -734) {
            x2 = 734;
        }
        g2.drawImage(image1, x1, 0, null);
        g2.drawImage(image2, x2, 0, null);
        x_yun-=1;
        if (x_yun<=0){
            g2.drawImage(image_yun, x_yun, y_yun, null);
            x_yun=750;
            if ((y_yun+=26)<80)
            y_yun+=26;
        }



    }


}


[Java] 纯文本查看 复制代码
package tn;

import sun.applet.Main;

import javax.swing.*;
import java.awt.*;

//线程类
public class FreshThread extends Thread {
    GamePanl p;

    public FreshThread(GamePanl p) {
        this.p = p;//给类成员属性赋值
    }

    @Override
    public void run() {

        while (!p.isFinish()) {
            p.repaint();//重新绘制图片
            try {
                Thread.sleep(p.FRESH);//休眠刷新时间
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        Container c=p.getParent();
        while(!(c instanceof  MainFrame)){
            c=c.getParent();
        }
        MainFrame f= (MainFrame) c;
        JOptionPane.showMessageDialog(f,"         G A M E  O V E R");

       f.restart();

    }

    }




[Java] 纯文本查看 复制代码
package tn;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 继承窗体面板的图片
 */
@SuppressWarnings("all")
public class GamePanl extends JPanel implements KeyListener {
    //主图片
    BufferedImage image;

    //绘图工具,美工
    Graphics2D g2;
    kLong klong;//恐龙
    boolean finish = false;//游戏结束

    static final int FRESH = 10;//刷新时间,毫秒

    BackgroundImage background;



    List<object> list = new ArrayList<object>();//障碍物集合

    int addObjectTimer = 0;
    object ob=new object();


    int score=0;//分数
    int addScoreTimer=0;

    //构造方法
    public GamePanl() {
        image = new BufferedImage(734, 286, BufferedImage.TYPE_INT_BGR);
        g2 = image.createGraphics();
        klong = new kLong();//实例化恐龙类
        background = new BackgroundImage();//实例化背景图片

        list.add(new object());


        FreshThread t = new FreshThread(this);//刷新线程
        t.start();

    }

    private void painImage() {//绘制图片
        klong.move();//让小恐龙移动
        background.roll();//调用滚动图片
        g2.drawImage(background.image, 0, 0, this);//绘制背景
        g2.drawImage(background.image_yun, background.x_yun,background.y_yun, this);//绘制白云

         g2.drawImage(klong.image, klong.x, klong.y, this);//绘制恐龙

        if (addObjectTimer >= 1400) {//计算分数时间判断
            list.add(new object());
            addObjectTimer = 0;
        }

        for (int i = 0; i < list.size(); i++) {
            object o = list.get(i);
            o.move();
            o.bridMove();

            g2.drawImage(o.image, o.x, o.y, this);//绘制障碍



            //判断障碍物是否和头、脚相撞
            if (o.bounds().intersects(klong.bounds1()) || o.bounds().intersects(klong.bounds2())){

                gameOver();//游戏结束

            }
        }
        //分数++
        if (addObjectTimer>=50){
            score+=1;
            addScoreTimer=0;
        }

        //分数显示代码---"%05d"---指的是分数以五位数显示
        g2.drawString(String.format("%05d",score),600,35 );
        g2.drawString("Hi",536,35 );
        g2.drawString(String.format("%05d",+MainFrame.topScore),550,35 );


        addObjectTimer += FRESH;
        addScoreTimer +=FRESH;
    }


    //游戏结束
    public void gameOver(){
        finish=true;
//        image=background.image_over;
        g2.drawImage(background.image_over,background.x_over,background.y_over,null);
        if (score>MainFrame.topScore){//判断最高分
            MainFrame.topScore=score;
        }


    }

    @Override
    public void paint(Graphics g) {
        painImage();
        g.drawImage(image, 0, 0, this);
    }


    public boolean isFinish() {
        return finish;
    }

        //键盘监听
    @Override
    public void keyTyped(KeyEvent e) {//按键类型

    }

    @Override
    public void keyPressed(KeyEvent e) {//按键按下
        int code = e.getKeyCode();//获取按下的编码
        if (code == KeyEvent.VK_SPACE) {
            klong.jump();
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {//按键抬起

    }
}


[Java] 纯文本查看 复制代码
package tn;

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

/**
 * 恐龙类
 */
public class kLong {
    public int x, y;//坐标
    BufferedImage image;//图片
    BufferedImage image1, image2, image3,image_over;
    int stepTime = 0;//计时器
    int fresh = GamePanl.FRESH;

    boolean jumpState = false;//跳跃的状态
    int jumpHeight = 100;//跳跃高度
    final int LOWEST_Y = 200;//最低坐标
    int jumpValue = 5;//跳跃的增变量

    public kLong() {

        try {
            image1 = ImageIO.read(new File("image/long1.png"));
            image2 = ImageIO.read(new File("image/long2.png"));
            image3 = ImageIO.read(new File("image/long3.png"));
            image_over = ImageIO.read(new File("image/over.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        x = 50;//起始坐标
        y = LOWEST_Y;//起始
    }

    public void move() {//移动
        step();//踏步
        if (jumpState) {
            if (y >= LOWEST_Y) {
                jumpValue = -4;
            }
            if (y <= LOWEST_Y - jumpHeight) {
                jumpValue = 4;
            }
            y += jumpValue;
            image = image3;
            if (y >= LOWEST_Y) {
                jumpState = false;
            }

        }
    }


    //踏步
    void step() {
        int tmp = stepTime / 100 % 2;
        if (tmp == 1) {
            image = image1;
        } else {
            image = image2;
        }

        stepTime += fresh;

    }


    public void jump() {//跳跃
        jumpState = true;
    }

    //接触死亡区域
    public Rectangle bounds1(){

        return new Rectangle(x+20,y,20,10);
    } //接触死亡区域
    public Rectangle bounds2(){
        return new Rectangle(x+5,y+35,20,10);
    }

}

[Java] 纯文本查看 复制代码
package tn;

import javax.swing.*;
import java.awt.*;

public class MainFrame extends JFrame {
    GamePanl p;

    public MainFrame() {
        //窗口关闭
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        p = new GamePanl();
        //获取窗体的主容器
        Container c = getContentPane();
        c.add(p);//把面板添加到主容器中

        addKeyListener(p);//添加键盘监听

    }

    public static void main(String[] args) {

        MainFrame frame = new MainFrame();
        //窗体大小
        frame.setBounds(340, 220, 734, 320);
        //窗体可见
        frame.setVisible(true);
        //屏幕居中
        frame.setLocationRelativeTo(null);
        frame.setTitle("跳一跳");


    }


    //复活,把死掉的东西删掉,重新来过
    public void restart(){
        Container c=getContentPane();
        //删除所有死的组件
        c.removeAll();;

        GamePanl gp=new GamePanl();
        c.add(gp);

        addKeyListener(gp);

        c.validate();//重新验证容器组件

    }


public static   int topScore=0;//最高分




}



[Java] 纯文本查看 复制代码
package tn;

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 {
    kLong klong=new kLong();
    Random r = new Random();
    int x, y;
    public boolean judge=false;
    BufferedImage image;
    BufferedImage cactus01, cactus02, cactus03,bird1,bird2;//创建仙人掌+鸟
    int speed = BackgroundImage.SPEED -1;
    int temp = r.nextInt(4) + 1;//1-2-3

    public object() {
        //读取图片
        try {
            cactus01 = ImageIO.read(new File("image/cactus01.png"));
            cactus02 = ImageIO.read(new File("image/cactus02.png"));
            cactus03 = ImageIO.read(new File("image/cactus03.png"));
              bird1= ImageIO.read(new File("image/bird1.png"));
              bird2= ImageIO.read(new File("image/bird2.png"));

        } catch (IOException e) {
            e.printStackTrace();
        }
        x = 800;
        y = 209;

        //随机输出障碍物
        switch (temp) {
            case 1:
                image = cactus01;
                break;
            case 2:
                image = cactus02;
                break;
            case 3:
                image = cactus03;
                y = 200;
                break;
                default:
                    judge=true;
        }


    }

    //鸟的运动
    void bridMove() {
        if (judge){
//            if (temp>2){
//                y-=20;
//            }
        int tmp = klong.stepTime / 100 % 2;
        if (tmp == 1) {
            image = bird1;
        } else {
            image = bird2;
        }

        klong.stepTime += klong.fresh;
      }
}

    //滚动
    public void move() {
        x -= speed;
        x -= speed;
    }


    //死亡区域
    public Rectangle bounds() {
        return new Rectangle(x,y+2,image.getWidth()-2,image.getHeight()-10);
    }


}



最后附上图片资源以及源码
跳一跳.rar (84.83 KB, 下载次数: 239)


免费评分

参与人数 6吾爱币 +8 热心值 +5 收起 理由
疑惑弟弟 + 1 谢谢@Thanks!
LuRenKu + 1 谢谢@Thanks!
agoodboy1dcc + 1 谢谢@Thanks!
苏紫方璇 + 5 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
沐雨红尘 + 1 + 1 加油,吾爱老坛友,给你个热心值。java继续写下去吧
独行风云 + 1 + 1 感谢发布原创作品,吾爱破解论坛因你更精彩!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

童子tz 发表于 2019-5-3 15:10
谢谢分享,学习了。
time2s 发表于 2019-5-3 16:35
 楼主| leiyanfu 发表于 2019-5-3 21:52
xiangnianguxian 发表于 2019-5-7 15:17
貌似有点意思。
布鲁斯特 发表于 2019-5-7 17:59
楼主,学什么方向的- -
这个gui界面我是直接放弃了
Light紫星 发表于 2019-5-25 08:54
感谢分享,学习了
七夜夜 发表于 2019-11-28 22:24
大佬,学习一下
Notoday 发表于 2019-11-30 15:50
感谢分享,看一看
英和 发表于 2019-12-27 03:09
谢谢楼主分享
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 03:26

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表