最好的Java俄罗斯方块源代码.pdf.doc_第1页
最好的Java俄罗斯方块源代码.pdf.doc_第2页
最好的Java俄罗斯方块源代码.pdf.doc_第3页
最好的Java俄罗斯方块源代码.pdf.doc_第4页
最好的Java俄罗斯方块源代码.pdf.doc_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

import java.awt.Color; 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.JFrame; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.Timer;public class myblock extends JFrame private static final long serialVersionUID = 1L; public static void main(String args)final JFrame frame = new JFrame(俄罗斯方块。); final Tetrisblok a = new Tetrisblok();frame.addKeyListener(a);frame.add(a);final Timer timer = new Timer(400, a.new TimerListener(); timer.start();JMenuBar menu = new JMenuBar(); frame.setJMenuBar(menu); /定义游戏菜单项JMenu gameMenu = new JMenu(游戏(G)); JMenuItem newitem = new JMenuItem(新游戏(N)); gameMenu.add(newitem);final JMenuItem pauseitem = new JMenuItem(暂停(P)); gameMenu.add(pauseitem);JMenuItem contitem = new JMenuItem(继续(c)); gameMenu.add(contitem);JMenuItem exititem = new JMenuItem(退出(E)); gameMenu.add(exititem); /添加监听器来实现游戏菜单上的各个菜单项的功能,采用匿名内部类 /新游戏菜单项的功能实现newitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)a.newmap();a.drawwall(); a.score = 0; a.x=4; a.y=-1;a.blockType=a.ran.nextInt(7); a.turnState=a.ran.nextInt(4); a.nextb=a.ran.nextInt(7); a.nextt=a.ran.nextInt(4);); /暂停菜单项的功能实现pauseitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)timer.stop(); pauseitem.setEnabled(false);); /继续菜单项的功能实现contitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)timer.start(); pauseitem.setEnabled(true);); /退出菜单项的功能实现exititem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)timer.stop();Object options = 是的,我要退出, 不好意思,点错了 ; int option = JOptionPane.showOptionDialog(null, 您确定要退出吗?,退出提 示.,JOptionPane.OK_CANCEL_OPTION,JOptionPane.WARNING_MESSAGE,null,options, options0); if(option = JOptionPane.OK_OPTION) System.exit(0);timer.start();); /定义帮助菜单项JMenu helpMenu = new JMenu(帮助(H));JMenuItem aboutitem = new JMenuItem(关于游戏(G)); helpMenu.add(aboutitem);JMenuItem writeitem = new JMenuItem(关于作者(W)); helpMenu.add(writeitem);helpMenu.addSeparator();JMenuItem adviitem = new JMenuItem(游戏忠告(A)); helpMenu.add(adviitem);/添加监听器来实现帮助菜单上的各个菜单项的功能,采用匿名内部类 /关于游戏菜单项的功能实现aboutitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)timer.stop(); JOptionPane.showMessageDialog(frame,本游戏由孤独的野狼制作!n如需要源代码,随时欢迎联系作者!n +作者邮箱:nQQ号:2442701497n本游戏功能基本上是齐全的!n +并新增了“暂停”、“重新开始”等功能 .n希望您喜欢!n + 如有任何疑问及改善意见,随时欢迎指出。n我们将尽最大的努力满足您的需求!n +最后谢谢您的使用!n版权所有,请勿侵权!,关于游 戏.,JOptionPane.INFORMATION_MESSAGE);timer.start();); /关于作者菜单项的功能实现writeitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)timer.stop(); JOptionPane.showMessageDialog(frame,作者:孤独的野狼n性别:男n籍贯:湖南邵阳n出生日:1990年11月9日n + 本科院校:上海应用技术学院n现居地:上海n自我介绍:不帅也不丑n偶像:爱因斯坦n +最喜欢的歌手:刀郎n最向往的地方:北京n座右铭:疯狂源自梦想n + 勤奋铸就辉煌n最喜欢的话:我愿变成一座石桥,受五百年风吹,五百年雨打,n +五百年日晒,只求你从上面走过.n +梦想:天地有多大,梦有多潇洒n,关于作 者.,JOptionPane.INFORMATION_MESSAGE);timer.start();); /游戏忠告菜单项的功能实现adviitem.addActionListener(new ActionListener() public void actionPerformed(ActionEvent e)timer.stop();JOptionPane.showMessageDialog(frame,抵制不良游戏 , 拒绝盗版游戏nn注意自我保护 , 谨防受骗上当nn +适度游戏益脑 , 沉迷游戏伤身nn合理安排时间 , 享受健康生活n,健康游戏忠告。, JOptionPane.INFORMATION_MESSAGE); timer.start(););menu.add(gameMenu);menu.add(helpMenu); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(300,100,400,520); frame.setVisible(true);frame.setResizable(false);/ 创建一个俄罗斯方块类class Tetrisblok extends JPanel implements KeyListener private static final long serialVersionUID = 1L; Random ran = new Random(); /使用Random函数;public int blockType; / blockType代表方块类型 public int score = 0; /分数;public int turnState; / turnState代表方块状态 public int x; /方块起始位置的横坐标public int y; /方块起始位置的纵坐标public int nextb = ran.nextInt(7); /下一个方块类型;public int nextt = ran.nextInt(4); /下一个方块的形状; private int i = 0;private int j = 0;private boolean flag = false; int map = new int1323; Tetrisblok()newblock();newmap();drawwall();/第一组代表方块的形状,方块形状类型有S、Z、L、J、I、O、T 7种 /第二组代表方块旋转几次 /第三四组为方块矩阵private final int shapes = new int / 棒型方块 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,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0, 0, 0, 0, 0 ;/ 生成新方块的方法public void newblock() blockType = nextb; turnState = nextt;nextb = ran.nextInt(7);nextt = ran.nextInt(4); x = 4;y = 0;if (gameover(x, y) JOptionPane.showMessageDialog(null,不好意思,游戏结束,请再接再厉!); newmap();drawwall(); score = 0;/ 判断游戏结束的方法public boolean gameover(int x, int y)if (blow(x, y, blockType, turnState) return true;elsereturn false;/ 是否合法的方法public boolean blow(int x, int y, int blockType, int turnState) for (int a = 0; a 4; a+) for (int b = 0; b 4; b+)if (shapesblockTypeturnStatea * 4 + b = 1) & (mapx+ b + 1y + a = 1)| (shapesblockTypeturnStatea * 4 + b = 1) & (mapx+ b + 1y + a = 2)return true;return false;/ 初始化地图public void newmap()for (i = 0; i 12; i+) for (j = 0; j 22; j+)mapij = 0;/ 画围墙public void drawwall()for (i = 0; i 12; i+) mapi21 = 2;for (j = 0; j 22; j+) map11j = 2; map0j = 2;/ 旋转的方法public void turn()int tempturnState = turnState; turnState = (turnState + 1) % 4;if (!blow(x, y, blockType, turnState)if (blow(x, y, blockType, turnState) turnState = tempturnState;repaint();/ 左移的方法public void left()if (!blow(x - 1, y, blockType, turnState) x = x - 1;repaint();/ 右移的方法public void right()if (!blow(x + 1, y, blockType, turnState) x = x + 1;repaint();/ 下落的方法public void down()if (!blow(x, y + 1, blockType, turnState) y = y + 1;if (blow(x, y + 1, blockType, turnState) add(x, y, blockType, turnState); newblock();delline();repaint();/ 消行的方法public void delline() int c = 0;int i = 0; /i用来确定本次消了几行 for (int b = 0; b 22; b+)for (int a = 0; a 0; d-) for (int e = 0; e 11; e+) maped = maped - 1;c = 0;/确定消行分数 switch (i) case 1:score = score +1; break;case 2:score = score +3; break;case 3:score = score +6; break;case 4:score = score +10; break;default: break;/ 把当前添加mappublic void add(int x, int y, int blockType, int turnState) int j = 0;for (int a = 0; a 4; a+) for (int b = 0; b 4; b+)if (mapx + b + 1y + a = 0)mapx + b + 1y + a = shapesblockTypeturnStatej;j+;/ 画方块的的方法public void paintComponent(Graphics g)/Object yanse = BLUE,GREEN,LIGHT_GRAY,YELLOW,PINK; super.paintComponent(g);/ 画当前方块for (j=0;j16;j+)if (shapesblockTypeturnStatej=1) /画矩形区域g.setColor(Color.BLUE); g.fill3DRect(j%4+x+1)*20,(j/4+y)*20,20,20,true);g.setColor(Color.BLACK); g.draw3DRect(j%4+x+1)*20,(j/4+y)*20,20,20, true);/画下一个方块for (j = 0; j 16; j+)if (shapesnextbnexttj = 1) g.setColor(Color.BLUE);g.fill3DRect(j%4+1)*20+250,(j/4)*20+40, 20, 20,true); g.setColor(Color.BLACK);g.draw3DRect(j%4+1)*20+250,(j/4)*20+40, 20, 20, true);/ 画已经固定的方块for (j = 0; j 22; j+) for (i = 0; i 12; i+)if (mapij=2) /画围墙 g.setColor(Color.BLACK);g.fill3DRect(i*20,j*20,20,20,true); g.

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论