Ich habe diese Technik letztes Jahr gelernt und sie noch nicht geschrieben. Jetzt habe ich mir die Zeit genommen, ein Tetris -Spiel zu schreiben.
Nur einfaches neues Spiel, Pause, Fortsetzung, Punkte Funktion. Es erkennt einfach die klassischen russischen Funktionen.
Ich werde es nicht mehr vorstellen. Wenn Sie interessiert sind, führen Sie es selbst aus und veröffentlichen Sie später ein Bild.
Code:
Paket Cn.Hncu; Import Java.awt.Color; Import Java.awt.font; Import Java.awt.graphics; Import Java.awt.event.ActionEvent; Import Java.awt.event.ActionListener; javax.swing.jmenu; import javax.swing.jmenubar; import Javax.swing.jmenuitem; import Javax.swing.joptionpane; Import Javax.swing.jpanel; Javax.Swing.timer; Public Class Tetrise erweitert JFRAME {public static void Main (String [] args. te.setvisible (true); // Wenn Sie der Schnittstelle ein Bearbeitungsfeld hinzufügen, das den Fokus übernimmt, müssen Sie den folgenden Code verwenden. JMenuitem itemPause; JMenuitem itemContinue; public tetris () {this.setDefaultCloseOperation (exit_on_close); this.setLocation (700, 200); this.setSize (220, 275); this.setResizable (falsch); TP = neuer Tetrispanel (); this.getContentPane (). add (tp); // Menü fügen Sie JMenuuBar Menubar = new JMenubar (); this.setjmenubar (Menubar); JMenu Menugame = New Jmenu ("Spiel"); Menubar.Add (Menugame); JMenuitem itemNew = new JMenuitem ("neues Spiel"); itemNew.setActionCommand ("neu"); itemPause = new JMenuitem ("pause"); itemPause.setActActionCommand ("pause"); itemContinue = new JMenuitem ("Weiter"); itemContinue.setActionCommand ("Fortsetzung"); itemContinue.setenabled (false); Menugame.Add (ItemNew); Menugame.add (itemPause); Menugame.add (itemContinue); Menulistener Menulistener = neuer Menulistener (); itemnew.addactionListener (Menulistener); itemPause.addactionListener (Menulistener); itemContinue.addactionListener (Menulistener); // Lassen Sie das gesamte JFRAME Tastatur hören. } class MENULISTENER implementiert ActionListener {@Override public void actionPerformed (actionEvent e) {// Neues Spiel spielen if (e.getActionCommand (). Equals ("neu") {tp.newGame (); } if (e.getActionCommand (). Equals ("pause") {Timer.Stop (); itemContinue.setenabled (true); itemPause.setEnabled (false); } if (e.getActionCommand (). Equals ("Fortsetzung") {timer.restart (); itemContinue.setenabled (false); itemPause.setEnabled (true); }}} privater Timer -Timer; Die Klasse Tetrispanel erweitert JPanel {// Form des Quadrats: // Die erste Dimension repräsentiert den Blocktyp (einschließlich 7 Typen: S, Z, L, J, I, T) // Die zweite Dimension repräsentiert die Anzahl der Drehungen // Die dritte und vierte Dimension repräsentiert die Blockmatrix // Formes. = new int [] [] [] { / * * Vorlage { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0.0 nicht jedoch nicht nicht jedoch jedoch ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, {0,0,0,0,0,0,0,0,0, * 0,0,0,0,0,0,0,0,0}} */ // I (※ ※ den Balken in Version 1 von Zeile 1 von Zeile 1 bis 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0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0, 0, 0, 0 } }, // J { { 0, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // O { { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // L { { 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } }, // T { { 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 } } }; private int blocktype; // Blocktyp Private int-Turnstate; // Rotationszustand privat int x; // Blockposition x--column Position-Column-Nummer private int y; // Blockposition y-row Position-Reihen-Nummer private int map [] [] = new int [13] [23]; // Karte: 12 Spalten und 22 Steigungen. Um grenzüberschreitend zu verhindern, wird das Array geöffnet in: 13 Säulen und 23 Zeilen private int delay = 1000; public timerKelister louser = new TimerKleklister (); Private int Score = 0; // Score public tetrispanel () {newGame (); Nextblock (); // Timer = neuer Timer (Verzögerung, Hörer); //timer.start (); } public void newGame () {blocktype = (int) (math.random ()*1000)%7; TurnState = (int) (math.random ()*1000)%4; x = 4; y = 0; für (int i = 0; i <12; i ++) {// Zeile für (int j = 0; j <21; j ++) {// Walking if (i == 0 || i == 11) {// 3 ist das Grid der Schnittstelle der Schnittstellenborder -Randkarte [i] [j] = 3; // Wirklich, nur intimistische Intupers verwenden. }} map [i] [21] = 3; // 3 ist das Gitter des Schnittstellenrandes} if (timer! = null) {timer.stop (); } Delay = 1000; Timer = neuer Timer (Verzögerung, Hörer); Timer.Start (); } private void Nextblock () {blocktype = (int) (math.random ()*1000)%7; TurnState = (int) (math.random ()*1000)%4; x = 4; y = 0; // Game Over if if (crash (x, y, blocktype, rendState) == 0) {timer.stop (); int option = joptionpane.showConFirMdialog (dies, "Spiel vorbei !!, wagst du es immer noch zu kommen ..."); if (option == joptionpane.ok_option) {newGame (); } else if (option == joptionPane.no_option) {System.exit (0); }}} private void down () {if (crash (x, y+1, blocktype, turnstate) == 0) {// Beachten Sie, dass y+1 verwendet wird, um festzustellen, ob die entsprechende Position in der Karte ein gestapelter Block oder einen Frame ist, nachdem der Block abgelegt wurde. } else {y ++; } repaint (); } private void links () {if (x> = 0) {x -= crash (x -1, y, blocktype, raststate); } repaint (); } private void right () {if (x <8) {x += crash (x +1, y, blocktype, rendestate); } repaint (); } private void Turn () {if (crash (x, y, blocktype, (TurnState+1)%4) == 1) {TurnState = (TurnState+1)%4; } repaint (); } private void add (int x, int y, int blocktype, int turnstate) {für (int a = 0; a <4; a ++) {für (int b = 0; b <4; b ++) {if (formes [blocktype] [turnstate] [a*4+b] == 1) {map [x+b+1] [y+b+1] [y+b+1] [y+b+1] [y+b+1] [y+b+1]; }}} trydelline (); } // private void trydelline () {for (int b = 0; b <21; b ++) {int c = 1; für (int a = 0; a <12; a ++) {c & = map [a] [b]; // Alle sind 1, das Ergebnis von C ist 1} if (c == 1) {// Es gibt eine Zeile, die eliminiert werden muss // eine Zeile nach unten nach unten für (int d = b; d> 0; Karte [e] [d-1]; }} // Punkte hinzufügen += 100; Verzögerung /=1.05; Timer.SetDelay (Verzögerung); } } } // Parameter example: 4,3,2,3 //Judge whether there is a collision private int crash(int x, int y, int blockType, int turnState){ for(int a=0; a<4; a++){ for(int b=0; b<4; b++){// if( (shapes[blockType][turnState][a*4+b]==1 && map[x+b+1][y+a] ==1) ||// (shapes[blockType][turnState][a*4+b]==1 && map[x+b+1][y+a] ==3 ) ){// } if( (shapes[blockType][turnState][a*4+b] & map[x+b+1][y+a]) ==1 ){ return 0; // kollision}}} return 1; // no collision} @Override public void Paint (Graphics g) {// blocktype = 6; // TurnState = 3; // x = 4; // y = 6; Super.Paint (g); // Nachjage G.SetColor (neue Farbe (153,51,205)); // den aktuellen Block für (int j = 0; j <16; j ++) {if (formes [blocktype] [TurnState] [j] == 1) {G.FillRect ((j%4+x+1)*10, (j/4+y)*10, 10, 10, 10, 10, 10) zeichnen; G.SetColor (color.cyan); g.drawrect ((j%4+x+1)*10, (j/4+y)*10, 10, 10); G.SetColor (neue Farbe (153,51,205)); }} // Zeichnen Sie den Schnittstellenrahmen und die Stapelblöcke --- Die gesamte Karte G.SetColor (color.red); für (int i = 0; i <12; i ++) {// Gehen für (int j = 0; j <22; j ++) {// Walking if (map [i] [j] == 3) {g.drawrect (i*10, j*10, 10, 10); } else if (map [i] [j] == 1) {g.fillRect (i*10, j*10, 10, 10); g.setColor (color.green); g.drawrect (i*10, j*10, 10, 10); G.SetColor (color.red); }}} // Zeigen Sie die Punktzahl an, und das Layout ist wunderschön, fügen Sie der Schnittstelle etwas hinzu // Zeichnen Sie den richtigen Teil der quadratischen Fläche G.SetColor (color.red); G.SetFont (neue Schriftart ("aa", font.bold, 11)); G.Drawstring ("Score =" + Score, 130, 20); G.SetFont (neue Schriftart ("aa", font.plain, 13)); G.SetColor (color.Blue); G.Drawstring ("Raubkopien ablehnen", 125, 70); G.Drawstring ("Achten Sie auf Selbstschutz.", 125, 90); G.Drawstring ("Vorsicht, getäuscht zu werden.", 125, 110); G.Drawstring ("Moderates Spiel ist gut für das Gehirn", 125, 130); G.Drawstring ("Add to Play ist für den Körper schädlich.", 125, 150); G.Drawstring ("Zeitplanzeit vernünftig", 125, 170); G.Drawstring ("Genießen Sie ein gesundes Leben.", 125, 190); } Klasse TimerKeLister erweitert KeyAdapter implementiert actionListener {@Override public void actionPerformed (actionEvent e) {down (); } @Override public void keypressed (KeyEvent e) {switch (e.getKeyCode ()) {case keyEvent.vk_down: down (); brechen; case keyEvent.vk_left: links (); Break; case keyEvent.vk_right: right (); brechen; case keyEvent.vk_up: Turn (); Break; case keyEvent.vk_f1: plug (); case keyEvent.vk_f2: time (); }} public void plug () {Score+= 100; } public void time () {delay = 1000; Timer.SetDelay (Verzögerung); }}}Ausführende Schnittstelle:
Weitere aufregende Spiele finden Sie auf dem speziellen Thema "Java Classic Games".
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.