ฉันได้เรียนรู้เทคนิคนี้เมื่อปีที่แล้วและยังไม่ได้เขียน ตอนนี้ฉันใช้เวลาในการเขียนเกม Tetris แล้ว
เกมใหม่ที่เรียบง่ายง่าย ๆ หยุดดำเนินการต่อฟังก์ชั่นคะแนน มันเพียงแค่ตระหนักถึงฟังก์ชั่นรัสเซียคลาสสิก
ฉันจะไม่แนะนำอีกต่อไป หากคุณสนใจให้เรียกใช้ตัวเองและโพสต์ภาพในภายหลัง
รหัส:
แพ็คเกจ cn.hncu; นำเข้า java.awt.color; นำเข้า java.awt.font; นำเข้า java.awt.graphics; นำเข้า java.awt.event.actionEvent; นำเข้า java.awt.event.actionListener; javax.swing.jframe; นำเข้า Javax.swing.jmenu; นำเข้า Javax.swing.jmenubar; นำเข้า Javax.swing.jmenuitem; นำเข้า Javax.swing.joptionpane; นำเข้า Javax.swing.jpanel; tetris te = tetris ใหม่ (); te.setVisible (จริง); // ถ้าคุณเพิ่มกล่องแก้ไขลงในอินเทอร์เฟซที่จะยึดโฟกัสคุณจะต้องใช้รหัสต่อไปนี้ //te.requestfocus(true);//let แผงเกมรับโฟกัส-รับการฟังของคีย์บอร์ด jmenuitem itempause; jmenuitem itemcontinue; public tetris () {this.setDefaultCloseOperation (exit_on_close); this.setLocation (700, 200); this.setsize (220, 275); this.setResizable (เท็จ); tp = tetrispanel ใหม่ (); this.getContentPane (). เพิ่ม (tp); // เพิ่มเมนู jmenubar menubar = new jmenubar (); this.setjmenubar (Menubar); JMENU MENUGAME = ใหม่ JMENU ("GAME"); Menubar.add (Menugame); jMenuitem itemNew = new JMenuitem ("เกมใหม่"); itemnew.setActionCommand ("ใหม่"); itempause = ใหม่ jmenuitem ("หยุดชั่วคราว"); itempause.setActionCommand ("หยุดชั่วคราว"); itemContinue = new JMenuitem ("ดำเนินการต่อ"); itemcontinue.setActionCommand ("ดำเนินการต่อ"); itemcontinue.setenabled (เท็จ); menugame.add (itemnew); menugame.add (itempause); menugame.add (itemcontinue); menulistener menulistener = menulistener ใหม่ (); itemnew.addactionListener (Menulistener); itempause.addactionListener (Menulistener); itemcontinue.addactionListener (Menulistener); // ให้ JFrame ทั้งหมดเพิ่มคีย์บอร์ดฟัง this.addkeyListener (tp.listener); } คลาส Menulistener ใช้ ActionListener {@Override โมฆะสาธารณะ ActionPerformed (ActionEvent E) {// เล่นเกมใหม่ถ้า (e.getActionCommand (). เท่ากับ ("ใหม่")) {tp.newgame (); } if (e.getActionCommand (). เท่ากับ ("หยุดชั่วคราว")) {timer.stop (); itemcontinue.setenabled (จริง); itempause.setEnabled (เท็จ); } if (e.getActionCommand (). เท่ากับ ("ดำเนินการต่อ")) {timer.restart (); itemcontinue.setenabled (เท็จ); itempause.setEnabled (จริง); }}} ตัวจับเวลาส่วนตัว; คลาส tetrispanel ขยาย jpanel {// รูปร่างของสี่เหลี่ยม: // มิติแรกหมายถึงประเภทบล็อก (รวมถึง 7 ประเภท: s, z, l, j, i, o, t) // มิติที่สองหมายถึงจำนวนการหมุน // มิติที่สามและสี่หมายถึงบล็อก matrix // รูปร่าง [] [] [] = new int [] [] [] { / * * เทมเพลต { {0,0,0,0,0,0,0,0,0,0,0,0,0s0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0s0,0,0,0,0,0,0,0,0,0,0,0,0s0,0,0,0,0,0,0,0,0,0,0,0s0,0,0,0,0,0,0,0,0,0s ,, 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,0s0,0,0,0,0,0,0,0,0,0,0,0s ,, 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,0s0,0,0,0,0,0,0,0,0,0,0,0s ,, 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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0}}, // l {{1, 0, 0, 0, 0, 0, 0, 0, 0, 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}, {0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 0, 0, 0}, {0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0}, {1, 1, 1, 0, 0, 1, 0, 0, 0} 0} int private int blockType; // block type private int turnstate; // การหมุนสถานะส่วนตัว int x; // ตำแหน่งบล็อก x-คอลัมน์ตำแหน่งคอลัมน์คอลัมน์ส่วนตัว int y; // ตำแหน่งบล็อก y-ตำแหน่งตำแหน่ง-หมายเลขโรว์แผนที่ส่วนตัว [] [] = ใหม่ int [13] [23]; เพื่อป้องกันการข้ามพรมแดนอาร์เรย์จะถูกเปิดเป็น: 13 คอลัมน์และ 23 แถวล่าช้า int ส่วนตัว = 1,000; TimerkeyLister Public Listener = ใหม่ TimerKeyLister (); คะแนน int ส่วนตัว = 0; // คะแนนสาธารณะ tetrispanel () {newGame (); nextblock (); // ตัวจับเวลา = ตัวจับเวลาใหม่ (ล่าช้าผู้ฟัง); //timer.start (); } โมฆะสาธารณะ newGame () {blockType = (int) (math.random ()*1000)%7; turnstate = (int) (math.random ()*1000)%4; x = 4; y = 0; สำหรับ (int i = 0; i <12; i ++) {// line สำหรับ (int j = 0; j <21; j ++) {// เดินถ้า (i == 0 || i == 11) {// 3 คือกริดของแผนที่เส้นขอบอินเตอร์เฟส [i] [j] = 3; // }} แผนที่ [i] [21] = 3; // 3 คือกริดของเส้นขอบอินเตอร์เฟส} ถ้า (จับเวลา! = null) {timer.stop (); } ล่าช้า = 1,000; ตัวจับเวลา = ตัวจับเวลาใหม่ (ล่าช้าผู้ฟัง); timer.start (); } โมฆะส่วนตัว NextBlock () {blockType = (int) (math.random ()*1000)%7; turnstate = (int) (math.random ()*1000)%4; x = 4; y = 0; // game over if (crash (x, y, blocktype, turnstate) == 0) {timer.stop (); ตัวเลือก int = joptionpane.showconfirmdialog (นี่ "เกมมากกว่า !! คุณยังคงกล้าที่จะมา ... "); if (ตัวเลือก == joptionpane.ok_option) {newGame (); } อื่นถ้า (ตัวเลือก == joptionpane.no_option) {system.exit (0); }}} โมฆะส่วนตัวลง () {ถ้า (crash (x, y+1, blockType, turnstate) == 0) {// โปรดทราบว่าใช้ Y+1 ที่นี่เพื่อตรวจสอบว่าตำแหน่งที่สอดคล้องกันในแผนที่เป็นบล็อกหรือเฟรมที่ซ้อนกัน } else {y ++; } repaint (); } โมฆะส่วนตัวซ้าย () {ถ้า (x> = 0) {x -= crash (x -1, y, blockType, turnstate); } repaint (); } โมฆะส่วนตัวขวา () {ถ้า (x <8) {x += crash (x +1, y, blockType, turnstate); } repaint (); } เทิร์นโมฆะส่วนตัว () {if (crash (x, y, blockType, (turnstate+1)%4) == 1) {turnstate = (turnstate+1)%4; } repaint (); } โมฆะส่วนตัวเพิ่ม (int x, int y, int blockType, int turnstate) {สำหรับ (int a = 0; a <4; a ++) {สำหรับ (int b = 0; b <4; b ++) {ถ้า (รูปร่าง [blockType] }}} trydelline (); } // กำจัดโมฆะส่วนตัว trydelline () {สำหรับ (int b = 0; b <21; b ++) {int c = 1; สำหรับ (int a = 0; a <12; a ++) {c & = map [a] [b]; // ทั้งหมดคือ 1 ผลลัพธ์ของ c คือ 1} ถ้า (c == 1) {// มีบรรทัดที่ต้องกำจัด // <int e <int (int = 0; แผนที่ [E] [D-1]; }} // เพิ่มคะแนนคะแนน += 100; ล่าช้า /=1.05; timer.setDelay (ล่าช้า); }}} // ตัวอย่างพารามิเตอร์: 4,3,2,3 // ตัดสินว่ามีการชนกันระหว่าง INT INT (int x, int y, int blockType, int turnstate) {สำหรับ (int a = 0; a <4; a ++) {สำหรับ (int b = 0; b <4; b ++) แผนที่ [x+b+1] [y+a] == 1) || // (รูปร่าง [blockType] [turnstate] [a*4+b] == 1 && แผนที่ [x+b+1] [y+a] == 3)) {//} if ((shapes [blockType] 0; // collision}}} return 1; // ไม่มีการชน} @Override โมฆะสาธารณะสี (กราฟิก g) {// blockType = 6; // turnstate = 3; // x = 4; // y = 6; super.paint (g); // กำจัด afterimage g.setcolor (สีใหม่ (153,51,205)); // วาดบล็อกปัจจุบันสำหรับ (int j = 0; j <16; j ++) {ถ้า (รูปร่าง [blockType] [turnstate] [j] == 1) {g.fillrect ((J%4+x+1)*10, (J/4+y)*10, 10, 10, 10); G.SetColor (color.cyan); G.DrawRect ((J%4+X+1)*10, (J/4+Y)*10, 10, 10); G.SetColor (สีใหม่ (153,51,205)); }} // วาดเฟรมอินเตอร์เฟสและบล็อกสแต็ก --- แผนที่ทั้งหมด g.setColor (color.red); สำหรับ (int i = 0; i <12; i ++) {// เดินไปหา (int j = 0; j <22; j ++) {// เดินถ้า (แผนที่ [i] [j] == 3) {g.drawrect (i*10, j*10, 10, 10); } อื่นถ้า (แผนที่ [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); }}} // แสดงคะแนนและเค้าโครงมีความสวยงามเพิ่มบางสิ่งบางอย่างลงในอินเตอร์เฟส // วาดส่วนขวาของพื้นที่สี่เหลี่ยมจัตุรัส G.SetColor (color.red); G.SetFont (ฟอนต์ใหม่ ("AA", Font.Bold, 11)); G.DrawString ("คะแนน =" + คะแนน, 130, 20); G.SetFont (ฟอนต์ใหม่ ("AA", Font.Plain, 13)); G.SetColor (color.blue); G.DrawString ("ปฏิเสธเกมละเมิดลิขสิทธิ์", 125, 70); G.DrawString ("ระวังการป้องกันตัวเอง", 125, 90); G.DrawString ("ระวังการถูกหลอก", 125, 110); G.DrawString ("เกมปานกลางดีต่อสมอง", 125, 130); G.DrawString ("เพิ่มการเล่นเป็นอันตรายต่อร่างกาย", 125, 150); G.DrawString ("กำหนดเวลาอย่างสมเหตุสมผล", 125, 170); G.DrawString ("สนุกกับชีวิตที่มีสุขภาพดี", 125, 190); } คลาส TimerkeyLister ขยาย KeyAdapter ใช้ ActionListener {@Override โมฆะสาธารณะ ActionPerformed (ActionEvent E) {Down (); } @Override โมฆะสาธารณะคีย์กด (KeyEvent E) {switch (e.getKeyCode ()) {case keyEvent.vk_down: down (); หยุดพัก; Case KeyEvent.vk_left: left (); break; Case KeyEvent.vk_right: Right (); Break; Case KeyEvent.vk_up: Turn (); Break; Case KeyEvent.vk_f1: plug (); Case KeyEvent.vk_f2: Time (); }} ปลั๊กโมฆะสาธารณะ () {คะแนน+= 100; } เวลาโมฆะสาธารณะ () {ล่าช้า = 1,000; timer.setDelay (ล่าช้า); -อินเทอร์เฟซที่รัน:
สำหรับเกมที่น่าตื่นเต้นยิ่งขึ้นโปรดดูหัวข้อพิเศษ "Java Classic Games"
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น