Cet article décrit le simple code mini-jeu en mouvement carré en mouvement écrit en Java. Partagez-le pour votre référence, comme suit:
La capture d'écran de l'effet de course est la suivante:
La première fois que j'ai écrit une interface graphique en Java, c'était encore un peu naïf. . Continuez à travailler dur à l'avenir! ! Le code spécifique est le suivant:
// Little Box Game par Alexyui // Game.java par 1093710210 @ HitImport Javax.swing. *; Importer java.awt.event. *; Importer Java.awt.Geom. *; Importer Java.awt. *; MyFrame (); GameFrame.SetTitle ("Java Box Game by Alexyui"); GameFrame.SetDefaultCloseOperation (jframe.exit_on_close); GameFrame.SetVisible (true); }} // catégorie de trame MyFrame étend jFrame {private int flag = 0; Double protégé x [] = {0,0,270,0,270}; Double protégé y [] = {0,0,0,270,270}; Winner int protégé [] = {0,0,0,0,0,0}; public static final int default_width = 317; public static final int default_height = 339; public myFrame () {mypanel a = new myPanel (); addKeyListener (new myKeyListener ()); this.add (a); setSize (default_width, default_height); } class MyPanel étend jpanel {public mypanel () {} public void peinture (graphics g) {super.paint (g); Graphics2d G2 = (Graphics2d) G; Rectangle2d rect1 = new rectangle2d.double (x [1], y [1], 30f, 30f); Rectangle2d rect2 = new rectangle2d.double (x [2], y [2], 30f, 30f); Rectangle2d rect3 = new rectangle2d.double (x [3], y [3], 30f, 30f); Rectangle2d rect4 = new rectangle2d.double (x [4], y [4], 30f, 30f); // dessine les quatre cases dans le milieu g2.setpaint (Color.Red); G2.Drawrect (120 130,30,30); G2.Drawrect (120 150, 30,30); G2.Drawrect (150 120, 30,30); G2.Drawrect (150 150, 30,30); // remplit les blocs sur les quatre coins G2.SetPaint (Color.Blue); if (gagnant [1] == 0) g2.Fill (rect1); if (gagnant [2] == 0) g2.Fill (rect2); if (gagnant [3] == 0) g2.Fill (rect3); if (gagnant [4] == 0) g2.Fill (rect4); G2.SetPaint (Color.Red); // Photos les blocs sélectionnés en rouge if (Flag == 1) G2.Fill (rect1); if (flag == 2) g2.Fill (rect2); if (flag == 3) g2.Fill (rect3); if (Flag == 4) g2.Fill (rect4); G2.SetPaint (Color.Blue); if (gagnant [1] == 1) g2.Fill (rect1); if (gagnant [2] == 1) g2.Fill (rect2); if (gagnant [3] == 1) g2.Fill (rect3); if (gagnant [4] == 1) g2.Fill (rect4); G2.SetPaint (Color.Red); if (gagnant [1] == 1 && gagnant [2] == 1 && gagnant [3] == 1 && gagnant [4] == 1) g2.Drawstring ("Succes! Vous avez gagné!", 100,75); // Gardez la couleur de la bordure G2.Drawrect (120,120, 30,30); G2.Drawrect (120 150, 30,30); G2.Drawrect (150 120, 30,30); G2.Drawrect (150 150, 30,30); repeindre(); }} class MyKeyListener implémente keyListener {// override public void keyPdress (keyEvent Event) {int keyCode = event.getKeycode (); if (keycode == keyevent.vk_1) {flag = 1;} if (keycode == keyEvent.vk_2) {flag = 2;} if (keycode == keyevent.vk_3) {flag = 3;} if (keycode == KeyEvent.vk_up) {move (0, -30, flag); win ();} if (keycode == keyevent.vk_down) {move (0,30, file); win ();} if (keycode == keyevent.vk_left) {move (-30,0, drap); win ();} if (keycode == KeyEvent.vk_right) {move (30,0, drapeau); win ();}} public void keyrelent (event keyEvent) {} public void keyped (keyevent event) {}} // utilisé pour déterminer les règles de base, Move the Block Void Move (int mx, int my, int mfg) {int i; Boolean Teller = False; x [mflag] + = mx; y [mflag] + = mon; if (! ((x [flag]> = 0 && x [flag] <= 270) && (y [Flag]> = 0 && y [Flag] <= 270))) {x [mflag] - = mx; y [mflag] - = mon; } pour (i = 1; i <5; i ++) {if (mflag! = i) if (x [mflag] == x [i] && y [mflag] == y [i]) teater = true; } if (teather == true) {x [mflag] - = mx; y [mflag] - = mon; }} // Utilisé pour déterminer le bloc qui a entré la zone qualifiée void win () {int j1, j2; pour (j1 = 1; j1 <5; j1 ++) if ((x [j1] <= 150 && x [j1]> = 120) && (y [j1] <= 150 && y [j1]> = 120)) winner [j1] = 1; pour (j2 = 1; j2 <5; j2 ++) if (! ((x [j2] <= 150 && x [j2]> = 120) && (y [j2] <= 150 && y [j2]> = 120)) Winner [j2] = 0;}}J'espère que cet article sera utile à la programmation Java de tous.