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

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

[img]http://dl.iteye.com/upload/attachment/0062/3665/9a5815df-c7c4-30ae-98b9-be4c0e52bbd0.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0062/3653/28239eb8-4abb-3f97-ab32-60d48878dd8e.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0062/3655/50a582d2-1c03-3653-b6ad-8e097433e9bb.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0062/3657/f88e74f6-3bcd-36f5-837c-f2b2fd70b21c.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0062/3659/438b31d1-6396-3cd6-b5fa-0ce785070f3f.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0062/3661/0998c4c6-46c8-365c-a126-df2a1f43799d.gif[/img]

[img]http://dl.iteye.com/upload/attachment/0062/3663/d7bca383-fa61-3a0f-882a-beeb20aed519.gif[/img]


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) {
}
}


图片资源见附件.. :lol: 横向滚动飞机时,有个中间状态,半侧身,截图是在是不好截,暂时就一步到位,转过去了.. @_@
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值