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

俄罗斯方块实现

 2009-09-09 00:00:00 来源:WEB开发网   
核心提示: AbsBoxItem类 Java代码 packagecom.zsyao.tetris;importjava.awt.Graphics;importjava.awt.Point;importjava.awt.event.KeyEvent;publicabstractclassAbsBoxItem{

AbsBoxItem类

Java代码

package com.zsyao.tetris; 
 
import java.awt.Graphics; 
import java.awt.Point; 
import java.awt.event.KeyEvent; 
 
public abstract class AbsBoxItem 
{ 
 public static final int WAY_OF_UP = 0; 
 public static final int WAY_OF_RIGHT = 1; 
 public static final int WAY_OF_DOWN = 2; 
 public static final int WAY_OF_LEFT = 3; 
 
 protected int m_x; 
 protected int m_y; 
 protected int m_way = WAY_OF_UP; 
 protected long m_move_space_time;//移动间隔时间 
 protected boolean m_is_stop = false;//是否已停止 
 
 private TetrisComponent m_back_ground; 
 private Point m_point; 
 
 public Box[][] getCurrentBoxItemList() 
 { 
 Box [][] result = null; 
 Box[][] box_item_list = getBoxItemList(); 
  
 int x_size = box_item_list.length; 
 int y_size = box_item_list[0].length; 
  
 switch (m_way) 
 { 
  case WAY_OF_UP: 
  result = box_item_list; 
  break; 
  case WAY_OF_RIGHT: 
  result = new Box[y_size][x_size]; 
  for (int i =0; i < result.length; i++) 
  { 
   for (int j = 0; j < result[i].length; j++) 
   { 
   result[i][j] = box_item_list[j][y_size - 1 - i]; 
   } 
  } 
  break; 
  case WAY_OF_DOWN: 
  result = new Box[x_size][y_size]; 
  for (int i =0; i < result.length; i++) 
  { 
   for (int j = 0; j < result[i].length; j++) 
   { 
   result[i][j] = box_item_list[x_size - 1 - i][y_size - 1 - j]; 
   } 
  } 
  break; 
  case WAY_OF_LEFT: 
  result = new Box[y_size][x_size]; 
  for (int i =0; i < result.length; i++) 
  { 
   for (int j = 0; j < result[i].length; j++) 
   { 
   result[i][j] = box_item_list[x_size - 1 - j][y_size - 1 - i]; 
   } 
  } 
  break; 
 } 
 return result; 
 } 
 
 public void setPoint(Point p) 
 { 
 m_point = p; 
 } 
 
 public int getX() 
 { 
 return m_x; 
 } 
 
 public int getY() 
 { 
 return m_y; 
 } 
 
 public void setX(int x) 
 { 
 m_x = x; 
 } 
 
 public void setY(int y) 
 { 
 m_y = y; 
 } 
 /** 
 * 旋转 
 * 
 */ 
 public boolean turn() 
 { 
 int old_way = m_way; 
 m_way++; 
 m_way = m_way % 4; 
 if (!isCanMove(m_x, m_y)) 
 { 
  m_way = old_way; 
  return false; 
 } 
 return true; 
 } 
 /** 
 * 左移 
 * 
 */ 
 public boolean moveLeft() 
 { 
 m_x--; 
 if (!isCanMove(m_x, m_y)) 
 { 
  m_x++; 
  return false; 
 } 
 return true; 
 } 
 /** 
 * 右移 
 * 
 */ 
 public boolean moveRight() 
 { 
 m_x++; 
 if (!isCanMove(m_x, m_y)) 
 { 
  m_x--; 
  return false; 
 } 
 return true; 
 } 
 /** 
 * 下移 
 * 
 */ 
 public boolean moveDown() 
 { 
 m_y++; 
 if (!isCanMove(m_x, m_y)) 
 { 
  m_y--; 
  return false; 
 } 
 return true; 
 } 
 /** 
 * 键按下事件 
 * @param e 
 */ 
 public void keyPressed(KeyEvent e) 
 { 
 int key = e.getKeyCode(); 
 switch (key) 
 { 
  case KeyEvent.VK_LEFT: 
  moveLeft(); 
  break; 
  case KeyEvent.VK_UP: 
  turn(); 
  break; 
  case KeyEvent.VK_RIGHT: 
  moveRight(); 
  break; 
  case KeyEvent.VK_DOWN: 
  moveDown(); 
  break; 
 } 
 } 
 
 public boolean isStop() 
 { 
 return m_is_stop; 
 } 
 
 public void stop() 
 { 
 m_is_stop = true; 
 } 
 
 /** 
 * 键释放事件 
 * @param e 
 */ 
 public void keyReleased(KeyEvent e) 
 { 
  
 } 
 
 public void setBackGroundInfo(TetrisComponent bg) 
 { 
 m_back_ground = bg; 
 } 
 
 
 private Box[][] getBackGroundItemList() 
 { 
 if (m_back_ground != null) 
  return m_back_ground.getBoxItemList(); 
  
 return null; 
 } 
 
 public void draw(Graphics g) 
 { 
 Box[][] box_item_list = getCurrentBoxItemList(); 
 for (int i = 0; i < box_item_list.length; i++) 
 { 
  for (int j = 0; j < box_item_list[i].length; j++) 
  { 
  Box box = box_item_list[i][j]; 
  if (box != null) 
  { 
   if (m_point != null) 
   { 
   box.draw(g, m_point.x + i * Def.STEP, m_point.y + j * Def.STEP); 
   } 
   else 
   { 
   box.draw(g, (m_x + i) * Def.STEP + 5, (m_y + j) * Def.STEP + 25); 
   } 
//   box.draw(g, m_x + i, m_y + j); 
  } 
  } 
 } 
 } 
 
 public boolean isCanMove(int x, int y) 
 { 
 Box[][] box_item_list = getCurrentBoxItemList(); 
 try 
 { 
  for (int i = 0; i < box_item_list.length; i++) 
  { 
  for (int j = 0; j < box_item_list[i].length; j++) 
  { 
   Box box = box_item_list[i][j]; 
   if (box != null) 
   { 
   Box[][] bg_item_list = getBackGroundItemList(); 
    
   if (x + i > bg_item_list.length - 1) 
    return false; 
   if (y + j > bg_item_list[0].length -1) 
    return false; 
    
   if (getBackGroundItemList()[x + i][y + j] != null) 
    return false; 
   } 
  } 
  } 
 } 
 catch(Exception e) 
 { 
  return false; 
 } 
 return true; 
 } 
 
 protected abstract Box[][] getBoxItemList(); 
} 

Tags:俄罗斯 方块 实现

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