过年没事,一天时间写了一个是男人就撑住20秒的Java版

每年过年都没事,都得写点小东西,今年是是男人就撑过20秒(20秒还是21秒来着,忘了) 可能有点小bug,再完善了.希望大家能支持,支持的就顶一下了
如果努努力也是可以压缩在100行以内的,90行的俄罗斯方块,100行的男人20秒
剩余问题
1.标题
2.开始没有splash
3.不能暂停
4.不能重新开始
5.速度慢
6.左右键有点冲突
7.结束统计
8.新的bomb出现时向飞机当前位置附近发射,现在是向中心附近发射

过年没事,一天时间写了一个是男人就撑住20秒的Java版





过年没事,一天时间写了一个是男人就撑住20秒的Java版







import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferStrategy;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

/**
 * 转载请注明出处 http://mid.iteye.com 2012-01-24
 * 
 * @author http://mid.iteye.com
 * 
 */
public class Fly21S extends JFrame implements Runnable, KeyListener {
	private int isPlaying = 0;
	private int bg[][] = new int[100][4];// x,y,color,speed
	private int bgMove[] = new int[100];
	private Color[] colors = new Color[] { Color.BLUE, Color.CYAN, Color.GREEN, Color.ORANGE, Color.RED, Color.WHITE, Color.PINK,
			Color.YELLOW };
	private int movePlanDir[] = new int[2];
	// private long timeOffSet = new Date().getTime();
	// private long paintBlank = 1;
	private Image bomb = new ImageIcon("D:\\Java\\eclipse\\workspace\\21sFly\\src\\bomb.gif").getImage();
	private Image over = new ImageIcon("D:\\Java\\eclipse\\workspace\\21sFly\\src\\over.gif").getImage();
	private Image[] planImages = new Image[] { new ImageIcon("D:\\Java\\eclipse\\workspace\\21sFly\\src\\plan1.gif").getImage(),
			new ImageIcon("D:\\Java\\eclipse\\workspace\\21sFly\\src\\plan3.gif").getImage(),
			new ImageIcon("D:\\Java\\eclipse\\workspace\\21sFly\\src\\plan5.gif").getImage(),
			new ImageIcon("D:\\Java\\eclipse\\workspace\\21sFly\\src\\crash.gif").getImage() };
	private int[] planPos = new int[] { 150, 230 };// Plan's current position
	private double bombPos[][] = new double[50][4];// bomb's position,x direct, y direct.

	public Fly21S() {
		setSize(300, 300);
		setVisible(true);
		createBufferStrategy(2);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		addKeyListener(this);
		this.setResizable(false);
		// init bg star
		for (int i = 0; i < bg.length; i++) {
			bg[i] = new int[] { getRandomLessThen(300), getRandomLessThen(300), getRandomLessThen(7), getRandomLessThen(2) + 1 };
			bgMove[i] = 1;
		}
		// init bomb
		// up and down
		for (int i = 0; i < 25; i++) {
			int xPos = getRandomLessThen(300);
			int yPos = (int) (getPositiveOrNegative() == -1 ? -1 * getRandomLessThen(50) : (300 + getRandomLessThen(50)));
			int xDir = 150 - xPos + (getPositiveOrNegative() * getRandomLessThen(2));
			int yDir = 150 - yPos + (getPositiveOrNegative() * getRandomLessThen(2));
			bombPos[i] = new double[] { xPos, yPos, xDir, yDir };//
		}
		// left right
		for (int i = 0; i < 25; i++) {
			int xPos = (int) (getPositiveOrNegative() == -1 ? -1 * getRandomLessThen(50) : (300 + getRandomLessThen(50)));
			int yPos = getRandomLessThen(300);
			int xDir = 150 - xPos + (getPositiveOrNegative() * getRandomLessThen(2));
			int yDir = 150 - yPos + (getPositiveOrNegative() * getRandomLessThen(2));
			bombPos[25 + i] = new double[] { xPos, yPos, xDir, yDir };//
		}
	}

	private int getPositiveOrNegative() {
		return (Math.round(Math.random()) - 1) == 0 ? 1 : -1;
	}

	private int getRandomLessThen(int num) {
		return (int) Math.round(Math.random() * num);
	}

	public void paint(Graphics g) {
		BufferStrategy bs = this.getBufferStrategy();
		if (bs == null)
			return;
		Graphics tg = bs.getDrawGraphics();
		tg.fillRect(0, 20, 300, 300);
		// paint bg
		for (int i = 0; i < bg.length; i++) {
			tg.setColor(colors[bg[i][2]]);
			tg.drawLine(bg[i][0], bg[i][1], bg[i][0], bg[i][1]);
		}
		// paint bomb
		for (int i = 0; i < bombPos.length; i++) {
			tg.drawImage(bomb, (int) bombPos[i][0], (int) bombPos[i][1], null);
		}
		if (isPlaying == 0) {
			if (movePlanDir[0] == -1)
				tg.drawImage(planImages[0], planPos[0], planPos[1], null);
			else if (movePlanDir[0] == 1)
				tg.drawImage(planImages[2], planPos[0], planPos[1], null);
			else
				tg.drawImage(planImages[1], planPos[0], planPos[1], null);
		} else {
			tg.drawImage(planImages[3], planPos[0], planPos[1], null);
			tg.drawImage(over, 90, 60, null);
			// tg.drawChars(new char[] { 'H' }, 0, 1, 150, 150);
		}
		this.getBufferStrategy().show();
	}

	public static void main(String[] args) {
		new Thread(new Fly21S()).start();
	}

	public void run() {
		while (isPlaying == 0)
			try {
				for (int i = 0; i < bg.length; i++) {
					if (bgMove[i] % bg[i][3] == 0) {
						bg[i][1] = bg[i][1] + 1;
						if (bg[i][1] > 300) {
							bg[i][0] = (int) Math.round(Math.random() * 300);
							bg[i][1] = 0;
							bg[i][2] = (int) Math.round(Math.random() * 7);
						}
						bgMove[i] = 1;
					} else {
						bgMove[i]++;
					}
				}
				// move bomb
				for (int i = 0; i < bombPos.length; i++) {
					double speedXY = Math.sqrt(bombPos[i][2] * bombPos[i][2] + bombPos[i][3] * bombPos[i][3]);
					double xRate = bombPos[i][2] / speedXY;
					double yRate = bombPos[i][3] / speedXY;
					bombPos[i][0] = bombPos[i][0] + xRate * 1.3;// x to
					bombPos[i][1] = bombPos[i][1] + yRate * 1.3;// y to
					// new bomb
					if (bombPos[i][1] > 300 || bombPos[i][1] < 0) {
						int xPos = getRandomLessThen(300);
						int yPos = (int) (getPositiveOrNegative() == -1 ? -1 * getRandomLessThen(50) : (300 + getRandomLessThen(50)));
						int xDir = 150 - xPos + (getPositiveOrNegative() * getRandomLessThen(2));
						int yDir = 150 - yPos + (getPositiveOrNegative() * getRandomLessThen(2));
						bombPos[i] = new double[] { xPos, yPos, xDir, yDir };//
					} else if (bombPos[i][0] > 300 || bombPos[i][0] < 0) {
						int xPos = (int) (getPositiveOrNegative() == -1 ? -1 * getRandomLessThen(50) : (300 + getRandomLessThen(50)));
						int yPos = getRandomLessThen(300);
						int xDir = 150 - xPos + (getPositiveOrNegative() * getRandomLessThen(2));
						int yDir = 150 - yPos + (getPositiveOrNegative() * getRandomLessThen(2));
						bombPos[i] = new double[] { xPos, yPos, xDir, yDir };//
					}
					// check impact
					if (bombPos[i][0] > planPos[0] && bombPos[i][0] < planPos[0] + 10 && bombPos[i][1] > planPos[1]
							&& bombPos[i][1] < planPos[1] + 10) {
						// crash
						isPlaying = 1;
						System.out.println("Crash~");
					}
				}
				planPos[0] = planPos[0] + movePlanDir[0];
				planPos[1] = planPos[1] + movePlanDir[1];
				repaint();
				Thread.sleep(33);
			} catch (InterruptedException e) {
			}
	}

	public void keyPressed(KeyEvent e) {// 38-上 40-下 37-左 39-右
		// movePlanDir left right up down
		if ((e.getKeyCode() == 65 || e.getKeyCode() == 37)) {// left
			movePlanDir[0] = -1;
		} else if ((e.getKeyCode() == 68 || e.getKeyCode() == 39)) {// right
			movePlanDir[0] = 1;
		} else if ((e.getKeyCode() == 87 || e.getKeyCode() == 38)) {// up
			movePlanDir[1] = -1;
		} else if ((e.getKeyCode() == 83 || e.getKeyCode() == 40)) {//
			movePlanDir[1] = 1;
		}
		repaint();
	}

	public void keyReleased(KeyEvent e) {
		if ((e.getKeyCode() == 65 || e.getKeyCode() == 37)) {// left
			movePlanDir[0] = 0;
		} else if ((e.getKeyCode() == 68 || e.getKeyCode() == 39)) {// right
			movePlanDir[0] = 0;
		} else if ((e.getKeyCode() == 87 || e.getKeyCode() == 38)) {// up
			movePlanDir[1] = 0;
		} else if ((e.getKeyCode() == 83 || e.getKeyCode() == 40)) {//
			movePlanDir[1] = 0;
		}
		repaint();
	}

	public void keyTyped(KeyEvent e) {
	}
}


图片资源见附件.. 横向滚动飞机时,有个中间状态,半侧身,截图是在是不好截,暂时就一步到位,转过去了..  @_@

你可能感兴趣的:(java版,21秒,是男人,java游戏)