GameWin类
package com.DinosaurL;
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 {
/** 游戏状态 0未开始,1游戏中,2通关失败,3通关成功,4暂停,5重新开始*/
//定义游戏默认状态
static int state = 0;
Image offScreenImage;
//宽高
int width = 1440;
int height = 900; //设置窗口大小
double random;
//计数器
int time = 0; //记录游戏重绘次数即paint调用次数
//背景
Background background = new Background();
//敌方鱼类
Enemy enemy;
//是否生成boss
boolean isboss = false;
//boss类
Enemy boss;
//我方鱼类
MyFish myFish = new MyFish();
public void launch(){
this.setVisible(true);
this.setSize(width,height);
this.setLocationRelativeTo(null); //设置窗口位置
//this.setResizable(false);
this.setTitle("DinosaurL");
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);
//WASD
if (e.getKeyCode()==87){
GameUtils.UP = true;
}
if (e.getKeyCode()==83){
GameUtils.DOWN = true;
}
if (e.getKeyCode()==65){
GameUtils.LEFT = true;
}
if (e.getKeyCode()==68){
GameUtils.RIGHT = true;
}
if (e.getKeyCode()==32){
switch (state){
case 1:
state = 4;
GameUtils.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){
GameUtils.UP = false;
}
if (e.getKeyCode()==83){
GameUtils.DOWN = false;
}
if (e.getKeyCode()==65){
GameUtils.LEFT = false;
}
if (e.getKeyCode()==68){
GameUtils.RIGHT = false;
}
}
});
while (true){
repaint();
time++;
try {
Thread.sleep(40); //每间隔40ms调用一次repaint()方法
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void paint(Graphics g) {
//懒加载模式初始化对象
offScreenImage = createImage(width,height);
Graphics gImage = offScreenImage.getGraphics();
background.paintSelf(gImage , myFish.level);
switch (state){
case 0:
break;
case 1:
logic();
myFish.paintSelf(gImage);
for (Enemy enemy:GameUtils.EnemyList) {
enemy.paintSelf(gImage);
}
if (isboss){
boss.x = boss.x + boss.dir * boss.speed;
boss.paintSelf(gImage);
if (boss.x<0){
gImage.setColor(Color.red);
gImage.fillRect(boss.x,boss.y,2400,boss.height/30);
}
}
break;
case 2:
for (Enemy enemy:GameUtils.EnemyList) {
enemy.paintSelf(gImage);
}
if (isboss){
boss.paintSelf(gImage);
}
break;
case 3:
myFish.paintSelf(gImage);
break;
case 4:
return;
default:
}
g.drawImage(offScreenImage,0,0,null);
}
void logic(){
//关卡难度
if (GameUtils.count<5){
GameUtils.level = 0;
myFish.level = 1;
}else if (GameUtils.count<=15){
GameUtils.level = 1;
}else if (GameUtils.count<=50){
GameUtils.level = 2;
myFish.level = 2;
}else if (GameUtils.count<=150){
enemy = new Enemy_3_L();
enemy.width = 150;
enemy.height = 75;
enemy.speed = 50;
enemy.count = 1;
enemy.type = 1;
GameUtils.EnemyList.add(enemy);
GameUtils.level = 3;
myFish.level = 3;
}else if (GameUtils.count<=200){
GameUtils.level = 4;
myFish.level = 3;
}else if (GameUtils.count>200){
state = 3;
}
random =Math.random();
switch (GameUtils.level) {
case 4:
if (time%60==0){
if (random>0){
boss = new Enemy_Boss();
isboss =true;
}
}
case 3:
case 2:
if (time%30==0){
if (random>0.5){
enemy = new Enemy_3_L();
}else {
enemy = new Enemy_3_R();
}
GameUtils.EnemyList.add(enemy);
}
case 1:
if (time%20==0){
if (random>0.5){
enemy = new Enemy_2_L();
}else {
enemy = new Enemy_2_R();
}
GameUtils.EnemyList.add(enemy);
}
case 0:
if (time % 10 == 0) {
if (random > 0.5) {
enemy = new Enemy_1_L();
} else {
enemy = new Enemy_1_R();
}
GameUtils.EnemyList.add(enemy);
}
break;
default:
}
for (Enemy enemy:GameUtils.EnemyList) {
enemy.x = enemy.x + enemy.dir * enemy.speed;
if (isboss){
if (boss.getRec().intersects(enemy.getRec())){
enemy.x = -200;
enemy.y = -200;
}
if (boss.getRec().intersects(myFish.getRec())){
state = 2;
}
}
//我方鱼与敌方鱼碰撞检测
if (myFish.getRec().intersects(enemy.getRec())) {
if (myFish.level >= enemy.type) {
enemy.x = -200;
enemy.y = -200;
GameUtils.count = GameUtils.count + enemy.count;
} else {
state = 2;
}
}
}
}
//重新开始
void reGame(){
GameUtils.EnemyList.clear();
time = 0;
myFish.level = 1;
GameUtils.count = 0;
myFish.x = 700;
myFish.y = 500;
myFish.width = 50;
myFish.height = 50;
boss =null;
isboss = false;
}
public static void main(String[] args) {
GameWin gameWin = new GameWin();
gameWin.launch();
}
}
GameUtils类
package com.DinosaurL;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;
public class GameUtils {
//方向
static boolean UP =false;
static boolean DOWN =false;
static boolean LEFT =false;
static boolean RIGHT =false;
//分数
static int count = 60;
//关卡等级
static int level = 0;
//敌方鱼类集合
public static List<Enemy> EnemyList = new ArrayList<>();
//背景图
public static Image bgimg = Toolkit.getDefaultToolkit().createImage("images/sea.jpg");
//敌方鱼类
public static Image enemy1_img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish1_r.gif");
public static Image enemyr_img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish1_l.gif");
public static Image enemyl_2img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish2_r.png");
public static Image enemyr_2img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish2_l.png");
public static Image enemyl_3img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish3_r.png");
public static Image enemyr_3img = Toolkit.getDefaultToolkit().createImage("images/enemyFish/fish3_l.png");
public static Image bossimg = Toolkit.getDefaultToolkit().createImage("images/enemyFish/boss.gif");
//我方鱼类
public static Image MyFishimg_L = Toolkit.getDefaultToolkit().createImage("images/myFish/myfish_left.gif");
public static Image MyFishimg_R = Toolkit.getDefaultToolkit().createImage("images/myFish/myfish_right.gif");
//绘制文字的工具类
public static void drawWord(Graphics g,String str ,Color color,int size,int x, int y){
g.setColor(color);
g.setFont(new Font("仿宋",Font.BOLD,size));
g.drawString(str, x, y);
}
}
Background类
package com.DinosaurL;
import java.awt.*;
public class Background {
void paintSelf(Graphics g , int fishLevel){
g.drawImage(GameUtils.bgimg,0,0,null); //在绘制背景的方法中定义所需参数
switch (GameWin.state){
case 0:
GameUtils.drawWord(g,"开始",Color.red,80,700,500);
break;
case 1:
GameUtils.drawWord(g,"积分"+GameUtils.count,Color.ORANGE,50,200,120);
GameUtils.drawWord(g,"难度"+GameUtils.level,Color.ORANGE,50,600,120);
GameUtils.drawWord(g,"等级"+fishLevel,Color.ORANGE,50,1000,120);
break;
case 2:
GameUtils.drawWord(g,"积分"+GameUtils.count,Color.ORANGE,50,200,120);
GameUtils.drawWord(g,"难度"+GameUtils.level,Color.ORANGE,50,600,120);
GameUtils.drawWord(g,"等级"+fishLevel,Color.ORANGE,50,1000,120);
GameUtils.drawWord(g,"失败",Color.red,80,700,500);
break;
case 3:
GameUtils.drawWord(g,"积分"+GameUtils.count,Color.ORANGE,50,200,120);
GameUtils.drawWord(g,"难度"+GameUtils.level,Color.ORANGE,50,600,120);
GameUtils.drawWord(g,"等级"+fishLevel,Color.ORANGE,50,1000,120);
GameUtils.drawWord(g ,"胜利",Color.red,80,700,500);
break;
case 4:
GameUtils.drawWord(g,"积分"+GameUtils.count,Color.ORANGE,50,200,120);
GameUtils.drawWord(g,"难度"+GameUtils.level,Color.ORANGE,50,600,120);
GameUtils.drawWord(g,"等级"+fishLevel,Color.ORANGE,50,1000,120);
default:
}
}
}
Enemy类
package com.DinosaurL;
import java.awt.*;
public class Enemy {
//定义图片
Image img;
//定义物体坐标
int x;
int y;
int width;
int height;
//移动速度
int speed;
//方向
int dir = 1; //从左向右
//类型
int type;
//分值
int count;
//绘制自身方法
public void paintSelf(Graphics g){
g.drawImage(img,x,y,width,height,null);
}
//获取自身矩形用于碰撞检测
public Rectangle getRec(){
return new Rectangle(x,y,width,height);
}
}
//敌方鱼左类
class Enemy_1_L extends Enemy{
Enemy_1_L(){
this.x = -45;
this.y = (int)(Math.random()*700+100);
this.width = 45;
this.height = 69;
this.speed = 10;
this.count = 1;
this.type = 1;
this.img = GameUtils.enemy1_img;
}
}
class Enemy_1_R extends Enemy_1_L{
Enemy_1_R(){
this.x = 1400;
dir = -1;
this.img =GameUtils.enemyr_img;
}
}
class Enemy_2_L extends Enemy{
Enemy_2_L(){
this.x = -100;
this.y = (int)(Math.random()*700+100);
this.width = 100;
this.height = 100;
this.speed = 5;
this.count = 2;
this.type = 2;
this.img =GameUtils.enemyl_2img;
}
}
class Enemy_2_R extends Enemy_2_L{
Enemy_2_R(){
this.x = 1400;
dir = -1;
this.img = GameUtils.enemyr_2img;
}
}
class Enemy_3_L extends Enemy{
Enemy_3_L(){
this.x =-300;
this.y = (int)(Math.random()*700+100);
this.width = 300;
this.height = 150;
this.speed = 20;
this.count = 3;
this.type = 3;
this.img =GameUtils.enemyl_3img;
}
@Override
public Rectangle getRec(){
return new Rectangle(x+40,y+30,width-80,height-60);
}
}
class Enemy_3_R extends Enemy_3_L{
Enemy_3_R(){
this.x = 1400;
dir = -1;
this.img =GameUtils.enemyr_3img;
}
}
class Enemy_Boss extends Enemy{
Enemy_Boss(){
this.x = -1000;
this.y = (int)(Math.random()*700+100);
this.width = 200;
this.height = 200;
this.speed = 60;
this.count = 0;
this.type = 10;
this.img = GameUtils.bossimg;
}
}
MyFish类
package com.DinosaurL;
import java.awt.*;
public class MyFish {
//图片
Image img = GameUtils.MyFishimg_L;
//坐标
int x = 700;
int y = 500;
int width = 50;
int height = 50;
//移动速度
int speed = 20;
//等级
int level = 1;
void logic(){
if (GameUtils.UP){
y = y-speed;
}
if (GameUtils.DOWN){
y = y+speed;
}
if (GameUtils.LEFT){
x =x-speed;
img = GameUtils.MyFishimg_L;
}
if (GameUtils.RIGHT){
x = x+speed;
img = GameUtils.MyFishimg_R;
}
}
//绘制自身的方法
public void paintSelf(Graphics g){
logic();
g.drawImage(img,x,y,width+GameUtils.count,height+GameUtils.count,null);
}
//获取自身矩形的方法,用于碰撞检测'
public Rectangle getRec(){
return new Rectangle(x,y,width+GameUtils.count,height+GameUtils.count);
}
}