童年的欢乐,虽然游戏很简单,但已经实现了基本的移动,发炮,碰撞,声音等效果,如下图:
请尊重别人的劳动成果 转载请务必注明出处 - 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);
- }
- }
- }
-