俄罗斯方块 java_java代码实现俄罗斯方块
本文实例为大家分享了java实现俄罗斯方块的具体代码,供大家参考,具体内容如下
俄罗斯方块设计思想
俄罗斯方块都从小玩到大吧,什么规则大家都知道了吧,以前感觉那玩意贼好玩,但是就是老赢不了,现在学会了自己写一个天天练!
键盘操作:
左键:左移; 右键:右移;
上键:变换造型 下键:加速下掉(没毛病吧,没有继续整)
任意一行的方块满格,这一行就消除,消除一行方块得10分,目前小主我还没有设置关卡,各位喜欢的宝宝们可以自己设置关卡哦;
那么那些方块的造型到底从哪里来的呢,那就是我们自己设计的,常见的几种造型就是:I型,T型,L型,田字格型等等吧,自己个加呗!
那么到底咋整的咧?其实啊就是一个4*4的数组,当然了你开心设计n*n也可以,你牛皮你说了算!
那么下面举了一个例子,用来告诉你们为啥你们看见的造型可以变换的原因就是这样提前设计好,0为空,1为填充格,这样你就可以在你的游戏里面凹造型了!
算了:直接放图先看代码运行结果吧:
喜欢吗?喜欢就直接做吧,可能代码写的不够好,请各位大神多多包涵,我回头也会多总结,会不断更新代码的;
GamePanel类:游戏界面类,整个方块掉落和显示,游戏的逻辑斯洛都在这个类里面实现;
package tetris;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.Timer;
public class GamePanel extends JPanel implements KeyListener{
private int mapRow = 21;
private int mapCol = 12;
private int mapGame[][] = new int[mapRow][mapCol];//开辟一个二维数组空间,用来存放我们的地图信息
private Timer timer;
private int score = 0;//记录成绩
Random random = new Random();
private int curShapeType = -1;
private int curShapeState = -1;//设置当前的形状类型和当前的形状状态
private int nextShapeType = -1;
private int nextShapeState = -1;//设置下一次出现的方块组的类型和状态
private int posx = 0;
private int posy = 0;
private final int shapes[][][] = new int[][][]{
//T字形按逆时针的顺序存储
{
{0,1,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0},
{0,1,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0},
{1,1,1,0, 0,1,0,0, 0,0,0,0, 0,0,0,0},
{0,1,0,0, 0,1,1,0, 0,1,0,0, 0,0,0,0}
},
//I字形按逆时针的顺序存储
{
{0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0},
{0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0},
{0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0},
{0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0}
},
//倒Z形按逆时针的顺序存储
{
{0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0},
{1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0},
{0,1,1,0, 1,1,0,0, 0,0,0,0, 0,0,0,0},
{1,0,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0}
},
//Z形按逆时针的顺序存储
{
{1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0},
{0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0},
{1,1,0,0, 0,1,1,0, 0,0,0,0, 0,0,0,0},
{0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0}
},
//J字形按逆时针的顺序存储
{
{0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0},
{1,1,1,0, 0,0,1,0, 0,0,0,0, 0,0,0,0},
{1,1,0,0, 1,0,0,0, 1,0,0,0, 0,0,0,0},
{1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0}
},
//L字形按逆时针的顺序存储
{
{1,0,0,0, 1,0,0,0, 1,1,0,0, 0,0,0,0},
{0,0,1,0, 1,1,1,0, 0,0,0,0, 0,0,0,0},
{1,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0},
{1,1,1,0, 1,0,0,0, 0,0,0,0, 0,0,0,0}
},
//田字形按逆时针的顺序存储
{
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0},
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0},
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0},
{1,1,0,0, 1,1,0,0, 0,0,0,0, 0,0,0,0}
}
};
private int rowRect = 4;
private int colRect = 4;//这里我们把存储的图像看成是一个4*4的二维数组,虽然在上面我们采用一维数组来存储,但实际还是要看成二维数组来实现
private int RectWidth = 10;
public GamePanel()//构造函数----创建好地图
{
CreateRect();
initMap();//初始化这个地图
SetWall();//设置墙
// CreateRect();
timer = new Timer(500,new TimerListener());
timer.start();
}
class TimerListener implements ActionListener{
public void actionPerformed(ActionEvent e)
{
MoveDown();
}
}
public void SetWall()//第0列和第11列都是墙,第20行也是墙
{
for(int i = 0; i < mapRow; i++)//先画列
{
mapGame[i][0] = 2;
mapGame[i][11] = 2;
}
for(int j = 1; j < mapCol-1; j++)//画最后一行
{
mapGame[20][j] = 2;
}
}
public void initMap()//初始化这个地图,墙的ID是2,空格的ID是0,方块的ID是1
{
for(int i = 0; i < mapRow; i++)
{
for(int j = 0; j < mapCol; j++)
{
mapGame[i][j] = 0;
}
}
}
public void CreateRect()//创建方块---如果当前的方块类型和状态都存在就设置下一次的,如果不存在就设置当前的并且设置下一次的状态和类型
{
if(curShapeType == -1 && curShapeState == -1)//当前的方块状态都为1,表示游戏才开始
{
curShapeType = random.nextInt(shapes.length);
curShapeState = random.nextInt(shapes[0].length);
}
else
{
curShapeType = nextShapeType;
curShapeState = nextShapeState;
}
nextShapeType = random.nextInt(shapes.length);
nextShapeState = random.nextInt(shapes[0].length);
posx = 0;
posy = 1;//墙的左上角创建方块
if(GameOver(posx,posy,curShapeType,curShapeState))
{
JOptionPane.showConfirmDialog(null, "游戏结束!", "提示", JOptionPane.OK_OPTION);
System.exit(0);
}
}
public boolean GameOver(int x, int y, int ShapeType, int ShapeState)//判断游戏是否结束
{
if(IsOrNoMove(x,y,ShapeType,ShapeState))
{
return false;
}
return true;
}
public boolean IsOrNoMove(int x, int y, int ShapeType, int ShapeState)//判断当前的这个图形是否可以移动,这里重点强调x,y的坐标是指4*4的二维数组(描述图形的那个数组)的左上角目标
{
for(int i = 0; i < rowRect ; i++)
{
for(int j = 0; j < colRect; j++)
{
if(shapes[ShapeType][ShapeState][i*colRect+j] == 1 && mapGame[x+i][y+j] == 1
|| shapes[ShapeType][ShapeState][i*colRect+j] == 1 && mapGame[x+i][y+j] == 2)
{
return false;
}
}
}
return true;
}
public void Turn()//旋转
{
int temp = curShapeState;
curShapeState = (curShapeState+1) % shapes[0].length;
if(IsOrNoMove(posx,posy,curShapeType,curShapeState))
{
}
else
{
curShapeState = temp;
}
repaint();
}
public void MoveDown()//向下移动
{
if(IsOrNoMove(posx+1,posy,curShapeType,curShapeState))
{
posx++;
}
else
{
AddToMap();//将此行固定在地图中
CheckLine();
CreateRect();//重新创建一个新的方块
}
repaint();
}
public void MoveLeft()//向左移动
{
if(IsOrNoMove(posx,posy-1,curShapeType,curShapeState))
{
posy--;
}
repaint();
}
public void MoveRight()//向右移动
{
if(IsOrNoMove(posx,posy+1,curShapeType,curShapeState))
{
posy++;
}
repaint();
}
public void AddToMap()//固定掉下来的这一图像到地图中
{
for(int i = 0; i < rowRect; i++)
{
for(int j = 0; j < colRect; j++)
{
if(shapes[curShapeType][curShapeState][i*colRect+j] == 1)
{
mapGame[posx+i][posy+j] = shapes[curShapeType][curShapeState][i*colRect+j];
}
}
}
}
public void CheckLine()//检查一下这些行中是否有满行的
{
int count = 0;
for(int i = mapRow-2; i >= 0; i--)
{
count = 0;
for(int j = 1; j < mapCol-1; j++)
{
if(mapGame[i][j] == 1)
{
count++;
}
else
break;
}
if(count >= mapCol-2)
{
for(int k = i; k > 0; k--)
{
for(int p = 1; p < mapCol-1; p++)
{
mapGame[k][p] = mapGame[k-1][p];
}
}
score += 10;
i++;
}
}
}
public void paint(Graphics g)//重新绘制窗口
{
super.paint(g);
for(int i = 0; i < rowRect; i++)//绘制正在下落的方块
{
for(int j = 0; j < colRect; j++)
{
if(shapes[curShapeType][curShapeState][i*colRect+j] == 1)
{
g.fillRect((posy+j+1)*RectWidth, (posx+i+1)*RectWidth, RectWidth, RectWidth);
}
}
}
for(int i = 0; i < mapRow; i++)//绘制地图上面已经固定好的方块信息
{
for(int j = 0; j < mapCol; j++)
{
if(mapGame[i][j] == 2)//画墙
{
g.drawRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth);
}
if(mapGame[i][j] == 1)//画小方格
{
g.fillRect((j+1)*RectWidth, (i+1)*RectWidth, RectWidth, RectWidth);
}
}
}
g.drawString("score = "+ score, 225, 15);
g.drawString("下一个方块:", 225, 50);
for(int i = 0; i < rowRect; i++)
{
for(int j = 0; j < colRect; j++)
{
if(shapes[nextShapeType][nextShapeState][i*colRect+j] == 1)
{
g.fillRect(225+(j*RectWidth), 100+(i*RectWidth), RectWidth, RectWidth);
}
}
}
}
public void NewGame()//游戏重新开始
{
score = 0;
initMap();
SetWall();
CreateRect();
repaint();
}
public void StopGame()//游戏暂停
{
timer.stop();
}
public void ContinueGame()
{
timer.start();
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
switch(e.getKeyCode())
{
case KeyEvent.VK_UP://上----旋转
Turn();
break;
case KeyEvent.VK_DOWN://下----向下移动
MoveDown();
break;
case KeyEvent.VK_LEFT://左----向左移动
MoveLeft();
break;
case KeyEvent.VK_RIGHT://右----向右移动
MoveRight();
break;
}
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
GameFrame类:整个游戏的进入口,好吧,说白了就是有main()函数的类,这个类里面实现游戏界面的一些设计,你可以理解为一个小小小小的UI;
package tetris;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
public class GameFrame extends JFrame implements ActionListener{
private int widthFrame = 500;
private int heightFrame = 600;
private JMenu menuone = new JMenu("游戏");//创建一个菜单
private JMenuItem newGame = menuone.add("重新开始");//创建一个内置菜单选项
private JMenuItem exitGame = menuone.add("游戏退出");
private JMenuItem stopGame = menuone.add("游戏暂停");
private JMenuItem goOnGame = menuone.add("游戏继续");
private JMenu menutwo = new JMenu("帮助");//创建第二个菜单
private JMenuItem aboutGame = menutwo.add("关于游戏");
GamePanel gamepanel = new GamePanel();
public GameFrame()//构造函数
{
addKeyListener(gamepanel);
newGame.addActionListener(this);
exitGame.addActionListener(this);
stopGame.addActionListener(this);
goOnGame.addActionListener(this);
aboutGame.addActionListener(this);
this.add(gamepanel);
JMenuBar menu = new JMenuBar();
menu.add(menuone);
menu.add(menutwo);
this.setJMenuBar(menu);
this.setTitle("俄罗斯方块");
this.setBounds(50, 10, widthFrame, heightFrame);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == newGame)//游戏重新开始
{
gamepanel.NewGame();
}
if(e.getSource() == exitGame)//游戏退出
{
System.exit(0);
}
if(e.getSource() == stopGame)//游戏暂停
{
gamepanel.StopGame();
}
if(e.getSource() == goOnGame)//游戏继续
{
gamepanel.ContinueGame();
}
if(e.getSource() == aboutGame)//关于游戏信息
{
JOptionPane.showMessageDialog(null, "左右键移动,向上建旋转", "提示", JOptionPane.OK_OPTION);
}
}
public static void main(String[] args) {
new GameFrame();
}
}
更多关于俄罗斯方块的文章,请点击查看专题:《俄罗斯方块》
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
俄罗斯方块 java_java代码实现俄罗斯方块相关推荐
- 俄罗斯方块java代码_俄罗斯方块源代码
[c++]代码库#include #include #include #define CELL 20 #define ROWS 25 #define COLS 15 //升级所需分数值 #define ...
- linux下c语言俄罗斯方块,Centos 6.2下的C语言编写俄罗斯方块游戏代码
俄罗斯方块游戏代码如下: 运行结果请点击:http://blog.chinaunix.net/uid- ...
- python编程小游戏代码-Python小游戏之300行代码实现俄罗斯方块
前言 本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写一个俄罗斯方块.但是想到旋转,停靠,消除等操作,感觉好像很 ...
- python小游戏源码-Python小游戏之300行代码实现俄罗斯方块
Python小游戏之300行代码实现俄罗斯方块 来源:中文源码网 浏览: 次 日期:2019年11月5日 [下载文档: Python小游戏之300行代码实现俄罗斯方块.txt ] (友情提示:右键点上 ...
- python童年_300行Python代码实现俄罗斯方块,致敬逝去的童年
原标题:300行Python代码实现俄罗斯方块,致敬逝去的童年 本文代码基于 python3.6 和 pygame1.9.4. 俄罗斯方块是儿时最经典的游戏之一,刚开始接触 pygame 的时候就想写 ...
- 60行JavaScript代码写俄罗斯方块
教你看懂网上流传的60行JavaScript代码俄罗斯方块游戏 早就听说网上有人仅仅用60行JavaScript代码写出了一个俄罗斯方块游戏,最近看了看,今天在这篇文章里面我把我做的分析整理一下(主要 ...
- python编写小游戏代码_Python小游戏之300行代码实现俄罗斯方块
Python小游戏之300行代码实现俄罗斯方块 来源:中文源码网 浏览: 次 日期:2019年11月5日 [下载文档: Python小游戏之300行代码实现俄罗斯方块.txt ] (友情提示:右键点上 ...
- 52行c代码实现俄罗斯方块
一个简易版的控制台俄罗斯方块游戏 代码如下: #include <stdio.h> #include <windows.h> #include <conio.h> ...
- 小翼java_小翼俄罗斯方块java代码
[实例简介] 感谢小翼的java学习视频教程,个人感觉讲的非常好. 小翼的俄罗斯方块工程项目代码.自己对着视频教学敲的,有些地方对原作者的代码思路有改动.项目完成90%,还有些许功能没有加入进去.下载 ...
- c语言经典案例 俄罗斯方块,C语言实现俄罗斯方块经典游戏课程设计
C语言实现俄罗斯方块经典游戏课程设计 计算机实习报告 一.功能说明 1.1总体功能说明 本工程用C++语言实现了俄罗斯方块经典游戏. 俄罗斯方块游戏特点:俄罗斯方块的基本规则是通过键盘控制移动.旋转和 ...
最新文章
- 【FFmpeg】ffmpeg中函数返回的错误码:AVERROR及AVERROR_*
- HTTP和HTTPS协议的区别
- 三种方式实现自定义圆形进度条ProgressBar
- 9岁印度女孩成为最年轻微软认证专家
- 17/100. Maximum Subarray
- “睡服”面试官系列第十四篇之数组的扩展(建议收藏学习)
- 如何在VMWare的Ubuntu虚拟机中设置共享文件夹
- 来啊,一起造作啊!10月仍然还有十多场新品发布会...
- python文字识别 训练_Python3.x:pytesseract识别率提高(样本训练)
- BAT批处理文件语法(转)
- 基于树莓派2代的DIY无线路由器
- 联合光伏回应“天价收购”质疑:并非每瓦10.24元
- 广东省计算机一级网络题分值,计算机一级考试内容题型以及分值
- 电信光猫超级密码破解
- 阿里云域名注册+服务器购买+备案教程(图文教程)
- RabbitMQ安装问题
- 光猫接交换机,交换机下接无线路由器,电脑网线连接交换机进不去无线路由器后台是为什么?
- 如何解释vue的生命周期才能令面试官满意?
- 16. 求两点之间的最短路径
- redis主从+哨兵
热门文章
- Java jar 如何防止被反编译
- 冰封USB3.0导入工具
- 推荐几个e书下载地址
- 深圳市科技创新委员会关于2021年高新技术企业培育库拟入库企业名单公示的通知
- html5页面设计报告书,网站设计报告模板.docx
- html网页中获取vf数据到mysql_vfp教程之用FoxWeb在网上快速发布你的FOXPRO数据库
- itunes卸载工具_iTunes卸载麻烦
- ffmpeg源码简析(十三)ffmpeg API变更 2009-03-01—— 2017-05-09变更
- vc2013使用经验
- 第5讲:VUE3工程中实现页面加载中效果和页面切换动画效果。