package com.cjc.fish;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class GameWin extends JFrame {
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
public int width = 1440;
public int height = 900;
public double random;
//计数器
public int time = 0;
public static int state = 0;//默认游戏状态
Image offScreenImage;
BG bg = new BG();
//敌方鱼类
Enamy enamy;
//我方鱼类
MyFish myFish = new MyFish();
//boss鱼类
Enamy boss;
//是否生产boss
boolean isBoss = false;
//重新开始游戏
public void reGame(){
FishUtils.enamyList.clear();
time = 0;
myFish.level = 1;
FishUtils.count = 0;
myFish.x = 700;
myFish.y = 500;
myFish.width = 50;
myFish.height = 50;
boss = null;
isBoss = false;
}
/**
* 设置窗口信息
*/
public void launch(){
this.setVisible(true);//窗口是否显示
this.setSize(width,height);//窗口大小
this.setLocationRelativeTo(null);//设置窗口在屏幕居中位置。
// this.setResizable(false);//设置游戏窗口不能改变
this.setTitle("鱼了个鱼");//设置窗口标题
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭窗口
this.addMouseListener(new MouseAdapter(){
@Override
public void mouseClicked(MouseEvent e) {
super.mouseClicked(e);
if (e.getButton() == 1 && state == 0){
state = 1;
repaint();
}
if (e.getButton() == 1&&(state == 2 || state == 3)){
reGame();
state = 1;
}
}
});
//键盘移动
this.addKeyListener(new KeyAdapter() {
//按下
@Override
public void keyPressed(KeyEvent e) {
super.keyPressed(e);
if (e.getKeyCode() == 87){
FishUtils.UP = true;
}
if (e.getKeyCode() == 83){
FishUtils.DOWN = true;
}
if (e.getKeyCode() == 65){
FishUtils.LEFT = true;
}
if (e.getKeyCode() == 68){
FishUtils.RIGHT = true;
}
//空格暂停
if (e.getKeyCode() == 32){
switch (state){
case 1:
state = 4;
FishUtils.drawWord(getGraphics(),"游戏暂停",Color.red,50,600,400);
break;
case 4:
state = 1;
break;
}
}
}
//抬起
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
if (e.getKeyCode() == 87){
FishUtils.UP = false;
}
if (e.getKeyCode() == 83){
FishUtils.DOWN = false;
}
if (e.getKeyCode() == 65){
FishUtils.LEFT = false;
}
if (e.getKeyCode() == 68){
FishUtils.RIGHT = false;
}
}
});
while (true){
repaint();
time++;
try {
Thread.sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//批量添加敌方鱼类
public void logic(){
//关卡难度
if (FishUtils.count<=5){
FishUtils.level = 0;
myFish.level = 1;
}else if (FishUtils.count <= 15){
FishUtils.level = 1;
}else if (FishUtils.count <= 50){
FishUtils.level = 2;
myFish.level = 2;
}else if (FishUtils.count <= 150){
FishUtils.level = 3;
myFish.level = 3;
}else if (FishUtils.count <= 300){
FishUtils.level = 4;
}else if (FishUtils.count > 300){
state = 3;
}
random = Math.random();
//敌方鱼生成,根据等级判断
switch (FishUtils.level){
case 4:
if (time%60==0){
if (random>0){
boss = new EnamyBoss();
isBoss = true;
}
}
case 3:
case 2:
if (time % 30 == 0) {
if (random > 0.5) {
enamy = new EnamyLeft3();
} else {
enamy = new EnamyRight3();
}
FishUtils.enamyList.add(enamy);
}
case 1:
if (time % 20 == 0) {
if (random > 0.5) {
enamy = new EnamyLeft2();
} else {
enamy = new EnamyRight2();
}
FishUtils.enamyList.add(enamy);
}
case 0:
if (time%10 == 0) {
if (random > 0.5) {
enamy = new EnamyLeft();
} else {
enamy = new EnamyRight();
}
FishUtils.enamyList.add(enamy);
}
break;
default:
}
//绘制敌方鱼的移动方向
for (Enamy enamy : FishUtils.enamyList){
enamy.x = enamy.x + enamy.dir*enamy.speed;
//检测boss鱼与其他鱼碰撞
if (isBoss){
if (boss.getRec().intersects(enamy.getRec())){
enamy.x = -200;
enamy.y = -200;
}
if (boss.getRec().intersects(myFish.getRec())){
state = 2;
}
}
//检测我方鱼与敌方鱼碰撞,使用等级判断
if (myFish.getRec().intersects(enamy.getRec())){
//System.out.println("碰撞了");
if (myFish.level >= enamy.type){
enamy.x = -200;
enamy.y = -200;
FishUtils.count = FishUtils.count+enamy.count;
}else {
state = 2;//返回游戏失败
}
}
}
}
@Override
public void paint(Graphics g) {
//使用懒加载模式初始化对象
offScreenImage = createImage(width,height);
Graphics graphics = offScreenImage.getGraphics();
bg.paintSelf(graphics,myFish.level);
//g.drawImage(FishUtils.bimage,0,0,null);
switch (state){
case 0:
break;
case 1:
myFish.paintSelf(graphics);
logic();
for (Enamy enamy:FishUtils.enamyList) {
enamy.paintSelf(graphics);
}
if (isBoss){
boss.x = boss.x + boss.dir * boss.speed;
boss.paintSelf(graphics);
if (boss.x < 0){
graphics.setColor(Color.RED);
graphics.fillRect(boss.x,boss.y,2400,boss.height/30);
}
}
评论0