Complete source code: 【File】 Fangkuai.java- package com;
-
-
- class Fangkuai {
-
-
- int rgb;
-
- int run_x= 5 ,run_y;
-
- int d = 1 ;
-
- int xingzhuang[][];
-
- static final int width= 20 ,height= 20 ;
-
- public static final int xingzhuangs[][][]={
- {{ 0 , 0 , 0 , 0 , 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 , 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 }},
-
- {{ 0 , 0 , 1 , 0 },{ 0 , 1 , 1 , 1 },{ 0 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 }},
- {{ 0 , 0 , 0 , 0 },{ 1 , 0 , 0 , 0 },{ 1 , 1 , 0 , 0 },{ 0 , 1 , 0 , 0 }},
- {{ 1 , 1 , 1 , 1 },{ 1 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 },{ 0 , 0 , 0 , 0 }},
- {{ 1 , 1 },{ 1 , 1 }}
-
- };
-
-
3. [File] Mypanel.java ~ 2KB- package com;
-
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.util.Random;
-
- import javax.swing.JPanel;
-
- class Mypanel extends JPanel{
-
-
-
- protected Fangkuai Fangkuai;
-
- protected Image image;
-
- protected int w, h;
-
-
-
-
-
-
- protected void drawFangkui(Graphics g, int curt_xing[][]){
-
-
- if (curt_xing!= null ){
- int y = 0 ; boolean b = false ;
- for ( int i = 0 ; i < curt_xing.length; i++) {
- for ( int j = 0 ; j < curt_xing[i].length; j++) {
- if (curt_xing[i][j]!= 0 )
- {
- g.fill3DRect((fangkuai.run_x+j)*Fangkuai.width, (fangkuai.run_y+y)*Fangkuai.height,
- Fangkuai.width- 1 , Fangkuai.height- 1 , true );
- b= true ;
- }
-
- }
- if (b)
- y++;
-
-
- }
-
- }
- }
-
-
-
-
- protected Fangkuai createNewFangkui(){
-
- int index = 0 ;
- Random random= new Random();
- Fangkuai fangkuai= new Fangkuai();
- Color color= new Color(random.nextInt( 255 ),
- random.nextInt( 255 ),random.nextInt( 255 ));
- index=random.nextInt( 4 );
-
-
- fangkuai.xingzhuang=Fangkuai.xingzhuangs[index];
- fangkuai.rgb=color.getRGB();
- return fangkuai;
- }
-
- @Override
- public void paint(Graphics g) {
- super .paint(g);
-
- if (image!= null )
- g.drawImage(image, 0 , 0 ,w,h, null );
-
- Color color= new Color(fangkuai.rgb);
-
- g.setColor(color);
- drawFangkui(g,fangkuai.xingzhuang);
- }
- }
4. [File] Eluo_panel.java- package com;
-
- import java.applet.Applet;
- import java.applet.AudioClip;
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Image;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Random;
-
- import javax.imageio.ImageIO;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
-
- import sun.audio.AudioPlayer;
- import sun.audio.AudioStream;
-
- class Eluo_panel extends Mypanel implements Runnable{
-
-
- int huatu[][][];
- long now_time= 0 ;
- Random random= new Random();
- Color color= new Color( 0 );
- static final int MOVE_LEFT = 1 ;
- static final int MOVE_RIGHT = 2 ;
-
- final int array_length;
- final int array_length0;
-
- My panel next;
- int score_int;
-
- AudioClip left,right,down;
- JLabel score;
-
- AudioPlayer backplayer;
-
- AudioStream backstream;
-
- private int draw_speed= 500 ,down_speed= 1000 ;
- private long now=down_speed,last;
- Thread thread;
-
- public boolean game_over= false ,stop;
- public Eluo_panel( int w, int h,Mypanel next,JLabel score){
-
- this .w = w;
- this .h=h;
- this .next = next;
- this .score=score;
-
- try {
- image=ImageIO.read( new File( "images/panel.jpg" ));
- } catch (IOException e) {
- e.printStackTrace();
- }
- array_length=h/Fangkuai.height;
- array_length0=w/Fangkuai.width;
- initMusic();
-
-
- initGame();
- }
-
- public void initGame(){
- score_int = 0 ;
- game_over = false ;
- fangkuai=createNewFangkui();
-
- this .next.fangkuai=next.createNewFangkui();
- next.fangkuai.run_x= 2 ;
- next.fangkuai.run_y= 3 ;
-
- huatu= new int [array_length][array_length0][ 2 ];
-
- if (thread== null ||thread.getState()==Thread.State.TERMINATED)
- {
- thread= new Thread( this );
- thread.start();
- }
- }
- @Override
- public void paint(Graphics g) {
- super .paint(g);
-
- g.drawLine( 0 ,h- 2 , w,h- 2 );
- g.drawLine( 0 , 0 , 0 ,h- 2 );
-
- drawBack(g);
-
- if (now-last>=down_speed)
- {
- moveDown(fangkuai.xingzhuang);
- last=now;
- }
- now=System.currentTimeMillis();
- }
-
-
-
-
-
- void drawBack(Graphics g){
-
- for ( int i = 0 ; i < huatu.length; i++) {
- for ( int j = 0 ; j < huatu[i].length; j++) {
- Color color= new Color(huatu[i][j][ 1 ]);
- g.setColor(color);
- if (huatu[i][j][ 0 ] != 0 )
- g.fillRect(j*Fangkuai.width,
- i*Fangkuai.width, Fangkuai.width- 1 ,Fangkuai.height- 1 );
- }
- }
- }
-
-
-
-
-
-
-
-
- boolean isCan_down( int xingzhuang[][]){
-
-
- int y = 0 ; boolean b = false ;
- for ( int i = 0 ; i < xingzhuang.length; i++) {
- for ( int j = 0 ; j < xingzhuang[i].length; j++) {
- if (xingzhuang[i][j]!= 0 )
- {
- b = true ;
- if (fangkuai.run_y+y>=array_length- 1
- ||huatu[fangkuai.run_y+y+ 1 ][fangkuai.run_x+j][ 0 ]!= 0 ){
- return false ;
- }
-
- }
-
- }
- if (b)
- y++;
-
- }
-
- return true ;
- }
-
-
-
-
- public int [][] bianXing( int d, int arr[][]){
-
- if (arr== null ||arr[ 0 ]== null )
- return null ;
-
- int arr2[][] = new int [arr.length][arr[ 0 ].length];
-
-
- switch (d) {
- case 1 :
- for ( int i = 0 ; i < arr.length; i++) {
- for ( int j = 0 ; j < arr[i].length; j++) {
- arr2[j][arr[i].length- 1 -i]=arr[i][j];
- }
- }
- break ;
-
- default :
- break ;
- }
-
- for ( int i = 0 ; i < arr2.length; i++) {
-
- for ( int j = 0 ; j < arr2[i].length; j++) {
-
- if (arr2[i][j]!= 0 )
- {
- if (fangkuai.run_x+j>array_length0- 1
- ||fangkuai.run_y+i>array_length- 1 ||fangkuai.run_x+j< 0
- ||huatu[fangkuai.run_y+i][fangkuai.run_x+j][ 0 ]!= 0 )
- return arr;
- }
- }
- }
-
- return arr2;
-
- }
-
- void moveDownInterface(){
- moveDown(fangkuai.xingzhuang);
- }
-
-
-
-
- private void moveDown( int xingzhuang[][]){
-
- if (isCan_down(xingzhuang))
- fangkuai.run_y++;
-
- else
- {
-
-
-
- int y = 0 ; boolean b = false ;
- for ( int i = 0 ; i < xingzhuang.length; i++) {
- for ( int j = 0 ; j < xingzhuang[i].length; j++) {
- if (xingzhuang[i][j]!= 0 )
- {
- huatu[fangkuai.run_y+y][fangkuai.run_x+j][ 0 ] = 1 ;
- huatu[fangkuai.run_y+y][fangkuai.run_x+j][ 1 ]=fangkuai.rgb;
- b= true ;
- }
-
- }
- if (b)
- y++;
-
- }
-
-
- xiaoChu();
- for ( int i = 0 ; i < huatu[ 0 ].length; i++) {
- if (huatu[ 0 ][i][ 0 ] != 0 )
- {
- game_over= true ;
- JOptionPane.showMessageDialog( null , "Game Over" );
- }
- }
- down.play();
- fangkuai=next.fangkuai;
- fangkuai.run_x= 5 ;
- fangkuai.run_y= 0 ;
-
- next.fangkuai=next.createNewFangkui();
- next.fangkuai.run_x= 2 ;
- next.fangkuai.run_y= 3 ;
- next.repaint();
- }
-
- }
- public void xiaoChu(){
-
- boolean xiao = false ;
-
- for ( int i = huatu.length- 1 ; i >= 0 ; i--) {
-
- xiao = false ;
- int j = 0 ;
- for ( j = 0 ; j < huatu[i].length; j++) {
- if (huatu[i][j][ 0 ] == 0 )
- break ;
- }
-
- if (j==huatu[i].length)
- xiao= true ;
-
-
- if (xiao){
- score_int++;
- for ( j = i; j > 0 ; j--) {
- for ( int j2 = 0 ; j2 < huatu[j].length; j2++) {
- huatu[j][j2]=huatu[j- 1 ][j2];
- }
- }
- for ( j = 0 ; j <huatu[ 0 ].length; j++) {
- huatu[ 0 ][j][ 0 ]= 0 ;
- }
- i++;
-
- score.setText(score_int+ "" );
- }
-
- }
- }
-
-
-
-
-
- void moveOther( int d, int xingzhuang[][]){
-
- int dx=d==MOVE_LEFT? 1 :- 1 ;
- if (is_CanMoveOther(d, xingzhuang)){
- fangkuai.run_x+=dx;
- }
- }
- private boolean is_CanMoveOther( int d, int xingzhuang[][]){
-
- int dx=d==MOVE_LEFT? 1 :- 1 ;
- int y= 0 ; boolean has= false ;
- for ( int i = 0 ; i < xingzhuang.length; i++) {
- has= false ;
- for ( int j = 0 ; j < xingzhuang[i].length; j++) {
-
- if (xingzhuang[i][j]!= 0 )
- {
- if (d==MOVE_LEFT&&fangkuai.run_x+j>=array_length0- 1
- ||d==MOVE_RIGHT&&fangkuai.run_x+j<= 0 )
-
- return false ;
- has= true ;
- if (huatu[fangkuai.run_y+y][fangkuai.run_x+j+dx][ 0 ]!= 0 ){
- return false ;
- }
- }
- }
- if (has)
- y++;
- }
-
- return true ;
- }
-
- public void initMusic(){
- URL classpath=getClass().getClassLoader().getResource( "" );
- try {
- left=Applet.newAudioClip( new URL(classpath, "sounds/SOUND8.WAV" ));
- down=left=Applet.newAudioClip( new URL(classpath, "sounds/SOUND104.WAV" ));
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
-
- try {
- FileInputStream fileInputStream= new FileInputStream( "sounds/without_you.wav" );
- backstream= new AudioStream(fileInputStream);
- backplayer=AudioPlayer.player;
- backplayer.start(backstream);
- } catch (Exception e) {
- e.printStackTrace();
- }
-
-
- }
-
-
- @Override
- public void run() {
- while (!game_over)
- {
- if (!stop)
- this .repaint();
- try {
- Thread.sleep(draw_speed);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
-
- }
5. [File] Eluos.java- package com;
-
- import java.awt.Color;
- 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.io.File;
- import java.io.IOException;
-
- import javax.imageio.ImageIO;
- import javax.swing.ImageIcon;
- import javax.swing.JButton;
- import javax.swing.JFrame;
- import javax.swing.JLabel;
- import javax.swing.JOptionPane;
- import javax.swing.JTextArea;
-
- public class Eluos extends JFrame{
-
-
- private Eluo_panel jPanel;
-
- private Mypanel panel_next;
-
- private int this_width= 600 ,this_height= 600 ;
-
- private int panel_width= 300 , panel_height= 400 ;
-
- JButton button,button_stop,button_help;
-
- JLabel score_label,label_score;
-
- public Eluos(){
- this .setSize(this_width, this_height);
-
- panel_next= new Mypanel();
- panel_next.setBackground(Color.RED);
- panel_next.setBounds( 400 , 200 , 200 , 200 );
-
-
- label_score= new JLabel( "0" );
- label_score.setBounds( 450 , 100 , 40 , 20 );
-
- jPanel= new Eluo_panel(panel_width,panel_height,panel_next,label_score);
- jPanel.setBounds( 2 , 2 , panel_width, panel_height);
- jPanel.setBackground(Color.red);
-
-
- button= new JButton( new ImageIcon( "images/start.png" ));
- button.setBounds( 400 , 10 , 100 , 30 );
-
- button_help = new JButton( "Help" );
- button_help.setBounds( 400 , 160 , 100 , 30 );
-
- button_stop= new JButton( new ImageIcon( "images/stop.png" ));
- button_stop.setBounds( 400 , 60 , 100 , 30 );
-
-
- score_label = new JLabel( "Score" );
- score_label.setBounds( 400 , 100 , 40 , 20 );
-
- this .add(button);
- this .add(button_help);
- this .add(button_stop);
- this .setTitle( "My Russia" );
- this .setLayout( null );
- this .add(jPanel);
- this .add(panel_next);
- this .add(score_label);
- this .add(label_score);
- this .setDefaultCloseOperation(EXIT_ON_CLOSE);
- this .setVisible( true );
-
- addListener();
-
-
-
- }
-
- public void addListener(){
-
-
- button_help.addActionListener( new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
-
-
- 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" +
- "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" +
- "Keyboard operation: →: Move the block to the right.\n" +
- "←: Move the block to the left.\n" +
- "↓: Increase the speed at which blocks fall down.\n" +
- "↑: Rotate the falling block.\n" +
- "Click the Restart button to start over.\n" +
- "Click the Pause button to pause the game.\n" ;
-
- JTextArea area= new JTextArea();
- area.setText(message);
- area.setSize( 400 , 400 );
-
- JOptionPane.showMessageDialog(Eluos. this , area);
-
-
- }
- });
- button.addActionListener( new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- jPanel.initGame();
- Eluos. this .requestFocus();
- }
- });
-
- button_stop.addActionListener( new ActionListener() {
-
- @Override
- public void actionPerformed(ActionEvent e) {
- jPanel.stop=!jPanel.stop;
- if (!jPanel.stop)
- Eluos. this .requestFocus();
- }
- });
-
- this .addKeyListener( new KeyListener() {
-
- @Override
- public void keyTyped(KeyEvent e) {
- }
-
- @Override
- public void keyReleased(KeyEvent e) {
-
- }
-
- @Override
- public void keyPressed(KeyEvent e) {
-
- if (jPanel.game_over)
- return ;
- switch (e.getKeyCode()) {
- case KeyEvent.VK_LEFT:
-
- jPanel.left.play();
- Eluos. this .jPanel.moveOther(Eluo_panel.MOVE_RIGHT, Eluos. this .jPanel.fangkuai.xingzhuang);
- break ;
- case KeyEvent.VK_RIGHT:
- jPanel.left.play();
- Eluos. this .jPanel.moveOther(Eluo_panel.MOVE_LEFT, Eluos. this .jPanel.fangkuai.xingzhuang);
- break ;
- case KeyEvent.VK_UP:
- Eluos. this .jPanel.fangkuai.xingzhuang =
- Eluos.this.jPanel.bianXing ( Eluos.this.jPanel.fangkuai.d , Eluos.this.jPanel.fangkuai.xingzhuang ) ;
- break ;
-
- case KeyEvent.VK_DOWN:
- Eluos. this .jPanel.moveDownInterface();
- break ;
- }
- jPanel.repaint();
- }
- });
-
- this .requestFocusInWindow();
-
-
-
-
- }
-
-
- @Override
- public void paint(Graphics g) {
- super .paint(g);
-
- try {
- g.drawImage(ImageIO.read( new File( "images/c.jpg" ))
- , 0 , 0 , null );
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- panel_next.repaint();
- button.repaint();
- button_stop.repaint();
- button_help.repaint();
- score_label.repaint();
- label_score.repaint();
- }
- public static void main(String[] args) {
-
- new Eluos();
-
- }
-
- }
|