本文主要是介绍分享网上的一个小蜜蜂游戏,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
童年的欢乐,虽然游戏很简单,但已经实现了基本的移动,发炮,碰撞,声音等效果,如下图:
请尊重别人的劳动成果 转载请务必注明出处 - http://www.zuidaima.com/share/1550463409654784.htm
相关代码如下:
package zuidaima.Game;import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;import javax.swing.JApplet;class Cannonball {static int y = 560, score = 0;int temp = 240;ClassLoader classLoader = this.getClass().getClassLoader();public void paint(Graphics g, int x2) {int t = 0;if (y == 560) {temp = x2;}g.setColor(Color.red);g.fillOval(temp + 20, y, 10, 10);if (y < 560)y--;g.setColor(Color.LIGHT_GRAY);g.fillOval(temp + 20, y + 10, 10, 10);if (((temp + 20) % 40 == 0 && y == 70 && HoneyBee.a[0][(temp + 20) / 40 - 1] == 1)|| ((temp + 20) % 40 == 0 && y == 110 && HoneyBee.a[1][(temp + 20) / 40 - 1] == 1)|| ((temp + 20) % 40 == 0 && y == 150 && HoneyBee.a[2][(temp + 20) / 40 - 1] == 1)) {AudioClip au = JApplet.newAudioClip(classLoader.getResource("112.wav"));au.play();g.setColor(Color.LIGHT_GRAY);g.fillRect(temp + 20, y, 20, 30);if (y == 70) {t = 0;} else if (y == 110) {t = 1;} else if (y == 150) {t = 2;}HoneyBee.a[t][(temp + 20) / 40 - 1] = 0;score += 100;y = 560;}if (y == 0) {y = 560;}}
}public class HoneyBee extends Frame {static int x1 = 200;static int[][] a = { { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }, };ClassLoader classLoader = this.getClass().getClassLoader();public HoneyBee() {AudioClip au = JApplet.newAudioClip(classLoader.getResource("start.wav"));au.play();addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent e) {dispose();System.exit(0);}});addKeyListener(new KeyAdapter() {public void keyPressed(KeyEvent e) {int keycode = e.getKeyCode();if (keycode == KeyEvent.VK_LEFT) {x1 = x1 - 10;} else if (keycode == KeyEvent.VK_RIGHT) {x1 = x1 + 10;} else if (keycode == KeyEvent.VK_SPACE) {if (Cannonball.y == 560) {AudioClip au = JApplet.newAudioClip(classLoader.getResource("BONG.wav"));au.play();Cannonball.y = 559;} else {}}repaint();}});}public void paint(Graphics g) {int num;g.setColor(Color.BLUE);g.drawString("分数:" + Cannonball.score, 20, 50);g.fillOval(x1, 560, 50, 30);g.setColor(Color.BLACK);num = 0;for (int i = 0; i < 11; i++) {if (a[0][i] == 1)g.fillOval(num = num + 40, 70, 10, 10);elsenum = num + 40;}num = 0;for (int i = 0; i < 11; i++) {if (a[1][i] == 1)g.fillOval(num = num + 40, 110, 10, 10);elsenum = num + 40;}num = 0;for (int i = 0; i < 11; i++) {if (a[2][i] == 1)g.fillOval(num = num + 40, 150, 10, 10);elsenum = num + 40;}}public static void main(String[] args) {HoneyBee th = new HoneyBee();th.setBackground(Color.LIGHT_GRAY);th.setSize(500, 600);th.setTitle("小蜜蜂游戏");th.setVisible(true);Graphics g = th.getGraphics();Cannonball cb = new Cannonball();while (true) {try {Thread.sleep(4);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}cb.paint(g, x1);}}
}
这篇关于分享网上的一个小蜜蜂游戏的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!