package eat;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
public class FishJPanel extends JPanel implements MouseMotionListener{
//用于记录鱼上一刻位置
int shike;
int flag=0;
int score=0;
//关卡
int guanka;
//数量统计
int count = 0;
//背景
Image backgroundImg;
int x, y;
//鱼前方的引导点
int erx,ery;
int speedx=1,speedy=0;
//我的鱼
Image myImg;
int myFishX=300;
int myFishY=300;
int myFishW=40;
int myFishH=50;
//其他鱼
List<OtherFish> otherFishs = new ArrayList<OtherFish>();
public FishJPanel() {
//背景
backgroundImg = new ImageIcon("picture/background1.jpg").getImage();
//我的鱼
myImg= new ImageIcon("picture/r1.png").getImage();
//添加监听器
addMouseMotionListener(this);
}
public void paint(Graphics g){
super.paint(g);
//背景
g.drawImage(backgroundImg, x, y, 1000,800,null);
//画我的鱼
g.drawImage(myImg, myFishX, myFishY,myFishW,myFishH, null);
//引导点颜色和画
g.setColor(Color.green);
g.fillOval(erx-1, ery-1, 1,1);
//画其他小鱼
for (int i = 0; i < otherFishs.size(); i++) {
OtherFish OF = otherFishs.get(i);
OF.drawOtherFish(g);
}
//添加分数计数
Graphics2D g2=(Graphics2D)g;
g2.setStroke(new BasicStroke(10));
g.setColor(Color.red);
g2.drawString("score="+score, 700,50);
if(flag==2){
JOptionPane.showConfirmDialog(null, "游戏结束。恭喜通关!!!");
}
if(flag==1){
int res = JOptionPane.showConfirmDialog(null, "游戏结束,您的成绩为"+score, "退出?", JOptionPane.OK_OPTION);
if (res == JOptionPane.OK_OPTION) {
// System.out.println("选择是后执行的代码"); // 点击“是”后执行这个代码块
System.exit(0);
}
else
// System.exit(0);
new FishJFrame();
}
}
public void startGame(){
new Thread(){
@SuppressWarnings("deprecation")
public void run(){
while(true){
//1
//1.1背景
if(flag==1){
this.stop();
}
if(flag==2){
this.stop();
}
//关卡判断
if(myFishH>200){
guanka++;
if(guanka==1){
backgroundImg=new ImageIcon("picture/background2.jpg").getImage();
speedx=3;speedy=0;
}
if(guanka==2){
backgroundImg=new ImageIcon("picture/bg3.jpg").getImage();
speedx=5;
}
if(guanka==3){
flag=2;
}
otherFishs.clear();
myFishW=50;
myFishH=40;
}
shike=myFishX;
int x=0, y=0;
myFishX+=(erx-myFishX-myFishW/2)/20;
myFishY+=(ery-myFishY-myFishH/2)/20;
//
count++;
//鱼转向
// int s=count%100;
//鱼的摆动
if(shike>myFishX){
myImg= new ImageIcon("picture/l1.png").getImage();
}
else { myImg= new ImageIcon("picture/r1.png").getImage(); }
if(count == 10000){
count = 0;
}
//其他鱼移动
for (int i = 0; i < otherFishs.size(); i++) {
OtherFish OF = otherFishs.get(i);
int d = OF.moveFish();
if(d==1){
otherFishs.remove(i);
break;
}
}
//显示其他鱼
if(count%100==0){
int num=(int)(Math.random()*4+5);
int num1=(int)(Math.random()*4+1);
OtherFish OF =new OtherFish(new ImageIcon("picture/"+num+".png").getImage(),-100,
(int)(Math.random()*600),speedx,speedy,num);
OtherFish OF2 =new OtherFish(new ImageIcon("picture/"+num1+".png").getImage(),900,
(int)(Math.random()*600),-speedx,speedy,num1);
otherFishs.add(OF);
otherFishs.add(OF2);
}
//大鱼吃小鱼
for(int i=0;i<otherFishs.size();i++)
{
OtherFish OF=otherFishs.get(i);
if(myFishX<OF.oImg.getWidth(null)*0.8+OF.x
&&myFishY<OF.oImg.getHeight(null)*0.8+OF.y
&&myFishX+myFishW>OF.x+OF.oImg.getWidth(null)*0.2
&&myFishY+myFishH>OF.y+OF.oImg.getHeight(null)*0.2
||myFishX<OF.x+OF.oImg.getWidth(null)*0.8
&&myFishY<OF.y+OF.oImg.getHeight(null)*0.8
&&myFishX+myFishW>OF.x+OF.oImg.getWidth(null)*0.2
&&myFishY+myFishH>OF.y+myFishY*0.2)
/* if(myFishX<OF.oImg.getWidth(null)+OF.x
&&myFishY>OF.y-OF.oImg.getHeight(null)
&&myFishX+myFishW>OF.x
&&myFishY-myFishH>OF.y
)*/
/* if(myFishX<OF.oImg.getWidth(null)+OF.x
&&myFishY<OF.oImg.getHeight(null)+OF.y
&&myFishX+myFishW>OF.x
&&myFishY+myFishH>OF.y
||myFishX<OF.x+OF.oImg.getWidth(null)
&&myFishY<OF.y+OF.oImg.getHeight(null)
&&myFishX+myFishW>OF.x
&&myFishY+myFishH>OF.y)*/
{
if(OF.oImg.getHeight(null)*OF.oImg.getWidth(null)<myFishH*myFishW){
otherFishs.remove(i);
myFishW+=10;
myFishH+=10;
score=score+3;
break;
}
if(OF.oImg.getHeight(null)*OF.oImg.getWidth(null)>myFishH*myFishW){
flag=1;
}
}
}
//2
repaint();
//3
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}.start();
}
@Override
public void mouseDragged(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseMoved(MouseEvent e) {
erx= e.getX();
ery= e.getY();
}
}