




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、Java俄罗斯方块源码import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import javax.swing.*;import javax.swing.Timer;public class Tetris extends JFrame public Tetris() Tetrisblok a = new Tetrisblok(); addKe
2、yListener(a); add(a); public static void main(String args) Tetris frame = new Tetris(); JMenuBar menu = new JMenuBar(); frame.setJMenuBar(menu); JMenu game = new JMenu("游戏"); JMenuItem newgame = game.add("新游戏"); JMenuItem pause = game.add("暂停"); JMenuItem goon = game.ad
3、d("继续"); JMenuItem exit = game.add("退出"); JMenu help = new JMenu("帮助"); JMenuItem about = help.add("关于"); menu.add(game); menu.add(help); frame.setLocationRelativeTo(null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(220, 275); frame.se
4、tTitle("Tetris内测版"); / frame.setUndecorated(true); frame.setVisible(true); frame.setResizable(false); / 创建一个俄罗斯方块类class Tetrisblok extends JPanel implements KeyListener / blockType 代表方块类型 / turnState代表方块状态 private int blockType; private int score = 0; private int turnState; private int x;
5、private int y; private int i = 0; int j = 0; int flag = 0; / 定义已经放下的方块x=0-11,y=0-21; int map = new int1323; / 方块的形状 第一组代表方块类型有S、Z、L、J、I、O、T 7种 第二组 代表旋转几次 第三四组为 方块矩阵 private final int shapes = new int / 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
6、, 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 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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 ; / 生成新方块的方法 public void newblock() 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; JOp
11、tionPane.showMessageDialog(null, "GAME OVER"); / 画围墙 public void drawwall() for (i = 0; i < 12; i+) mapi21 = 2; for (j = 0; j < 22; j+) map11j = 2; map0j = 2; / 初始化地图 public void newmap() for (i = 0; i < 12; i+) for (j = 0; j < 22; j+) mapij = 0; / 初始化构造方法 Tetrisblok() newblock
12、(); newmap(); drawwall(); Timer timer = new Timer(1000, new TimerListener(); timer.start(); / 旋转的方法 public void turn() int tempturnState = turnState; turnState = (turnState + 1) % 4; if (blow(x, y, blockType, turnState) = 1) if (blow(x, y, blockType, turnState) = 0) turnState = tempturnState; repain
13、t(); / 左移的方法 public void left() if (blow(x - 1, y, blockType, turnState) = 1) x = x - 1; ; repaint(); / 右移的方法 public void right() if (blow(x + 1, y, blockType, turnState) = 1) x = x + 1; ; repaint(); / 下落的方法 public void down() if (blow(x, y + 1, blockType, turnState) = 1) y = y + 1; delline(); ; if
14、(blow(x, y + 1, blockType, turnState) = 0) add(x, y, blockType, turnState); newblock(); delline(); ; repaint(); / 是否合法的方法 public int 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) && (map
15、x + b + 1y + a = 1) | (shapesblockTypeturnStatea * 4 + b = 1) && (mapx + b + 1y + a = 2) return 0; return 1; / 消行的方法 public void delline() int c = 0; for (int b = 0; b < 22; b+) for (int a = 0; a < 12; a+) if (mapab = 1) c = c + 1; if (c = 10) score += 10; for (int d = b; d > 0; d-)
16、 for (int e = 0; e < 11; e+) maped = maped - 1; c = 0; / 判断你挂的方法 public int gameover(int x, int y) if (blow(x, y, blockType, turnState) = 0) return 1; return 0; / 把当前添加map public 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
17、; b+) if (mapx + b + 1y + a = 0) mapx + b + 1y + a = shapesblockTypeturnStatej; ; j+; / 画方块的的方法 public void paintComponent(Graphics g) super.paintComponent(g); / 画当前方块 for (j = 0; j < 16; j+) if (shapesblockTypeturnStatej = 1) g.fillRect(j % 4 + x + 1) * 10, (j / 4 + y) * 10, 10, 10); / 画已经固定的方块
18、for (j = 0; j < 22; j+) for (i = 0; i < 12; i+) if (mapij = 1) g.fillRect(i * 10, j * 10, 10, 10); if (mapij = 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("享受健康生活。&quo
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 管道安装考试题及答案
- 孤儿救助考试题及答案
- 幼儿园教学教案设计:不跟陌生人走
- 我最喜爱的书籍读后感(5篇)
- 防范病毒考试题及答案
- (正式版)DB15∕T 3685-2024 《严寒地区预制拼装箱型涵洞设计与施工技术规范》
- 车辆买卖合同及其附加条款
- (正式版)DB15∕T 3651-2024 《光伏项目防沙治沙技术规程》
- 动物口语考试题及答案
- 顶尖学校考试题及答案
- 2025年医疗工作人员定向招聘考试笔试试题(含答案)
- 第二单元混合运算单元测试卷(含答案) 2025-2026学年人教版三年级数学上册
- 2025年中央一号文件客观题及参考答案
- 出境人员行前安全培训课件
- 俄乌局势进展
- 2025甘肃兰州兴蓉环境发展有限责任公司招聘内控管理岗等岗位5人笔试模拟试题及答案解析
- 苏教版三年级上册数学全册教学设计(配2025年秋新版教材)
- 用电安全与消防知识培训课件
- 2025年法考真题及答案
- 基孔肯雅热防护知识科普课件
- 2025年思想政治教育实践考试试题及答案解析
评论
0/150
提交评论