我自己写了一份,但觉得还是老师写的有注释容易懂:
不多bb,具体代码如下:
Ball.java:
package day04;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
public class Ball {
// 成员属性
private int x,y;// 坐标
private int r; // 半径
private int ori; // 运动方向:向上 0 ,向下 1
private Image img; // 图片对象
private int die;
// 声明方向常量
public static final int UP = 0; //向上
public int getDie() {
return die;
}
public void setDie(int die) {
this.die = die;
}
public static final int DOWN = 1; //向下
public Ball(){
}
public Ball(int x,int y,int r,int ori,Image img){
this.x = x;
this.y = y;
this.r = r;
this.ori = ori;
this.img = img;
}
public Ball(int x,int y,int r,Image img){
this.x = x;
this.y = y;
this.r = r;
this.img = img;
}
/**
* 画
* @param g
*/
public void drawImg(Graphics g){
g.drawImage(img,x,y,2*r,2*r,null);
}
/**
* 移动的方法
*/
public void move(){
switch(ori){
case UP:
y--;
break;
case DOWN:
y++;
break;
}
}
/**
* 相遇的方法
* 判断相遇
* 根据子弹和敌机的距离:半径与半径比较
* 发生碰撞返回true,没有碰撞返回false
* @param ball
* @return
*/
public boolean isTouch(Ball ball){
// 第一个小球的圆心坐标
int x1 = x+r;
int y1 = y+r;
// 第二个小球的圆心坐标
int x2 = ball.getX()+ball.getR();
int y2 = ball.getY()+ball.getR();
// 两点之间的距离 Math.sqrt(x^2+y^2)
double s = Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
// 两个半径之和
double sr = r + ball.getR();
return s <= sr;
}
public Image getImg() {
return img;
}
public void setImg(Image img) {
this.img = img;
}
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 getR() {
return r;
}
public void setR(int r) {
this.r = r;
}
public int getOri() {
return ori;
}
public void setOri(int ori) {
this.ori = ori;
}
}
BallJPanel.java
package day04;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class BallJPanel extends JPanel implements
MouseMotionListener,MouseListener {
private Ball hreo; // 我机
private boolean isFire; // 定义子弹是否发射
private int sums;
// 图片对象
private Image img_back = new ImageIcon("img/back.JPG").getImage();
private Image img_hreo = new ImageIcon("img/hero.gif").getImage();
//图片对象
private Image img_fire = new ImageIcon("img/fire.gif").getImage();// 子弹
private Image img_foe = new ImageIcon("img/el_0.gif").getImage(); // 敌机
private Image img_bomb = new ImageIcon("img/bomb_0.gif").getImage();
/*无限子弹*/
private List
/*无限敌机*/
private List
private List
// 构造器
public BallJPanel(){
hreo = new Ball(180,480,25,img_hreo);
/*无限子弹*/
fires = new ArrayList
/*无限敌机*/
foes = new ArrayList
bombs=new ArrayList
// 添加鼠标移动监听
this.addMouseMotionListener(this);
/*
* 第一个this 是当前面板
* 第二个this 是鼠标监听对象
*/
this.addMouseListener(this);
}
/**
* 画
*/
@Override
public void paint(Graphics g) {
super.paint(g);
g.drawImage(img_back,0,0,600,900,this); // 画背景
g.setColor(Color.white);
g.setFont(new Font("宋体",Font.BOLD,15));
g.drawString("积分",300,25);
g.drawString(String.valueOf(sums), 345, 25);
//画飞机
hreo.drawImg(g);
/*画子弹 fires.size() 集合的长度*/
for (int i = 0; i < fires.size(); i++) {
Ball ball = fires.get(i); // fires.get(i); 获取集合中的元素
ball.drawImg(g);
}
/*画敌机*/
for (int i = 0; i < foes.size(); i++) {
Ball el = foes.get(i); // foes.get(i); 获取集合中的元素
el.drawImg(g);
}
for (int i = 0; i < bombs.size(); i++) {
Ball bomb=bombs.get(i);
bomb.drawImg(g);
}
}
public void startRun(){
new Thread(){
public void run() {
int count = 0;// 用来计数 可以控制 敌机或子弹出现的频率
Random rd = new Random(); // 随机
while(true){
count++;
// 子弹
//1.产生子弹 按下鼠标时
if(isFire && count%30 == 0){
Ball ball = new Ball(hreo.getX()+5,hreo.getY()-20,20,Ball.UP,img_fire);
fires.add(ball); // 添加到子弹集合
}
/*2.所有子弹都移动*/
for (int i = 0; i < fires.size(); i++) {
Ball ball = fires.get(i);
ball.move();
// 超出屏幕的删除
if(ball.getY() < 0){
fires.remove(i); // 删除该集合中的第i个元素
}
}
// 敌机
// 1.生产敌机
if(count % 100 == 0){
Ball el = new Ball(rd.nextInt(400),0,20,Ball.DOWN,img_foe);
foes.add(el);// 添加到敌机集合里面去
}
// 2.敌机移动
for (int i = 0; i < foes.size(); i++) {
Ball el = foes.get(i);
el.move();// 敌机移动
if(el.getY() > 600){
foes.remove(i);
}
}
for (int i = 0; i < fires.size(); i++) {
for (int j = 0; j < foes.size(); j++) {
if(i
fires.remove(i);
foes.remove(j);
s.setImg(img_bomb);
s.setDie(20);
bombs.add(s);
sums+=10;
}
}
}
for (int i = 0; i
int y1=hreo.getY()+20;
int x2 = foes.get(i).getX()+foes.get(i).getR();
int y2 = foes.get(i).getY()+foes.get(i).getR();
double s = Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2));
double sr =40;
if(s
y1=0;
}
}
for (int i = 0; i < bombs.size(); i++) {
Ball bom=bombs.get(i);
bom.setDie(bom.getDie()-1);
if(bom.getDie()<=0){
bombs.remove(i);
}
}
try {
Thread.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
repaint();//刷新面板
}
};
}.start();
}
/**
* 鼠标拖动时
*/
@Override
public void mouseDragged(MouseEvent e) {
//MouseEvent e 是鼠标的坐标
hreo.setX(e.getX()-20);
hreo.setY(e.getY()-20);
repaint();// 刷新面板
}
/**
* 鼠标移动时
*/
@Override
public void mouseMoved(MouseEvent e) {
//MouseEvent e 是鼠标的坐标
hreo.setX(e.getX()-20);
hreo.setY(e.getY()-20);
repaint();// 刷新面板
}
/**
* 点击鼠标时调用
*/
@Override
public void mouseClicked(MouseEvent arg0) {
//System.out.println("666");
}
/**
* 鼠标出现在组件 中时调用
*/
@Override
public void mouseEntered(MouseEvent arg0) {
//System.out.println("鼠标出现在组件 中时调用");
}
/**
* 鼠标移出组件时
*/
@Override
public void mouseExited(MouseEvent arg0) {
//System.out.println("鼠标移出组件时");
}
/**
* 鼠标按下时调用
*/
@Override
public void mousePressed(MouseEvent arg0) {
//System.out.println("鼠标按下时调用");
isFire = true;// 鼠标按下时发射子弹
}
/**
* 按下的鼠标松开时
*/
@Override
public void mouseReleased(MouseEvent arg0) {
//System.out.println("按下的鼠标松开时");
isFire = false; // 鼠标松开时, 停止发射
}
}
balljframe.java:
package day04;
import javax.swing.JFrame;
// 继承窗口类
public class BallJFrame extends JFrame {
private BallJPanel panel;
public BallJFrame(){
panel = new BallJPanel();
this.add(panel);
}
public void showJF(){
this.setSize(600,900); // 设置窗口大小
this.setTitle("雷电游戏"); // 设置标题
//关闭
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.startRun();//调用线程
this.setVisible(true); // 显示窗口
}
public static void main(String[] args) {
BallJFrame jf = new BallJFrame();
jf.showJF();
}
}
具体效果如下: