资源描述:
《一个java编写的俄罗斯方块游戏代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、如图packagetest;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);
2、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("帮助");JMenuItema
3、bout=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);}}//创建一个俄罗斯方块类classTetrisblokexte
4、ndsJPanelimplementsKeyListener{//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种第二组代表旋转几次第三四组为方块矩阵privatefinalintshape
5、s[][][]=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,
6、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,
7、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