過年沒事,一天時間寫了一個是男人就撐住20秒的Java版

來源:互聯網
上載者:User

每年過年都沒事,都得寫點小東西,今年是是男人就撐過20秒(20秒還是21秒來著,忘了) 可能有點小bug,再完善了.希望大家能支援,支援的就頂一下了
如果努努力也是可以壓縮在100行以內的,90行的俄羅斯方塊,100行的男人20秒

剩餘問題

1.標題

2.開始沒有splash

3.不能暫停

4.不能重新開始

5.速度慢

6.左右鍵有點衝突

7.結束統計

8.新的炸彈出現時向飛機當前位置附近發射,現在是向中心附近發射

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.