jiang_sl
发贴: 13
积分: 0
|
于 2004-03-19 16:32
/** *俄罗斯方块的源码 * 我测试了很久都通不过, *程序是没有语法错误的 *大家帮我一把吧, *把程序修改通过,基本要求是尽可能保持我的设计思路,代码修改量最小 *修改完成后,请把完整的代码贴出来, *谢谢~~ **/
import javax.swing.*; import java.awt.*; import java.awt.event.*;
public class Tetris extends JPanel { Game game = new Game(); Workspace ws = new Workspace(); public Tetris() { game.start(); } public void paint(Graphics g){ g.setColor(Color.BLUE); for(int h = 0; h < ws.getWorkspaceHeight(); h++) for(int w = 0; w < ws.getWorkspaceWidth(); w++) if(game.getWorkspaceValue(w,h)==1){ g.fill3DRect(w*12,h*12,11,11,true); } } public void addKeyListener(KeyEvent e){ if(e.getKeyCode() == KeyEvent.VK_LEFT) game.moveLeft(); if(e.getKeyCode() == KeyEvent.VK_DOWN) game.moveDown(); if(e.getKeyCode() == KeyEvent.VK_RIGHT) game.moveRight(); if(e.getKeyCode() == KeyEvent.VK_UP) game.turn(); } /** public void run(){ while(true){ repaint(); } } **/ //public void repaint(){} public static void main(String[] args) { JFrame jf = new JFrame(); Tetris te = new Tetris(); // Thread thread = new Thread(te); // thread.start(); Container cp = jf.getContentPane(); cp.add(te); jf.setSize(350,500); jf.setVisible(true); } }
/** * 方块类定义了方块的基本图形和操作 */ class Block { private static final int BLOCK_WIDTH = 12; // 方块的像素大小 private static final int BLOCK_HEIGHT = 12; private static final int BLOCK_TYPE = 7; //方块的基本种类 private static final int BLOCK_CORNER = 4;//方块的四个转角 public int getBlockWidth() { return BLOCK_WIDTH; } public int getBlockHeight() { return BLOCK_HEIGHT; } public int getBlockType() { return BLOCK_TYPE; } public int getBlockCorner() { return BLOCK_CORNER; } private int[][] emptyShape = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][] tShape = {{0, 1, 0, 0}, {1, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][] leftsShape = {{1, 1, 0, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][] rightsShape= {{0, 1, 1, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][] leftlShape = {{1, 1, 1, 0}, {0, 0, 1, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][] rightlShape = {{1, 1, 1, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][] boxShape = {{1, 1, 0, 0}, {1, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][] stickShape = {{1, 1, 1, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}; private int[][][] shapeContainer = {tShape,leftsShape,rightsShape, leftlShape,rightlShape,boxShape,stickShape}; /** * 产生一个方块 * @return */ public int[][] generateShape() { java.util.Random random = new java.util.Random(); int ran = random.nextInt(BLOCK_TYPE); return shapeContainer[ran]; } public int[][] turn(int[][] shape) { int[][] tempShape = emptyShape; int[][] finalShape = emptyShape; int yy = 0; //向左旋转 shape for(int y = 0; y < BLOCK_CORNER; y++) for(int x = 0; x < BLOCK_CORNER; x++) tempShape[BLOCK_CORNER - 1 -x][y] = shape[y][x]; /*旋转90度后最上一行会变为空, 0 1 0 0 0 0 0 0 * 所以要上移 1 1 1 0 ----------> 0 1 0 0 * 0 0 0 0 ----------> 1 1 0 0 * 0 0 0 0 0 1 0 0 */ for(int y = 0; y < BLOCK_CORNER; y++){ for(int x = 0; x < BLOCK_CORNER; x++){ if(tempShape[y][x] == 1){ yy = y; break; } } break; } for(int y = yy; y < BLOCK_CORNER; y++) for(int x = 0; x < BLOCK_CORNER; x++) finalShape[y-yy][x] = tempShape[y][x]; return finalShape; } }
|