This example is shared for you java Implementation of Tetris specific code , For your reference , The details are as follows

Tetris design idea

Tetris has grown up since childhood , What rules do you know , I used to think it was fun , But you just can't win , Now I have learned to write one by myself and practice it every day !

Keyboard operation :

Left key : Shift left ; Right click : Shift right ;

Up key : Transform modeling Down key : Accelerate falling ( Is everything all right , No further adjustment )

The box in any row is full , This line is eliminated , Eliminate a row of squares 10 branch , I haven't set a level yet , Your favorite babies can set their own level ;

So where did the shapes of those squares come from , That's what we designed ourselves , Several common shapes are :I type ,T type ,L type , Tian zigzag and so on , Add it yourself !

So what the hell ? Actually, it's one 4*4 Array of , Of course, you're happy n*n it's fine too , You has the final say. !

So here's an example , It's used to tell you why the shapes you see can be changed. That's why it's designed in advance ,0 Empty ,1 Fill cells for , So you can make concave shapes in your game !

forget it : Let's look at the running results of the code first :

Do you like it? ? Just do it if you like , Maybe the code is not written well enough , Please forgive me , I'll summarize more later , The code will be updated continuously ;

GamePanel class : Game interface class , The whole box is dropped and displayed , The logic of the game is implemented in this class ;

package tetris;

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;

import javax.swing.JPanel;

import javax.swing.Timer;

public class GamePanel extends JPanel implements KeyListener{

private int mapRow = 21;

private int mapCol = 12;

private int mapGame[][] = new int[mapRow][mapCol];// Open up a two-dimensional array space , Used to store our map information

private Timer timer;

private int score = 0;// Record results

Random random = new Random();

private int curShapeType = -1;

private int curShapeState = -1;// Sets the current shape type and current shape state

private int nextShapeType = -1;

private int nextShapeState = -1;// Set the type and status of the next block group

private int posx = 0;

private int posy = 0;

private final int shapes[][][] = new int[][][]{

//T Glyphs are stored in counterclockwise order

{

{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,1,0,0, 0,1,1,0, 0,1,0,0, 0,0,0,0}

},

//I Glyphs are stored in counterclockwise order

{

{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}

},

// inverted Z Shapes are stored in counterclockwise order

{

{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},

{1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0}

},

//Z Shapes are stored in counterclockwise order

{

{1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0},

{0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0},

{1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0},

{0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0}

},

//J Glyphs are stored in counterclockwise order

{

{0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0},

{1,1,1,0, 0,0,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,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0}

},

//L Glyphs are stored in counterclockwise order

{

{1,0,0,0, 1,0,0,0, 1,1,0,0, 0,0,0,0},

{0,0,1,0, 1,1,1,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},

{1,1,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0}

},

// Field glyphs are stored in counterclockwise order

{

{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}

}

};

private int rowRect = 4;

private int colRect =
4;// Here we regard the stored image as a 4*4 Two dimensional array of , Although in the above, we use one-dimensional array to store , But in fact, it should be realized as a two-dimensional array

private int RectWidth = 10;

public GamePanel()// Constructor ---- Create a map

{

CreateRect();

initMap();// Initialize this map

SetWall();// Set wall

// CreateRect();

timer = new Timer(500,new TimerListener());

timer.start();

}

class TimerListener implements ActionListener{

public void actionPerformed(ActionEvent e)

{

MoveDown();

}

}

public void SetWall()// The first 0 Liehedi 11 Columns are walls , The first 20 Line is also a wall

{

for(int i = 0; i < mapRow; i++)// Draw a column first

{

mapGame[i][0] = 2;

mapGame[i][11] = 2;

}

for(int j = 1; j < mapCol-1; j++)// Draw the last line

{

mapGame[20][j] = 2;

}

}

public void initMap()// Initialize this map , Wall ID yes 2, Blank ID yes 0, Square ID yes 1

{

for(int i = 0; i < mapRow; i++)

{

for(int j = 0; j < mapCol; j++)

{

mapGame[i][j] = 0;

}

}

}

public void
CreateRect()// Create Box --- If the current block type and state exist, set the next block , If it does not exist, set the current and set the next state and type

{

if(curShapeType == -1 && curShapeState == -1)// The current box status is 1, Indicates that the game has just begun

{

curShapeType = random.nextInt(shapes.length);

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;// Create a square in the upper left corner of the wall

if(GameOver(posx,posy,curShapeType,curShapeState))

{

JOptionPane.showConfirmDialog(null, " game over !", " Tips ", JOptionPane.OK_OPTION);

System.exit(0);

}

}

public boolean GameOver(int x, int y, int ShapeType, int ShapeState)// Determine whether the game is over

{

if(IsOrNoMove(x,y,ShapeType,ShapeState))

{

return false;

}

return true;

}

public boolean IsOrNoMove(int x, int y, int ShapeType, int
ShapeState)// Judge whether the current figure can be moved , It is emphasized here x,y The coordinates of are 4*4 Two dimensional array of ( The array that describes the graph ) Top left target

{

for(int i = 0; i < rowRect ; i++)

{

for(int j = 0; j < colRect; j++)

{

if(shapes[ShapeType][ShapeState][i*colRect+j] == 1 && mapGame[x+i][y+j] == 1

|| shapes[ShapeType][ShapeState][i*colRect+j] == 1 && mapGame[x+i][y+j] == 2)

{

return false;

}

}

}

return true;

}

public void Turn()// rotate

{

int temp = curShapeState;

curShapeState = (curShapeState+1) % shapes[0].length;

if(IsOrNoMove(posx,posy,curShapeType,curShapeState))

{

}

else

{

curShapeState = temp;

}

repaint();

}

public void MoveDown()// Move down

{

if(IsOrNoMove(posx+1,posy,curShapeType,curShapeState))

{

posx++;

}

else

{

AddToMap();// Pin this row in the map

CheckLine();

CreateRect();// Recreate a new square

}

repaint();

}

public void MoveLeft()// Move left

{

if(IsOrNoMove(posx,posy-1,curShapeType,curShapeState))

{

posy--;

}

repaint();

}

public void MoveRight()// Move right

{

if(IsOrNoMove(posx,posy+1,curShapeType,curShapeState))

{

posy++;

}

repaint();

}

public void AddToMap()// Fix the falling image into the map

{

for(int i = 0; i < rowRect; i++)

{

for(int j = 0; j < colRect; j++)

{

if(shapes[curShapeType][curShapeState][i*colRect+j] == 1)

{

mapGame[posx+i][posy+j] = shapes[curShapeType][curShapeState][i*colRect+j];

}

}

}

}

public void CheckLine()// Check if these rows are full

{

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];

}

}

score += 10;

i++;

}

}

}

public void paint(Graphics g)// Redraw window

{

super.paint(g);

for(int i = 0; i < rowRect; i++)// Draw the falling square

{

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, RectWidth, RectWidth);

}

}

}

for(int i = 0; i < mapRow; i++)// Draw the fixed block information on the map

{

for(int j = 0; j < mapCol; j++)

{

if(mapGame[i][j] == 2)// Painting wall

{

g.drawRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth);

}

if(mapGame[i][j] == 1)// Draw a small square

{

g.fillRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth);

}

}

}

g.drawString("score = "+ score, 225, 15);

g.drawString(" Next box :", 225, 50);

for(int i = 0; i < rowRect; i++)

{

for(int j = 0; j < colRect; j++)

{

if(shapes[nextShapeType][nextShapeState][i*colRect+j] == 1)

{

g.fillRect(225+(j*RectWidth), 100+(i*RectWidth), RectWidth, RectWidth);

}

}

}

}

public void NewGame()// The game starts again

{

score = 0;

initMap();

SetWall();

CreateRect();

repaint();

}

public void StopGame()// Game pause

{

timer.stop();

}

public void ContinueGame()

{

timer.start();

}

@Override

public void keyTyped(KeyEvent e) {

}

@Override

public void keyPressed(KeyEvent e) {

switch(e.getKeyCode())

{

case KeyEvent.VK_UP:// upper ---- rotate

Turn();

break;

case KeyEvent.VK_DOWN:// lower ---- Move down

MoveDown();

break;

case KeyEvent.VK_LEFT:// Left ---- Move left

MoveLeft();

break;

case KeyEvent.VK_RIGHT:// right ---- Move right

MoveRight();

break;

}

}

@Override

public void keyReleased(KeyEvent e) {

// TODO Auto-generated method stub

}

}

GameFrame class : Entrance to the whole game , ok , To put it bluntly, there is main() Class of function , This class implements some design of the game interface , You can understand it as a little UI;

package tetris;

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;

import javax.swing.JOptionPane;

public class GameFrame extends JFrame implements ActionListener{

private int widthFrame = 500;

private int heightFrame = 600;

private JMenu menuone = new JMenu(" game ");// Create a menu

private JMenuItem newGame = menuone.add(" restart ");// Create a built-in menu option

private JMenuItem exitGame = menuone.add(" Game exit ");

private JMenuItem stopGame = menuone.add(" Game pause ");

private JMenuItem goOnGame = menuone.add(" The game continues ");

private JMenu menutwo = new JMenu(" help ");// Create a second menu

private JMenuItem aboutGame = menutwo.add(" About games ");

GamePanel gamepanel = new GamePanel();

public GameFrame()// Constructor

{

addKeyListener(gamepanel);

newGame.addActionListener(this);

exitGame.addActionListener(this);

stopGame.addActionListener(this);

goOnGame.addActionListener(this);

aboutGame.addActionListener(this);

this.add(gamepanel);

JMenuBar menu = new JMenuBar();

menu.add(menuone);

menu.add(menutwo);

this.setJMenuBar(menu);

this.setTitle(" Tetris ");

this.setBounds(50, 10, widthFrame, heightFrame);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource() == newGame)// The game starts again

{

gamepanel.NewGame();

}

if(e.getSource() == exitGame)// Game exit

{

System.exit(0);

}

if(e.getSource() == stopGame)// Game pause

{

gamepanel.StopGame();

}

if(e.getSource() == goOnGame)// The game continues

{

gamepanel.ContinueGame();

}

if(e.getSource() == aboutGame)// About game information

{

JOptionPane.showMessageDialog(null, " Left and right key movement , Rotate up ", " Tips ",
JOptionPane.OK_OPTION);

}

}

public static void main(String[] args) {

new GameFrame();

}

}

More articles on Tetris , Please click to view the topic :《 Tetris 》

The above is the whole content of this paper , I hope it will be helpful to everyone's study , I also hope you can support the script house .

Technology