每年过年都没事,都得写点小东西,今年是是男人就撑过20秒(20秒还是21秒来着,忘了)
可能有点小bug,再完善了.希望大家能支持,支持的就顶一下了
如果努努力也是可以压缩在100行以内的,90行的俄罗斯方块,100行的男人20秒
![](https://i-blog.csdnimg.cn/blog_migrate/68e99d109ecf83acf10e6bdd875cef42.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/23cd56db85d92c2694b213531b599330.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/23cd56db85d92c2694b213531b599330.gif)
如果努努力也是可以压缩在100行以内的,90行的俄罗斯方块,100行的男人20秒
剩余问题
1.标题
2.开始没有splash
3.不能暂停
4.不能重新开始
5.速度慢
6.左右键有点冲突
7.结束统计
8.新的炸弹出现时向飞机当前位置附近发射,现在是向中心附近发射
![](http://dl.iteye.com/upload/attachment/0062/3665/9a5815df-c7c4-30ae-98b9-be4c0e52bbd0.gif)
![](http://dl.iteye.com/upload/attachment/0062/3653/28239eb8-4abb-3f97-ab32-60d48878dd8e.gif)
![](http://dl.iteye.com/upload/attachment/0062/3655/50a582d2-1c03-3653-b6ad-8e097433e9bb.gif)
![](http://dl.iteye.com/upload/attachment/0062/3657/f88e74f6-3bcd-36f5-837c-f2b2fd70b21c.gif)
![](http://dl.iteye.com/upload/attachment/0062/3659/438b31d1-6396-3cd6-b5fa-0ce785070f3f.gif)
![](http://dl.iteye.com/upload/attachment/0062/3661/0998c4c6-46c8-365c-a126-df2a1f43799d.gif)
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) {
}
}