当前位置:   article > 正文

分享网上的一个小蜜蜂游戏

java游戏小蜜蜂

童年的欢乐,虽然游戏很简单,但已经实现了基本的移动,发炮,碰撞,声音等效果,如下图:

 

 

 

请尊重别人的劳动成果 转载请务必注明出处 - http://www.zuidaima.com/share/1550463409654784.htm

相关代码如下:

  1. package zuidaima.Game;
  2. import java.applet.AudioClip;
  3. import java.awt.Color;
  4. import java.awt.Frame;
  5. import java.awt.Graphics;
  6. import java.awt.event.KeyAdapter;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.WindowAdapter;
  9. import java.awt.event.WindowEvent;
  10. import javax.swing.JApplet;
  11. class Cannonball {
  12. static int y = 560, score = 0;
  13. int temp = 240;
  14. ClassLoader classLoader = this.getClass().getClassLoader();
  15. public void paint(Graphics g, int x2) {
  16. int t = 0;
  17. if (y == 560) {
  18. temp = x2;
  19. }
  20. g.setColor(Color.red);
  21. g.fillOval(temp + 20, y, 10, 10);
  22. if (y < 560)
  23. y--;
  24. g.setColor(Color.LIGHT_GRAY);
  25. g.fillOval(temp + 20, y + 10, 10, 10);
  26. if (((temp + 20) % 40 == 0 && y == 70 && HoneyBee.a[0][(temp + 20) / 40 - 1] == 1)
  27. || ((temp + 20) % 40 == 0 && y == 110 && HoneyBee.a[1][(temp + 20) / 40 - 1] == 1)
  28. || ((temp + 20) % 40 == 0 && y == 150 && HoneyBee.a[2][(temp + 20) / 40 - 1] == 1)) {
  29. AudioClip au = JApplet.newAudioClip(classLoader
  30. .getResource("112.wav"));
  31. au.play();
  32. g.setColor(Color.LIGHT_GRAY);
  33. g.fillRect(temp + 20, y, 20, 30);
  34. if (y == 70) {
  35. t = 0;
  36. } else if (y == 110) {
  37. t = 1;
  38. } else if (y == 150) {
  39. t = 2;
  40. }
  41. HoneyBee.a[t][(temp + 20) / 40 - 1] = 0;
  42. score += 100;
  43. y = 560;
  44. }
  45. if (y == 0) {
  46. y = 560;
  47. }
  48. }
  49. }
  50. public class HoneyBee extends Frame {
  51. static int x1 = 200;
  52. static int[][] a = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  53. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
  54. { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, };
  55. ClassLoader classLoader = this.getClass().getClassLoader();
  56. public HoneyBee() {
  57. AudioClip au = JApplet.newAudioClip(classLoader
  58. .getResource("start.wav"));
  59. au.play();
  60. addWindowListener(new WindowAdapter() {
  61. public void windowClosing(WindowEvent e) {
  62. dispose();
  63. System.exit(0);
  64. }
  65. });
  66. addKeyListener(new KeyAdapter() {
  67. public void keyPressed(KeyEvent e) {
  68. int keycode = e.getKeyCode();
  69. if (keycode == KeyEvent.VK_LEFT) {
  70. x1 = x1 - 10;
  71. } else if (keycode == KeyEvent.VK_RIGHT) {
  72. x1 = x1 + 10;
  73. } else if (keycode == KeyEvent.VK_SPACE) {
  74. if (Cannonball.y == 560) {
  75. AudioClip au = JApplet.newAudioClip(classLoader
  76. .getResource("BONG.wav"));
  77. au.play();
  78. Cannonball.y = 559;
  79. } else {
  80. }
  81. }
  82. repaint();
  83. }
  84. });
  85. }
  86. public void paint(Graphics g) {
  87. int num;
  88. g.setColor(Color.BLUE);
  89. g.drawString("分数:" + Cannonball.score, 20, 50);
  90. g.fillOval(x1, 560, 50, 30);
  91. g.setColor(Color.BLACK);
  92. num = 0;
  93. for (int i = 0; i < 11; i++) {
  94. if (a[0][i] == 1)
  95. g.fillOval(num = num + 40, 70, 10, 10);
  96. else
  97. num = num + 40;
  98. }
  99. num = 0;
  100. for (int i = 0; i < 11; i++) {
  101. if (a[1][i] == 1)
  102. g.fillOval(num = num + 40, 110, 10, 10);
  103. else
  104. num = num + 40;
  105. }
  106. num = 0;
  107. for (int i = 0; i < 11; i++) {
  108. if (a[2][i] == 1)
  109. g.fillOval(num = num + 40, 150, 10, 10);
  110. else
  111. num = num + 40;
  112. }
  113. }
  114. public static void main(String[] args) {
  115. HoneyBee th = new HoneyBee();
  116. th.setBackground(Color.LIGHT_GRAY);
  117. th.setSize(500, 600);
  118. th.setTitle("小蜜蜂游戏");
  119. th.setVisible(true);
  120. Graphics g = th.getGraphics();
  121. Cannonball cb = new Cannonball();
  122. while (true) {
  123. try {
  124. Thread.sleep(4);
  125. } catch (InterruptedException e) {
  126. // TODO Auto-generated catch block
  127. e.printStackTrace();
  128. }
  129. cb.paint(g, x1);
  130. }
  131. }
  132. }

 

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

闽ICP备14008679号