Java Tetris Small Game _java

Source: Internet
Author: User

Last year has learned this technology, has not been to write, now take a time to write a Tetris game.
Only simple new game, pause, continue, integration function. The simple realization of the Russian classic function.
Not introduced, interested in their own run, the back posted a picture.

Code:

Package Cn.hncu;
Import Java.awt.Color;
Import Java.awt.Font;
Import Java.awt.Graphics;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Java.awt.event.KeyAdapter;

Import java.awt.event.KeyEvent;
Import Javax.swing.JFrame;
Import Javax.swing.JMenu;
Import Javax.swing.JMenuBar;
Import Javax.swing.JMenuItem;
Import Javax.swing.JOptionPane;
Import Javax.swing.JPanel;

Import Javax.swing.Timer;
 public class Tetris extends jframe{public static void Main (string[] args) {Tetris te = new Tetris ();
 Te.setvisible (TRUE);
 If you add an edit box in the interface that will preempt the focus, you need to use the following code//te.requestfocus (TRUE);//Let the game panel get the focus--grab the keyboard monitor} private Tetrispanel TP;
 JMenuItem Itempause;

 JMenuItem itemcontinue;
 Public Tetris () {this.setdefaultcloseoperation (exit_on_close);
 This.setlocation (700, 200);
 This.setsize (220, 275);
 This.setresizable (FALSE);
 TP = new Tetrispanel ();

 This.getcontentpane (). Add (TP);
 Add menu JMenuBar menubar = new JMenuBar ();
 This.setjmenubar (menubar); JMenu MenuGame = new JMenu ("game");

 Menubar.add (Menugame);
 JMenuItem = Itemnew = new JMenuItem ("New Game");
 Itemnew.setactioncommand ("new");
 Itempause = new JMenuItem ("Paused");

 Itempause.setactioncommand ("pause");
 Itemcontinue = new JMenuItem ("continue");
 Itemcontinue.setactioncommand ("continue");

 Itemcontinue.setenabled (FALSE);
 Menugame.add (itemnew);
 Menugame.add (Itempause);

 Menugame.add (itemcontinue);
 Menulistener Menulistener = new Menulistener ();
 Itemnew.addactionlistener (Menulistener);
 Itempause.addactionlistener (Menulistener);


 Itemcontinue.addactionlistener (Menulistener);
 Let the whole jframe add keyboard monitor This.addkeylistener (Tp.listener); Class Menulistener implements actionlistener{@Override public void actionperformed (ActionEvent e) {//Play new game if (E.
  Getactioncommand (). Equals ("new")) {tp.newgame ();
  } if (E.getactioncommand (). Equals ("pause")) {timer.stop ();
  Itemcontinue.setenabled (TRUE);
  Itempause.setenabled (FALSE); } if (E.getactioncommand (). Equals ("continue")) {Timer.restaRT ();
  Itemcontinue.setenabled (FALSE);
  Itempause.setenabled (TRUE);

 }} private timer timer; Class Tetrispanel extends jpanel{//Square Shape://The first dimension represents the type of block (including 7: S, Z, L, J, I, O, T)//The second dimension represents the number of rotations//The 34th dimension represents the square matrix//shapes [Type] [Turnstate] [i] i--> block[i/4][i%4] int shapes[][][] = new int[][][] {/* template {{0,0,0,0,0,0,0,0, 0,0,0,0, 0,0,0,0}, {0, 0, 
   0,0,0,0,0,0, 0,0,0,0, * 0,0,0,0}, {0,0,0,0,0,0,0,0, 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 (※ Change the horizontal bar in version 1 from line 1th to line 2nd) {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}},//S {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, 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} },//Z {{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 {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, 1, 0, 0, 0, 1, 1

 , 0, 0, 1, 0, 0, 0, 0, 0, 0}}; private int blocktype;//block type private int turnstate;//rotation State private int x;//square position x--column position--column number private int y;//square position y--row position --line number private int map[][]=new int[13][23];//map: 12 columns 22 rows.
 To prevent the crossing, the array is opened: 13 columns, 23 rows, private int delay=1000; 

 Public Timerkeylister listener=new Timerkeylister ();
  private int score=0;//score Public Tetrispanel () {newgame ();
  Nextblock ();
  Timer = new timer (delay,listener);
 Timer.start ();
  public void Newgame () {blocktype = (int) (Math.random () *1000)%7;
  Turnstate = (int) (Math.random () *1000)%4;
  x=4;

  y=0; for (int i=0;i<12;i++) {//GO column for (int j=0;j<21;j++) {//Walk line if (i==0 | | i==11) {//3 is the lattice of the interface border map[i][j]=3;//is supposedly just 0 and 1 away.
   integers, but there is a special effect here with 3-}else{map[i][j]=0;
  } if (Timer!=null) {timer.stop () Map[i][21]=3;//3 for the interface border;
  } delay=1000; Timer = new Timer (delay,Listener);
 Timer.start ();
  private void 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 option = Joptionpane.showconfirmdialog (This, "Game over!!, dare to come ...");
  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) {//note, here with y+1, is the judgment block down a grid, whether the corresponding position in the map is a stacked block or frame ad
  D (x,y,blocktype,turnstate);//To save the information of the current box to the map Nextblock ();
  }else{y++;
 } repaint ();
  private void Left () {if (x>=0) {x-= Crash (x-1,y,blocktype,turnstate);
 } repaint ();
  private void Right () {if (x<8) {x + = crash (x+1,y,blocktype,turnstate);
 } 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) {for (int a=0; a<4; a++) {for (int b=0; b<4; b++) {if (Shapes[blocktype][turn
   STATE][A*4+B] ==1) {Map[x+b+1][y+a] = 1;
 }} trydelline ();
  }//Line private void Trydelline () {for (int b=0;b<21;b++) {int c=1; for (int a=0;a<12;a++) {c &= map[a][b];//all 1,c result is 1} if (c==1) {//one line needs to be deleted//to move down a row for (int d=b; d> 0;
   d--) {for (int e=0;e<11;e++) {Map[e][d] = map[e][d-1];
   }//Add-score +=100;
   Delay/=1.05;
  Timer.setdelay (delay); }}//Parameter example: 4,3,2,3//judgment there is no collision private int crash (int x, int y, int blocktype, int turnstate) {for (int a=0; a&lt ; 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;//collision}}
 1;//didn't collide.@Override public void Paint (Graphics g) {//Blocktype =6;//turnstate = 3;//x=4;//y=6;
  Super.paint (g);//Eliminate Residual shadow G.setcolor (new Color (153,51,205)); Draw the current box for (int j=0;j<16;j++) {if (shapes[blocktype][turnstate][j]==1) {g.fillrect (j%4+x+1) *10, (j/4+y) *10, 10,
   10);
   G.setcolor (Color.cyan);
   G.drawrect ((j%4+x+1) *10, (j/4+y) *10, 10, 10);
  G.setcolor (New Color (153,51,205));
  }//Draw the interface frame as well as the stacked block---the entire map g.setcolor (color.red);
   for (int i=0;i<12;i++) {//GO column for (int j=0;j<22;j++) {//Walk row 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);
  }}//Show score, at the same time for the layout is beautiful, add something in the interface/draw the right part of the square g.setcolor (color.red);
  G.setfont (New Font ("AA", Font.Bold, 11));

  g.DrawString ("score=" + score, 130, 20);
  G.setfont (New Font ("AA", Font.plain, 13));
  G.setcolor (Color.Blue); g.DrawString ("Deny piracy games,", 125, 70); g.DrawString ("Pay attention to self protection.")
  ", 125, 90); g.DrawString ("Beware of Being fooled.")
  ", 125, 110);
  g.DrawString ("Moderate game benefit Brain,", 125, 130); g.DrawString ("Addicted to the game kills.")
  ", 125, 150);
  g.DrawString ("Reasonable arrangement of time,", 125, 170); g.DrawString ("Enjoy a healthy life.")

 ", 125, 190); Class Timerkeylister extends Keyadapter implements actionlistener{@Override public void actionperformed (Actioneve
  NT e) {down (); @Override public void keypressed (KeyEvent e) {switch (E.getkeycode ()) {case KeyEvent.VK_DOWN:down ();
   ;
   Case KeyEvent.VK_LEFT:left ();
   Case KeyEvent.VK_RIGHT:right ();
   Case KeyEvent.VK_UP:turn ();
   Case KeyEvent.VK_F1:plug ();
  Case KeyEvent.VK_F2:time ();
 }} public void plug () {score+=100;
  public void time () {delay = 1000;
 Timer.setdelay (delay);

 }

 }


}

Run the interface:

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.