当前位置:   article > 正文

JAVA小游戏黄金矿工_黄金矿工java课设报告

黄金矿工java课设报告

游戏需要的图片在尾部进行获取。

运行结果如下:

游戏测试类,主要负责调用,进入,绘画 

  1. package game;
  2. import javax.swing.*;
  3. import java.awt.*;
  4. import java.awt.event.MouseAdapter;
  5. import java.awt.event.MouseEvent;
  6. import java.util.ArrayList;
  7. import java.util.List;
  8. public class GameWin extends JFrame {
  9. //0未开始 1 运行中 2 商店 3 失败 4 胜利
  10. static int state;
  11. //存储金块,石块
  12. List<Object> objectList = new ArrayList<>();
  13. Bg bg = new Bg();
  14. //引入线类
  15. line line = new line(this);
  16. //引入金块类
  17. //Gold gold=new Gold();
  18. {
  19. //是否可以放置
  20. boolean isPlace = true;
  21. for (int i = 0; i < 11; i++) {
  22. double random = Math.random();
  23. Gold gold;//存放当前生成的金块
  24. if (random < 0.3) {
  25. gold = new GoldMini();
  26. } else if (random < 0.7) {
  27. gold = new Gold();
  28. } else {
  29. gold = new GoldPlus();
  30. }
  31. for (Object obj : objectList) {
  32. if (gold.getRec().intersects(obj.getRec())) {
  33. //不放置,需要重新生成
  34. isPlace = false;
  35. }
  36. }
  37. if (isPlace) {
  38. objectList.add(gold);
  39. } else {
  40. isPlace = true;
  41. i--;
  42. }
  43. }
  44. for (int i = 0; i < 5; i++) {
  45. Rock rock = new Rock();
  46. for (Object obj : objectList) {
  47. if (rock.getRec().intersects(obj.getRec())) {
  48. isPlace = false;
  49. }
  50. }
  51. if (isPlace) {
  52. objectList.add(rock);
  53. } else {
  54. isPlace = true;
  55. i--;
  56. }
  57. }
  58. }
  59. //引入新图片类
  60. Image offScreenImage;
  61. void launch() {
  62. //设置窗口是否可见
  63. this.setVisible(true);
  64. //设置窗口大小
  65. this.setSize(768, 1000);
  66. //设置窗口位置
  67. this.setLocationRelativeTo(null);
  68. //设置窗口标题
  69. this.setTitle("黄金矿工");
  70. //设置关闭窗口的方法
  71. setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  72. //创建鼠标点击事件
  73. addMouseListener(new MouseAdapter() {
  74. @Override
  75. public void mouseClicked(MouseEvent e) {
  76. super.mouseClicked(e);
  77. switch (state) {
  78. case 0:
  79. if (e.getButton() == 3) {
  80. state = 1;
  81. bg.startTime = System.currentTimeMillis();
  82. }
  83. break;
  84. case 1:
  85. //左键编码为1
  86. //左右摇摆点击左键
  87. if (e.getButton() == 1 && line.state == 0) {
  88. line.state = 1;
  89. }
  90. //抓取返回,点击右键
  91. if (e.getButton() == 3 && line.state == 3 && Bg.waterNum > 0) {
  92. Bg.waterFlag = true;
  93. Bg.waterNum--;
  94. }
  95. break;
  96. case 2:
  97. if (e.getButton() == 1) {
  98. bg.shop = true;
  99. }
  100. if (e.getButton() == 3) {
  101. state = 1;
  102. bg.startTime = System.currentTimeMillis();
  103. }
  104. break;
  105. case 3:
  106. case 4:
  107. if (e.getButton() == 1) {
  108. state = 0;
  109. bg.reGame();
  110. line.reGame();
  111. }
  112. break;
  113. default:
  114. break;
  115. }
  116. }
  117. });
  118. while (true) {
  119. repaint();
  120. nextlevel();
  121. //设置延迟
  122. try {
  123. Thread.sleep(10);
  124. } catch (InterruptedException e) {
  125. e.printStackTrace();
  126. }
  127. }
  128. }
  129. //下一关
  130. public void nextlevel() {
  131. if (bg.gameTime() && state == 1) {
  132. if (Bg.count >= bg.goal) {
  133. if (Bg.level == 5) {
  134. state = 4;
  135. } else {
  136. state = 2;
  137. Bg.level++;
  138. }
  139. } else {
  140. state = 3;
  141. }
  142. dispose();
  143. GameWin gameWin = new GameWin();
  144. gameWin.launch();
  145. }
  146. }
  147. @Override
  148. public void paint(Graphics g) {
  149. //解决图片闪动问题
  150. offScreenImage = this.createImage(768, 1000);
  151. Graphics gImage = offScreenImage.getGraphics();
  152. bg.paintSelf(gImage);
  153. if (state == 1) {
  154. //绘制物体
  155. for (Object obj : objectList) {
  156. obj.paintSelf(gImage);
  157. }
  158. line.paintSelf(gImage);
  159. }
  160. //gold.paintSelf(gImage);
  161. g.drawImage(offScreenImage, 0, 0, null);
  162. }
  163. public static void main(String[] args) {
  164. //调用上面类
  165. GameWin gameWin = new GameWin();
  166. //调用类中方法
  167. gameWin.launch();
  168. }
  169. }

游戏实体类,父类object,该类主用于生成金块于石块的参数,然后以继承的方式去设置对应值

  1. package game;
  2. import java.awt.*;
  3. public class Object {
  4. //坐标
  5. int x;
  6. int y;
  7. //宽高
  8. int width;
  9. int height;
  10. //图片
  11. Image img;
  12. //标记,是否可移动
  13. boolean flag;
  14. //质量
  15. int m;
  16. //积分
  17. int count;
  18. //类型 1金块 2石块
  19. int type;
  20. void paintSelf(Graphics g){
  21. g.drawImage(img,x,y,null);
  22. }
  23. public int getWidth() {
  24. return width;
  25. }
  26. //获取矩形
  27. public Rectangle getRec(){
  28. return new Rectangle(x,y,width,height);
  29. }
  30. }

游戏实体类,金块类,该类继承object类,来设置金块的大小位置等

  1. package game;
  2. import java.awt.*;
  3. /*金块类*/
  4. public class Gold extends Object{
  5. /*普通金块*/
  6. Gold(){
  7. this.x=(int)(Math.random()*700);
  8. this.y=(int)(Math.random()*550+300);
  9. this.width=52;
  10. this.height=52;
  11. this.flag=false;
  12. this.m=30;
  13. this.count=4;
  14. this.type=1;
  15. this.img= Toolkit.getDefaultToolkit().getImage("imgs/gold1.gif");
  16. }
  17. }
  18. /*迷你金块*/
  19. class GoldMini extends Gold{
  20. GoldMini(){
  21. this.width=36;
  22. this.height=36;
  23. this.m=15;
  24. this.count=2;
  25. this.img=Toolkit.getDefaultToolkit().getImage("imgs/gold0.gif");
  26. }
  27. }
  28. /*大号金块*/
  29. class GoldPlus extends Gold{
  30. GoldPlus(){
  31. this.x=(int)(Math.random()*650);
  32. this.width=105;
  33. this.height=105;
  34. this.m=60;
  35. this.count=8;
  36. this.img=Toolkit.getDefaultToolkit().getImage("imgs/gold2.gif");
  37. }
  38. }

游戏实体类,石块类,该类继承object类,来设置金块的大小位置等

  1. package game;
  2. import java.awt.*;
  3. public class Rock extends Object{
  4. /*石块类*/
  5. Rock(){
  6. this.x=(int)(Math.random()*700);
  7. this.y=(int)(Math.random()*550+300);
  8. this.width=71;
  9. this.height=71;
  10. this.flag=false;
  11. this.m=50;
  12. this.count=1;
  13. this.type=2;
  14. this.img= Toolkit.getDefaultToolkit().getImage("imgs/rock1.png");
  15. }
  16. }

游戏实体类线类,该类设置游戏的线条长度以及判断是否勾到

  1. package game;
  2. import java.awt.*;
  3. public class line {
  4. //起点坐标
  5. int x=380;
  6. int y=180;
  7. //终点坐标
  8. int endx=500;
  9. int endy=500;
  10. //线长
  11. double length = 100;
  12. //线长最小值
  13. double MIN_length = 100;
  14. //线长最大值
  15. double MAX_length = 750;
  16. double n = 0;
  17. //方向
  18. int dir = 1;
  19. //状态 0 摇摆 1 抓取 2 收回 3 抓取返回
  20. int state;
  21. //钩爪图片
  22. Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");
  23. GameWin frame;
  24. line(GameWin frame){this.frame=frame;}
  25. //碰撞检测,检测物体是否被抓取
  26. void logic(){
  27. for(Object obj:this.frame.objectList){
  28. if(endx>obj.x && endx<obj.x+obj.width
  29. && endy>obj.y && endy< obj.y+obj.height){
  30. state=3;
  31. obj.flag=true;
  32. }
  33. }
  34. }
  35. //绘制方法
  36. void lines(Graphics g){
  37. endx = (int) (x +length*Math.cos(n*Math.PI));
  38. endy = (int) (y +length*Math.sin(n*Math.PI));
  39. //设置线的颜色
  40. g.setColor(Color.red);
  41. //绘制线
  42. g.drawLine(x-1,y,endx-1,endy);
  43. g.drawLine(x,y,endx,endy);
  44. g.drawLine(x+1,y,endx+1,endy);
  45. //绘制钩爪
  46. g.drawImage(hook,endx-36,endy-2,null);
  47. }
  48. void paintSelf(Graphics g){
  49. logic();
  50. switch (state){
  51. case 0:
  52. //根据角度设置方向
  53. if(n<0.1){ dir = 1;}
  54. else if (n>0.9){dir = -1;}
  55. //设置线的旋转
  56. n=n+0.005*dir;
  57. lines(g);
  58. break;
  59. case 1:
  60. //伸长
  61. if(length<MAX_length){
  62. length=length+5;
  63. lines(g);
  64. }else {state=2;}
  65. break;
  66. case 2:
  67. //收回
  68. if(length>MIN_length){
  69. length=length-5;
  70. lines(g);
  71. }else {
  72. state=0;
  73. }
  74. break;
  75. case 3:
  76. int m=1;
  77. if(length>MIN_length){
  78. length=length-5;
  79. lines(g);
  80. for(Object obj:this.frame.objectList){
  81. if(obj.flag){
  82. m=obj.m;
  83. obj.x=endx-obj.getWidth()/2;
  84. obj.y=endy;
  85. if(length<=MIN_length){
  86. obj.x=-150;
  87. obj.y=-150;
  88. obj.flag=false;
  89. Bg.waterFlag=false;
  90. //加分
  91. Bg.count+=obj.count;
  92. state=0;
  93. }
  94. if (Bg.waterFlag){
  95. if (obj.type==1){
  96. m=1;
  97. }
  98. if (obj.type==2){
  99. obj.x=-150;
  100. obj.y=-150;
  101. obj.flag=false;
  102. Bg.waterFlag=false;
  103. state=2;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. try {
  110. Thread.sleep(m);
  111. } catch (InterruptedException e) {
  112. e.printStackTrace();
  113. }
  114. break;
  115. default:
  116. }
  117. }
  118. //重置线
  119. void reGame(){
  120. n=0;
  121. length=100;
  122. }
  123. }

游戏实体类背景类,该类用于设置背景,设置关卡数,设置过关得分项,以及药水等道具

  1. package game;
  2. import java.awt.*;
  3. public class Bg {
  4. //管卡数
  5. static int level=1;
  6. //目标得分
  7. int goal = level*15;
  8. //总分
  9. static int count=0;
  10. //药水数量
  11. static int waterNum=3;
  12. //药水状态,默认为F,T标识正在使用
  13. static boolean waterFlag = false;
  14. //开始时间
  15. long startTime;
  16. //结束时间
  17. long endTime;
  18. //药水价格
  19. int price = (int) (Math.random()*10);
  20. //是否进入商店 f 不购买
  21. boolean shop=false;
  22. //载入药水图片
  23. Image water = Toolkit.getDefaultToolkit().getImage("imgs/water.png");
  24. //设置背景图片
  25. Image bg = Toolkit.getDefaultToolkit().getImage("imgs/bg.jpg");
  26. Image bg1 = Toolkit.getDefaultToolkit().getImage("imgs/bg1.jpg");
  27. Image peo = Toolkit.getDefaultToolkit().getImage("imgs/peo.png");
  28. //传入画笔元素
  29. void paintSelf(Graphics g){
  30. g.drawImage(bg1,0,0,null);
  31. g.drawImage(bg,0,200,null);
  32. switch (GameWin.state){
  33. case 0:
  34. drawWord(g,20,Color.green,"准备开始(右键开始、左键抓取,右键快速收回)",180,400);
  35. break;
  36. case 1:
  37. g.drawImage(peo,310,50,null);
  38. //调用传参
  39. drawWord(g,30,Color.black,"积分:"+count,30,150);
  40. //绘制药水
  41. g.drawImage(water,450,40,null);
  42. drawWord(g,30,Color.black,"*"+waterNum,510,70);
  43. //关卡数
  44. drawWord(g,20,Color.black,"第"+level+"关",30,60);
  45. //目标积分
  46. drawWord(g,30,Color.black,"目标积分"+goal,30,110);
  47. //时间组件
  48. endTime = System.currentTimeMillis();
  49. long tim = 20-(endTime-startTime)/1000;
  50. drawWord(g,30,Color.black,"时间"+(tim>0?tim:0),520,150);
  51. break;
  52. case 2:
  53. g.drawImage(water,300,400,null);
  54. drawWord(g,30,Color.black,"价格:"+price,300,500);
  55. drawWord(g,20,Color.black,"是否购买?(左键购买)",300,550);
  56. if (shop){
  57. count=count-price;
  58. waterNum++;
  59. shop=false;
  60. GameWin.state=1;
  61. startTime=System.currentTimeMillis();
  62. }
  63. break;
  64. case 3:
  65. drawWord(g,40,Color.cyan,"失败(左键准备)",250,350);
  66. drawWord(g,80,Color.cyan,"积分:"+count,200,450);
  67. break;
  68. case 4:
  69. drawWord(g,40,Color.RED,"成功(左键准备)",250,350);
  70. drawWord(g,80,Color.RED,"积分:"+count,200,450);
  71. break;
  72. default:
  73. break;
  74. }
  75. }
  76. //t倒计时完成 f正在倒计时
  77. boolean gameTime(){
  78. long tim = (endTime-startTime)/1000;
  79. if (tim>20){
  80. return true;
  81. }
  82. return false;
  83. }
  84. //重置元素
  85. void reGame(){
  86. //管卡数
  87. level=1;
  88. //目标得分
  89. goal = level*15;
  90. //总分
  91. count=0;
  92. //药水数量
  93. waterNum=3;
  94. //药水状态,默认为F,T标识正在使用
  95. waterFlag = false;
  96. }
  97. //绘制字符串
  98. public static void drawWord(Graphics g,int size,Color color,String str,int x,int y){
  99. g.setColor(color);
  100. g.setFont(new Font("仿宋",Font.BOLD,size));
  101. g.drawString(str,x,y);
  102. }}

 游戏需要的场景图片

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/127468
推荐阅读
相关标签
  

闽ICP备14008679号