package MyGame;/*** Created by zu on 2015/3/30.*/
/*
整个游戏界面分为两部分,左边显示信息,右边是游戏区,是两个JPanel,它们的父窗口是MainWindowFrame。*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class MainWindowFrame extends JFrame
{public MainWindowFrame(){setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);setResizable(false);//不可设置大小infoPanel=new InfoPanel();gamePanel=new GamePanel();timer=new Timer(500,new timeActionListener());//新建一个定时器对象,每0.5s触发一次Container contentPane=getContentPane();contentPane.add(infoPanel,BorderLayout.WEST);contentPane.add(gamePanel,BorderLayout.CENTER);//全局键盘监控事件Toolkit toolkit = Toolkit.getDefaultToolkit();toolkit.addAWTEventListener(new ImplAWTEventListener(), AWTEvent.KEY_EVENT_MASK);}public static void startTimer(){timer.start();}public static void stopTimer(){timer.stop();}private static Timer timer;//每触发一次,方块便向下移动一格private class timeActionListener implements ActionListener{public void actionPerformed(ActionEvent e){gamePanel.move(DIRECTION_NONE);infoPanel.setScore(gamePanel.checkLine());infoPanel.repaint();}}//全局事件,用来监控键盘输入class ImplAWTEventListener implements AWTEventListener{@Overridepublic void eventDispatched(AWTEvent event) {if (event.getClass() == KeyEvent.class) {// 被处理的事件是键盘事件.KeyEvent keyEvent = (KeyEvent) event;if (keyEvent.getID() == KeyEvent.KEY_PRESSED) {//按下时你要做的事情keyPressed(keyEvent);} else if (keyEvent.getID() == KeyEvent.KEY_RELEASED) {//放开时你要做的事情keyReleased(keyEvent);}}}private void keyPressed(KeyEvent e){int keyCode=e.getKeyCode();if(keyCode==KeyEvent.VK_LEFT){gamePanel.move(DIRECTION_LEFT);infoPanel.setScore(gamePanel.checkLine());}else if(keyCode==KeyEvent.VK_RIGHT){gamePanel.move(DIRECTION_RIGHT);infoPanel.setScore(gamePanel.checkLine());}else if(keyCode==KeyEvent.VK_UP){gamePanel.changePosition();infoPanel.setScore(gamePanel.checkLine());}else if(keyCode==KeyEvent.VK_DOWN){gamePanel.moveToBottom();infoPanel.setScore(gamePanel.checkLine());}}private void keyReleased(KeyEvent event) {}}InfoPanel infoPanel;GamePanel gamePanel;private final int DEFAULT_WIDTH=550;private final int DEFAULT_HEIGHT=633;private int DIRECTION_LEFT=-1;private int DIRECTION_RIGHT=1;private int DIRECTION_NONE=0;
}
package MyGame;/*** Created by zu on 2015/3/30.*/
/*
存储各种block的类,每个类都有一个getPosition方法,这个方法接受一个参考点的坐标,然后返回其他各点坐标*/
public class Blocks
{public int[][] getLocation(int x,int y){return new int[4][2];}public void changePosition(){currentMethod++;if(currentMethod>3)currentMethod=0;}protected int currentMethod=0;}class OneBlock extends Blocks//立形
{public int[][] getLocation(int x,int y){int[][] points=new int[4][2];points[0]=new int[]{x,y};if(currentMethod==0){points[1]=new int[]{x-1,y};points[2]=new int[]{x+1,y};points[3]=new int[]{x,y-1};}else if(currentMethod==1){points[1]=new int[]{x,y-1};points[2]=new int[]{x,y-2};points[3]=new int[]{x-1,y-1};}else if(currentMethod==2){points[1]=new int[]{x,y-1};points[2]=new int[]{x-1,y-1};points[3]=new int[]{x+1,y-1};}else if(currentMethod==3){points[1]=new int[]{x,y-1};points[2]=new int[]{x,y-2};points[3]=new int[]{x+1,y-1};}return points;}}class LeftTwoBlock extends Blocks
{public int[][] getLocation(int x,int y){int[][] points=new int[4][2];points[0]=new int[]{x,y};if(currentMethod==0){points[1]=new int[]{x,y-1};points[2]=new int[]{x,y-2};points[3]=new int[]{x-1,y-2};}else if(currentMethod==1){points[1]=new int[]{x-1,y};points[2]=new int[]{x+1,y};points[3]=new int[]{x+1,y-1};}else if(currentMethod==2){points[1]=new int[]{x,y-1};points[2]=new int[]{x,y-2};points[3]=new int[]{x+1,y};}else if(currentMethod==3){points[1]=new int[]{x,y-1};points[2]=new int[]{x+1,y-1};points[3]=new int[]{x+2,y-1};}return points;}
}class RightTwoBlock extends Blocks
{public int[][] getLocation(int x,int y){int[][] points=new int[4][2];points[0]=new int[]{x,y};if(currentMethod==0){points[1]=new int[]{x,y-1};points[2]=new int[]{x,y-2};points[3]=new int[]{x+1,y-2};}else if(currentMethod==1){points[1]=new int[]{x,y-1};points[2]=new int[]{x-1,y-1};points[3]=new int[]{x-2,y-1};}else if(currentMethod==2){points[1]=new int[]{x-1,y};points[2]=new int[]{x,y-1};points[3]=new int[]{x,y-2};}else if(currentMethod==3){points[1]=new int[]{x,y-1};points[2]=new int[]{x+1,y};points[3]=new int[]{x+2,y};}return points;}
}class LeftThreeBlock extends Blocks
{public int[][] getLocation(int x,int y){int[][] points=new int[4][2];points[0]=new int[]{x,y};if(currentMethod==0||currentMethod==2){points[1]=new int[]{x,y-1};points[2]=new int[]{x-1,y-1};points[3]=new int[]{x-1,y-2};}else if(currentMethod==1||currentMethod==3){points[1]=new int[]{x-1,y};points[2]=new int[]{x,y-1};points[3]=new int[]{x+1,y-1};}return points;}
}class RightThreeBlock extends Blocks
{public int[][] getLocation(int x,int y){int[][] points=new int[4][2];points[0]=new int[]{x,y};if(currentMethod==0||currentMethod==2){points[1]=new int[]{x,y-1};points[2]=new int[]{x+1,y-1};points[3]=new int[]{x+1,y-2};}else if(currentMethod==1||currentMethod==3){points[1]=new int[]{x+1,y};points[2]=new int[]{x,y-1};points[3]=new int[]{x-1,y-1};}return points;}
}class FourBlock extends Blocks
{public int[][] getLocation(int x,int y){int[][] points=new int[4][2];points[0]=new int[]{x,y};points[1]=new int[]{x+1,y};points[2]=new int[]{x,y-1};points[3]=new int[]{x+1,y-1};return points;}
}class FiveBlock extends Blocks
{public int[][] getLocation(int x,int y){int[][] points=new int[4][2];points[0]=new int[]{x,y};if(currentMethod==0||currentMethod==2){points[1]=new int[]{x,y-1};points[2]=new int[]{x,y-2};points[3]=new int[]{x,y-3};}else if(currentMethod==1||currentMethod==3){points[1]=new int[]{x+1,y};points[2]=new int[]{x+2,y};points[3]=new int[]{x-1,y};}return points;}
}
package MyGame;/*** Created by zu on 2015/3/30.*/
/*
用来显示游戏信息的,还包含一个next显示面板*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.geom.*;public class InfoPanel extends JPanel
{public InfoPanel(){setBounds(POSITION_X,POSITION_Y,WIDTH,HEIGHT);setLayout(new GridLayout(6,1));JLabel nextLabel=new JLabel("Next");nextLabel.setFont(new Font("Serif",Font.BOLD,30));JLabel scoreLabel=new JLabel("Score");scoreLabel.setFont(new Font("Serif",Font.BOLD,30));//显示分数的面板displayScoreLabel.setFont(new Font("Serif",Font.BOLD,30));//开始按钮JButton startButton=new JButton("Start");startButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {MainWindowFrame.startTimer();}});//暂停按钮JButton pauseButton=new JButton("Pause");pauseButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {MainWindowFrame.stopTimer();}});//重新开始按钮JButton restartButton=new JButton("Restart");restartButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GamePanel.resetGamePanel();score=0;setScore(0);}});JPanel buttonPanel1=new JPanel();buttonPanel1.add(startButton);buttonPanel1.add(pauseButton);JPanel buttonPanel2=new JPanel();buttonPanel2.add(restartButton);add(nextLabel);add(nextPaintPanel);add(scoreLabel);add(displayScoreLabel);add(buttonPanel1);add(buttonPanel2);}//设置分数public void setScore(int s){score+=s*100;displayScoreLabel.setText(String.valueOf(score));}private final int POSITION_X=0;private final int POSITION_Y=0;private final int WIDTH=200;private final int HEIGHT=600;private JLabel displayScoreLabel=new JLabel("0");private int score=0;private paintPanel nextPaintPanel=new paintPanel();
}
//显示next的面板
class paintPanel extends JPanel
{public paintPanel(){nextBlock=getNextBlock();}public void paintComponent(Graphics g){super.paintComponent(g);Graphics2D g2=(Graphics2D)g;int[][] locations=nextBlock.getLocation(4,4);g2.setPaint(Color.BLUE);for(int i=0;i<4;i++){Rectangle2D rectangle=new Rectangle2D.Double(locations[i][0]*POINT_DISTANCE-1,locations[i][1]*POINT_DISTANCE-1,BLOCK_WIDTH,BLOCK_HEIGHT);g2.fill(rectangle);}}//获得下一个block的方法private static Blocks getNextBlock(){int i=(int)(Math.random()*7);if(i==0)nextBlock=new OneBlock();else if(i==1)nextBlock=new LeftTwoBlock();else if(i==2)nextBlock=new RightTwoBlock();else if(i==3)nextBlock=new LeftThreeBlock();else if(i==4)nextBlock=new RightThreeBlock();else if(i==5)nextBlock=new FourBlock();else if(i==6)nextBlock=new FiveBlock();return nextBlock;}//向GamePanel传递block的方法public static Blocks getBlock(){Blocks temp=nextBlock;nextBlock=getNextBlock();return temp;}private static Blocks nextBlock;private final double BLOCK_WIDTH=10;private final double BLOCK_HEIGHT=10;private final double POINT_DISTANCE=12;
}
package MyGame;
/*** Created by zu on 2015/3/30.*/
import java.awt.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;public class GamePanel extends JPanel
{public GamePanel(){for(int i=0;i<20;i++){for(int j=0;j<30;j++)grid[i][j]=0;}setBackground(Color.BLACK);//黑色背景currentBlock=paintPanel.getBlock();currentLocation[0]=9;currentLocation[1]=0;locations=currentBlock.getLocation(currentLocation[0], currentLocation[1]);flag=false;}public void paintComponent(Graphics g)//绘制游戏区{super.paintComponent(g);Graphics2D g2=(Graphics2D)g;g2.setPaint(Color.BLUE);//蓝色方块for(int i=0;i<20;i++)//将grid里所有为1的区域都绘制正方形{for(int j=0;j<30;j++){if(grid[i][j]==1){Rectangle2D rectangle=new Rectangle2D.Double(i*POINT_DISTANCE-1,j*POINT_DISTANCE-1,BLOCK_WIDTH,BLOCK_HEIGHT);g2.fill(rectangle);}}}//然后再将当前方块对象绘制在游戏区for(int i=0;i<4;i++){if(locations[i][1]>=0){Rectangle2D rectangle=new Rectangle2D.Double(locations[i][0]*POINT_DISTANCE-1,locations[i][1]*POINT_DISTANCE-1,BLOCK_WIDTH,BLOCK_HEIGHT);g2.fill(rectangle);}}}//在游戏中移动方块的函数,是主要函数public void move(int direction){//flag==true,表示当前区域没有活动的对象,需要得到下一个对象if(flag==true){currentBlock=paintPanel.getBlock();currentLocation[0]=9;currentLocation[1]=0;locations=currentBlock.getLocation(currentLocation[0], currentLocation[1]);flag=false;}//表示当前区域有活动的对象//判断是否到底,如果不能再落下,那么将当前对象的块坐标添加进grid里,然后设置flag为trueif(isBottom(locations)==true){flag=true;for(int i=0;i<4;i++){if(locations[i][1]>=0)grid[locations[i][0]][locations[i][1]]=1;}repaint();if(checkOver()==true){MainWindowFrame.stopTimer();JOptionPane.showMessageDialog(null,"Game Over");}return;}//判断是否能左右移动,如果不行,那么当前点坐标的y值不再变化,如果可以,则y值加上direction,direction=1表示右移,-1表示左移,0表示没有左右移动if(isEdge(direction)==false){currentLocation[0]+=direction;}if(direction==0)currentLocation[1]++;locations=currentBlock.getLocation(currentLocation[0], currentLocation[1]);repaint();}//在按下了向下的键后,直接移到底部的函数public void moveToBottom(){int i=0;while(isBottom(locations)==false){i++;for(int j=0;j<4;j++){locations[j][1]++;}}for(int j=0;j<4;j++){if(locations[j][1]>=0){grid[locations[j][0]][locations[j][1]]=1;}}flag=true;repaint();}//检测是否游戏结束public boolean checkOver(){for(int i=0;i<4;i++){if(locations[i][1]<0)return true;}return false;}//检测是否到底private boolean isBottom(int[][] l){for(int i=0;i<4;i++){if(l[i][1]>=0&&l[i][0]>=0&&l[i][0]<20){if((l[i][1]+1)>=30||grid[l[i][0]][l[i][1]+1]==1){return true;}}}return false;}//检测是否到达边缘private boolean isEdge(int direction){for(int i=0;i<4;i++){if(locations[i][1]>=0){if((locations[i][0]+direction)>=20||(locations[i][0]+direction)<0||grid[locations[i][0]+direction][locations[i][1]]==1){return true;}}}return false;}//检测该行是否已满,并且进行消行处理public int checkLine(){int lines=0;for(int i=0;i<30;i++){int sum=0;for(int j=0;j<20;j++){sum+=grid[j][i];}if(sum==20){lines++;for(int k=i;k>0;k--){for(int h=0;h<20;h++){grid[h][k]=grid[h][k-1];}}for(int k=0;k<20;k++){grid[k][0]=0;}i--;repaint();}}return lines;}//改变方块姿态的函数,不仅要改变姿态,还要检测改变后的方块是否越界,如果越界,就要进行调整public void changePosition(){currentBlock.changePosition();int min=0;int max=0;locations=currentBlock.getLocation(currentLocation[0], currentLocation[1]);for(int i=0;i<4;i++){if(min>locations[i][0]){min=locations[i][0];}if(max<locations[i][0]){max=locations[i][0];}}//System.out.println("min:"+min+"  max:"+max);if(min<0){currentLocation[0]+=(-min);}if(max>19){currentLocation[0]-=(max-19);}locations=currentBlock.getLocation(currentLocation[0], currentLocation[1]);repaint();}//当按下reset键的时候,重置GamePanelpublic static void resetGamePanel(){for(int i=0;i<30;i++){for(int j=0;j<20;j++){grid[j][i]=0;}}flag=true;}private static int[][] grid=new int[20][30];//用来存储游戏区数据的数组,为1表示当前格有方块,前个坐标表示x,后一个表示y,与一般的数组有点不一样private Blocks currentBlock;//目前正在游戏区活动的方块private int[][] locations;//当前活动方块的各个方块的坐标private int[] currentLocation=new int[]{0,0};//当前参考点的坐标private int BLOCK_WIDTH=18;//方块的边长private int BLOCK_HEIGHT=18;private int POINT_DISTANCE=20;//坐标点的间距private static boolean flag=true;//为true时,表示当前没有活动的方块}
package MyGame;import javax.swing.*;/*** Created by zu on 2015/3/30.*/
/*整个游戏的思路:在MainWindowFrame进程里建立一个定时器对象,每0.5s触发一次定时器事件,然后用此事件驱动游戏进行。*/
public class Tetris
{public static void main(String args[]){MainWindowFrame mainWindow=new MainWindowFrame();mainWindow.setTitle("Tetris");mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);mainWindow.setVisible(true);}}

所用jdk版本为1.8.0,工具为IDEA 14.0.3

简单的俄罗斯方块代码(Java)相关推荐

  1. java简单的输入输出代码,java输入输出代码 java怎么样实现字符串输入输出问题...

    java代码 控制台输入输出 如何将控制台输出的内容如上.在线等 就比如说 输出helloworld 怎么把这个接收到我的jsp字符串我可能不太理解楼主这么做的意义, System.out.print ...

  2. LeetCode——LCP 29. 乐团站位[简单]——分析及代码(Java)

    LeetCode--LCP 29. 乐团站位[简单]--分析及代码[Java] 一.题目 二.分析及代码 1. 直接计算 (1)思路 (2)代码 (3)结果 三.其他 一.题目 某乐团的演出场地可视作 ...

  3. 用shell脚本写的一个简单的俄罗斯方块

    用shell脚本写的一个简单的俄罗斯方块 代码 代码 测试 下载链接 代码 代码 #!/bin/bash #version 1.2,若非正常退出,请使用附带的killel.sh脚本杀死进程 #定义用于 ...

  4. 用VB6写的一个简单俄罗斯方块代码

    网络上有很多俄罗斯方块代码.它们大都为了视觉效果,程序比较复杂,不利于学习游戏编程.所以我写了个简单俄罗斯方块代码,尽量用VB本身的功能,没有复杂的DirectX. 下载(注意修改下载后的扩展名) m ...

  5. sqllite java 代码,非常简单的SQLite的Java程序

    /* 在你的代码中引用这个驱动: Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConn ...

  6. 一款java的篮球游戏_NBA-BASKETBALL 一个非常有趣的nba 篮球游戏 简单操作容易上手代码值得学习 Game Program 238万源代码下载- www.pudn.com...

    文件名称: NBA-BASKETBALL下载 收藏√  [ 5  4  3  2  1 ] 开发工具: Java 文件大小: 5441 KB 上传时间: 2014-01-20 下载次数: 1 提 供 ...

  7. 超简单,一行代码解决:如何快速将Excel表格数据,映射到Java中的ListVO对象,然后转存数据库,生成SQL脚本

    前言-真的很快速,之前用的是Apache下的Jar包,用起来太麻烦了 <dependency><groupId>org.apache.poi</groupId>&l ...

  8. java 方法数统计_利用Java简单实现一个代码行数统计器方法实例

    前言 哈喽,我是小黑, 最近学了java的输入输出流后一直心痒痒,总想找一点事情来做,所以用java代码来实现了一下统计代码的所有行数,看一下我上大学以来到底打了多少行. 先附上实现代码吧! pack ...

  9. java传值的代码_SpringMVC的简单传值(实现代码)

    之前学习SpringMVC时感觉他的传值很神奇:简便,快捷,高效. 今天写几个简单的传值与大家分享,希望能对大家有帮助. 一. 从后往前传: (1) @Controller @RequestMappi ...

最新文章

  1. flask读取数据库(mysql)并展示表格(讲解获取表头的方法)【附上flask好看点的helloworld】
  2. MySQL 笔记4 -- 数据完整性
  3. sharepoint部件webparth关闭找回的方法
  4. Phantomjs代理设置
  5. leetcode674. 最长连续递增序列
  6. react(91)--debugger
  7. matlab里数据类型转换,Matlab数据类型及转换(Matlab data type and conversion).doc
  8. 小型elf Hello,World程序
  9. jinfo-jvm参数信息工具
  10. 阻止页面电话号码变蓝
  11. linux的环境变量相关的小记
  12. Linux下怎么刷显卡bios,nvidia显卡如何刷bios?nvidia显卡刷bios教程
  13. arcgis裁剪多个shp文件_ArcGIS批量裁剪矢量数据
  14. Java代码优化提点
  15. 永洪报表工具_2020年度10大BI工具排行榜
  16. linux驱动更新软件下载,NVIDIA英伟达显卡驱动程序更新下载(32/64位) v384.90 Linux版...
  17. 读薄《高性能MySql》(三)索引优化
  18. Vue父传子详细教程
  19. 阿诺德·施瓦辛格的演讲(拼命才会优秀,自律才会自由~)
  20. java8精简的jre,精简版jre,只有9M多哦!

热门文章

  1. POJ 1948 Triangular Pastures (二维01背包)
  2. linux CentOS普通用户无法从ssh登录解决方案
  3. XAF 如何使用复合主键和复合外键
  4. readonly 与 const
  5. java_opts 参数与JVM内存调优
  6. Ubuntu-安装MySQL5.7并配置用户名密码
  7. 转lua解析脚本过程中的关键数据结构介绍
  8. Android_L(64bit) 模拟器配置及创建项目
  9. Android深度探索第五章总结
  10. jQuery 文件碎片