环境:jdk1.8 idea2017.4
效果图
类图
代码
BallJFrame .java
import javax.swing.*;
public class BallJFrame extends JFrame {
private BallJPanel jPanel;
public BallJFrame(){
jPanel=new BallJPanel(900,1080);
this.add(jPanel);
}
public void showJF(){
this.setSize(900,1080);
this.setTitle("雷电游戏plus");
this.setResizable(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jPanel.startRun();
this.setVisible(true);
}
public static void main(String[] args) {
BallJFrame jFrame=new BallJFrame();
jFrame.showJF();
}
}
BallJPanel .java
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.Random;
public class BallJPanel extends JPanel implements MouseMotionListener, MouseListener, Runnable {
private Ball airPlane;
private int sum = 0;
private int num = 0;
private int speed = 50;
private int nandu, nandu2;
private boolean isOver = false;
private boolean isFire = false;
private ArrayList<Ball> fireList = new ArrayList<Ball>();
private ArrayList<Ball> enmeyList = new ArrayList<Ball>();
private ArrayList<Ball> deathList = new ArrayList<Ball>();
private ArrayList<Ball> backList = new ArrayList<Ball>();
private ArrayList<Boss> boss = new ArrayList<Boss>();
private ArrayList<Ball> deathBoss = new ArrayList<Ball>();
private ArrayList<Ball> prop = new ArrayList<Ball>();
private ArrayList<Ball> boss_fire = new ArrayList<Ball>();
private ArrayList<Ball> boss_fire_bomb = new ArrayList<Ball>();
private Image img_back = new ImageIcon("img\\t\\bg.png").getImage();
private Image img_back2 = new ImageIcon("img\\t\\back.jpg").getImage();
private Image img_back3 = new ImageIcon("img\\t\\back.gif").getImage();
private Image img_hero = new ImageIcon("img\\t\\hero1.png").getImage();
private Image img_fire = new ImageIcon("img\\t\\fire.gif").getImage();
private Image img_enemy = new ImageIcon("img\\t\\enemy.png").getImage();
private Image img_bomb = new ImageIcon("img\\t\\bomb_0.gif").getImage();
private Image img_over = new ImageIcon("img\\t\\over.jpg").getImage();
private Image img_boss = new ImageIcon("img\\t\\e2.png").getImage();
private Image img_boss_bomb = new ImageIcon("img\\t\\bomb_1.gif").getImage();
private Image img_prop = new ImageIcon("img\\t\\gwls1.gif").getImage();
private Image img_boss_fire = new ImageIcon("img\\t\\el_bb_0.gif").getImage();
private Image img_boss_fire_bomb = new ImageIcon("img\\t\\bomb_3.gif").getImage();
BallJPanel() {
}
BallJPanel(int w, int h) {
backList.add(new Ball(0, 0, w, h, img_back));
backList.add(new Ball(0, 1080, w, h, img_back2));
backList.add(new Ball(0, 1080 * 2, w, h, img_back3));
airPlane = new Ball(w / 2, h - 100, 70, 70, img_hero);
this.addMouseMotionListener(this);
this.addMouseListener(this);
}
@Override
public void paint(Graphics g) {
super.paint(g);
for (int i = 0; i < backList.size(); i++) {
backList.get(i).drawImage(g);
}
airPlane.drawImage(g);
for (int i = 0; i < fireList.size(); i++) {
fireList.get(i).drawImage(g);
}
for (int i = 0; i < enmeyList.size(); i++) {
enmeyList.get(i).drawImage(g);
}
for (int i = 0; i < deathList.size(); i++) {
deathList.get(i).drawImage(g);
}
for (int i = 0; i < boss.size(); i++) {
boss.get(i).drawImage(g);
}
for (int i = 0; i < boss_fire.size(); i++) {
boss_fire.get(i).drawImage(g);
}
for (int i = 0; i < boss_fire_bomb.size(); i++) {
boss_fire_bomb.get(i).drawImage(g);
}
for (int i = 0; i < deathBoss.size(); i++) {
deathBoss.get(i).drawImage(g);
}
for (int i = 0; i < prop.size(); i++) {
prop.get(i).drawImage(g);
}
g.setColor(Color.red);
g.setFont(new Font("楷体", Font.BOLD, 18));
g.drawString("生命:" + String.valueOf(5 - num), 700, 30);
g.setColor(Color.blue);
g.drawString("积分:" + String.valueOf(sum), 700, 60);
g.setColor(Color.black);
g.drawString("难度:" + String.valueOf(10 - speed / 5), 700, 90);
if (isOver) {
g.drawImage(img_over, 200, 400, 400, 400, null);
g.setFont(new Font("楷体", Font.BOLD, 25));
g.setColor(Color.white);
g.fillRect(300, 830, 110, 30);
g.fillRect(450, 830, 70, 30);
g.setColor(Color.red);
g.drawString("Restart", 300, 850);
g.drawString("Exit", 450, 850);
}
}
@Override
public void mouseDragged(MouseEvent e) {
airPlane.setX(e.getX() - 35);
airPlane.setY(e.getY() - 35);
repaint();
}
@Override
public void run() {
int count = 0;
int Sw = 40;
int Offset = 2;
int shotSpeed = 2;
int generatShotSpeed = 20;
speed = 50;
nandu = 50;
nandu2 = 2;
Random rd = new Random();
while (!isOver) {
count++;
if (count == Integer.MAX_VALUE) {
count = 0;
}
if (isFire && count % generatShotSpeed == 0) {
Ball ball = new Ball(airPlane.getX() + 17, airPlane.getY() - 20, Sw, Sw, img_fire);
fireList.add(ball);
}
for (int i = 0; i < fireList.size(); i++) {
Ball fb = fireList.get(i);
fb.minusY(shotSpeed);
if (fb.getY() < -40) {
fireList.remove(i);
}
}
if (count % speed == 0) {
Ball el = new Ball(rd.nextInt(1000), 0, 40, 40, img_enemy);
enmeyList.add(el);
}
for (int i = 0; i < enmeyList.size(); i++) {
enmeyList.get(i).enemyPlaneMove();
if (enmeyList.get(i).getY() > 1300) {
enmeyList.remove(i);
}
}
if (count % 1000 == 0) {
int w = rd.nextInt(100) + 100;
Boss b = new Boss(new Random().nextInt(900), 0, w, w, w - 100, img_boss);
boss.add(b);
}
for (int i = 0; i < boss.size(); i++) {
if (boss.size() != 0 && count % 50 == 0) {
Ball ball = new Ball(boss.get(i).getX() + 70, boss.get(i).getY() + 50, 40, 40, img_boss_fire);
boss_fire.add(ball);
}
boss.get(i).bossMove();
if (boss.get(i).getY() > 1300) {
boss.get(i).setXY();
}
if (airPlane.isT(boss.get(i))) {
Boss B = boss.get(i);
B.setImg(img_bomb);
deathBoss.add(B);
boss.remove(i);
num += 2;
}
}
for (int i = 0; i < boss_fire.size(); i++) {
Ball fb = boss_fire.get(i);
fb.addY();
if (fb.getY() > 1080) {
boss_fire.remove(i);
}
}
for (int i = 0; i < boss_fire.size(); i++) {
for (int j = 0; j < fireList.size(); j++) {
if (boss_fire.get(i).isT(fireList.get(j))){
fireList.get(j).setImg(img_bomb);
deathList.add(fireList.get(j));
fireList.remove(j);
boss_fire.remove(i);
break;
}
}
}
for (int i = 0; i < boss_fire.size(); i++) {
if (boss_fire.get(i).isT(airPlane)) {
boss_fire.get(i).setImg(img_boss_fire_bomb);
boss_fire.get(i).setX(airPlane.getX()-20);
boss_fire.get(i).setY(airPlane.getY()-40);
boss_fire.get(i).setW(200);
boss_fire.get(i).setH(200);
boss_fire_bomb.add(boss_fire.get(i));
boss_fire.remove(i);
num++;
}
}
if (count % 2000 == 0) {
Ball b_prop = new Ball(new Random().nextInt(900), 0, 100, 100, img_prop);
prop.add(b_prop);
}
for (int i = 0; i < prop.size(); i++) {
prop.get(i).enemyPlaneMove();
if (airPlane.isT(prop.get(0))) {
Offset += 2;
shotSpeed += 4;
if (generatShotSpeed != 4) {
generatShotSpeed -= 4;
}
if (Sw < 100) {
Sw += 25;
}
prop.remove(0);
}
if (prop.size() != 0) {
if (prop.get(i).getY() > 1200) {
prop.remove(i);
}
}
}
for (int i = 0; i < fireList.size(); i++) {
for (int j = 0; j < enmeyList.size(); j++) {
if (fireList.get(i).isT(enmeyList.get(j))) {
Ball ball = enmeyList.get(j);
ball.setImg(img_bomb);
deathList.add(ball);
fireList.remove(i);
enmeyList.remove(j);
sum += 10;
if ((sum % nandu == 0) && (speed != 5)) {
speed -= 5;
nandu = nandu + 50 * nandu2;
nandu2 += 2;
}
break;
}
}
}
for (int i = 0; i < fireList.size(); i++) {
Ball bom = fireList.get(i);
for (int j = 0; j < boss.size(); j++) {
if (bom.isT(boss.get(j))) {
boss.get(j).minusLife(Offset);
bom.setImg(img_boss_bomb);
bom.setW(100);
bom.setH(100);
deathBoss.add(bom);
if (boss.get(j).getLife() < 0) {
boss.get(j).setImg(img_boss_bomb);
deathBoss.add(boss.get(j));
boss.remove(j);
}
fireList.remove(i);
break;
}
}
}
for (int i = 0; i < enmeyList.size(); i++) {
if (airPlane.isT(enmeyList.get(i))) {
Ball ball = enmeyList.get(i);
ball.setImg(img_bomb);
deathList.add(ball);
enmeyList.remove(i);
num++;
}
}
for (int i = 0; i < deathList.size(); i++) {
if (count % 30 == 0) {
deathList.remove(i);
}
}
for (int i = 0; i < deathBoss.size(); i++) {
if (count % 30 == 0) {
deathBoss.remove(i);
}
}
for (int i = 0; i < boss_fire_bomb.size(); i++) {
if (count % 30 == 0) {
boss_fire_bomb.remove(i);
}
}
if (num >= 5) {
isOver = true;
this.removeMouseMotionListener(this);
num = 5;
repaint();
break;
}
for (int i = 0; i < backList.size(); i++) {
Ball bg = backList.get(i);
if (bg.getY() > -1080) {
bg.setY(bg.getY() - 1);
} else {
bg.setY(1080 * (backList.size() - 1));
}
}
try {
Thread.sleep(5);
repaint();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void mouseClicked(MouseEvent e) {
if (isOver) {
if (e.getX() > 300 && e.getX() < 410 && e.getY() > 830 && e.getY() < 860) {
this.sum = 0;
this.num = 0;
this.isFire = false;
this.isOver = false;
this.addMouseMotionListener(this);
enmeyList.clear();
fireList.clear();
deathList.clear();
boss.clear();
deathBoss.clear();
boss_fire.clear();
boss_fire_bomb.clear();
this.startRun();
}
if (e.getX() > 450 && e.getX() < 520 && e.getY() > 830 && e.getY() < 860) {
System.exit(0);
}
}
}
@Override
public void mouseMoved(MouseEvent e) {
airPlane.setX(e.getX() - 35);
airPlane.setY(e.getY() - 35);
repaint();
}
public void startRun() {
Thread t = new Thread(this);
t.start();
}
@Override
public void mousePressed(MouseEvent e) {
isFire = true;
}
@Override
public void mouseReleased(MouseEvent e) {
isFire = false;
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
public boolean isOver() {
return isOver;
}
public void setOver(boolean over) {
isOver = over;
}
}
Ball .java
import java.awt.*;
import java.util.Random;
public class Ball {
private int x;
private int y;
private int w;
private int h;
private Image img;
Random rd = new Random();
private boolean flag = rd.nextBoolean();
Ball() {
}
Ball(int x, int y, Image img) {
this.x = x;
this.y = y;
this.img = img;
}
Ball(int x, int y, int w, int h, Image img) {
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.img = img;
}
public boolean isT(Ball ball) {
int r1 = this.w / 2;
int r2 = ball.getW() / 2;
int x2 = ball.getX();
int y2 = ball.getY();
double s = Math.sqrt(Math.pow(x2 - x, 2) + Math.pow(y2 - y, 2));
double sr = r1 + r2;
return s <= sr;
}
public void enemyPlaneMove() {
this.y++;
if (flag) {
this.x = this.x + rd.nextInt(3);
} else {
this.x = this.x - rd.nextInt(3);
}
flag = rd.nextBoolean();
}
public void minusY(int shotSpeed) {
this.y-=shotSpeed;
}
public void addY() {
this.y+=2;
}
public void drawImage(Graphics g) {
g.drawImage(img, x, y, w, h, null);
}
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 getW() {
return w;
}
public void setW(int w) {
this.w = w;
}
public int getH() {
return h;
}
public void setH(int h) {
this.h = h;
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
}
Boss .java
import java.awt.*;
public class Boss extends Ball {
private int life;
public void setXY(){
this.setX(rd.nextInt(1200));
this.setY(0);
}
public void minusLife(int offset){
this.life-=offset;
}
public void bossMove(){
super.enemyPlaneMove();
}
public boolean isShow() {
return isShow;
}
public void setShow(boolean show) {
isShow = show;
}
private boolean isShow;
Boss(){
}
Boss(int x, int y, int w, int h, int life,Image img){
super(x, y, w, h, img);
this.life=life;
}
public int getLife() {
return life;
}
public void setLife(int life) {
this.life = life;
}
}