• 大小: 91KB
    文件类型: .zip
    金币: 2
    下载: 0 次
    发布日期: 2024-01-20
  • 语言: Java
  • 标签: Java  小游戏  

资源简介

这是优化更新后的代码,之前的代码链接是https://download.csdn.net/download/zwyjg/9826678

资源截图

代码片段和文件信息

package com.hufan;

import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.Timer;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;

public class Hufan extends Jframe {
HuPanel hp;

public static void main(String[] args) {
new Hufan();
}
public Hufan() {
hp = new HuPanel();
this.addMouseMotionListener(hp);
this.add(hp);
this.settitle(“飞机大战“);
this.setSize(400 600);
this.setLocation(500 100);
this.setDefaultCloseOperation(Jframe.EXIT_ON_CLOSE);
this.setVisible(true);
}
}

class HuPanel extends JPanel implements Runnable MouseMotionListener {
Player player;// 玩家
static CopyOnWriteArrayList enemys;// 敌人飞机集合
static CopyOnWriteArrayList bullets;// 子弹集合
static CopyOnWriteArrayList bombs;// 炸弹集合
BufferedImage bground logo shoot bullet small;
BufferedImage smallBomb1 smallBomb2 smallBomb3;// 炸弹图片

public HuPanel() {
player = new Player();
enemys = new CopyOnWriteArrayList();
bullets = new CopyOnWriteArrayList();
bombs = new CopyOnWriteArrayList();
new Enemy();// 启动敌人飞机
try {
bground = ImageIO.read(new File(“images/shoot_background.png“));
logo = ImageIO.read(new File(“images/logo.png“));
bullet = ImageIO.read(new File(“images/bullet.png“));
small = ImageIO.read(new File(“images/enemy1.png“));
smallBomb1 = ImageIO.read(new File(“images/enemy1_down1.png“));
smallBomb2 = ImageIO.read(new File(“images/enemy1_down2.png“));
smallBomb3 = ImageIO.read(new File(“images/enemy1_down3.png“));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new Thread(this).start();
}

public void paint(Graphics g) {
super.paint(g);
// 画背景和玩家
g.drawImage(bground 0 -75 this);
g.drawImage(logo player.x player.y 60 70 this);
// 画子弹
for (int i = 0; i < bullets.size(); i++) {
Bullet b = bullets.get(i);
g.drawImage(bullet b.x b.y this);
}
// 画敌人飞机
for (int i = 0; i < enemys.size(); i++) {
Enemy e = enemys.get(i);
g.drawImage(small e.x e.y this);
}
// 画炸弹
for (int i = 0; i < bombs.size(); i++) {
Bomb b = bombs.get(i);
if (b.life > 12) {
g.drawImage(smallBomb1 b.x b.y this);
} else if (b.life > 5) {
g.drawImage(smallBomb2 b.x b.y this);
} else {
g.drawImage(smallBomb3 b.x b.y this);
}
// 让b的生命值减小
b.lifeDown();
// 如果炸弹生命值为0就把该炸弹重bombs向量去掉
if (b.life == 0) {
bombs.remove(b);
}
}
}

public void run() {
while (true) {
try {
Thread.sleep(10);
this.repaint();// 刷新
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void mouseDragged(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {
// 玩家的坐标跟随鼠标的坐标
playe

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-09-27 02:34  feiji\
     目录           0  2018-09-27 02:34  feiji\bin\
     目录           0  2018-09-27 02:34  feiji\bin\com\
     目录           0  2018-09-27 02:38  feiji\bin\com\hufan\
     文件        1243  2018-09-27 02:53  feiji\bin\com\hufan\Enemy$EnemyTask.class
     文件         863  2018-09-27 02:53  feiji\bin\com\hufan\Bomb.class
     文件        3528  2018-09-27 02:53  feiji\bin\com\hufan\HuPanel.class
     文件         894  2018-09-27 02:53  feiji\bin\com\hufan\Hufan.class
     文件        1145  2018-09-27 02:53  feiji\bin\com\hufan\Player$PlayerTask.class
     文件        1560  2018-09-27 02:53  feiji\bin\com\hufan\Bullet.class
     文件        1572  2018-09-27 02:53  feiji\bin\com\hufan\Enemy.class
     文件         650  2018-09-27 02:53  feiji\bin\com\hufan\Player.class
     文件         465  2018-09-27 02:53  feiji\bin\com\hufan\Plane.class
     目录           0  2018-09-27 02:34  feiji\images\
     文件        3004  2017-04-26 17:42  feiji\images\bullet.png
     文件       38123  2017-04-26 17:42  feiji\images\shoot_background.png
     文件        5797  2017-04-26 17:48  feiji\images\enemy1_down1.png
     文件        7361  2017-04-26 17:48  feiji\images\enemy1_down3.png
     文件        6181  2017-04-26 17:48  feiji\images\enemy1_down2.png
     文件       15017  2017-04-26 17:42  feiji\images\logo.png
     文件        5499  2017-04-26 17:42  feiji\images\enemy1.png
     文件         301  2017-04-26 17:33  feiji\.classpath
     目录           0  2018-09-27 02:34  feiji\.settings\
     文件         598  2017-04-26 17:33  feiji\.settings\org.eclipse.jdt.core.prefs
     文件         381  2017-04-26 17:33  feiji\.project
     目录           0  2018-09-27 02:34  feiji\src\
     目录           0  2018-09-27 02:34  feiji\src\com\
     目录           0  2018-09-27 02:34  feiji\src\com\hufan\
     文件        6729  2018-09-27 02:53  feiji\src\com\hufan\Hufan.java

评论

共有 条评论