iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java实现简易飞机大战
  • 638
分享到

java实现简易飞机大战

2024-04-02 19:04:59 638人浏览 薄情痞子

Python 官方文档:入门教程 => 点击学习

摘要

目录整体思路代码实现英雄战机类敌机类子弹类图片工具类游戏窗体类启动游戏类运行效果图本文实例为大家分享了java实现简易飞机大战的具体代码,供大家参考,具体内容如下 整体思路 1.创建

本文实例为大家分享了java实现简易飞机大战的具体代码,供大家参考,具体内容如下

整体思路

1.创建游戏窗体,添加面板JPanel,重写JPanel中的paint方法,遍历所有飞机和子弹绘制,用定时器进行重绘,实现动画效果
2.添加敌机和发射子弹用的是多线程
3.碰撞检测采用的是矩形类Rectangle中的intersects方法

代码实现

用手机查看代码好像只显示62行

英雄战机类

package com.cml.model;

import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

import com.cml.util.ImageUtil;

public class Hero {
    private int x, y;// 坐标
    private int width, height;    //宽高
    private Image heroImage; // 图片
    private boolean isAlive = true;
    private ArrayList<Bullet> bullets = new ArrayList<>();
    public Hero() {
        this.x = 180;
        this.y = 600;
        this.heroImage = ImageUtil.hero;
        width = heroImage.getWidth(null);
        height = heroImage.getHeight(null);
        initBullets();
    }
    private void initBullets() {
        //用线程发射子弹
        new Thread() {
            @Override
            public void run() {
                while (isAlive) {
                    bullets.add(new Bullet(Hero.this));
                    try {
                        sleep(200);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }.start();
    }

    public Hero(int x, int y, Image heroImage) {
        super();
        this.x = x;
        this.y = y;
        this.heroImage = heroImage;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public Image getHeroImage() {
        return heroImage;
    }

    public void setHeroImage(Image heroImage) {
        this.heroImage = heroImage;
    }

    //绘制英雄战机
    public void paint(Graphics g) {
        if (!isAlive) {
            heroImage = ImageUtil.hero_destory;
        }
        g.drawImage(heroImage, x, y, null);
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            if (bullet.getY() < 0) {
                bullets.remove(bullet);
            }
            bullet.paint(g);
        }
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    //鼠标拖拽移动
    public void mouseDragged(MouseEvent e) {
        if (isAlive) {
            int x0 = e.getX();
            int y0 = e.getY();
            if (isInScopePanel(x0, y0)) {
                if (isInScopeImageWidth(x0) && isInScopeImageheigth(y0)) {
                    this.x = x0 - width / 2;
                    this.y = y0 - height / 2;
                }
            } else {
                if (isInScopeImageWidth(x0)) {
                    this.x = x0 - width / 2;
                }
                if (isInScopeImageheigth(y0)) {
                    this.y = y0 - height / 2;
                }
            }

        }
        
    }

    private boolean isInScopePanel(int x0, int y0) {
        if (x0 > 10 && x0 < 460 && y0 > 140 && y0 < 730) {
            return true;
        }
        return false;
    }

    private boolean isInScopeImageheigth(int y0) {
        if (y0 >= y && y0 <= y + height) {
            if (y0 > 140 && y0 < 730) {
                return true;
            }
        }
        return false;
    }

    private boolean isInScopeImageWidth(int x0) {
        if (x0 >= x && x0<= x + width) {
            if (x0 > 10 && x0 < 460) {
                return true;
            }
        }
        return false;
    }
    public ArrayList<Bullet> getBullets() {
        return bullets;
    }
    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }
    public boolean isAlive() {
        return isAlive;
    }
}

敌机类

package com.cml.model;

import java.awt.Graphics;
import java.awt.Image;
import java.util.Random;

import com.cml.util.ImageUtil;

public class Enemy {
    private Random random = new Random();

    private int x, y;// 坐标
    private int width, height; // 宽高
    private boolean isAlive = true;
    private static final int SPEED = 4;
    private Image enemyImage; // 图片

    public Enemy() {
        RandomEnemyXY();
        enemyImage = ImageUtil.enemy;
        width = enemyImage.getWidth(null);
        height = enemyImage.getHeight(null);
    }

    private void RandomEnemyXY() {
        x = random.nextInt(430);
        y = 0;
    }

    public void paint(Graphics g) {
        if (!isAlive) {
            enemyImage = ImageUtil.bomb;
        }
        g.drawImage(enemyImage, x, y, null);
        move();
    }

    public boolean isAlive() {
        return isAlive;
    }

    public void setAlive(boolean isAlive) {
        this.isAlive = isAlive;
    }

    private void move() {
        if (isAlive) {
            y += SPEED;
        }
        
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

子弹类

package com.cml.model;

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

import com.cml.util.ImageUtil;

public class Bullet {
    private int x, y;// 坐标
    private int width, height; // 宽高
    private static final int SPEED = 10; // 速度
    private Image bulletImage; // 图片

    public Bullet(Hero hero) {
        bulletImage = ImageUtil.bullet;
        width = bulletImage.getWidth(null);
        height = bulletImage.getHeight(null);
        this.x = hero.getX() + hero.getWidth() / 2 - width / 2;
        this.y = hero.getY();
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public void paint(Graphics g) {
        g.drawImage(bulletImage, x, y, null);
        move();
    }

    private void move() {
        y -= SPEED;
    }
}

图片工具类

package com.cml.util;

import java.awt.image.BufferedImage;
import java.io.IOException;

import javax.imageio.ImageIO;

public class ImageUtil {
    public static BufferedImage hero;
    public static BufferedImage enemy;
    public static BufferedImage bullet;
    public static BufferedImage bg;
    public static BufferedImage bomb;
    public static BufferedImage hero_destory;
    public static BufferedImage login;
    
    static {
        try {
            hero = ImageIO.read(ImageUtil.class.getResource("/img/hero.png"));
            enemy = ImageIO.read(ImageUtil.class.getResource("/img/enemy.png"));
            bullet = ImageIO.read(ImageUtil.class.getResource("/img/bullet.png"));
            bg = ImageIO.read(ImageUtil.class.getResource("/img/bg.png"));
            bomb = ImageIO.read(ImageUtil.class.getResource("/img/bomb.png"));
            hero_destory = ImageIO.read(ImageUtil.class.getResource("/img/hero_destory.png"));
//            login = ImageIO.read(new File("img/login.png"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

游戏窗体类

package com.cml.frame;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.cml.model.Bullet;
import com.cml.model.Enemy;
import com.cml.model.Hero;
import com.cml.util.ImageUtil;
import com.sun.java.swing.plaf.windows.resources.windows;

public class GameFrame extends JFrame {

    private JPanel gamePanel;

    private Hero hero;
    private ArrayList<Enemy> enemies = new ArrayList<Enemy>();
    private ArrayList<Bullet> hero_bullet;
    private Timer timer;

    public GameFrame() {
        // 初始化游戏窗体
        initGameFrame();
        // 初始化英雄战机
        initHero();
        // 初始化游戏面板
        initGamePanel();
        // 初始化定时器
        initTimer();
        // 初始化敌军战机
        initEnemies();
    }

    private void initEnemies() {
        new Thread() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                    enemies.add(new Enemy());
                }

            }
        }.start();

    }

    private void initTimer() {
        timer = new Timer();
        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                gamePanel.repaint();
            }
        };
        timer.scheduleAtFixedRate(task, 0, 20);
    }

    private void initHero() {
        hero = new Hero();
        hero_bullet = hero.getBullets();
    }

    private void initGameFrame() {
        setTitle("打飞机");
        setSize(480, 800);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
    }

    private void initGamePanel() {
        gamePanel = new JPanel() {
            private int score = 0;
            
            public boolean isHit(Enemy enemy, Bullet bullet) {
                Rectangle r1 = new Rectangle(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
                Rectangle r2 = new Rectangle(bullet.getX(), bullet.getY(), bullet.getWidth(), bullet.getHeight());
                return r1.intersects(r2);
            }
            
            
            public boolean isHit(Enemy enemy, Hero hero) {
                Rectangle r1 = new Rectangle(enemy.getX(), enemy.getY(), enemy.getWidth(), enemy.getHeight());
                Rectangle r2 = new Rectangle(hero.getX() + hero.getWidth() / 3, hero.getY() + hero.getHeight() / 3,
                        hero.getWidth() / 3, hero.getHeight() / 3);
                return r1.intersects(r2);
            }

            @Override
            public void paint(Graphics g) {
                super.paint(g);
                g.drawImage(ImageUtil.bg, 0, 0, 480, 800, null);
                for (int i = 0; i < enemies.size(); i++) {
                    Enemy enemy = enemies.get(i);
                    for (int j = 0; j < hero_bullet.size(); j++) {
                        Bullet bullet = hero_bullet.get(j);
                        if (isHit(enemy, bullet) && enemy.isAlive()) {
                            enemy.setAlive(false);
                            hero_bullet.remove(bullet);
                            new Thread() {
                                public void run() {
                                    score += 10;
                                    try {
                                        sleep(200);
                                    } catch (InterruptedException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                    enemies.remove(enemy);
                                };
                            }.start();
                            break;
                        }
                    }
                    if (isHit(enemy, hero)) {
                        timer.cancel();
                        hero.setAlive(false);
                        enemy.setAlive(false);
                        JOptionPane.showMessageDialog(this, "游戏结束,您的得分是:" + score);
                        System.exit(0);
                    }
                    if (enemy.getY() > 800) {
                        enemies.remove(enemy);
                    }
                    enemy.paint(g);
                }
                if (hero != null) {
                    hero.paint(g);
                }
                g.setFont(new Font("宋体", Font.BOLD, 24));
                g.drawString("得分:" + score, 350, 30);

            }
        };
        add(gamePanel);
        // 设置拖拽监听事件
        gamePanel.addMouseMotionListener(new MouseAdapter() {

            @Override
            public void mouseDragged(MouseEvent e) {
                if (hero != null) {
                    hero.mouseDragged(e);
                }
            }
        });
    }

}

启动游戏类

package com.cml.main;

import com.cml.frame.GameFrame;

public class Start {

    public static void main(String[] args) {
        
        new GameFrame().setVisible(true);
    }
}

运行效果图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: java实现简易飞机大战

本文链接: https://www.lsjlt.com/news/148233.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • java实现简易飞机大战
    目录整体思路代码实现英雄战机类敌机类子弹类图片工具类游戏窗体类启动游戏类运行效果图本文实例为大家分享了java实现简易飞机大战的具体代码,供大家参考,具体内容如下 整体思路 1.创建...
    99+
    2024-04-02
  • JavaScript实现简易飞机大战
    本文实例为大家分享了JavaScript实现简易飞机大战的具体代码,供大家参考,具体内容如下 话不多说,直接上代码 <!DOCTYPE html> <html la...
    99+
    2024-04-02
  • Pygame库200行代码实现简易飞机大战
    目录安装使用库pygame简介pgame中主要模块介绍pygame的安装验证安装程序原理程序升级设置飞机大战的精灵类和常量背景类和敌机类主机类和子弹类主游戏类事件监听方法碰撞检测精灵...
    99+
    2024-04-02
  • python实现简单的飞机大战
    本文实例为大家分享了python实现简单的飞机大战的具体代码,供大家参考,具体内容如下 制作初衷 这几天闲来没事干,就想起来好长时间没做过游戏了,于是就想做一个游戏练练手,为了起到一...
    99+
    2024-04-02
  • Java实现简单的飞机大战游戏(控制主飞机篇)
    本文实例为大家分享了Java实现简单的飞机大战游戏,控制主飞机的具体代码,供大家参考,具体内容如下 接着上一篇:Java实现简单的飞机大战游戏(敌机下落篇),首先我们需要明白,在控制...
    99+
    2024-04-02
  • Java开发实现飞机大战
    目录一、飞机大战1 封装所有飞行物公共属性和功能的父类2 封装英雄机属性和功能类3 封装敌机属性和功能的类4 封装大飞机属性和功能的类5 子弹类6 飞机大战射击的主方法二、测试结果本...
    99+
    2024-04-02
  • java实现飞机大战游戏
    java实现飞机大战,供大家参考,具体内容如下 用Java写个飞机大战游戏练习一下,实现可播放游戏背景音乐和游戏的基本功能 设计 1、准备好相应的图片和背景音乐(.wav文件); 2...
    99+
    2024-04-02
  • 【JAVA】飞机大战
    代码和图片放在这个地址了: https://gitee.com/r77683962/fighting/tree/master 最新的代码运行,可以有两架飞机,分别通过WASD(方向),F(发子弹);上...
    99+
    2023-09-29
    java python 前端
  • javascript实现简单飞机大战小游戏
    本文实例为大家分享了javascript实现飞机大战小游戏的具体代码,供大家参考,具体内容如下 效果图 html文件 <!DOCTYPE html> <html ...
    99+
    2024-04-02
  • java实现飞机大战小游戏
    本文实例为大家分享了java实现飞机大战游戏的具体代码,供大家参考,具体内容如下 MyPanel类 package  P; import java.awt.Font; import...
    99+
    2024-04-02
  • 怎么用Java实现飞机大战
    这篇文章将为大家详细讲解有关怎么用Java实现飞机大战,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。前言《飞机大战》是一款融合了街机、竞技等多种元素的经典射击手游。华丽精致的游戏画面,超炫带感的技能特效,...
    99+
    2023-06-29
  • python实现简单飞机大战小游戏
    为了熟悉Python基础语法,学习了一个经典的案例:飞机大战,最后实现效果如下: 实现步骤: ①下载64位对应python版本的pygame:pygame-1.9.6-cp38-c...
    99+
    2024-04-02
  • Java实现简单的飞机大战游戏(敌机下落篇)
    本文实例为大家分享了Java实现简单飞机大战游戏,敌机下落的具体代码,供大家参考,具体内容如下 在实现这个游戏之前,我们首先需要知道项目可能要用到哪些知识点: 重绘,线程,双缓冲,数...
    99+
    2024-04-02
  • python实现简单的飞机大战游戏
    本文实例为大家分享了python实现飞机大战游戏的具体代码,供大家参考,具体内容如下 1、准备环境 下载python(这里建议不需要安装最新的,好像pygame还没有对3.8支持的包...
    99+
    2024-04-02
  • Pygame库200行代码实现简易飞机大战的示例分析
    这篇文章将为大家详细讲解有关Pygame库200行代码实现简易飞机大战的示例分析,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。写在开头,因为这个小游戏的实验主要是帮助我熟悉pygame库的使...
    99+
    2023-06-22
  • java实现飞机大战案例详解
    前言 飞机大战是一个非常经典的案例,因为它包含了多种新手需要掌握的概念,是一个非常契合面向对象思想的入门练习案例 程序分析: 在此游戏中共有六个对象: 小敌机Airplane,大敌机...
    99+
    2024-04-02
  • js+canvas实现飞机大战
    本文实例为大家分享了js canvas实现飞机大战的具体代码,供大家参考,具体内容如下 首先我们绘制一个canvas区域,确实其宽高为480px*852px;水平居中 <!DO...
    99+
    2024-04-02
  • PythonPygame实战之飞机大战的实现
    目录导语一、环境安装1)各种素材(图片、字体等)2)运行环境二、代码展示1)文章思路2)附代码讲解3)主程序三、效果展示总结导语 三月疫情原因,很多地方都封闭式管理了! 在回家无聊的...
    99+
    2024-04-02
  • Java实现飞机大战-II游戏详解
    目录前言主要设计功能截图代码实现启动类玩家飞机总结前言 《飞机大战-II》是一款融合了街机、竞技等多种元素的经典射击手游。华丽精致的游戏画面,超炫带感的技能特效,超火爆画面让你肾上腺...
    99+
    2024-04-02
  • java如何实现飞机大战小游戏
    本篇内容介绍了“java如何实现飞机大战小游戏”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!MyPanel类package &nb...
    99+
    2023-07-01
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作