JAVA——俄罗斯方块
Posted 薛定谔的Submit
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA——俄罗斯方块相关的知识,希望对你有一定的参考价值。
package Tetris_JCoder; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; import javax.swing.*; public class Tetris extends JFrame{ public Tetris() { Block NewBlock = new Block(); addKeyListener(NewBlock); add(NewBlock); } public static void main(String[] args) { Tetris window = new Tetris(); JMenuBar menu = new JMenuBar(); window.setJMenuBar(menu); JMenu game = new JMenu("Game..."); JMenu help = new JMenu("Help..."); JMenuItem newgame = game.add("NewGame"); JMenuItem about = help.add("About"); menu.add(game); menu.add(help); window.setLocation(100,100); window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); window.setSize(600,600); window.setTitle("Tetris By-J_Coder"); window.setVisible(true); window.setResizable(false); } } class Block extends JPanel implements KeyListener { static int diff = 500; Timer timer = new Timer(diff, new TimerListener()); static int isST = 1; Block() { ANewBlock(); init(); walls(); timer = new Timer(diff, new TimerListener()); timer.start(); } static int type = -1; static int dirc = -1; static int nowX; static int nowY; static int secT; static int secD; static int score = 0; static int flag = 0; static int mmp[][] = new int[50][50]; static String stp = ""; static int boxes[][][] = { { { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 }, { 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0 } },//shape I { { 0, 1, 1, 0, 1, 1, 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, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },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0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0 } } //shape T }; public void init() { for(int i = 0;i < 50;i ++) { for(int j = 0;j < 50;j ++) { mmp[i][j] = 0; } } } public void walls() { for(int i = 0;i < 12;i ++) { mmp[i][21] = -1; } for (int i = 0;i < 22;i ++) { mmp[11][i] = -1; mmp[0][i] = -1; } } public void paintComponent(Graphics g) { super.paintComponent(g); for (int j = 0; j < 16; j ++) { if (boxes[type][dirc][j] == 1) { g.fillRect((j % 4 + nowX + 1) * 20, (j / 4 + nowY) * 20, 20, 20); } } for (int j = 0; j < 22; j ++) { for (int i = 0; i < 12; i ++) { if (mmp[i][j] == 1) { g.fillRect(i * 20, j * 20, 20, 20); } if (mmp[i][j] == -1) { g.drawRect(i * 20, j * 20, 20, 20); } } } Font f1 = new Font(null,Font.PLAIN,18); Font f2= new Font(null,Font.PLAIN,30); g.setFont(f1); g.drawString("Press up button to change the shape", 250, 200); g.drawString("Press left button to move left", 250, 225); g.drawString("Press right button to move right", 250, 250); g.drawString("Press down button to drop faster", 250, 275); g.drawString("Press ‘P‘ to pause/continue the game", 250, 300); //g.drawString("Press ‘K‘ to speed-up", 250, 325); //g.drawString("Press ‘L‘ to speed-down", 250, 350); g.drawString("Happy game,happy life !", 350, 375); g.drawString("Code changes the world !", 350, 400); g.setFont(f2); g.setColor(Color.RED); g.drawString(stp,60,150); g.setColor(Color.BLACK); g.drawString("Score : " + score, 20, 480); g.drawRect(240, 0, 100, 100); g.setFont(f1); g.drawString("NEXT",270,120); for (int j = 0; j < 16; j ++) { if (boxes[secT][secD][j] == 1) { g.fillRect(240 + (j % 4 + 1) * 20,20 + (j / 4) * 20, 20, 20); } } } public void keyPressed(KeyEvent e) { switch(e.getKeyCode()) { case KeyEvent.VK_DOWN: moveDown(); break; case KeyEvent.VK_UP: shapeChange(); break; case KeyEvent.VK_RIGHT: moveRight(); break; case KeyEvent.VK_LEFT: moveLeft(); break; case KeyEvent.VK_P: if(isST == 1) {timer.stop();stp = "PAUSED";} else {timer.start();stp = "";} isST = -isST; repaint(); break; /* case KeyEvent.VK_K: // speed on if(diff > 50) {diff -= 50;} break; case KeyEvent.VK_L: // speed off if(diff < 1000) {diff += 50;} break; */ } } public void moveLeft() { if (isOK(nowX - 1,nowY) == 1) { nowX --; } repaint(); } public void moveRight() { if (isOK(nowX + 1,nowY) == 1) { nowX ++; } repaint(); } public void moveDown() { int OK = isOK(nowX, nowY + 1); if (OK == 1) { nowY ++; dltLine(); } else { addToMap(nowX,nowY); ANewBlock(); dltLine(); } repaint(); } public void shapeChange(){ int t = dirc; dirc ++; dirc %= 4; int OK = isOK(nowX,nowY); //System.out.println(OK); if(OK == 0) { dirc = t; } repaint(); } public void keyReleased(KeyEvent e) {} public void keyTyped(KeyEvent e) {} public void ANewBlock() { if(gameover() != 0) { init(); walls(); if(score <= 1000) { JOptionPane.showMessageDialog(null, "You are so weak !","Game Over",JOptionPane.PLAIN_MESSAGE); } else if(score <= 10000) { JOptionPane.showMessageDialog(null, "Just so so !","Game Over",JOptionPane.PLAIN_MESSAGE); } else if(score <= 100000) { JOptionPane.showMessageDialog(null, "Nice Work !","Game Over",JOptionPane.PLAIN_MESSAGE); } else if(score <= 1000000) { JOptionPane.showMessageDialog(null, "You are awesome !","Game Over",JOptionPane.PLAIN_MESSAGE); } else if(score <= 10000000) { JOptionPane.showMessageDialog(null, "Incredible !","Game Over",JOptionPane.PLAIN_MESSAGE); } else { JOptionPane.showMessageDialog(null, "What a crazy man !","Game Over",JOptionPane.PLAIN_MESSAGE); } score = 0; } if(type == -1 && dirc == -1) { type = (int)(Math.random() * 10000) % 7; dirc = (int)(Math.random() * 10000) % 4; secT = (int)(Math.random() * 10000) % 7; secD = (int)(Math.random() * 10000) % 4; } else { type = secT; dirc = secD; secT = (int)(Math.random() * 10000) % 7; secD = (int)(Math.random() * 10000) % 4; } nowX = 4;nowY = 0; } public void addToMap(int x, int y) { int k = 0; for (int i = 0; i < 4; i ++) { for (int j = 0; j < 4; j ++) { if (mmp[x + j + 1][y + i] == 0) { mmp[x + j + 1][y + i] = boxes[type][dirc][k]; } k ++; } } } public int isOK(int x,int y) { for (int i = 0; i < 4; i ++) { for (int j = 0; j < 4; j ++) { if (((boxes[type][dirc][i * 4 + j] == 1) && (mmp[x + j + 1][y + i] == 1)) || ((boxes[type][dirc][i * 4 + j] == 1) && (mmp[x + j + 1][y + i] == -1))) { return 0; } } } return 1; } public int gameover() { int ans = 0; for(int i =1;i<= 10;i ++) { ans += mmp[i][1]; } return ans; } public void dltLine() { int t = 0; int lines = 0; for (int j = 0; j < 22; j ++) { for (int i = 0; i < 12; i ++) { if (mmp[i][j] == 1) { t ++; if (t == 10) { lines ++; for (int k = j; k > 0; k --) { for (int p = 0; p < 11; p ++) { mmp[p][k] = mmp[p][k - 1]; } } } } } t = 0; } if(lines == 1) {score += 100;} else if(lines == 2) {score += 300;} else if(lines == 3) {score += 600;} else if(lines == 4) {score += 1000;} } class TimerListener implements ActionListener { public void actionPerformed(ActionEvent e) { repaint(); //System.out.println("t"); int OK = isOK(nowX, nowY + 1); if(OK == 1) { nowY ++; dltLine(); } else { if(flag == 1) { addToMap(nowX,nowY); dltLine(); ANewBlock(); flag = 0; } flag = 1; } } } }
以上是关于JAVA——俄罗斯方块的主要内容,如果未能解决你的问题,请参考以下文章