⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 teris.java

📁 A java source code to teach you how to program a Teris game. Using swing and awt
💻 JAVA
字号:
import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;

/**
 * <p>Title: Teris</p>
 * <p>Description: Teris Java Demo</p>
 * @author not attributable
 * @version 1.0
 */

public class Teris extends JFrame {
  private Image image = null;
  private Graphics img = null;
  private Random rand = new Random();

  //Teris Data Group
  private final int cubes_x = 110,cubes_y = 50;
  private final int cubes_height = 25;
  private final int cubes_width = 10;
  private boolean cubes[][] = null;
  private Color cubes_color[][] = null;
  private int dispose_lines = 0;
  private final int STATUS_NONE = 0;
  private final int STATUS_PLAYING = 1;
  private final int STATUS_PAUSE = 2;
  private final int STATUS_END = 3;
  private int status = STATUS_NONE;
  private int current_cube = 0;
  private int next_cube = 0;
  private int cube_direction = 0;
  private int cube_posi_x = 0;
  private int cube_posi_y = 21;

  public Teris() {
    try {
      jbInit();
      setSize(350,350);
      setVisible(true);
      image = this.createImage(400,400);
      img = image.getGraphics();

      cubes = new boolean[cubes_width][];
      for(int i = 0; i < cubes_width; i++) cubes[i] = new boolean[cubes_height];
      cubes_color = new Color[cubes_width][];
      for(int i = 0; i < cubes_width; i++) cubes_color[i] = new Color[cubes_height];

      initTeris();
      refreshImage();
      new Game().start();
    }
    catch(Exception e) {
      e.printStackTrace();
    }
  }

  private void initTeris()
  {
    for(int i = 0; i < cubes_width; i ++)
      for(int j = 0; j < cubes_height; j++)
      {
        cubes[i][j] = false;
        cubes_color[i][j] = Color.blue;
      }

    dispose_lines = 0;
    current_cube = rand.nextInt(Cube.total);
    next_cube = rand.nextInt(Cube.total);
    cube_direction = 0;
    cube_posi_x = 4;
    cube_posi_y = 21;
  }

  private boolean isHit(int posix,int posiy,int rotate)
  {
    int x = this.cube_posi_x + posix;
    int y = this.cube_posi_y - posiy;
    if(y < 0) return true;
    if(x < 0) return true;

    int d = (this.cube_direction + rotate) % 4;
    if((x+Cube.cubex[this.current_cube][d]) > cubes_width) return true;
    //x = Math.min(x,this.cubes_width-Cube.cubex[this.current_cube][d]);

    y = y + Cube.cubey[current_cube][d] - 1;

    int sx = Cube.cubex[current_cube][d];
    int sy = Cube.cubey[current_cube][d];
    for(int i = 0; i < sx; i++)
      for(int j = 0; j < sy; j++)
      {
        if((y-j) < 0) return true;
        if(Cube.shapes[current_cube][d][i][j] & this.cubes[x + i][y - j])
          return true;
      }

    return false;
  }

  private void refreshImage()
  {

    img.setColor(Color.black);
    img.drawRect(cubes_x-5,cubes_y-5,8+12*cubes_width,8+12*(cubes_height-4));
    img.setColor(Color.white);
    img.fillRect(cubes_x-4,cubes_y-4,6+12*cubes_width,6+12*(cubes_height-4));

    for(int i = 0; i < cubes_width; i ++)
      for(int j = 0; j < cubes_height - 4; j++)
      {
        if(cubes[i][j] == true)
        {
          paintCube(i,j,cubes_color[i][j]);
        }
      }

    //command
    img.setColor(Color.red);
    img.drawString("UP : Rotate",10,200);
    img.drawString("LEFT : Move L",10,210);
    img.drawString("RIGHT : Move R",10,220);
    img.drawString("P : Pause",10,230);
    img.drawString("S : (re)start",10,240);

    //lines
    img.setColor(Color.black);
    img.drawRect(250,50,80,40);
    img.setColor(Color.white);
    img.fillRect(251,51,78,38);
    img.setColor(Color.blue);
    img.drawString("Lines : "+this.dispose_lines,255,75);

    //next cube
    img.setColor(Color.black);
    img.drawRect(15,50,70,100);
    img.setColor(Color.white);
    img.fillRect(16,51,68,98);
    img.setColor(Color.blue);
    img.drawString("Next",35,65);
    int x = Cube.cubex[this.next_cube][0];
    int y = Cube.cubey[this.next_cube][0];
    for(int i = 0; i < x; i++)
      for(int j = 0; j < y; j++)
      {
        if(Cube.shapes[next_cube][0][i][j])
        {
          img.setColor(Color.black);
          img.drawRect(25+i*12,80+j*12,10,10);
          img.setColor(Cube.colors[next_cube]);
          img.fillRect(27+i*12,82+j*12,8,8);
        }
      }

    //current_cube
    x = Cube.cubex[current_cube][cube_direction];
    y = Cube.cubey[current_cube][cube_direction];
    for(int i = 0; i < x; i++)
      for(int j = 0; j < y; j++)
      {
        if(Cube.shapes[current_cube][cube_direction][i][j])
        {
          int p = cube_posi_y+y-1-j;
          if((p) >=0 & (p) <= 20)
            paintCube(cube_posi_x+i,p,Cube.colors[current_cube]);
        }
      }

    //ENDING GAME
    if(status == STATUS_END)
    {
      img.setColor(Color.black);
      img.drawRect(cubes_x+10,cubes_y+100,100,50);
      img.setColor(Color.yellow);
      img.fillRect(cubes_x+11,cubes_y+101,98,48);
      img.setColor(Color.red);
      img.drawString("END GAME!!!",cubes_x+21,cubes_y+131);
    }
    else if(status == STATUS_PAUSE)
    {
      img.setColor(Color.black);
      img.drawRect(cubes_x+10,cubes_y+100,100,50);
      img.setColor(Color.yellow);
      img.fillRect(cubes_x+11,cubes_y+101,98,48);
      img.setColor(Color.red);
      img.drawString("PAUSE!!!",cubes_x+41,cubes_y+131);
    }

    this.repaint();
  }

  private void paintCube(int x, int y, Color c)
  {
    img.setColor(Color.black);
    img.drawRect(cubes_x + x*12, cubes_y+12*(cubes_height-5-y),10,10);
    img.setColor(c);
    img.fillRect(cubes_x + x*12 + 2, cubes_y+12*(cubes_height-5-y)+2,8,8);
  }

  public void paint(Graphics g)
  {
    if(image != null)
    {
      g.drawImage(image,0,0,null);
    }

  }

  public static void main(String[] args) {
    Teris teris = new Teris();
  }
  private void jbInit() throws Exception {
    this.getContentPane().setBackground(Color.white);
    this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    this.setResizable(false);
    this.setTitle("Teris");
    this.addKeyListener(new Teris_this_keyAdapter(this));
  }

  class Game extends Thread
  {
    public boolean run = true;
    private long time = 0;
    private long sec = 0;
    private long bt;


    public void run()
    {
      bt = time;
//      status = STATUS_PLAYING;
//      try{
        while (run) {
STATUS:   switch (status) {
            case STATUS_NONE:
            case STATUS_END:
              switch(KeyQ.get())
              {
                case KeyEvent.VK_S:
                  initTeris();
                  bt = time;
                  status = STATUS_PLAYING;
                  break STATUS;
              }
              break;
            case STATUS_PAUSE:
              switch(KeyQ.get())
              {
                case KeyEvent.VK_P:
                  status = STATUS_PLAYING;
                  break STATUS;
              }
              refreshImage();
              break;
            case STATUS_PLAYING:
              switch(KeyQ.get())
              {
                case KeyEvent.VK_P:
                  status = STATUS_PAUSE;
                  break STATUS;
                case KeyEvent.VK_RIGHT:
                  if(isHit(1,0,0) == false)
                  {
                    cube_posi_x++;
                  }
                  break;
                case KeyEvent.VK_LEFT:
                  if(isHit(-1,0,0) == false)
                  {
                    cube_posi_x--;
                  }
                  break;
                case KeyEvent.VK_UP:
                  if(isHit(0,0,3) == false)
                  {
                    cube_direction = (cube_direction+3)%4;
                  }
                  break;
                case KeyEvent.VK_S:
                  initTeris();
                  bt = time;
                  status = STATUS_PLAYING;
                  break STATUS;
              }

              long t = System.currentTimeMillis();
              time++;

              if((time-bt)>20)
              {
                if(isHit(0,1,0))
                {
                  int sx = Cube.cubex[current_cube][cube_direction];
                  int sy = Cube.cubey[current_cube][cube_direction];

                  if((cube_posi_y+sy-1)>=21)
                  {
                    status = STATUS_END;
                  }

                  for(int i = 0; i < sx; i++)
                    for(int j = 0; j < sy; j++)
                    {
                      if(Cube.shapes[current_cube][cube_direction][i][j])
                      {
                        cubes[i + cube_posi_x][cube_posi_y+sy-1 - j] = true;
                        cubes_color[i + cube_posi_x][cube_posi_y+sy-1 - j]
                          = Cube.colors[current_cube];
                      }
                    }

                  int p = 0;
                  for(int j = 0; j < cubes_height; j++)
                  {
                    int i;
                    for(i = 0; i < cubes_width; i++)
                      if(cubes[i][j] == false) break;
                    if(i < cubes_width)
                    {
                      if(j > p)
                      {
                        for(i = 0; i < cubes_width; i++)
                        {
                          cubes[i][p] = cubes[i][j];
                          cubes_color[i][p] = cubes_color[i][j];
                        }
                      }
                      p++;
                    }
                    else dispose_lines++;
                  }
                  for(int j = p; j < cubes_height; j++)
                    for(int i = 0; i < cubes_width; i++)
                      cubes[i][j] = false;

                  current_cube = next_cube;
                  next_cube = rand.nextInt(Cube.total);
                  cube_posi_y = 21;
                  cube_posi_x = 4;
                }
                else cube_posi_y--;
              }

              refreshImage();
              long t1 = System.currentTimeMillis();
              //if((t1 - t) < 30) sleep(t1-t);
              break;
          }
          try{sleep(100);}catch(Exception ex){}
        }
/*      }
      catch(Exception ex)
      {
        JOptionPane.showMessageDialog(null,ex.toString(),"ERROR",
                                      JOptionPane.ERROR_MESSAGE);
      }*/
    }
  }

  static private class Cube
  {
    static private final boolean shapes[][][][] =
      {
        {
          {{true},{true},{true},{true}},
          {{true,true,true,true}},
          {{true},{true},{true},{true}},
          {{true,true,true,true}}
        },
        {
          {{true,false,false},{true,true,true}},
          {{false,true},{false,true},{true,true}},
          {{true,true,true},{false,false,true}},
          {{true,true},{true,false},{true,false}}
        },
        {
          {{true,true,true},{true,false,false}},
          {{true,false},{true,false},{true,true}},
          {{false,false,true},{true,true,true}},
          {{true,true},{false,true},{false,true}}
        },
        {
          {{true,true},{true,true}},
          {{true,true},{true,true}},
          {{true,true},{true,true}},
          {{true,true},{true,true}}
        },
        {
          {{true,true,false},{false,true,true}},
          {{false,true},{true,true},{true,false}},
          {{true,true,false},{false,true,true}},
          {{false,true},{true,true},{true,false}}
        },
        {
          {{false,true,true},{true,true,false}},
          {{true,false},{true,true},{false,true}},
          {{false,true,true},{true,true,false}},
          {{true,false},{true,true},{false,true}}
        }
      };
    static private final int cubex[][] = {{4,1,4,1},{2,3,2,3},{2,3,2,3},
        {2,2,2,2},{2,3,2,3},{2,3,2,3}};
    static private final int cubey[][] = {{1,4,1,4},{3,2,3,2},{3,2,3,2},
        {2,2,2,2},{3,2,3,2},{3,2,3,2}};
    static private final Color colors[] = {Color.red,Color.blue,Color.cyan,Color.green,
      Color.darkGray,Color.yellow};
    static public final int total = 6;
  }

  void this_keyTyped(KeyEvent e) {
  }

  static class KeyQ{
    private static int[] q = new int[10];
    private static int f = 0;
    private static int b = 0;

    static int get()
    {
      if(f == b) return 0;
      int ans = q[f++];
      f = f%10;
      return ans;
    }

    static void push(int key)
    {
      if(((b+1)%10) == f) return;
      q[b] = key;
      b = (b+1)%10;
    }

    static void clearAll()
    {
      b = f;
    }
  }

  void this_keyPressed(KeyEvent e) {
    KeyQ.push(e.getKeyCode());
  }

}

class Teris_this_keyAdapter extends java.awt.event.KeyAdapter {
  Teris adaptee;

  Teris_this_keyAdapter(Teris adaptee) {
    this.adaptee = adaptee;
  }
  public void keyTyped(KeyEvent e) {
    adaptee.this_keyTyped(e);
  }
  public void keyPressed(KeyEvent e) {
    adaptee.this_keyPressed(e);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -