Java写的经典俄罗斯方块代码.docx
- 文档编号:11761232
- 上传时间:2023-06-02
- 格式:DOCX
- 页数:12
- 大小:16.42KB
Java写的经典俄罗斯方块代码.docx
《Java写的经典俄罗斯方块代码.docx》由会员分享,可在线阅读,更多相关《Java写的经典俄罗斯方块代码.docx(12页珍藏版)》请在冰点文库上搜索。
Java写的经典俄罗斯方块代码
importjava.awt.*;
importjava.awt.event.ActionEvent;
importjava.awt.event.ActionListener;
importjava.awt.event.KeyEvent;
importjava.awt.event.KeyListener;
importjavax.swing.*;
importjavax.swing.Timer;
publicclassTetrisextendsJFrame{
publicTetris(){
Tetrisbloka=newTetrisblok();
addKeyListener(a);
add(a);
}
publicstaticvoidmain(String[]args){
Tetrisframe=newTetris();
JMenuBarmenu=newJMenuBar();
frame.setJMenuBar(menu);
JMenugame=newJMenu("游戏");
JMenuItemnewgame=game.add("新游戏");
JMenuItempause=game.add("暂停");
JMenuItemgoon=game.add("继续");
JMenuItemexit=game.add("退出");
JMenuhelp=newJMenu("帮助");
JMenuItemabout=help.add("关于");
menu.add(game);
menu.add(help);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(220,275);
frame.setTitle("Tetris内测版");
//frame.setUndecorated(true);
frame.setVisible(true);
frame.setResizable(false);
}
}
//创建一个俄罗斯方块类
classTetrisblokextendsJPanelimplementsKeyListener{
//blockType代表方块类型
//turnState代表方块状态
privateintblockType;
privateintscore=0;
privateintturnState;
privateintx;
privateinty;
privateinti=0;
intj=0;
intflag=0;
//定义已经放下的方块x=0-11,y=0-21;
int[][]map=newint[13][23];
//方块的形状第一组代表方块类型有S、Z、L、J、I、O、T7种第二组代表旋转几次第三四组为方块矩阵
privatefinalintshapes[][][]=newint[][][]{
//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}},
//s
{{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,0,0,0,1,1,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,1,1,0,0,0,1,0,0,0,0,0,0,0,0,0}},
//o
{{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}},
//l
{{1,0,0,0,1,0,0,0,1,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,0,1,0,0,0,1,0,0,0,0,0,0},
{0,0,1,0,1,1,1,0,0,0,0,0,0,0,0,0}},
//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}}};
//生成新方块的方法
publicvoidnewblock(){
blockType=(int)(Math.random()*1000)%7;
turnState=(int)(Math.random()*1000)%4;
x=4;
y=0;
if(gameover(x,y)==1){
newmap();
drawwall();
score=0;
JOptionPane.showMessageDialog(null,"GAMEOVER");
}
}
//画围墙
publicvoiddrawwall(){
for(i=0;i<12;i++){
map[i][21]=2;
}
for(j=0;j<22;j++){
map[11][j]=2;
map[0][j]=2;
}
}
//初始化地图
publicvoidnewmap(){
for(i=0;i<12;i++){
for(j=0;j<22;j++){
map[i][j]=0;
}
}
}
//初始化构造方法
Tetrisblok(){
newblock();
newmap();
drawwall();
Timertimer=newTimer(1000,newTimerListener());
timer.start();
}
//旋转的方法
publicvoidturn(){
inttempturnState=turnState;
turnState=(turnState+1)%4;
if(blow(x,y,blockType,turnState)==1){
}
if(blow(x,y,blockType,turnState)==0){
turnState=tempturnState;
}
repaint();
}
//左移的方法
publicvoidleft(){
if(blow(x-1,y,blockType,turnState)==1){
x=x-1;
}
;
repaint();
}
//右移的方法
publicvoidright(){
if(blow(x+1,y,blockType,turnState)==1){
x=x+1;
}
;
repaint();
}
//下落的方法
publicvoiddown(){
if(blow(x,y+1,blockType,turnState)==1){
y=y+1;
delline();
}
;
if(blow(x,y+1,blockType,turnState)==0){
add(x,y,blockType,turnState);
newblock();
delline();
}
;
repaint();
}
//是否合法的方法
publicintblow(intx,inty,intblockType,intturnState){
for(inta=0;a<4;a++){
for(intb=0;b<4;b++){
if(((shapes[blockType][turnState][a*4+b]==1)&&(map[x
+b+1][y+a]==1))
||((shapes[blockType][turnState][a*4+b]==1)&&(map[x
+b+1][y+a]==2))){
return0;
}
}
}
return1;
}
//消行的方法
publicvoiddelline(){
intc=0;
for(intb=0;b<22;b++){
for(inta=0;a<12;a++){
if(map[a][b]==1){
c=c+1;
if(c==10){
score+=10;
for(intd=b;d>0;d--){
for(inte=0;e<11;e++){
map[e][d]=map[e][d-1];
}
}
}
}
}
c=0;
}
}
//判断你挂的方法
publicintgameover(intx,inty){
if(blow(x,y,blockType,turnState)==0){
return1;
}
return0;
}
//把当前添加map
publicvoidadd(intx,inty,intblockType,intturnState){
intj=0;
for(inta=0;a<4;a++){
for(intb=0;b<4;b++){
if(map[x+b+1][y+a]==0){
map[x+b+1][y+a]=shapes[blockType][turnState][j];
}
;
j++;
}
}
}
//画方块的的方法
publicvoidpaintComponent(Graphicsg){
super.paintComponent(g);
//画当前方块
for(j=0;j<16;j++){
if(shapes[blockType][turnState][j]==1){
g.fillRect((j%4+x+1)*10,(j/4+y)*10,10,10);
}
}
//画已经固定的方块
for(j=0;j<22;j++){
for(i=0;i<12;i++){
if(map[i][j]==1){
g.fillRect(i*10,j*10,10,10);
}
if(map[i][j]==2){
g.drawRect(i*10,j*10,10,10);
}
}
}
g.drawString("score="+score,125,10);
g.drawString("抵制不良游戏,",125,50);
g.drawString("拒绝盗版游戏。
",125,70);
g.drawString("注意自我保护,",125,90);
g.drawString("谨防受骗上当。
",125,110);
g.drawString("适度游戏益脑,",125,130);
g.drawString("沉迷游戏伤身。
",125,150);
g.drawString("合理安排时间,",125,170);
g.drawString("享受健康生活。
",125,190);
}
//键盘监听
publicvoidkeyPressed(KeyEvente){
switch(e.getKeyCode()){
caseKeyEvent.VK_DOWN:
down();
break;
caseKeyEvent.VK_UP:
turn();
break;
caseKeyEvent.VK_RIGHT:
right();
break;
caseKeyEvent.VK_LEFT:
left();
break;
}
}
//无用
publicvoidkeyReleased(KeyEvente){
}
//无用
publicvoidkeyTyped(KeyEvente){
}
//定时器监听
classTimerListenerimplementsActionListener{
publicvoidactionPerformed(ActionEvente){
repaint();
if(blow(x,y+1,blockType,turnState)==1){
y=y+1;
delline();
}
;
if(blow(x,y+1,blockType,turnState)==0){
if(flag==1){
add(x,y,blockType,turnState);
delline();
newblock();
flag=0;
}
flag=1;
}
;
}
}
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Java 经典 俄罗斯方块 代码