package Fly.Game;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class JPanel_game extends JPanel implements MouseMotionListener,MouseListener{
int x,y; //主机的坐标
int xx[]; int yy[];//背景星星数组
Font[] fonts;
List<Choose> downs; //下落
List<Choose> fires; //上升的子弹
int fires_speed = 20; //运动的速度(数值越大 速度越慢)
int downs_speed = 60;
//飞机数量
int sum = 0;
Boolean isFires=false;
public JPanel_game() {
// TODO Auto-generated constructor stub
this.addMouseMotionListener(this);
this.addMouseListener(this);
x=150; y=600; //设置主机坐标
xx = new int[50];yy = new int[50];
for(int i = 0 ; i<50;i++){
xx[i]=(int)(Math.random()*400);
yy[i]=(int)(Math.random()*700);
}
fonts = new Font[4]; //设置字体 四个参数
fonts[0] = new Font("微软雅黑", Font.BOLD, 18);
fonts[1] = new Font("微软雅黑", Font.BOLD, 24);
fonts[2] = new Font("微软雅黑", Font.BOLD, 14);
fonts[3] = new Font("微软雅黑", Font.BOLD, 32);
downs = new ArrayList<Choose>();
fires = new ArrayList<Choose>();
}
@Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
//调用Choose
Choose choose = new Choose();
//背景图片 import java.net.URL;
URL B_img = getClass().getResource("back.jpg");
ImageIcon B_icon = new ImageIcon(B_img);
g.drawImage(B_icon.getImage(), 0, 0, 400, 700,null);
//显示飞机数量
g.setColor(Color.white);
g.drawString("干掉飞机:"+sum+"架", 10, 20);
//画星星
// g.setColor(Color.white);
g.setColor(new Color((int)(Math.random()*0xffffff))); //随机颜色
for(int i=0; i<xx.length;i++){
g.setFont(fonts[(int)(Math.random()*4)]); //改变字体大小
g.drawString("★", xx[i], yy[i]);
}
//显示主机
URL My_img = getClass().getResource("el_0.gif");
ImageIcon My_icon = new ImageIcon(My_img);
g.drawImage(My_icon.getImage(), x, y, 60, 60,null);
//显示敌机
for(int i=0;i<downs.size();i++){
choose = downs.get(i);
choose.draw(g);
}
//显示子弹
for(int i=0;i<fires.size();i++){
choose = fires.get(i);
choose.draw(g);
}
}
//创建线程
public void st() {
// TODO Auto-generated method stub
Thread th = new Thread(){
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
int count = 0; //设定速度初始值
while (true) {
count++;
if(count%downs_speed==0){
//当count 自增到60的时候,敌机开始往下落
Choose ch = new Choose();
int tem_x = (int) (Math.random()*400); //敌机的x轴坐标
int tem_y = -40; //y轴坐标
ch.setX(tem_x); //将数据放进X
ch.setY(tem_y);
ch.setR(20);
ch.setType(1);
ch.setOperation(1);
//将信息加入 downs集合
downs.add(ch);
}
for(int i=0;i<downs.size();i++){
Choose c = downs.get(i);
c.move();
if(c.getY()>=JPanel_game.this.getHeight()){
downs.remove(i);
}
}
//子弹往上升的动作
if(isFires&&count%fires_speed==0){
//isFires满足ture
//count%fires_speed==0
Choose ch = new Choose();
ch.setR(10);
ch.setX(x+20);
ch.setY(y);
ch.setType(0);
ch.setOperation(0);
fires.add(ch);
}
for(int i=0;i<fires.size();i++){
Choose c = fires.get(i);
c.move();
if(c.getY()<0){
fires.remove(i);
}
}
//判断飞机子弹是否接触
//敌r+飞r <=2R 则消除
for(int i=0;i<fires.size();i++){
for(int j=0;j<downs.size();j++){
if(i<fires.size()&&j<downs.size()&&fires.get(i).isTouch(downs.get(j))){
////&&fires.get(i).isTouch(downs.get(j)) 对半径判断的引用
fires.remove(i);
downs.remove(j);
sum++;
}
}
}
try {
//休眠时间
Thread.sleep(5);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
repaint();
}
}
};
th.start();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isFires = true;
}
//按下
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
isFires = false;
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
//按下移动
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX()-20;
y=e.getY()-20;
repaint();
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
x=e.getX()-20;
y=e.getY()-20;
repaint();
}
}