이 기사의 예는 귀하의 참조를위한 특정 Java 구현 테트리스의 특정 코드를 공유합니다. 특정 내용은 다음과 같습니다
테트리스 디자인 아이디어
테트리스는 어린 시절부터 연주 해 왔습니다. 누구나 규칙을 알고 있습니다. 과거에는 그 일이 매우 재미 있다고 생각했지만 이길 수 없었습니다. 이제 나는 혼자서 하나를 쓰고 매일 연습하는 법을 배웠습니다!
키보드 작동 :
왼쪽 키 : 왼쪽 이동; 오른쪽 키 : 오른쪽으로 이동하십시오.
UP 키 : 모양과 하단 키 변경 : 드롭을 가속화합니다 (문제 없음, 더 이상 교정 없음)
행의 제곱이 가득 차면이 라인이 제거됩니다. 제곱 행을 제거하면 10 점이 얻을 수 있습니다. 현재, 나는 레벨을 설정하지 않았다. 그것을 좋아하는 아기는 레벨을 스스로 설정할 수 있습니다.
그래서 그 정사각형 모양은 어디에서 왔습니까? 그것은 우리 자신에 의해 설계되었습니다. 일반적인 모양은 다음과 같습니다. i 유형, t 유형, l 유형, Tianzi 그리드 유형 등, 직접 추가하십시오!
그래서 무슨 일이 있었나요? 사실, 그것은 단지 4*4 배열 일뿐입니다. 물론, 당신은 행복하게 n*n을 디자인 할 수 있습니다. 당신은 마지막 말을합니다!
따라서 아래의 예를 제시하여 변경 될 수있는 모양이 사전에 설계되었으며 0은 비어 있고 1은 필러이므로 게임에서 모양을 만들 수 있습니다!
잊어 버리십시오 : 그림을 넣고 코드 실행 결과를 먼저 확인하십시오.
좋아? 당신이 그것을 좋아한다면, 그냥 직접하십시오. 코드가 충분히 잘 쓰여지지 않았을 수도 있습니다. 저를 용서 해주세요. 되돌아 보면 더 많은 것을 요약하고 코드를 계속 업데이트 할 것입니다.
Gamepanel 클래스 : 게임 인터페이스 클래스, 전체 블록이 삭제되어 표시되며 게임의 로직 느린 가이 클래스에서 구현됩니다.
패키지 테트리스; import java.awt.graphics; import java.awt.event.actionevent; import java.awt.event.actionListener; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.util.random; import javax.swing.joptionpane; javax.swing.jpanel; import javax.swing.timer; public class gamepanel 확장 jpanel 구현 Keylistener {private int maprow = 21; 개인 int mapcol = 12; 개인 int mapgame [] [] = new int [maprow] [mapcol]; // 맵 정보를 저장하기 위해 2 차원 배열 공간을 열어 개인 타이머 타이머를 저장합니다. private int score = 0; // 점수 random random = new random (); 개인 int curshapeType = -1; 개인 int curshapestate = -1; // 현재 모양 유형과 현재 모양 상태를 설정하십시오. 개인 int nextshapestate = -1; // 다음 번에 나타나는 블록 그룹의 유형과 상태를 SET PRIVATE INT POSX = 0; 개인 int posy = 0; Private Final Int Shapes [] [] [] = new int [] [] {// t glyphs는 반 시계 방향으로 저장되어 있습니다. 주문 {{0,0,0,0, 1,1,1,1,1,1,0,0,0,0, 0,0,0,0,0}, {0,1,0,0,0, 0,1,0,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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, 1,1,0,0, 0,0,0,0,0,0}, {1,0,0,0, 1,1,0,0,0,0,0}, // Z 자형 {1,1,0,0,0,0, 0,1,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,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0}}}}; 개인 int rowrect = 4; 개인 int colrect = 4; // 여기서 저장된 이미지를 4*4 2 차원 배열로 간주합니다. 우리는 1 차원 배열을 사용하여 위에 저장하지만 개인 int rectwidth = 10을 구현하기 위해 2 차원 배열로 간주해야합니다. public gamepanel () // 생성자 ---- 좋은지도 생성 {createrect (); initMap (); //이 맵 초기화 setWall (); // wall // createrect (); TIMER = New Timer (500, New TimerListener ()); timer.start (); } class timerListener는 ActionListener {public void ActionPerformed (ActionEvent e) {mazoun (); }} public void setWall () // 열 0과 11은 벽이며, 20 행은 벽입니다. 맵 게임 [i] [11] = 2; } for (int j = 1; }} public void initMap () //이 맵 초기화, 벽 ID는 2이고, 공간 ID는 0이고, 블록 ID는 1 {e (int i = 0; i <maprow; i ++) {for (int j = 0; j <mapcol; j ++) {mapGame [i] [j] = 0; }}} public void createrect () // 블록 생성 --- 현재 블록 유형과 상태가 존재하는 경우 다음에 다음 시간을 설정하고, 그렇지 않은 경우 {if (curshapeType == -1 && curshapestate == -1) // 현재 블록 상태가 1임을 나타냅니다. curshapestate = random.nextInt (shapes [0] .length); } else {curshapeType = nextsHapeType; curshapestate = nextshapestate; } nextsHapEtype = random.nextInt (shapes.length); nextshapestate = random.nextInt (shapes [0] .length); posx = 0; posy = 1; // 벽의 왼쪽 상단에 정사각형을 만듭니다. System.exit (0); }} public boolean gameover (int x, int y, int shapetype, int shapestate) // 게임이 종료되는지 {if (isornomove (x, y, shapetype, shapestate)) {return false; } true를 반환합니다. } public boolean isornomove (int x, int y, int shapetype, int shapestate) // 현재 그림을 이동할 수 있는지 판단합니다. 여기서 우리는 X와 Y의 좌표가 4*4의 2 차원 배열의 상단 대상 (그림을 설명하는 배열) {for (int i = 0; i <rowrect; i ++) {for (int j = 0; MapGame [x+i] [y+j] == 1 || [ShapeType] [Shapestate] [i*colrect+j] == 1 && mapGame [x+i] [y+j] == 2) {return false; }}} return true; } public void turn () // 회전 {int temp = curshapestate; curshapestate = (curshapestate+1) % 모양 [0] .length; if (isornomove (posx, posy, curshapetype, curshapestate)) {} else {curshapestate = temp; } Repaint (); } public void mizedown () // 아래로 이동 {if (iSornomove (posx+1, posy, curshapetype, curshapestate)) {posx ++; } else {addTomap (); // 맵 checkline () 에서이 줄을 수정합니다. Createrect (); // 새 블록을 재현} retaint (); } public void moveleft () // 왼쪽으로 이동 {if (iSornomove (posx, posy-1, curshapeType, curshapestate)) {posy-; } Repaint (); } public void moveright () // 오른쪽으로 이동 {if (iSornomove (posx, posy+1, curshapeType, curshapestate)) {posy ++; } Repaint (); } public void addTomap () // 타락한 이미지를 맵에 수정 {for (int i = 0; i <rowrect; i ++) {for (int j = 0; 모양 [curshapeType] [curshapestate] [i*colrect+j]; }}}} public void checkline () //이 줄에 전체 행이 있는지 확인하십시오 {int count = 0; for (int i = maprow-2; i> = 0; i-) {count = 0; for (int j = 1; j <mapcol-1; j ++) {if (mapGame [i] [j] == 1) {count ++; } else break; } if (count> = mapcol-2) {for (int k = i; k> 0; k--) {for (int p = 1; p <mapcol-1; p ++) {mapGame [k] [p] = mapGame [k-1] [p]; }} 점수 += 10; i ++; }}} public void Paint (그래픽 g) // Repaint Window {super.paint (g); for (int i = 0; i <rowrect; i ++) // 떨어지는 블록 {for (int j = 0; j <colrect; j ++) {if (shapes [curshapeType] [curshapestate] [i*colrect+j] == 1) {g.fillRect ((posy+j+1)*rectwidth, (posx+i+1) rectwidth); }}} for (int i = 0; i <maprow; i ++) // 맵에 고정 된 블록 정보를 그려 {or (int j = 0; j <mapcol; j ++) {if (mapGame [i] [j] == 2) // 벽을 그립니다 {g.wrawRect ((j+1))*rectwidth, (i+1)*+1) } if (mapGame [i] [j] == 1) // 작은 정사각형 {g.fillRect ((j+1))*rectwidth, (i+1)*rectwidth, rectwidth, rectwidth); }}} G.DrawString ( "score ="+ score, 225, 15); G.DrawString ( "다음 광장 :", 225, 50); for (int i = 0; }}}} public void newGame () // 게임이 다시 시작 {score = 0; initmap (); setwall (); Createrect (); 리 페인트 (); } public void stopgame () // 게임이 일시 중지 {timer.stop (); } public void 계속기 게임 () {timer.start (); . KeyEvent.VK_UP://up--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- keyevent.vk_right : // 오른쪽 ---- Moveright (); 부서지다; }} @override public void keyreleased (keyevent e) {// todo 자동 생성 메소드 스터브}}} GameFrame 클래스 : 전체 게임의 진입 포트는 무뚝뚝하게 말하면 main () 함수가있는 클래스입니다. 이 클래스는 게임 인터페이스의 일부 디자인을 구현하며 작은 UI로 이해할 수 있습니다.
패키지 테트리스; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jmenu; import javax.swing.jmenubar; import javax.swing.jmenuitem; problic jframe jframe jframe jframe emptions instrements; ActionListener {private int widthframe = 500; 개인 int heightframe = 600; 개인 jmenu menu = new Jmenu ( "게임"); // 메뉴 생성 개인 jmenuitem newGame = menu.add ( "다시 시작"); // 내장 메뉴 옵션 생성 개인 jmenuitem exitgame = menu.add ( "Game Exit"); private jmenuitem stopgame = menu.add ( "게임 일시 정지"); private jmenuitem goongame = menu.add ( "게임 계속"); private jmenu menutwo = new Jmenu ( "도움말"); // 두 번째 메뉴 생성 개인 jmenuitem aboutgame = menutwo.add ( "게임 정보"); gamepanel gamepanel = new Gamepanel (); public gameframe () // 생성자 {addkeylistener (gamepanel); newGame.addactionListener (this); exitgame.addactionListener (this); stopgame.addactionListener (this); goongame.addactionListener (this); aboutgame.addactionListener (this); this.add (gamepanel); jmenubar 메뉴 = 새로운 jmenubar (); menu.add (menuone); menu.add (menutwo); this.setjmenubar (메뉴); this.settitle ( "테트리스"); this.setbounds (50, 10, widthframe, heightframe); this.set -Visible (true); this.setDefaultCloseOperation (jframe.exit_on_close); } public void actionPerformed (actionEvent e) {if (e.getSource () == newGame) // 게임이 다시 시작됩니다 {gamePanel.newgame (); } if (e.getSource () == exitGame) // 게임 종료 {system.exit (0); } if (e.getSource () == STOP -GAME) // GAME PAVAUSE {GamePanel.stopGame (); } if (e.getSource () == goongame) // 게임 계속 {gamepanel.continuegame (); } if (e.getSource () == About -Game) // 게임 정보 정보 {joptionpane.showMessagedIalog (null, "왼쪽 및 오른쪽 키 이동, 위쪽으로 회전을 만듭니다", "프롬프트", joptionpane.ok_option); }} public static void main (String [] args) {new GameFrame (); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.