Tetris source code

Tetris source code

Complete source code:

【File】 Fangkuai.java

  1. package com;
  2.   
  3.   
  4. class Fangkuai {
  5.   
  6.       
  7. int rgb;
  8.       
  9. int run_x= 5 ,run_y;
  10.        
  11. int d = 1 ;
  12.        
  13. int xingzhuang[][];
  14.        
  15. static   final   int width= 20 ,height= 20 ;
  16.        
  17. public   static   final   int xingzhuangs[][][]={
  18. {{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 } ,
  19. { 0 , 0 , 0 , 0 , 1 , 1 , 1 , 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 }},
  20.                
  21. {{ 0 , 0 , 1 , 0 },{ 0 , 1 , 1 , 1 },{ 0 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 }}, //мапн  
  22. {{ 0 , 0 , 0 , 0 },{ 1 , 0 , 0 , 0 },{ 1 , 1 , 0 , 0 },{ 0 , 1 , 0 , 0 }},
  23. {{ 1 , 1 , 1 , 1 },{ 1 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 }}, //Tпн  
  24. {{ 1 , 1 },{ 1 , 1 }}
  25.                
  26. };
  27.    
  28.        

3. [File] Mypanel.java ~ 2KB

  1. package com;
  2.   
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.util.Random;
  7.   
  8. import javax.swing.JPanel;
  9.   
  10. class Mypanel extends JPanel{
  11.       
  12.   
  13.       
  14. protected Fangkuai Fangkuai;
  15.       
  16. protected Image image;
  17.       
  18. protected   int w, h;
  19.       
  20. /**
  21. * Draw a square
  22. * @param g
  23. * @param curt_xing
  24. */  
  25. protected    void drawFangkui(Graphics g, int curt_xing[][]){
  26.   
  27.   
  28. if (curt_xing!= null ){
  29. int y = 0 ; boolean b = false ;
  30. for ( int i = 0 ; i < curt_xing.length; i++) {
  31. for ( int j = 0 ; j < curt_xing[i].length; j++) {
  32. if (curt_xing[i][j]!= 0 )
  33. {
  34. g.fill3DRect((fangkuai.run_x+j)*Fangkuai.width, (fangkuai.run_y+y)*Fangkuai.height,
  35. Fangkuai.width- 1 , Fangkuai.height- 1 , true );
  36. b= true ;
  37. }
  38.                            
  39. }
  40. if (b)
  41. y++;
  42.                            
  43.                    
  44. }
  45.                
  46. }
  47. }
  48. /**
  49. * Create a block
  50. * @return
  51. */  
  52. protected Fangkuai createNewFangkui(){
  53.                
  54. int index = 0 ;
  55. Random random= new Random();
  56. Fangkuai fangkuai= new Fangkuai();
  57. Color color= new Color(random.nextInt( 255 ),
  58. random.nextInt( 255 ),random.nextInt( 255 ));
  59. index=random.nextInt( 4 );
  60.           
  61.             
  62. fangkuai.xingzhuang=Fangkuai.xingzhuangs[index];
  63. fangkuai.rgb=color.getRGB();
  64. return fangkuai;
  65. }
  66.         
  67. @Override  
  68. public   void paint(Graphics g) {
  69. super .paint(g);
  70.           
  71. if (image!= null )
  72. g.drawImage(image, 0 , 0 ,w,h, null );
  73.           
  74. Color color= new Color(fangkuai.rgb);
  75.         
  76. g.setColor(color);
  77. drawFangkui(g,fangkuai.xingzhuang);
  78. }
  79. }

4. [File] Eluo_panel.java

  1. package com;
  2.   
  3. import java.applet.Applet;
  4. import java.applet.AudioClip;
  5. import java.awt.Color;
  6. import java.awt.Graphics;
  7. import java.awt.Image;
  8. import java.io.File;
  9. import java.io.FileInputStream;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.net.MalformedURLException;
  13. import java.net.URL;
  14. import java.util.Random;
  15.   
  16. import javax.imageio.ImageIO;
  17. import javax.swing.JLabel;
  18. import javax.swing.JOptionPane;
  19.   
  20. import sun.audio.AudioPlayer;
  21. import sun.audio.AudioStream;
  22.   
  23. class Eluo_panel extends Mypanel implements Runnable{
  24.        
  25.       
  26. int huatu[][][];
  27. long now_time= 0 ;
  28. Random random= new Random();
  29. Color color= new Color( 0 );
  30. static   final   int MOVE_LEFT = 1 ;
  31. static   final   int MOVE_RIGHT = 2 ;
  32.     
  33. final   int array_length;
  34. final   int array_length0;
  35.   
  36. My panel next;
  37. int score_int;
  38.       
  39. AudioClip left,right,down;
  40. JLabel score;
  41.    
  42. AudioPlayer backplayer;
  43.    
  44. AudioStream backstream;
  45.    
  46. private   int draw_speed= 500 ,down_speed= 1000 ;
  47. private   long now=down_speed,last;
  48. Thread thread;
  49.    
  50. public    boolean game_over= false ,stop;
  51. public Eluo_panel( int w, int h,Mypanel next,JLabel score){
  52.           
  53. this .w = w;
  54. this .h=h;
  55. this .next = next;
  56. this .score=score;
  57.      
  58. try {
  59. image=ImageIO.read( new File( "images/panel.jpg" ));
  60. } catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. array_length=h/Fangkuai.height;
  64. array_length0=w/Fangkuai.width;
  65. initMusic();
  66.       
  67.          
  68. initGame();
  69. }
  70.      
  71. public   void initGame(){
  72. score_int = 0 ;
  73. game_over = false ;
  74. fangkuai=createNewFangkui();
  75.           
  76. this .next.fangkuai=next.createNewFangkui();
  77. next.fangkuai.run_x= 2 ;
  78. next.fangkuai.run_y= 3 ;
  79.         
  80. huatu= new   int [array_length][array_length0][ 2 ];
  81.         
  82. if (thread== null ||thread.getState()==Thread.State.TERMINATED)
  83. {
  84. thread= new Thread( this );
  85. thread.start();
  86. }
  87. }
  88. @Override  
  89. public   void paint(Graphics g) {
  90. super .paint(g);
  91.   
  92. g.drawLine( 0 ,h- 2 , w,h- 2 );
  93. g.drawLine( 0 , 0 , 0 ,h- 2 );
  94.          
  95. drawBack(g);
  96.          
  97. if (now-last>=down_speed)
  98. {
  99. moveDown(fangkuai.xingzhuang);
  100. last=now;
  101. }
  102. now=System.currentTimeMillis();
  103. }
  104.       
  105. /**
  106. * Draw the background
  107. * @param g
  108. */  
  109. void drawBack(Graphics g){
  110.   
  111. for ( int i = 0 ; i < huatu.length; i++) {
  112. for ( int j = 0 ; j < huatu[i].length; j++) {
  113. Color color= new Color(huatu[i][j][ 1 ]);
  114. g.setColor(color);
  115. if (huatu[i][j][ 0 ] != 0 )
  116. g.fillRect(j*Fangkuai.width,
  117. i*Fangkuai.width, Fangkuai.width- 1 ,Fangkuai.height- 1 );
  118. }
  119. }
  120. }
  121.       
  122.   
  123.       
  124. /**
  125. * Determine whether it can move downward
  126. * @param xingzhuang
  127. * @return
  128. */  
  129. boolean isCan_down( int xingzhuang[][]){
  130.           
  131.           
  132. int y = 0 ; boolean b = false ;
  133. for ( int i = 0 ; i < xingzhuang.length; i++) {
  134. for ( int j = 0 ; j < xingzhuang[i].length; j++) {
  135. if (xingzhuang[i][j]!= 0 )
  136. {
  137. b = true ;
  138. if (fangkuai.run_y+y>=array_length- 1  
  139. ||huatu[fangkuai.run_y+y+ 1 ][fangkuai.run_x+j][ 0 ]!= 0 ){
  140. return   false ;
  141. }
  142.                       
  143. }
  144.                       
  145. }
  146. if (b)
  147. y++;
  148.           
  149. }
  150.           
  151. return   true ;
  152. }
  153. /**
  154. * Deformation
  155. */  
  156.       
  157. public   int [][] bianXing( int d, int arr[][]){
  158.           
  159. if (arr== null ||arr[ 0 ]== null )
  160. return   null ;
  161.           
  162. int arr2[][] = new   int [arr.length][arr[ 0 ].length];
  163.           
  164.            
  165. switch (d) {
  166. case   1 :
  167. for ( int i = 0 ; i < arr.length; i++) {
  168. for ( int j = 0 ; j < arr[i].length; j++) {
  169. arr2[j][arr[i].length- 1 -i]=arr[i][j];
  170. }
  171. }
  172. break ;
  173.   
  174. default :
  175. break ;
  176. }
  177.           
  178. for ( int i = 0 ; i < arr2.length; i++) {
  179.               
  180. for ( int j = 0 ; j < arr2[i].length; j++) {
  181.                   
  182. if (arr2[i][j]!= 0 )
  183. {
  184. if (fangkuai.run_x+j>array_length0- 1  
  185. ||fangkuai.run_y+i>array_length- 1 ||fangkuai.run_x+j< 0  
  186. ||huatu[fangkuai.run_y+i][fangkuai.run_x+j][ 0 ]!= 0 )
  187. return arr;
  188. }
  189. }
  190. }
  191.           
  192. return arr2;
  193.           
  194. }
  195.      
  196. void moveDownInterface(){
  197. moveDown(fangkuai.xingzhuang);
  198. }
  199. /**
  200. * Move down
  201. * @param xingzhuang
  202. */  
  203. private   void moveDown( int xingzhuang[][]){
  204.   
  205. if (isCan_down(xingzhuang))
  206. fangkuai.run_y++;
  207.           
  208. else  
  209. {
  210. /**
  211. * If you can't move down, map the 0 and 1 of the current block to the entire board and create a new block
  212. */  
  213. int y = 0 ; boolean b = false ;
  214. for ( int i = 0 ; i < xingzhuang.length; i++) {
  215. for ( int j = 0 ; j < xingzhuang[i].length; j++) {
  216. if (xingzhuang[i][j]!= 0 )
  217. {
  218. huatu[fangkuai.run_y+y][fangkuai.run_x+j][ 0 ] = 1 ;
  219. huatu[fangkuai.run_y+y][fangkuai.run_x+j][ 1 ]=fangkuai.rgb;
  220. b= true ;
  221. }
  222.                           
  223. }
  224. if (b)
  225. y++;
  226.               
  227. }
  228.               
  229.               
  230. xiaoChu();
  231. for ( int i = 0 ; i < huatu[ 0 ].length; i++) {
  232. if (huatu[ 0 ][i][ 0 ] != 0 )
  233. {
  234. game_over= true ;
  235. JOptionPane.showMessageDialog( null , "Game Over" );
  236. }
  237. }
  238. down.play();
  239. fangkuai=next.fangkuai;
  240. fangkuai.run_x= 5 ;
  241. fangkuai.run_y= 0 ;
  242.              
  243. next.fangkuai=next.createNewFangkui();
  244. next.fangkuai.run_x= 2 ;
  245. next.fangkuai.run_y= 3 ;
  246. next.repaint();
  247. }
  248.               
  249. }
  250. public   void xiaoChu(){
  251.           
  252. boolean xiao = false ;
  253.           
  254. for ( int i = huatu.length- 1 ; i >= 0 ; i--) {
  255.               
  256. xiao = false ;
  257. int j = 0 ;
  258. for ( j = 0 ; j < huatu[i].length; j++) {
  259. if (huatu[i][j][ 0 ] == 0 )
  260. break ;
  261. }
  262.               
  263. if (j==huatu[i].length)
  264. xiao= true ;
  265.               
  266.               
  267. if (xiao){
  268. score_int++;
  269. for ( j = i; j > 0 ; j--) {
  270. for ( int j2 = 0 ; j2 < huatu[j].length; j2++) {
  271. huatu[j][j2]=huatu[j- 1 ][j2];
  272. }
  273. }
  274. for ( j = 0 ; j <huatu[ 0 ].length; j++) {
  275. huatu[ 0 ][j][ 0 ]= 0 ;
  276. }
  277. i++;
  278.                   
  279. score.setText(score_int+ "" );
  280. }
  281.               
  282. }
  283. }
  284. /**
  285. *
  286. * @param d
  287. * @param xingzhuang
  288. */  
  289. void moveOther( int d, int xingzhuang[][]){
  290.           
  291. int dx=d==MOVE_LEFT? 1 :- 1 ;
  292. if (is_CanMoveOther(d, xingzhuang)){
  293. fangkuai.run_x+=dx;
  294. }
  295. }
  296. private   boolean is_CanMoveOther( int d, int xingzhuang[][]){
  297.           
  298. int dx=d==MOVE_LEFT? 1 :- 1 ;
  299. int y= 0 ; boolean has= false ;
  300. for ( int i = 0 ; i < xingzhuang.length; i++) {
  301. has= false ;
  302. for ( int j = 0 ; j < xingzhuang[i].length; j++) {
  303.                   
  304. if (xingzhuang[i][j]!= 0 )
  305. {
  306. if (d==MOVE_LEFT&&fangkuai.run_x+j>=array_length0- 1  
  307. ||d==MOVE_RIGHT&&fangkuai.run_x+j<= 0 )
  308.                           
  309. return   false ;
  310. has= true ;
  311. if (huatu[fangkuai.run_y+y][fangkuai.run_x+j+dx][ 0 ]!= 0 ){
  312. return   false ;
  313. }
  314. }
  315. }
  316. if (has)
  317. y++;
  318. }
  319.       
  320. return   true ;
  321. }
  322.       
  323. public   void initMusic(){
  324. URL classpath=getClass().getClassLoader().getResource( "" );
  325. try {
  326. left=Applet.newAudioClip( new URL(classpath, "sounds/SOUND8.WAV" ));
  327. down=left=Applet.newAudioClip( new URL(classpath, "sounds/SOUND104.WAV" ));
  328. } catch (MalformedURLException e) {
  329. e.printStackTrace();
  330. }
  331.       
  332. try {
  333. FileInputStream fileInputStream= new FileInputStream( "sounds/without_you.wav" );
  334. backstream= new AudioStream(fileInputStream);
  335. backplayer=AudioPlayer.player;
  336. backplayer.start(backstream);
  337. } catch (Exception e) {
  338. e.printStackTrace();
  339. }
  340.       
  341.          
  342. }
  343.       
  344.       
  345. @Override  
  346. public   void run() {
  347. while (!game_over)
  348. {
  349. if (!stop)
  350. this .repaint();
  351. try {
  352. Thread.sleep(draw_speed);
  353. } catch (InterruptedException e) {
  354. e.printStackTrace();
  355. }
  356. }
  357. }
  358.       
  359. }

5. [File] Eluos.java

  1. package com;
  2.    
  3. import java.awt.Color;
  4. import java.awt.Graphics;
  5. import java.awt.event.ActionEvent;
  6. import java.awt.event.ActionListener;
  7. import java.awt.event.KeyEvent;
  8. import java.awt.event.KeyListener;
  9. import java.io.File;
  10. import java.io.IOException;
  11.   
  12. import javax.imageio.ImageIO;
  13. import javax.swing.ImageIcon;
  14. import javax.swing.JButton;
  15. import javax.swing.JFrame;
  16. import javax.swing.JLabel;
  17. import javax.swing.JOptionPane;
  18. import javax.swing.JTextArea;
  19.    
  20. public   class Eluos extends JFrame{
  21.        
  22.        
  23. private Eluo_panel jPanel;
  24.       
  25. private Mypanel panel_next;
  26.        
  27. private   int this_width= 600 ,this_height= 600 ;
  28.       
  29. private   int panel_width= 300 , panel_height= 400 ;
  30.     
  31. JButton button,button_stop,button_help;
  32.       
  33. JLabel score_label,label_score;
  34.       
  35. public Eluos(){
  36. this .setSize(this_width, this_height);
  37.         
  38. panel_next= new Mypanel();
  39. panel_next.setBackground(Color.RED);
  40. panel_next.setBounds( 400 , 200 , 200 , 200 );
  41.         
  42.         
  43. label_score= new JLabel( "0" );
  44. label_score.setBounds( 450 , 100 , 40 , 20 );
  45.         
  46. jPanel= new Eluo_panel(panel_width,panel_height,panel_next,label_score);
  47. jPanel.setBounds( 2 , 2 , panel_width, panel_height);
  48. jPanel.setBackground(Color.red);
  49.         
  50.         
  51. button= new JButton( new ImageIcon( "images/start.png" ));
  52. button.setBounds( 400 , 10 , 100 , 30 );
  53.         
  54. button_help = new JButton( "Help" );
  55. button_help.setBounds( 400 , 160 , 100 , 30 );
  56.         
  57. button_stop= new JButton( new ImageIcon( "images/stop.png" ));
  58. button_stop.setBounds( 400 , 60 , 100 , 30 );
  59.       
  60.        
  61. score_label = new JLabel( "Score" );
  62. score_label.setBounds( 400 , 100 , 40 , 20 );
  63.       
  64. this .add(button);
  65. this .add(button_help);
  66. this .add(button_stop);
  67. this .setTitle( "My Russia" );
  68. this .setLayout( null );
  69. this .add(jPanel);
  70. this .add(panel_next);
  71. this .add(score_label);
  72. this .add(label_score);
  73. this .setDefaultCloseOperation(EXIT_ON_CLOSE);
  74. this .setVisible( true );
  75.   
  76. addListener();
  77.         
  78.      
  79.     
  80. }
  81.       
  82. public   void addListener(){
  83.           
  84.           
  85. button_help.addActionListener( new ActionListener() {
  86.                   
  87. @Override  
  88. public   void actionPerformed(ActionEvent e) {
  89.                       
  90.                       
  91. String message= "Game rules: After the game starts, blocks of different shapes will randomly fall down one by one. Players must use operations to fill up a row and eliminate them. When the screen is full, the game ends.\n" +
  92. "After the player enters the Tetris game interface, the interface is as shown in Figure 2-1. The following are detailed game operation instructions:\n" +
  93. "Keyboard operation: →: Move the block to the right.\n" +
  94. "←: Move the block to the left.\n" +
  95. "↓: Increase the speed at which blocks fall down.\n" +
  96. "↑: Rotate the falling block.\n" +
  97. "Click the Restart button to start over.\n" +
  98. "Click the Pause button to pause the game.\n" ;
  99.                       
  100. JTextArea area= new JTextArea();
  101. area.setText(message);
  102. area.setSize( 400 , 400 );
  103.                       
  104. JOptionPane.showMessageDialog(Eluos. this , area);
  105.                       
  106.                       
  107. }
  108. });
  109. button.addActionListener( new ActionListener() {
  110.               
  111. @Override  
  112. public   void actionPerformed(ActionEvent e) {
  113. jPanel.initGame();
  114. Eluos. this .requestFocus();
  115. }
  116. });
  117.           
  118. button_stop.addActionListener( new ActionListener() {
  119.                   
  120. @Override  
  121. public   void actionPerformed(ActionEvent e) {
  122. jPanel.stop=!jPanel.stop;
  123. if (!jPanel.stop)
  124. Eluos. this .requestFocus();
  125. }
  126. });
  127.           
  128. this .addKeyListener( new KeyListener() {
  129.               
  130. @Override  
  131. public   void keyTyped(KeyEvent e) {
  132. }
  133.                
  134. @Override  
  135. public   void keyReleased(KeyEvent e) {
  136.                 
  137. }
  138.                
  139. @Override  
  140. public   void keyPressed(KeyEvent e) {
  141.                   
  142. if (jPanel.game_over)
  143. return ;
  144. switch (e.getKeyCode()) {
  145. case KeyEvent.VK_LEFT:
  146.                          
  147. jPanel.left.play();
  148. Eluos. this .jPanel.moveOther(Eluo_panel.MOVE_RIGHT, Eluos. this .jPanel.fangkuai.xingzhuang);
  149. break ;
  150. case KeyEvent.VK_RIGHT:
  151. jPanel.left.play();
  152. Eluos. this .jPanel.moveOther(Eluo_panel.MOVE_LEFT, Eluos. this .jPanel.fangkuai.xingzhuang);
  153. break ;
  154. case KeyEvent.VK_UP:
  155. Eluos. this .jPanel.fangkuai.xingzhuang =
  156. Eluos.this.jPanel.bianXing ( Eluos.this.jPanel.fangkuai.d , Eluos.this.jPanel.fangkuai.xingzhuang ) ;
  157. break ;
  158.                         
  159. case KeyEvent.VK_DOWN:
  160. Eluos. this .jPanel.moveDownInterface();
  161. break ;
  162. }
  163. jPanel.repaint();
  164. }
  165. });
  166.       
  167. this .requestFocusInWindow();
  168.          
  169.          
  170.          
  171.          
  172. }
  173.      
  174.       
  175. @Override  
  176. public   void paint(Graphics g) {
  177. super .paint(g);
  178.       
  179. try {
  180. g.drawImage(ImageIO.read( new File( "images/c.jpg" ))
  181. , 0 , 0 , null );
  182. } catch (IOException e) {
  183. e.printStackTrace();
  184. }
  185.           
  186.           
  187. panel_next.repaint();
  188. button.repaint();
  189. button_stop.repaint();
  190. button_help.repaint();
  191. score_label.repaint();
  192. label_score.repaint();
  193. }
  194. public   static   void main(String[] args) {
  195.        
  196. new Eluos();
  197.        
  198. }
  199.        
  200. }

<<:  Exclusive interview with Xu Shiwei: Three transformations completed in eleven years How a successful entrepreneur was made

>>:  Android source code download: Android-like Tiantian Dongting player

Recommend

Bypass: The ticket grabbing software with the highest success rate in 2020

1. What is the account used for logging in? The a...

With global warming, is it feasible to send polar bears to Antarctica?

Polar bears' survival challenge Polar bears, ...

International Energy Agency: Energy Efficiency Report 2023

The International Energy Agency has released its ...

How to write a successful operation and promotion plan?

In the process of making the plan , I discovered ...

A timely help? NIO completes $100 million convertible bond financing

Recently, NIO announced that it has signed a conv...

A new definition of use it or lose it: Is AI reshaping the human brain?

Technology is quietly changing the structure of o...

The past and present of the "Angel Pill" Aspirin

Bayer is a global pharmaceutical company. The inf...