WEB开发网
开发学院软件开发Java 俄罗斯方块实现 阅读

俄罗斯方块实现

 2009-09-09 00:00:00 来源:WEB开发网   
核心提示:主要包括三个主类:TetrisComponent(主界面),AbsBoxItem(俄罗斯方块中各种图形的基类),俄罗斯方块实现,BoxItemManager(图形的管理类) 还有其他的实现类: BoxItem1、BoxItem2...可自行添加实现,在添加完实现后

主要包括三个主类:TetrisComponent(主界面),AbsBoxItem(俄罗斯方块中各种图形的基类),BoxItemManager(图形的管理类)

还有其他的实现类:

BoxItem1、BoxItem2...可自行添加实现,在添加完实现后,需要添加管理类(BoxItemManager.java)中:

Java代码

static 
 { 
 BoxItemManager.addAbsBoxItem("com.zsyao.tetris.item.BoxItem1"); 
 BoxItemManager.addAbsBoxItem("com.zsyao.tetris.item.BoxItem2"); 
...... 
 } 

TetrisComponent类

Java代码

package com.zsyao.tetris; 
 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Font; 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.KeyEvent; 
import java.awt.event.KeyListener; 
 
import javax.swing.JComponent; 
 
@SuppressWarnings("serial") 
public class TetrisComponent extends JComponent implements KeyListener 
{ 
 private Box [][] m_box_list = new Box[10][20]; 
 private int m_add_point = 50; 
 private int m_point = 0; 
 private AbsBoxItem m_current_box_item; 
 private AbsBoxItem m_next_box_item = BoxItemManager.getRandomAbsBox(); 
 private MoveThread m_move_thread = new MoveThread(); 
 private boolean m_need_clear = false; 
 private boolean m_is_start = false; 
 private boolean m_is_pause = false; 
 private int m_level = 0; 
 private int m_use_count = 0;//使用的数量 
 private boolean m_is_game_over = false; 
 
 public TetrisComponent() 
 { 
 this.setSize(new Dimension(800, 600)); 
 this.addKeyListener(this); 
 this.setLayout(null); 
 m_move_thread.start(); 
 } 
 
 public static void main(String [] args) 
 { 
 new TetrisClient().setVisible(true); 
 } 
 
 public void update(Graphics g) 
 { 
 super.update(g); 
 } 
 
 public void paint(Graphics g) 
 { 
 this.setDoubleBuffered(true); 
 super.paint(g); 
  
 g.draw3DRect(5, 25, m_box_list.length * Def.STEP, m_box_list[0].length * Def.STEP, true); 
  
 for (int i = 0; i < m_box_list.length; i++) 
 { 
  for (int j = 0; j < m_box_list[i].length; j++) 
  { 
  if (m_box_list[i][j] != null) 
  { 
   m_box_list[i][j].draw(g, i * Def.STEP + 5, j * Def.STEP + 25); 
  } 
  } 
 } 
  
 if (m_need_clear) 
 { 
  int count = 0; 
  for (int j = m_box_list[0].length - 1; j >= 0; j--) 
  { 
  boolean is_can_clear = true; 
   
  for (int i = m_box_list.length - 1; i >= 0; i--) 
  { 
   if (m_box_list[i][j] == null) 
   { 
   is_can_clear = false; 
   break; 
   } 
  } 
   
  if (is_can_clear) 
  { 
   for (int j2 = j; j2 >=1 ;j2--) 
   { 
   for (int i = m_box_list.length - 1; i >= 0; i--) 
   { 
    m_box_list[i][j2] = m_box_list[i][j2 - 1]; 
   } 
   } 
   for (int i = m_box_list.length - 1; i >= 0; i--) 
   { 
   m_box_list[i][0] = null; 
   } 
   j++; 
   m_point += m_add_point + m_add_point * count / 2; 
   count++; 
  } 
  } 
  m_need_clear = false; 
 } 
  
 if (m_current_box_item != null) 
 { 
  m_current_box_item.draw(g); 
 } 
 else 
 { 
  if (m_is_start && !m_is_game_over) 
  { 
  AbsBoxItem abs_box_item = m_next_box_item; 
  m_next_box_item = BoxItemManager.getRandomAbsBox(); 
  abs_box_item.setPoint(null); 
  abs_box_item.setBackGroundInfo(this); 
  addBoxItem(abs_box_item); 
  } 
 } 
  
 if (m_use_count == 50) 
 { 
  m_level++; 
  m_use_count = 0; 
 } 
  
 if (m_is_game_over) 
 { 
  g.setColor(Color.red); 
  g.setFont(new Font("Serif", Font.BOLD, 25)); 
  g.drawString("Game Over", m_box_list.length * Def.STEP / 2 - 50, m_box_list[0].length * Def.STEP /2); 
  g.setColor(Color.black); 
 } 
  
 //显示下一个 
 int view_next_x = m_box_list.length * Def.STEP + 10; 
 g.draw3DRect(view_next_x, 25, 5 * Def.STEP + 10, 5 * Def.STEP + 10, true); 
  
 if (m_is_start) 
 { 
  m_next_box_item.setPoint(new Point(view_next_x + 5, 30)); 
  m_next_box_item.draw(g); 
 } 
  
 g.setColor(Color.black); 
 g.setFont(new Font("Serif", Font.BOLD, 14)); 
 g.drawString("Key", view_next_x, 5 * Def.STEP + 50); 
 g.drawString("S:Start Game", view_next_x, 5 * Def.STEP + 70); 
 g.drawString("P:Pause Or Continue Game", view_next_x, 5 * Def.STEP + 90); 
 g.drawString("N:Next Level", view_next_x, 5 * Def.STEP + 110); 
 g.drawString("→:Right", view_next_x, 5 * Def.STEP + 130); 
 g.drawString("←:Left", view_next_x, 5 * Def.STEP + 150); 
 g.drawString("↑:Turn", view_next_x, 5 * Def.STEP + 170); 
 g.drawString("↓:Down", view_next_x, 5 * Def.STEP + 190); 
 g.setColor(Color.red); 
 g.drawString("Level:" + m_level, view_next_x, 5 * Def.STEP + 210); 
 g.drawString("Point:" + m_point, view_next_x, 5 * Def.STEP + 230); 
 g.setColor(Color.black); 
  
 if(!m_is_game_over && m_is_pause) 
 { 
  g.setColor(Color.red); 
  g.setFont(new Font("Serif", Font.BOLD, 25)); 
  g.drawString("Pause Game", m_box_list.length * Def.STEP / 2 - 50, m_box_list[0].length * Def.STEP /2); 
  g.setColor(Color.black); 
 } 
 } 
 
 public Box[][] getBoxItemList() 
 { 
 return m_box_list; 
 } 
 
 public AbsBoxItem getNextBoxItem() 
 { 
 return m_next_box_item; 
 } 
 
 public void addBoxItem(AbsBoxItem box_item) 
 { 
 m_current_box_item = box_item; 
  
 if (m_current_box_item.isCanMove(4, 0)) 
 { 
  m_current_box_item.setX(4); 
  m_current_box_item.setY(0); 
  m_use_count++; 
 } 
 else 
 { 
  m_current_box_item.setX(4); 
  m_current_box_item.setY(0); 
  m_is_game_over = true; 
  m_is_start = false; 
 } 
 this.repaint(); 
 } 
 
 public void saveCurrentBoxItem() 
 { 
 int x = m_current_box_item.getX(); 
 int y = m_current_box_item.getY(); 
  
 Box[][] box_list = m_current_box_item.getCurrentBoxItemList(); 
 for (int i = 0; i < box_list.length; i++) 
 { 
  for (int j = 0; j < box_list[i].length; j++) 
  { 
  Box box = box_list[i][j]; 
  if (box != null) 
  { 
   m_box_list[x + i][y + j] = box; 
  } 
  } 
 } 
 m_current_box_item = null; 
 m_need_clear = true; 
 } 
 
 public class MoveThread extends Thread 
 { 
 public void run() 
 { 
  while(true) 
  { 
  try 
  { 
   if (m_level * 50 < 1000) 
   sleep(1000 - m_level * 50); 
   else 
   sleep(1); 
  } 
  catch (InterruptedException e) 
  { 
  } 
   
  if (m_is_start && !m_is_pause) 
  { 
   if (m_current_box_item != null) 
   { 
   if (m_current_box_item.moveDown()) 
   { 
    TetrisComponent.this.repaint(); 
   } 
   else 
   { 
    saveCurrentBoxItem(); 
    TetrisComponent.this.repaint(); 
   } 
   } 
  } 
  } 
 } 
 } 
 
 public void pauseGame() 
 { 
 m_is_pause = true; 
 this.repaint(); 
 } 
 
 public void continueGame() 
 { 
 m_is_pause = false; 
 this.repaint(); 
 } 
 
 public void startGame() 
 { 
 m_box_list = new Box[10][20]; 
 m_current_box_item = null; 
 m_use_count = 0; 
 m_is_pause = false; 
 m_is_game_over = false; 
  
 m_is_start = true; 
 this.repaint(); 
 } 
 
 public void nextLevel() 
 { 
 if (!m_is_start) 
 { 
  if (m_level >= 50) 
  return; 
  m_level++; 
  this.repaint(); 
 } 
 } 
 
 public void keyPressed(KeyEvent e) 
 { 
 if (e.getKeyCode() == KeyEvent.VK_P) 
 { 
  if (m_is_pause) 
  { 
  this.continueGame(); 
  } 
  else 
  { 
  this.pauseGame(); 
  } 
  return; 
 } 
 if (e.getKeyCode() == KeyEvent.VK_S) 
 { 
  this.startGame(); 
  return; 
 } 
 if (e.getKeyCode() == KeyEvent.VK_N) 
 { 
  this.nextLevel(); 
  return; 
 } 
 if (m_current_box_item != null) 
 { 
  m_current_box_item.keyPressed(e); 
  this.repaint(); 
 } 
 } 
 
 
 public void keyReleased(KeyEvent e) 
 { 
 if (m_current_box_item != null) 
 { 
  m_current_box_item.keyReleased(e); 
  this.repaint(); 
 } 
 } 
 
 public void keyTyped(KeyEvent e) 
 { 
  
 } 
} 

1 2 3  下一页

Tags:俄罗斯 方块 实现

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接