It's okay for the Chinese New Year. Once written in a day, a man will hold the Java version for 20 seconds.

Source: Internet
Author: User

Every year, the Chinese New Year is okay. I have to write something small. This year, if it was a man who spent 20 seconds (20 seconds or 21 seconds, I forgot), it may be a little bug. I hope you can support it.
If you try hard, it can also be compressed within 100 rows, 90 rows of Tetris, 100 rows of men 20 seconds

Remaining Problems

1. Title

2. No splash at the beginning

3. Do not pause

4. Cannot start again

5. Slow speed

6. There is a conflict between the left and right keys.

7. End statistics

8. When a new bomb appeared, it was fired near the current position of the plane. Now it is fired near the center.

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;/*** reprinted please indicate the source 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 \ e Clipse \ 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); setdefaclocloseopera Tion (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 * getrandomlesstative (50): (300 + getrandomlessthen (50); int xdir = 150-xpos + (getpositiveornegative () * getrandomlessthen (2 )); int ydir = 150-ypos + (getpositiveornegative () * getrandomlesstative (2); bombpos [I] = new double [] {xpos, ypos, xdir, ydir }; /// left right for (INT I = 0; I <25; I ++) {int xpos = (INT) (getpositiveornegative () =-1? -1 * getrandomlesstative (50): (300 + getrandomlessthen (50); int ypos = getrandomlessthen (300); int xdir = 150-xpos + (getpositiveornegative () * getrandomlessthen (2); int ydir = 150-ypos + (getpositiveornegative () * getrandomlesstative (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 {T G. 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]/s Peedxy; 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 * getrandomlesstative (50): (300 + getrandomlessthen (50); int xdir = 150-xpos + (getpositiveornegative () * getrandomlessthen (2 )); int ydir = 150-ypos + (getpositiveornegative () * getrandomlesstative (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 * getrandomlesstative (50): (300 + getrandomlessthen (50); int ypos = getrandomlessthen (300); int xdir = 150-xpos + (getpositiveornegative () * getrandomlessthen (2); int ydir = 150-ypos + (getpositiveornegative () * getrandomlesstative (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-Top 40-bottom 37-left 39-Right // 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 ){}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.