java實現(xiàn)簡易飛機大戰(zhàn)
本文實例為大家分享了java實現(xiàn)簡易飛機大戰(zhàn)的具體代碼,供大家參考,具體內容如下
整體思路
1.創(chuàng)建游戲窗體,添加面板JPanel,重寫JPanel中的paint方法,遍歷所有飛機和子彈繪制,用定時器進行重繪,實現(xiàn)動畫效果
2.添加敵機和發(fā)射子彈用的是多線程
3.碰撞檢測采用的是矩形類Rectangle中的intersects方法
代碼實現(xiàn)
用手機查看代碼好像只顯示62行
英雄戰(zhàn)機類
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() {
?? ??? ?//用線程發(fā)射子彈
?? ??? ?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;
?? ?}
?? ?//繪制英雄戰(zhàn)機
?? ?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();
?? ??? ?// 初始化英雄戰(zhàn)機
?? ??? ?initHero();
?? ??? ?// 初始化游戲面板
?? ??? ?initGamePanel();
?? ??? ?// 初始化定時器
?? ??? ?initTimer();
?? ??? ?// 初始化敵軍戰(zhàn)機
?? ??? ?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;
?? ??? ??? ?/**
?? ??? ??? ? * 判斷敵機與子彈相撞
?? ??? ??? ? * @param enemy
?? ??? ??? ? * @param bullet
?? ??? ??? ? * @return
?? ??? ??? ? */
?? ??? ??? ?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);
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?/**
?? ??? ??? ? * 判斷英雄戰(zhàn)機與敵機相撞
?? ??? ??? ? * @param enemy
?? ??? ??? ? * @param hero
?? ??? ??? ? * @return
?? ??? ??? ? */
?? ??? ??? ?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);
?? ??? ?// 設置拖拽監(jiān)聽事件
?? ??? ?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);
?? ?}
}運行效果圖

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Java實現(xiàn)簡單的飛機大戰(zhàn)游戲(控制主飛機篇)
- Java實現(xiàn)簡單的飛機大戰(zhàn)游戲(敵機下落篇)
- Java開發(fā)實現(xiàn)飛機大戰(zhàn)
- 基于Java語言在窗體上實現(xiàn)飛機大戰(zhàn)小游戲的完整步驟
- Java實現(xiàn)游戲飛機大戰(zhàn)-III的示例代碼
- Java實現(xiàn)飛機大戰(zhàn)-II游戲詳解
- Java實現(xiàn)經典游戲飛機大戰(zhàn)-I的示例代碼
- 一天時間用Java寫了個飛機大戰(zhàn)游戲,朋友直呼高手
- java實戰(zhàn)之飛機大戰(zhàn)小游戲(源碼加注釋)
- java實現(xiàn)飛機大戰(zhàn)游戲
相關文章
Servlet+JavaBean+JSP打造Java Web注冊與登錄功能
比作MVC的話,控制器部分采用Servlet來實現(xiàn),模型部分采用JavaBean來實現(xiàn),而大部分的視圖采用Jsp頁面來實現(xiàn),接下來我們就來詳細看看如何用Servlet+JavaBean+JSP打造Java Web注冊與登錄功能2016-05-05
SpringCloud-Gateway轉發(fā)WebSocket失敗問題及解決
這篇文章主要介紹了SpringCloud-Gateway轉發(fā)WebSocket失敗問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
Spring Boot實戰(zhàn)之靜態(tài)資源處理
這篇文章主要介紹了Spring Boot實戰(zhàn)之靜態(tài)資源處理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01
springboot 多數(shù)據(jù)源的實現(xiàn)(最簡單的整合方式)
這篇文章主要介紹了springboot 多數(shù)據(jù)源的實現(xiàn)(最簡單的整合方式),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11
SpringBoot結合SpringSecurity實現(xiàn)圖形驗證碼功能
這篇文章主要介紹了SpringBoot + SpringSecurity 實現(xiàn)圖形驗證碼功能,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-05-05
springboot 在idea中實現(xiàn)熱部署的方法
這篇文章主要介紹了springboot 在idea中實現(xiàn)熱部署的方法,實現(xiàn)了熱部署,在每一次作了修改之后,都會自動的重啟,非常節(jié)約時間,感興趣的小伙伴們可以參考一下2018-10-10

