📄 gameframe.java~2~
字号:
this.bgInformation[x][y] = this.bgInformation[x][y-1]; } } /* 获得删的行 */ public int[] getDelLine(){ int[] line = new int[this.getCubeEach()]; int x, y; for(y = this.getCurrentY(); y < this.getCurrentY() + line.length; y++) { if(y >= this.getbgHeight()) break; for(x = 0; x < this.getbgWidth(); x++) if(this.bgInformation[x][y] == 0) break; if(x == this.getbgWidth()) line[y - this.getCurrentY()] = y; else line[y - this.getCurrentY()] = 0; } return line; }}class GameCanvas extends Canvas implements Runnable{ //画布类 private Block block; //方块的对象 private GameFrame gameFrame; private Thread thread; //线程对象 private Color bgColor = Color.white; //背景颜色 private int blockScore = 0, blockLevel = 0, blockSpeed = 0, blockHeight = 0; private boolean isStart = true,isContinue = true; //暂停标志位 private Color colors[] = {Color.red,Color.orange,Color.gray,Color.green, Color.blue,Color.pink,Color.magenta,Color.white}; public GameCanvas(GameFrame game){ block = new Block(); gameFrame = game; thread = new Thread(this,"gamethread"); this.addKeyListener(new KeyAdapter() { //当前对象加入键盘接口 public void keyPressed(KeyEvent e) { handleKeyEvent(e); } }); } public boolean getPause(){ return this.isContinue;} public Color getBGColor(){ return this.bgColor;} public Thread getThread(){ return this.thread; } public void setPause(boolean b){this.isContinue = b;} public void run(){ while(true){ if(!isStart)continue; if(!isContinue)continue; repaint(); this.setSleepTime(); if(!block.goDown()){ this.setBGInformation(); this.delLine(); block.setCurrentXY(4,0); block.setCurrentBlock(); block.setNextBlock(); paintScore(getGraphics()); repaint(); if(this.isStop()){ this.paintStop(); this.isStart = false; gameFrame.mi_pause.enable(false); this.popDialog(); } } } } /* 弹出对话框 */ public void popDialog(){ if(blockScore > BlockDialog.scorethree){ InputDialog inputDialog = new InputDialog(new GameFrame(),"恭喜!!!",true); inputDialog.score.setText(String.valueOf(this.blockScore)); inputDialog.setBounds(200,150,260,220); inputDialog.show(); } } /* 删行 */ public void delLine(){ int oneTime = 0; int[] line = block.getDelLine(); for(int i = 0; i < line.length; i++) { if(line[i] != 0) { block.delLine(line[i]); oneTime++; this.addLevel(); } } this.setScore(oneTime); } /* 判断是否结束 */ public boolean isStop(){ int x; for(x =4 ; x < block.getbgWidth()-5; x++){ if(block.bgInformation[x][1]==1) return true; } return false; } /* 设置速度 */ public void setSpeed(int speed){ this.blockSpeed = speed; } public void setSleepTime(){ int i; if(this.blockSpeed >= 10){ try{ thread.sleep(40); } catch(InterruptedException e){ return; } } else for(i = 10; i>0; i--){ if(10-i == this.blockSpeed) try{ thread.sleep(i*60); } catch(InterruptedException e){ return; } } } /* 设置高度 */ public void setHeight(int height){ this.blockHeight = height; } public void setBlockHeight(){ int i,x,y; for(i = 1; i<10; i++){ if(this.blockHeight == i) for(y = block.getbgHeight()-1; y > block.getbgHeight()-i-5 ; y--) for(x = 0; x < block.getbgWidth(); x++) block.bgInformation[x][y] = (byte)(Math.random()*2);repaint(); } } /* 画图 */ public void paint(Graphics g ){ repaintBG(getGraphics()); if(isStart) paintCurrentBlock(getGraphics()); } public void update(Graphics g){ paint(g); } public void paintScore(Graphics g){ g.setColor(Color.white); g.fillRect(95,70,40,70); } public void repaintBG(Graphics g){ String nextBlock = "下一个方块"; String score = "得分 : ", level = "层数 : " , speed = "速度 : "; g.setColor(Color.red); g.drawString(score+this.blockScore,60,80); g.drawString(level+this.blockLevel,60,110); g.drawString(speed+this.blockSpeed,60,140); g.setColor(Color.blue); g.drawString(nextBlock,50,200); g.setColor(Color.red); g.draw3DRect(190,40,240,400,false); g.setColor(Color.blue); g.draw3DRect(195,45,230,390,true); this.paintBackGround(getGraphics()); this.paintNextBlock(getGraphics()); } /* 计算分数 层数 速度 */ public void setScore(int delLine){ int s = delLine; switch(s){ case 0: this.blockScore += 5 ;break; case 1: this.blockScore += 20;break; case 2: this.blockScore += 60;break; case 3: this.blockScore += 100;break; case 4: this.blockScore += 150;break; } } public void addLevel(){ this.blockLevel++; this.addSpeed(); } public void addSpeed(){ if((this.blockLevel%5) == 0) this.blockSpeed++; } /* 设置背景信息 */ public void setBGInformation(){ int x,y; byte[][] currentBlockTemp; currentBlockTemp = block.getCurrentBlock(); for(x = 0; x < block.getCubeEach(); x++) for(y = 0; y < block.getCubeEach(); y++) if(currentBlockTemp[y][x] == 1) block.bgInformation[block.getCurrentX()+x][block.getCurrentY()+y]=1; } /* 画背景 */ public void paintBackGround(Graphics g){ int x,y; byte[][] currentBlockTemp; currentBlockTemp = block.bgInformation; for(x = 0; x < block.getbgWidth(); x++) for(y = 0; y < block.getbgHeight(); y++) if(currentBlockTemp[x][y] == 1){ g.setColor(new Color(0,128,128)); g.fill3DRect( block.getBGX()+x*block.getCubeHeight(), block.getBGY()+y*block.getCubeWidth(), block.getCubeWidth(),block.getCubeHeight(), true); } else { g.setColor(this.getBGColor()); g.fillRect( block.getBGX()+x*block.getCubeHeight(), block.getBGY()+y*block.getCubeWidth(), block.getCubeWidth(),block.getCubeHeight() ); } } /* 画当前的分数 */ public void paintCurrentBlock(Graphics g){ int x,y; byte[][] currentBlockTemp; g.setColor(block.getCurrentBlockColor()); currentBlockTemp = block.getCurrentBlock(); for(x = 0; x < block.getCubeEach(); x++) for(y = 0; y < block.getCubeEach(); y++) if(currentBlockTemp[y][x] == 1) g.fill3DRect( block.getBGX()+(block.getCurrentX()+x)*block.getCubeWidth(), block.getBGY()+(block.getCurrentY()+y)*block.getCubeHeight(), block.getCubeWidth(),block.getCubeHeight(), false); } /* 画下一个分数 */ public void paintNextBlock(Graphics g){ int x,y; byte[][] nextBlockTemp; clearBlock(getGraphics()); g.setColor(block.getNextBlockColor()); nextBlockTemp = block.getNextBlock(); for(x = 0; x < block.getCubeEach(); x++) for(y = 0; y < block.getCubeEach(); y++) if(nextBlockTemp[y][x] == 1) g.fill3DRect( 60+x*block.getCubeWidth(), 240+y*block.getCubeHeight(), block.getCubeWidth(),block.getCubeHeight(), false); } /* 清除方块 */ public void clearBlock(Graphics g){ int x,y; g.setColor(Color.white); for(x = 0; x < block.getCubeEach(); x++) for(y = 0; y < block.getCubeEach(); y++) g.fillRect( 60+x*block.getCubeWidth(), 240+y*block.getCubeHeight(), block.getCubeWidth(),block.getCubeHeight()); } /* 画停止时的画面 */ public void paintStop(){ int x,y; for(y = block.getbgHeight()-1; y >= 0 ; y--){ for(x = 0; x<block.getbgWidth(); x++){ block.bgInformation[x][y]=1; } this.paintBackGround(getGraphics()); try{thread.sleep(1);}catch(InterruptedException e){ return; } } } /* 加入键盘 */ public void handleKeyEvent(KeyEvent e) { if((this.isStart)&&(this.isContinue)){ if(e.getKeyCode() == SetDialog.getKeyLeft()){ if(block.goLeft()){ repaint(); } } else if(e.getKeyCode() == SetDialog.getKeyUp()){ if(block.goTurn()){ repaint(); } } else if(e.getKeyCode() == SetDialog.getKeyRight()){ if(block.goRight()){ repaint(); } } else if(e.getKeyCode() == SetDialog.getKeyDown()){ if(block.goDown()){ repaint(); } } else if(e.getKeyCode() == SetDialog.getKeyQuickDown()){ if(block.goDown()){ while(block.goDown()){} repaint(); } } } } /* 初始化 */ public void reset(){ int i,x,y; block.setCurrentXY(4,0) ; for(y = block.getbgHeight()-1; y >= 0 ; y--) for(x = 0; x<block.getbgWidth(); x++) block.bgInformation[x][y]=0; setBlockHeight(); this.isStart = true; this.setPause(true); this.blockScore = this.blockLevel = 0; for(i = 0; i < 10; i++) if(gameFrame.mi_speed[i].getState() == true) this.setSpeed(i); for(i = 0; i < 10; i++) if(gameFrame.mi_height[i].getState() == true) this.setHeight(i); block.setNextBlock(); block.reset(); repaint(); paintScore(getGraphics()); }}/* 键盘输入对话框 */class SetDialog extends Dialog { private static int keyLeft = 37, keyUp = 38, keyRight = 39, keyDown = 40, keyQuickDown = 32; private static int tempLeft = 37,tempUp = 38, tempRight = 39, tempDown = 40, tempQuickDown = 32; private KeyEvent e; private Panel panel = new Panel(); private Label labelUP = new Label(); private Label labelLEFT = new Label(); private Label labelRIGHT = new Label(); private Label labelDOWN = new Label(); private Label labelQUICKDOWN = new Label(); private TextField textFieldUP = new TextField(); private TextField textFieldDOWN = new TextField(); private TextField textFieldRIGHT = new TextField(); private TextField textFieldLEFT = new TextField(); private TextField textFieldQUICKDOWN = new TextField(); private Button btnYes = new Button(); private Button btnCancel = new Button(); private Button btnDefault = new Button(); public SetDialog(Frame frame, String title, boolean modal) { super(frame, title, modal); enableEvents(AWTEvent.WINDOW_EVENT_MASK); try { jbInit(); add(panel); pack(); } catch(Exception ex) { ex.printStackTrace(); } } public SetDialog(Frame frame) { this(frame, "", false); } public SetDialog(Frame frame, boolean modal) { this(frame, "", modal); } public SetDialog(Frame frame, String title) { this(frame, title, false); } private void jbInit() throws Exception { panel.setLayout(null); labelUP.setText("旋转"); labelUP.setForeground(Color.blue); labelUP.setBounds(new Rectangle(240, 15, 40, 25)); labelUP.setFont(new java.awt.Font("Dialog", 0, 15)); labelLEFT.setText("左移"); labelLEFT.setForeground(Color.blue); labelLEFT.setBounds(new Rectangle(40, 15, 40, 25)); labelLEFT.setFont(new java.awt.Font("Dialog", 0, 15)); labelRIGHT.setText("右移"); labelRIGHT.setForeground(Color.blue); labelRIGHT.setBounds(new Rectangle(140, 15, 40, 25));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -