Topic: 俄罗斯方块的源码~~~过来看一下 |
Print this page |
1.俄罗斯方块的源码~~~过来看一下 | Copy to clipboard |
Posted by: jiang_sl Posted on: 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; } } |
2.Re:俄罗斯方块的源码~~~过来看一下 [Re: jiang_sl] | Copy to clipboard |
Posted by: jiang_sl Posted on: 2004-03-19 16:34 //接上面 class Workspace { private static final int workspaceWidth = 10; private static final int workspaceHeight = 30; public int getWorkspaceWidth(){ return workspaceWidth;} public int getWorkspaceHeight(){ return workspaceHeight;} private int[][] workspace = new int[workspaceHeight][workspaceWidth]; public void setWorkspaceValue(int x,int y){ workspace[y][x] = 1; } public int getWorkspaceValue(int x,int y){ return workspace[y][x]; } /** *扫描workspace 是否有满的行 *返回一个数组,数组的值为满的行对应的行数 * @return */ public int[] scanWorkspace(){ int w; int[] fullLines = new int[workspaceHeight]; for(int h = 0; h < workspaceHeight; h++){ for(w = 0; w < workspaceWidth; w++){ if(workspace[h][w] == 0) break; } if(w == workspaceWidth) fullLines[h] = h; else fullLines[h] = 0; } int[] lines = {0, 0, 0, 0}; int i = 0; for(int h = 0; h < workspaceHeight; h++){ if(fullLines[h] != 0){ lines[i] = fullLines[h]; i++; } } return lines; } public void deleteLines(int[] lines){ for(int i = 0; i < lines.length; i++){ if(lines[i] != 0){ for(int y = lines[i]; y > 0; y--) for(int x = 0; x < workspaceWidth; x++) workspace[y][x] = workspace[y-1][x]; } } } } class Game extends Thread { private int score; private int level; private int speed; protected int getScore(){ return score; } protected int getLevel(){ return level; } protected int getSpeed(){ return speed; } private int[][] levelSpeed = {{0, 2000, 4000, 6000, 8000,10000,13000,16000,20000,0}, {0, 500, 400, 300, 200, 100, 80, 65, 50, 0}}; private int[] linesScore = {0, 100, 300, 500, 1000 }; private Workspace ws = new Workspace(); private Block block = new Block(); private int[][] newshape = new int[4][4];//用于对方块的打操作 private int shapePosX ; private int shapePosY ; protected synchronized void generateNewShape(){// 产生新的方块 newshape = block.generateShape(); shapePosX = (int)ws.getWorkspaceWidth()/2; shapePosY = ws.getWorkspaceHeight(); } public int getWorkspaceValue(int x,int y){ return ws.getWorkspaceValue(x,y); } /** * 将产生的方块映射到workspace里 * @param x * @param y * @param shape */ protected void blockToWorkspace(int x,int y,int[][] shape){ for(int yy = 0; yy < shape.length; yy++){ for(int xx = 0; xx < shape[yy].length; xx++){ //使方块不会超出workspace的范围 if(shape[yy][xx] == 1 && y+yy < ws.getWorkspaceHeight() && x+xx < ws.getWorkspaceWidth()) { ws.setWorkspaceValue(x+xx,y+yy); } } } } protected boolean canPlace(int x,int y,int[][] shape){ if((x >= 0)&&(x < ws.getWorkspaceWidth())//未越界 &&(y >= 0)&&(y < ws.getWorkspaceHeight())){ for(int yy = 0; yy < block.getBlockCorner(); y++){ for(int xx = 0; xx < block.getBlockCorner(); xx++){ //workspace是否不为空 if((shape[yy][xx] == 1)&&(ws.getWorkspaceValue(xx+x,yy+y) == 1)) return false; } } return true; } else { return false; }//越界 } protected synchronized void moveLeft(){ if(canPlace(shapePosX-1,shapePosY,newshape)){ shapePosX = shapePosX - 1; blockToWorkspace(shapePosX,shapePosY,newshape); } } protected synchronized void moveRight(){ if(canPlace(shapePosX+1,shapePosY,newshape)){ shapePosX = shapePosX + 1; blockToWorkspace(shapePosX,shapePosY,newshape); } } protected synchronized void moveDown(){ if(canPlace(shapePosX,shapePosY+1,newshape)){ shapePosY = shapePosY + 1; blockToWorkspace(shapePosX,shapePosY,newshape); } } protected synchronized void turn(){ int[][] turnedShape = block.turn(newshape); if(shapePosX+1 >= ws.getWorkspaceWidth())//shape挨着右壁 return ; if(canPlace(shapePosX,shapePosY,turnedShape)){ for(int y = 0; y < block.getBlockCorner(); y++) for(int x = 0; x < block.getBlockCorner(); x++){ //shape 紧挨的右壁是否为空的 if(newshape[y][x]==1 && newshape[y][x+1]!=1 && ws.getWorkspaceValue(shapePosX+x+1,shapePosY+y)==1) return ; } newshape = turnedShape; blockToWorkspace(shapePosX,shapePosY,newshape); return ; } else return ; } protected boolean gameOver(){ for(int x = 0; x < ws.getWorkspaceWidth(); x++){ if(ws.getWorkspaceValue(x,0)==1) return true; } return false; } private synchronized void gameHalt(int sp){ try{ Thread.sleep(sp); }catch(InterruptedException e){} } /** * 俄罗斯方块的主要运行函数 */ public void run(){ score = 0; level = 1; speed = 500; generateNewShape();//产生新的方块 blockToWorkspace(shapePosX,shapePosY,newshape); while(true){ System.out.println("game start!!"); gameHalt(speed);//方块停顿 moveDown(); if(!canPlace(shapePosX,shapePosY,newshape)){ int[] lines = ws.scanWorkspace(); ws.deleteLines(lines); System.out.println("game start!!"); int colines = 0;//一次共删除的行数 for(int i = 0; i < lines.length; i++){ if(lines[i] != 0) colines++; } score += linesScore[colines];//加分 if(score >= levelSpeed[0][level]){//够分时过关 level++; speed = levelSpeed[1][level]; } if(gameOver()) return; generateNewShape();//产生新的方块 } } } } |
3.Re:俄罗斯方块的源码~~~过来看一下 [Re: jiang_sl] | Copy to clipboard |
Posted by: prettyding Posted on: 2004-03-26 14:34 高手! |
4.Re:俄罗斯方块的源码~~~过来看一下 [Re: jiang_sl] | Copy to clipboard |
Posted by: sungo Posted on: 2004-03-26 16:18 u can visit it: http://www.javaworld.com.tw/jute/post/view?bid=35&id=5898&sty=1&tpg=1&age=0 Taiwan JavaWorld-程式分享區 |
Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1 客服电话 18559299278 客服信箱 714923@qq.com 客服QQ 714923 |