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

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


请尊重别人的劳动成果 转载请务必注明出处 - 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);
			else
				num = num + 40;
		}
		num = 0;
		for (int i = 0; i < 11; i++) {
			if (a[1][i] == 1)
				g.fillOval(num = num + 40, 110, 10, 10);
			else
				num = num + 40;
		}
		num = 0;
		for (int i = 0; i < 11; i++) {
			if (a[2][i] == 1)
				g.fillOval(num = num + 40, 150, 10, 10);
			else
				num = 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 block
				e.printStackTrace();
			}
			cb.paint(g, x1);
		}
	}
}

	    			


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值