java小游戏-java小游戏-黄金矿工
1 窗口绘制2 绘制图片3 红线绘制4 红线摇摆5 红线抓取6 创建金块7 双缓存技术8 抓取判断9 抓取返回10 多个金块11 Bug金块消失12 石块创建13 拉取速度14 多种金块15 物体堆叠16 红线完善17 积分设置18 力量与爆破19 关卡设置20 游戏状态21 计时效果22 失败状态23 成功状态24 游戏重置25 商店购物连接视频
1 窗口绘制
创建GameWin类
public class GameWin extends JFrame {public void launch(){this.setVisible(true);this.setSize(500,500);this.setLocationRelativeTo(null);this.setTitle("黄金矿工");this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);}public static void main(String[] args) {GameWin gameWin = new GameWin();gameWin.launch();}}
2 绘制图片
先导入图片到项目中img文件夹中
创建Bg类
//背景类public class Bg {Image bg = Toolkit.getDefaultToolkit().getImage("img/bg.jpg");Image bg1 = Toolkit.getDefaultToolkit().getImage("img/bg1.jpg");Image peo = Toolkit.getDefaultToolkit().getImage("img/peo.png");public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);g.drawImage(peo,310,50,null);}}
在GameWin中绘制
Bg bg = new Bg();@Overridepublic void paint(Graphics g) {bg.paintSelf(g);}
3 红线绘制
创建Line类
//红线类public class Line {//起点坐标int x = 380,y=180;//终点坐标int endX = 500,endY = 500;public void paintSelf(Graphics g){g.setColor(Color.red);g.drawLine(x,y,endX,endY);}}
在GameWin类中引用
Line line = new Line();@Overridepublic void paint(Graphics g) {bg.paintSelf(g);line.paintSelf(g);}
4 红线摇摆
在Line类添加参数
//线长double length = 100;double n = 0;//方向int dir = 1;public void paintSelf(Graphics g){if(n < 0.1){dir = 1;}else if(n > 0.9){dir = -1;}n = n + 0.005 * dir;endX = (int) (x + length*Math.cos(n * Math.PI));endY = (int) (y + length*Math.sin(n * Math.PI));g.setColor(Color.red);g.drawLine(x,y,endX,endY);}
在GameWin类中launch方法添加死循环
public void launch(){...while (true){repaint();try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}
5 红线抓取
在Line类添加参数
//状态 0 左右摇摆 1 抓取 2 收回int state;public void paintSelf(Graphics g){switch (state){case 0:if(n < 0.1){dir = 1;}else if(n > 0.9){dir = -1;}n = n + 0.005 * dir;lines(g);break;case 1:if(length< 500){length = length + 10;lines(g);}else {state = 2; }break;case 2:if (length > 100) {length = length - 10;lines(g);}else {state = 0; }break;default:}}void lines(Graphics g){endX = (int) (x + length*Math.cos(n * Math.PI));endY = (int) (y + length*Math.sin(n * Math.PI));g.setColor(Color.red);g.drawLine(x,y,endX,endY);}
在GameWin类中launch方法添加鼠标监听事件
public void launch(){...//鼠标监听事件this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);if(e.getButton() == 1){//鼠标左键点击line.state = 1;}}});...}
6 创建金块
创建Object父类
//金矿、石头public class Object {//坐标int x,y;//宽高int width,height;//图片Image img;public void paintSelf(Graphics g){g.drawImage(img,x,y,null);}}
创建Gold子类
//金矿类public class Gold extends Object{public Gold(){this.x = 300;this.y = 500;this.width = 52;this.height = 52;this.img = Toolkit.getDefaultToolkit().getImage("img/gold1.gif");}}
在GameWin类引用
Gold gold = new Gold();@Overridepublic void paint(Graphics g) {bg.paintSelf(g);line.paintSelf(g);gold.paintSelf(g);}
7 双缓存技术
在GameWin类添加画布
Image offScreenImage;@Overridepublic void paint(Graphics g) {offScreenImage = this.createImage(768,1000);Graphics graphics = offScreenImage.getGraphics();bg.paintSelf(graphics);line.paintSelf(graphics);gold.paintSelf(graphics);g.drawImage(offScreenImage,0,0,null);}
8 抓取判断
在Line类添加判断
GameWin frame;public Line(GameWin frame) {this.frame = frame;}//碰撞检测,检测物体是否被抓取void logic() {if (endX > this.frame.gold.x && endX < this.frame.gold.x + this.frame.gold.width&& endY > this.frame.gold.y && endY < this.frame.gold.y + this.frame.gold.height) {System.out.println("11111111");}}public void paintSelf(Graphics g) {logic();switch (state) {....
在GameWin类修改Line对象
Line line = new Line(this);
9 抓取返回
在Line类添加判断
//状态 0 左右摇摆 1 抓取 2 收回 3 抓取返回int state;//碰撞检测,检测物体是否被抓取void logic() {if (endX > this.frame.gold.x && endX < this.frame.gold.x + this.frame.gold.width&& endY > this.frame.gold.y && endY < this.frame.gold.y + this.frame.gold.height) {state = 3;}}public void paintSelf(Graphics g) {logic();switch (state) {...case 3:if (length > 100) {length = length - 10;lines(g);this.frame.gold.x = endX - 26;this.frame.gold.y = endY;} else {this.frame.gold.x = -150;this.frame.gold.y = -150;state = 0;}break;default:}}
10 多个金块
在GameWin类添加集合
// Gold gold = new Gold();//存储金块,石块List<Object> objectList = new ArrayList<>();{for (int i = 0; i < 3; i++) {objectList.add(new Gold());}}@Overridepublic void paint(Graphics g) {offScreenImage = this.createImage(768,1000);Graphics graphics = offScreenImage.getGraphics();bg.paintSelf(graphics);line.paintSelf(graphics);//gold.paintSelf(graphics);for (Object obj : objectList) {obj.paintSelf(graphics);}g.drawImage(offScreenImage,0,0,null);}
在Line类修改
//碰撞检测,检测物体是否被抓取void logic() {for (Object gold : this.frame.objectList) {if (endX > gold.x && endX < gold.x + gold.width&& endY > gold.y && endY < gold.y + gold.height) {state = 3;}}}public void paintSelf(Graphics g) {logic();switch (state) {...case 3:if (length > 100) {length = length - 10;lines(g);for (Object gold : this.frame.objectList) {gold.x = endX - 26;gold.y = endY;if(length <= 100){gold.x = -150;gold.y = -150;state = 0;}}}break;default:}}
在Gold类修改
public Gold(){this.x = (int)(Math.random()*700);this.y = (int)(Math.random()*550 + 300);...}
11 Bug金块消失
在Object类添加参数
//标记是否可以移动 false 不可移动 true 可以移动boolean flag = false;
在Gold类设置参数
public Gold(){this.x = (int)(Math.random()*700);this.y = (int)(Math.random()*550 + 300);this.width = 52;this.height = 52;this.flag = false;..}
在Line类添加设置
//碰撞检测,检测物体是否被抓取void logic() {for (Object gold : this.frame.objectList) {if (endX > gold.x && endX < gold.x + gold.width&& endY > gold.y && endY < gold.y + gold.height) {state = 3;gold.flag = true;}}}public void paintSelf(Graphics g) {logic();switch (state) {...case 3:if (length > 100) {length = length - 10;lines(g);for (Object gold : this.frame.objectList) {if(gold.flag){gold.x = endX - 26;gold.y = endY;if(length <= 100){gold.x = -150;gold.y = -150;gold.flag = false;state = 0;}}}}break;default:}}
12 石块创建
创建Rock类
//石块类public class Rock extends Object{public Rock(){this.x = (int)(Math.random()*700);this.y = (int)(Math.random()*550 + 300);this.width = 71;this.height = 71;this.flag = false;this.img = Toolkit.getDefaultToolkit().getImage("img/rock1.png");}}
在GameWin类中创建
{for (int i = 0; i < 3; i++) {objectList.add(new Gold());}for (int i = 0; i < 3; i++) {objectList.add(new Rock());}}
在Object类添加方法
public int getWidth() {return width;}
在Line类中调用
public void paintSelf(Graphics g) {logic();switch (state) {...case 3:if (length > 100) {length = length - 10;lines(g);for (Object gold : this.frame.objectList) {if(gold.flag){gold.x = endX - gold.getWidth()/2;gold.y = endY;if(length <= 100){gold.x = -150;gold.y = -150;gold.flag = false;state = 0;}}}}break;default:}}
13 拉取速度
在Object类添加参数
//质量int m;
在Gold类设值
public Gold(){this.m = 80;...}
在Rock类设值
public Rock(){this.m = 150;...}
在Linel类修改判断
public void paintSelf(Graphics g) {logic();switch (state) {case 0:if (n < 0.1) {dir = 1;} else if (n > 0.9) {dir = -1;}n = n + 0.005 * dir;lines(g);break;case 1:if (length < 500) {length = length + 5;lines(g);} else {state = 2;}break;case 2:if (length > 100) {length = length - 5;lines(g);} else {state = 0;}break;case 3:int m = 1;if (length > 100) {length = length - 10;lines(g);for (Object gold : this.frame.objectList) {if(gold.flag){m = gold.m;gold.x = endX - gold.getWidth()/2;gold.y = endY;if(length <= 100){gold.x = -150;gold.y = -150;gold.flag = false;state = 0;}}}}try {Thread.sleep(m);} catch (InterruptedException e) {e.printStackTrace();}break;default:}}
14 多种金块
在Gold类创建GoldMini外部类和GoldPlus外部类
public class Gold extends Object{...}class GoldMini extends Gold{public GoldMini(){this.width = 36;this.height = 36;this.flag = false;this.m = 30;this.img = Toolkit.getDefaultToolkit().getImage("img/gold0.gif");}}class GoldPlus extends Gold{public GoldPlus(){this.x = (int)(Math.random()*650);this.width = 105;this.height = 105;this.flag = false;this.m = 120;this.img = Toolkit.getDefaultToolkit().getImage("img/gold2.gif");}}
在GameWin类随机生成多种金块
{for (int i = 0; i < 11; i++) {double random = Math.random();if (random <0.3 ) {objectList.add(new GoldMini());}else if(random < 0.7 ){objectList.add(new Gold());} else {objectList.add(new GoldPlus());}}for (int i = 0; i < 3; i++) {objectList.add(new Rock());}}
15 物体堆叠
在Objectl类添加方法
//获取矩形,用于检测判断物体重叠public Rectangle getRec(){return new Rectangle(x,y,width,height);}
在GameWin类修改生成金块代码
{//是否可以放置boolean ifPlace = true;for (int i = 0; i < 11; i++) {double random = Math.random();Gold gold;//存放当前生成的金块if (random <0.3 ) {gold = new GoldMini();}else if(random < 0.7 ){gold = new Gold();} else {gold = new GoldPlus();}for (Object obj : objectList) {if(gold.getRec().intersects(obj.getRec())){//不能放置,需要重新生成ifPlace = false;}}if(ifPlace){objectList.add(gold);}else {ifPlace = true; i--; }}for (int i = 0; i < 5; i++) {Rock rock = new Rock();for (Object obj : objectList) {if(rock.getRec().intersects(obj.getRec())){//不能放置,需要重新生成ifPlace = false;}}if(ifPlace){objectList.add(rock);}else {ifPlace = true; i--; }}}
16 红线完善
在Line类中修改
//线长最小值double min_length = 100;//线长最小值double max_length = 750;public void paintSelf(Graphics g) {logic();switch (state) {case 0:if (n < 0.1) {dir = 1;} else if (n > 0.9) {dir = -1;}n = n + 0.005 * dir;lines(g);break;case 1:if (length < max_length) {length = length + 5;lines(g);} else {state = 2;}break;case 2:if (length > min_length) {length = length - 5;lines(g);} else {state = 0;}break;case 3:int m = 1;if (length > min_length) {length = length - 10;lines(g);for (Object gold : this.frame.objectList) {if(gold.flag){m = gold.m;gold.x = endX - gold.getWidth()/2;gold.y = endY;if(length <= min_length){gold.x = -150;gold.y = -150;gold.flag = false;state = 0;}}}}...break;default:}}//绘制方法void lines(Graphics g) {endX = (int) (x + length * Math.cos(n * Math.PI));endY = (int) (y + length * Math.sin(n * Math.PI));g.setColor(Color.red);g.drawLine(x-1, y, endX-1, endY);g.drawLine(x, y, endX, endY);g.drawLine(x+1, y, endX+1, endY);g.drawImage(hook,endX-36,endY-2,null);}
17 积分设置
在Object类添加参数
//积分int count;
在Gold类设值
public Gold(){this.count = 4;...public GoldMini(){this.count = 2;...public GoldPlus(){this.count = 8;
在Rock类设值
public Rock(){this.count = 1;...
在Line类加分
public void paintSelf(Graphics g) {logic();switch (state) {...case 3:int m = 1;if (length > min_length) {length = length - 10;lines(g);for (Object gold : this.frame.objectList) {if(gold.flag){m = gold.m;gold.x = endX - gold.getWidth()/2;gold.y = endY;if(length <= min_length){gold.x = -150;gold.y = -150;gold.flag = false;state = 0;//加分Bg.count+=gold.count;}}}}...
在Bg类绘制
//总分static int count = 0;//绘制public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);g.drawImage(peo,310,50,null);drawWord(g,30,Color.black,"积分:" + count,30,150);}//绘制字符串public static void drawWord(Graphics g,int size,Color color,String str,int x,int y){g.setColor(color);g.setFont(new Font("仿宋", Font.BOLD,size));g.drawString(str,x,y);}
18 力量与爆破
在Bg类添加参数
//药水图片Image water = Toolkit.getDefaultToolkit().getImage("img/water.png");//药水数量static int waterNmu = 3;//药水状态 默认false 正在使用truestatic boolean waterFlag = false;//绘制public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);g.drawImage(peo,310,50,null);drawWord(g,30,Color.black,"积分:" + count,30,150);//药水组件g.drawImage(water,450,40,null);drawWord(g,30,Color.black,"*" + waterNmu,510,70);}
在Object添加参数
//类型 1金块 2石块int type;
在Gold类设值
public Gold(){this.type = 1;...
在Rock类设值
public Rock(){this.type = 1;...
在GameWin添加鼠标点击判断
public void launch(){...//鼠标监听事件this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);//左右摇摆,点击左键if(e.getButton() == 1 && line.state == 0){//鼠标左键点击line.state = 1;}//抓取返回,点击右键if(e.getButton() == 3 && line.state == 3){//鼠标右键点击if(Bg.waterNmu > 0){Bg.waterFlag = true;Bg.waterNmu--;}}}});
在Line类添加修改判断
public void paintSelf(Graphics g) {logic();switch (state) {...case 3:int m = 1;if (length > min_length) {length = length - 10;lines(g);for (Object gold : this.frame.objectList) {if(gold.flag){m = gold.m;gold.x = endX - gold.getWidth()/2;gold.y = endY;if(length <= min_length){gold.x = -150;gold.y = -150;gold.flag = false;state = 0;//加分Bg.count+=gold.count;Bg.waterFlag= false;}if(Bg.waterFlag){if(gold.type == 1){m=1;}if(gold.type == 2){gold.x = -150;gold.y = -150;gold.flag = false;Bg.waterFlag= false;state = 2;}}}}}
19 关卡设置
在Bg类添加参数
//关卡数static int level = 1;//目标得分int goal = level * 15;//绘制public void paintSelf(Graphics g){...//关卡数drawWord(g,20,Color.black,"第" + level + "关",30,60);//目标积分drawWord(g,30,Color.black,"目标:" + goal,30,110);}
在GameWin类添加判断
public void launch(){...while (true){repaint();nextLevel();try {Thread.sleep(10);} catch (InterruptedException e) {e.printStackTrace();}}}//下一关public void nextLevel(){if (Bg.count >= bg.goal) {Bg.level++;dispose();GameWin gameWin1 = new GameWin();gameWin1.launch();}}
20 游戏状态
在GameWin类添加状态
//0 未开始 1 运行中 2 商店 3 失败 4 胜利static int state;public void launch(){...//鼠标监听事件this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);switch (state){case 0:if(e.getButton() == 3){state = 1;}break;case 1://左右摇摆,点击左键if(e.getButton() == 1 && line.state == 0){//鼠标左键点击line.state = 1;}//抓取返回,点击右键if(e.getButton() == 3 && line.state == 3){//鼠标右键点击if(Bg.waterNmu > 0){Bg.waterFlag = true;Bg.waterNmu--;}}break;case 2:break;case 3:break;case 4:break;default:}}});...}@Overridepublic void paint(Graphics g) {offScreenImage = this.createImage(768,1000);Graphics graphics = offScreenImage.getGraphics();bg.paintSelf(graphics);if(state == 1){//绘制物体for (Object obj : objectList) {obj.paintSelf(graphics);}line.paintSelf(graphics);}g.drawImage(offScreenImage,0,0,null);}//下一关public void nextLevel(){if (state == 1) {if (Bg.count >= bg.goal) {Bg.level++;dispose();GameWin gameWin1 = new GameWin();gameWin1.launch();}}}
在Bg类添加判断绘制
//绘制public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);switch (GameWin.state){case 0:drawWord(g,80,Color.green,"准备开始",200,400);break;case 1:g.drawImage(peo,310,50,null);drawWord(g,30,Color.black,"积分:" + count,30,150);//药水组件g.drawImage(water,450,40,null);drawWord(g,30,Color.black,"*" + waterNmu,510,70);//关卡数drawWord(g,20,Color.black,"第" + level + "关",30,60);//目标积分drawWord(g,30,Color.black,"目标:" + goal,30,110);break;case 2:break;case 3:break;case 4:break;default:}}
21 计时效果
在Bg类添加参数
//开始时间long startTime;//结束时间long endTime;//绘制public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);switch (GameWin.state){case 0:drawWord(g,80,Color.green,"准备开始",200,400);break;case 1:g.drawImage(peo,310,50,null);drawWord(g,30,Color.black,"积分:" + count,30,150);//药水组件g.drawImage(water,450,40,null);drawWord(g,30,Color.black,"*" + waterNmu,510,70);//关卡数drawWord(g,20,Color.black,"第" + level + "关",30,60);//目标积分drawWord(g,30,Color.black,"目标:" + goal,30,110);//时间组件endTime = System.currentTimeMillis();long time = 20 - (endTime - startTime)/1000;drawWord(g,30,Color.black,"时间:" + (time > 0 ? time : 0) ,520,150);break;case 2:...}
在GameWin类初始值
public void launch(){...//鼠标监听事件this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);switch (state){case 0:if(e.getButton() == 3){state = 1;bg.startTime = System.currentTimeMillis();}break;...}
22 失败状态
在Bg添加方法
//绘制public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);switch (GameWin.state){...case 3:drawWord(g,80,Color.cyan,"失败",250,350);drawWord(g,80,Color.cyan,"目标:" + count,200,450);break;case 4:break;default:}}//true倒计时完成 false正在倒计时public boolean gameTime(){long time = (endTime - startTime)/1000;if(time > 20){return true;}return false;}
在GameWin添加判断
//下一关public void nextLevel(){if (state == 1 && bg.gameTime()) {if (Bg.count >= bg.goal) {Bg.level++;dispose();GameWin gameWin1 = new GameWin();gameWin1.launch();}else {state = 3;}}}
23 成功状态
在GameWin类添加
//下一关public void nextLevel() {if (state == 1 && bg.gameTime()) {if (Bg.count >= bg.goal) {if (Bg.level == 5) {state = 4;} else {Bg.level++;}dispose();GameWin gameWin1 = new GameWin();gameWin1.launch();} else {state = 3;}}}
在Bg类绘制
//绘制public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);switch (GameWin.state){..case 4:drawWord(g,80,Color.red,"成功",250,350);drawWord(g,80,Color.red,"目标:" + count,200,450);break;default:}}
24 游戏重置
在Bg类添加方法
//重置元素void reGame(){level = 1;goal = level *15;count = 0;waterNmu = 3;waterFlag = false;}
在Line类添加方法
//重置线元素void reGame() {n =0;length = 100;}
在GameWin类添加判断
public void launch() {...//鼠标监听事件this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);switch (state) {case 0:if (e.getButton() == 3) {state = 1;bg.startTime = System.currentTimeMillis();}break;case 1://左右摇摆,点击左键if (e.getButton() == 1 && line.state == 0) {//鼠标左键点击line.state = 1;}//抓取返回,点击右键if (e.getButton() == 3 && line.state == 3) {//鼠标右键点击if (Bg.waterNmu > 0) {Bg.waterFlag = true;Bg.waterNmu--;}}break;case 2:break;case 3:case 4:if(e.getButton() == 1){state = 0;bg.reGame();line.reGame();}break;default:}}});...}//下一关public void nextLevel() {if (state == 1 && bg.gameTime()) {if (Bg.count >= bg.goal) {if (Bg.level == 5) {state = 4;} else {Bg.level++;bg.startTime = System.currentTimeMillis();}} else {state = 3;}dispose();GameWin gameWin1 = new GameWin();gameWin1.launch();}}
25 商店购物
在Bg类添加绘制
//药水价格int price = (int)(Math.random()*10);//是否进入商店 false 不购买boolean shop = false;//绘制public void paintSelf(Graphics g){g.drawImage(bg1,0,0,null);g.drawImage(bg,0,200,null);switch (GameWin.state){。。。case 2:g.drawImage(water,300,400,null);drawWord(g,30,Color.black,"价格:" + price,300,500);drawWord(g,30,Color.black,"是否购买?",300,550);if(shop){count = count - price;waterNmu++;shop = false;GameWin.state = 1;startTime = System.currentTimeMillis();}break;case 3:...}
在GameWin类修改
public void launch() {...//鼠标监听事件this.addMouseListener(new MouseAdapter() {@Overridepublic void mouseClicked(MouseEvent e) {super.mouseClicked(e);switch (state) {..case 2:if(e.getButton() == 1){bg.shop = true;}if(e.getButton() == 3){state =1;bg.startTime = System.currentTimeMillis();}break;case 3:case 4:if(e.getButton() == 1){state = 0;bg.reGame();line.reGame();}break;default:}}});...}//下一关public void nextLevel() {if (state == 1 && bg.gameTime()) {if (Bg.count >= bg.goal) {if (Bg.level == 5) {state = 4;} else {state =2;Bg.level++;}} else {state = 3;}dispose();GameWin gameWin1 = new GameWin();gameWin1.launch();}}