Java小游戏俄罗斯方块附完整源代码_毕业设计
- 格式:doc
- 大小:407.00 KB
- 文档页数:64
使用JavaGUI开发俄罗斯方块游戏摘要俄罗斯方块是一款十分经典的游戏,它的主要运行规律为对系统随机产生的图形进行上下左右移动、旋转等操纵,使之排列成完整的一行或多行并且消除得分。
它上手容易,难度循序渐进,老少皆宜,深入人心,标志着一代人的童年。
同时以俄罗斯方块为基础由衍生出了很多种应用,因此进行俄罗斯方块的设计十分必要。
本文遵循设计流程,通过系统分析与设计,系统实现以及系统测试与发布三个阶段实现游戏设计。
关键词:俄罗斯方块开发;游戏编程;程序开发全套设计加扣3012250582Using JavaGUI develop tetris gameAbstractTetris is a very classic game, the main running rules is that it can generate the random system graphics make the next move around, rotating manipulation, can be arranged into one or more rows and eliminate scores. It is easy to use, difficult step by step, ages, win support among the people, marked the first generation of childhood. At the same time in Tetris based by derived from many applications, so it is very necessary to design. This paper follows the design process, through the system analysis and design, system realization and system testing and release of three stages of game design.Key Words: Tetris development; game programming; program developmen目录摘要 (I)ABSTRACT........................................................... I I 1 绪论.. (1)1.1研究背景 (1)1.2 JAVA简介 (1)1.3J AVA GUI编程简介 (3)1.4开发环境搭建 (5)2 系统分析与设计 (7)2.1程序设计思想 (7)2.2设计分析 (8)2.3主要功能 (9)2.4游戏的操作流程 (9)3 游戏实现 (11)3.1游戏设计的具体实现 (11)3.1.1游戏界面的设计实现 (11)3.1.2俄罗斯方块的造型 (11)3.1.3俄罗斯方块的旋转 (12)3.1.4方块的运动和自动消除满行的方块 (12)3.1.5游戏速度和游戏级别自由选择 (13)3.1.6游戏得分的计算和游戏菜单的编辑 (14)3.2游戏区域涉及的数据结构 (14)3.2.1游戏区域 (14)3.2.2 基础小砖块 (14)3.2.3下坠物的数据结构算法 (14)3.2.4 下坠物形状和状态的随机出现 (15)3.2.5 游戏的实现算法设计 (15)3.3下坠物的关键代码示例 (15)3.3.1游戏结束的判断 (15)3.3.2游戏下坠物是否已落到底,停止下落的判断 (16)3.4游戏运行 (17)4 系统测试和发布 (21)4.1测试环境 (21)4.2测试遇到的问题 (21)4.3程序发布 (22)结论 (23)参考文献 (24)致谢 (25)外文原文...........................................错误!未定义书签。
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.*;public class Tetris extends JFrame {public Tetris() {//实现了键盘监听的俄罗斯方块面板Tetrisblok a = new Tetrisblok();this.addKeyListener(a);this.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.add("继续");JMenuItem exit = game.add("退出");JMenu help = new JMenu("帮助");//帮助菜单中添加子菜单JMenuItem about = help.add("关于");//将游戏菜单加入菜单条menu.add(game);//将帮助菜单加入菜单条menu.add(help);frame.setSize(220, 275);//窗体居中显示frame.setLocationRelativeTo(null);//窗体大小不能改变frame.setResizable(false);frame.setTitle("俄罗斯方块");frame.setVisible(true);}}// 创建一个俄罗斯方块类class Tetrisblok extends JPanel implements KeyListener { private int blockType; // blockType 代表方块类型private int turnState; // turnState代表方块状态private int score = 0; // 得分private Timer timer; //定时器int x,y; //当前快左下角的位置int flag = 0;// 定义显示、移动区域;int[][] map = new int[13][23]; //0:空1:有方块2:围墙// 方块的形状第一组代表方块类型有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 },{ 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 } },// ■■□□// ■■□□// □□□□// □□□□{ { 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 } } };// 生成新方块的方法public void newblock() {//随机产生S、Z、L、J、I、O、T 7中图形中的一种blockType = (int) (Math.random() * 1000) % 7;//随机产生四种旋转状态中的一个状态turnState = (int) (Math.random() * 1000) % 4;//方块最初位置:左下角为(4,0)// x=(int)Math.random()*6+1;x = 4;y = 0;if (gameover(x, y) == 1) {newmap(); //清空所有方块drawwall();//画围墙score = 0;timer.stop(); //计时器停止JOptionPane.showMessageDialog(null, "GAME OVER");timer.start();//计时器开启}// 画围墙public void drawwall() {//底边围墙for (int i = 0; i < 12; i++) {map[i][21] = 2;}//两边围墙for (int j = 0; j < 21; j++) {map[11][j] = 2;map[0][j] = 2;}}// 初始化中间区域public void newmap() {for (int i = 0; i < 11; i++) {for (int j = 0; j < 21; j++) {map[i][j] = 0;}}}// 初始化构造方法Tetrisblok() {newblock();//随机产生一个方块newmap(); //清空绘图区drawwall();//画围墙//创建定时器,间隔为1000毫秒,时间到了执行TimerListener方法timer = new Timer(1000, new TimerListener());//启动定时器timer.start();}// 旋转的方法public void turn() {int tempturnState = turnState;turnState = (turnState + 1) % 4;//旋转后能否旋转该方块if (play(x, y, blockType, turnState) == 0) {turnState = tempturnState;}repaint();}// 左移的方法public void left() {if (play(x - 1, y, blockType, turnState) == 1) {x = x - 1;};repaint();}// 右移的方法public void right() {if (play(x + 1, y, blockType, turnState) == 1) {x = x + 1;};repaint();}// 下落的方法public void down() {if (play(x, y + 1, blockType, turnState) == 1) {y = y + 1;delline();}if (play(x, y + 1, blockType, turnState) == 0) {add(x, y, blockType, turnState);newblock();delline();}repaint();}// 是否可以移动public int play(int x, int y, int blockType, int turnState) { //将方块的图形由16列=>4行4列for (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {if (shapes[blockType][turnState][i * 4 + j] == 1 &&map[x + j + 1][y + i]!= 0)return 0;}}return 1;}// 消行的方法public void delline() {int c = 0;for (int y = 1; y <=20 ; y++) {for (int x = 1; x <=10 ; x++) {if (map[x][y] == 1) {c = c + 1;if (c == 10) {score += 10;for (int j = y; j > 0; j--) {for (int i = 0; i < 11; i++) {map[i][j] = map[i][j - 1];}}}}}c = 0;}}// 判断游戏是否结束public int gameover(int x, int y) {//在(x,y)处能否放下这个方块,不能放则游戏结束if (play(x, y, blockType, turnState) == 0) {return 1;}return 0;}// 把当前快添加到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 (map[x + b + 1][y + a] == 0) {map[x + b + 1][y + a] = shapes[blockType][turnState][j];}j++;}}}// 画方块的的方法public void paint(Graphics g) {super.paint(g);// 画当前方块for (int j = 0; j < 16; j++) {if (shapes[blockType][turnState][j] == 1) {g.setColor(Color.BLACK);//设置画笔颜色黑色g.fillRect((j % 4 + x + 1) * 10, (j / 4 + y) * 10, 9, 9);}}// 画已经固定的方块for (int j = 0; j < 22; j++) {for (int i = 0; i < 12; i++) {if (map[i][j] == 1) {g.setColor(Color.BLACK);//设置画笔颜色黑色g.fillRect(i * 10, j * 10, 9, 9);}if (map[i][j] == 2) {g.setColor(Color.RED);//设置画笔颜色红色g.fillRect(i * 10, j * 10, 10, 10);}}}g.drawString("score=" + score, 125, 10);g.drawString("抵制不良游戏,", 125, 50);g.drawString("拒绝盗版游戏。
//不多说,直接可以拷贝下面的东西,就可以运行。
package day04;import java.awt.*;import java.awt.event.*;import javax.swing.*;import java.applet.*;import ng.String.*;import ng.*;import java.io.*;public class ERSBlock extends JPanel implements ActionListener,KeyListener//应该是继承JPanel{static Button but[] = new Button[6];static Button noStop = new Button("取消暂停"); static Label scoreLab = new Label("分数:");static Label infoLab = new Label("提示:");static Label speedLab = new Label("级数:");static Label scoreTex = new Label("0");static Label infoTex = new Label(" ");static Label speedTex = new Label("1");static JFrame jf = new JFrame();static MyTimer timer;static ImageIcon icon=new ImageIcon("resource/Block.jpg");static JMenuBar mb = new JMenuBar();static JMenu menu0 = new JMenu("游戏 ");static JMenu menu1 = new JMenu("帮助 ");static JMenuItem mi0 = new JMenuItem("新游戏"); static JMenuItem mi1 = new JMenuItem("退出");static JMenuItem mi1_0 = new JMenuItem("关于"); static JDialog dlg_1;static JTextArea dlg_1_text = new JTextArea(); static int startSign= 0;//游戏开始标志 0 未开始 1 开始 2 暂停static String butLab[] = {"开始游戏","重新开始","降低级数","提高级数","游戏暂停","退出游戏"};static int game_body[][] = new int[19][10];static int game_sign_x[] = new int[4];//用于记录4个方格的水平位置static int game_sign_y[] = new int[4];//用于记录4个方格的垂直位置static boolean downSign = false;//是否落下static int blockNumber = 1;//砖块的编号static int gameScore = 0;//游戏分数static int speedMark = 1;public static void main(String args[]) {ERSBlock myBlock = new ERSBlock();mb.add(menu0);mb.add(menu1);menu0.add(mi0);menu0.add(mi1);menu1.add(mi1_0);jf.setJMenuBar(mb);myBlock.init();jf.add(myBlock);jf.setSize(565,501);jf.setResizable(false);jf.setTitle("俄罗斯方块");jf.setIconImage(icon.getImage());jf.setLocation(200,100);jf.show();timer = new MyTimer(myBlock); //启动线程timer.setDaemon(true);timer.start();timer.suspend();}public void init(){setLayout(null);for(int i = 0;i < 6;i++){but[i] = new Button(butLab[i]);add(but[i]);but[i].addActionListener(this);but[i].addKeyListener(this);but[i].setBounds(360,(240 + 30 * i),160,25); }add(scoreLab);add(scoreTex);add(speedLab);add(speedTex);add(infoLab);add(infoTex);add(scoreLab);scoreLab.setBounds(320,15,30,20); scoreTex.setBounds(360,15,160,20); scoreTex.setBackground(Color.white); speedLab.setBounds(320,45,30,20); speedTex.setBounds(360,45,160,20); speedTex.setBackground(Color.white);but[1].setEnabled(false);but[4].setEnabled(false);infoLab.setBounds(320,75,30,20); infoTex.setBounds(360,75,160,20); infoTex.setBackground(Color.white); noStop.setBounds(360,360,160,25); noStop.addActionListener(this); noStop.addKeyListener(this);mi0.addActionListener(this);mi1.addActionListener(this);mi1_0.addActionListener(this);num_csh_game();rand_block();}public void actionPerformed(ActionEvent e){if(e.getSource() == but[0])//开始游戏{startSign = 1;infoTex.setText("游戏已经开始!");but[0].setEnabled(false);but[1].setEnabled(true);but[4].setEnabled(true);timer.resume();}if(e.getSource() == but[1]||e.getSource() == mi0)//重新开始游戏{startSign = 0;gameScore = 0;timer.suspend();num_csh_restart();repaint();rand_block();scoreTex.setText("0");infoTex.setText("新游戏!");but[0].setEnabled(true);but[1].setEnabled(false);but[4].setEnabled(false);}if(e.getSource() == but[2])//降低级数 {infoTex.setText("降低级数!"); speedMark--;if(speedMark <= 1){speedMark = 1;infoTex.setText("已经是最低级数!"); }speedTex.setText(speedMark + ""); }if(e.getSource() == but[3])//提高级数 {infoTex.setText("提高级数!");speedMark++;if(speedMark >= 9){speedMark = 9;infoTex.setText("已经是最高级数!"); }speedTex.setText(speedMark + ""); }if(e.getSource() == but[4])//游戏暂停 {this.add(noStop);this.remove(but[4]);infoTex.setText("游戏暂停!"); timer.suspend();}if(e.getSource() == noStop)//取消暂停 {this.remove(noStop);this.add(but[4]);infoTex.setText("继续游戏!"); timer.resume();}if(e.getSource() == but[5]||e.getSource() == mi1)//退出游戏{jf.dispose();}if(e.getSource() == mi1_0)//退出游戏{dlg_1 = new JDialog(jf,"关于");try{FileInputStream io = new FileInputStream("resource/guanyu.txt");//得到路径byte a[] = new byte[io.available()];io.read(a);io.close();String str = new String(a);dlg_1_text.setText(str);}catch(Exception g){}dlg_1_text.setEditable(false);dlg_1.add(dlg_1_text);dlg_1.pack();dlg_1.setResizable(false);dlg_1.setSize(200, 120);dlg_1.setLocation(400, 240);dlg_1.show();}}public void rand_block()//随机产生砖块{int num;num = (int)(Math.random() * 6) + 1;//产生0~6之间的随机数blockNumber = num;switch(blockNumber){case 1: block1(); blockNumber = 1; break;case 2: block2(); blockNumber = 2; break;case 3: block3(); blockNumber = 3; break;case 4: block4(); blockNumber = 4; break;case 5: block5(); blockNumber = 5; break;case 6: block6(); blockNumber = 6; break;case 7: block7(); blockNumber = 7; break;}}public void change_body(int blockNumber)//改变砖块状态{dingwei();if(blockNumber == 1&&downSign == false)//变换长条2种情况{if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 16)//说明长条是横着的{if(game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] != 2){num_csh_game();game_body[game_sign_y[0] - 1][game_sign_x[0] + 1] = 1;game_body[game_sign_y[1]][game_sign_x[1]] = 1;game_body[game_sign_y[2] + 1][game_sign_x[2] - 1] = 1;game_body[game_sign_y[3] + 2][game_sign_x[3] - 2] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] >= 1&&game_sign_x[3] <= 7)//说明长条是竖着的{if(game_body[game_sign_y[0] +1][game_sign_x[0]-1] != 2&&game_body[game_sign_y[3] -2][game_sign_x[3] + 2] != 2){num_csh_game();game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;game_body[game_sign_y[1]][game_sign_x[1]]=1;game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;game_body[game_sign_y[3] - 2][game_sign_x[3] + 2] = 1;infoTex.setText("游戏进行中!");repaint();}}}if(blockNumber == 3&&downSign == false)//变换转弯1有4种情况{if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] >= 1){if(game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2){num_csh_game();game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;game_body[game_sign_y[1]][game_sign_x[1]] = 1;= 1;game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] <= 17){if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2){num_csh_game();game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;game_body[game_sign_y[2]][game_sign_x[2]] = 1;= 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_x[1] == game_sign_x[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[3] <= 8){if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2){num_csh_game();game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1;game_body[game_sign_y[2]][game_sign_x[2]] = 1;game_body[game_sign_y[3] - 1][game_sign_x[3] + 1]= 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] == game_sign_x[3]) {if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2){num_csh_game();game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1;game_body[game_sign_y[1]][game_sign_x[1]] = 1;game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1;infoTex.setText("游戏进行中!");repaint();}}}if(blockNumber == 4&&downSign == false)//变换转弯2有4种情况{if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[3] <= 7){if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3]][game_sign_x[3] + 2] != 2){num_csh_game();game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;game_body[game_sign_y[2]][game_sign_x[2]] = 1;game_body[game_sign_y[3]][game_sign_x[3] + 2] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[1] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[2]) {if(game_body[game_sign_y[1]][game_sign_x[1] + 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2){num_csh_game();game_body[game_sign_y[0]][game_sign_x[0]] = 1;game_body[game_sign_y[1]][game_sign_x[1] + 2] = 1;game_body[game_sign_y[2] - 1][game_sign_x[2] + 1] = 1;game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] == game_sign_x[3]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[0] >= 2){if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2){num_csh_game();game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;game_body[game_sign_y[1]][game_sign_x[1]] = 1;game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[0] == game_sign_y[2]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[0] <= 16){if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] - 2] != 2){num_csh_game();game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;game_body[game_sign_y[1] + 1][game_sign_x[1] - 1] = 1;game_body[game_sign_y[2]][game_sign_x[2] - 2] = 1;game_body[game_sign_y[3]][game_sign_x[3]] = 1;infoTex.setText("游戏进行中!");repaint();}}}if(blockNumber == 5&&downSign == false)//变换转弯3有4种情况{if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[2] == game_sign_x[3]&&game_sign_y[0] == game_sign_y[1]&&game_sign_x[1] >= 2){if(game_body[game_sign_y[0] + 1][game_sign_x[0] -1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] -2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2){num_csh_game();game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1;game_body[game_sign_y[2]][game_sign_x[2]] = 1;game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[1] == game_sign_y[2]&&game_sign_y[2] == game_sign_y[3]&&game_sign_x[0] == game_sign_x[1]&&game_sign_y[0] <= 16){if(game_body[game_sign_y[0] + 2][game_sign_x[0]] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2){num_csh_game();game_body[game_sign_y[0] + 2][game_sign_x[0]] = 1;game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;game_body[game_sign_y[2]][game_sign_x[2]] = 1;game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[1] == game_sign_x[3]&&game_sign_y[2] == game_sign_y[3]) {if(game_body[game_sign_y[0] + 1][game_sign_x[0] -1] != 2&&game_body[game_sign_y[2]][game_sign_x[2] +2] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2){num_csh_game();game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;game_body[game_sign_y[1]][game_sign_x[1]] = 1;game_body[game_sign_y[2]][game_sign_x[2] + 2] = 1;game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[1] == game_sign_y[2]&&game_sign_x[2] == game_sign_x[3]){if(game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] != 2&&game_body[game_sign_y[3] - 2][game_sign_x[3]] != 2){num_csh_game();game_body[game_sign_y[0] + 1][game_sign_x[0] + 1] = 1;game_body[game_sign_y[1]][game_sign_x[1]] = 1;game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;game_body[game_sign_y[3] - 2][game_sign_x[3]] = 1;infoTex.setText("游戏进行中!");repaint();}}}if(blockNumber == 6&&downSign == false)//变换两层砖块1的2种情况{if(game_sign_x[0] == game_sign_x[2]&&game_sign_x[0] >= 2){if(game_body[game_sign_y[0]][game_sign_x[0] - 2] != 2&&game_body[game_sign_y[2] - 1][game_sign_x[2] -1 ] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] != 2){num_csh_game();game_body[game_sign_y[0]][game_sign_x[0] - 2] = 1;game_body[game_sign_y[1]][game_sign_x[1]] = 1;game_body[game_sign_y[2] - 1][game_sign_x[2] - 1] = 1;game_body[game_sign_y[3] - 1][game_sign_x[3] + 1] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[3] <= 17){if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] != 2){num_csh_game();game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1;game_body[game_sign_y[1] + 1][game_sign_x[1] + 1] = 1;game_body[game_sign_y[2]][game_sign_x[2]] = 1;game_body[game_sign_y[3] + 1][game_sign_x[3] - 1] = 1;infoTex.setText("游戏进行中!");repaint();}}}if(blockNumber == 7&&downSign == false)//变换两层砖块2的2种情况{if(game_sign_x[0] == game_sign_x[1]&&game_sign_x[0] <= 16){if(game_body[game_sign_y[0]][game_sign_x[0] + 2] != 2&&game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] != 2&&game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] != 2){num_csh_game();game_body[game_sign_y[0]][game_sign_x[0] + 2] = 1;game_body[game_sign_y[1] - 1][game_sign_x[1] + 1] = 1;game_body[game_sign_y[2]][game_sign_x[2]] = 1;game_body[game_sign_y[3] - 1][game_sign_x[3] - 1] = 1;infoTex.setText("游戏进行中!");repaint();}}if(game_sign_y[0] == game_sign_y[1]&&game_sign_y[2] <= 17)if(game_body[game_sign_y[0] + 1][game_sign_x[0] -1] != 2&&game_body[game_sign_y[1]][game_sign_x[1] -2] != 2&&game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] != 2){num_csh_game();game_body[game_sign_y[0] + 1][game_sign_x[0] - 1] = 1;game_body[game_sign_y[1]][game_sign_x[1] - 2] = 1;game_body[game_sign_y[2] + 1][game_sign_x[2] + 1] = 1;game_body[game_sign_y[3]][game_sign_x[3]] = 1;infoTex.setText("游戏进行中!");repaint();}}}}public void num_csh_game()//数组清零for(int i = 0;i < 19;i++){for(int j = 0;j < 10;j++){if(game_body[i][j] == 2){game_body[i][j] = 2;}else{game_body[i][j] = 0;}}}}public void num_csh_restart()//重新开始时数组清零 {for(int i = 0;i < 19;i++){for(int j = 0;j < 10;j++)game_body[i][j] = 0;}}}public void keyTyped(KeyEvent e){}public void keyPressed(KeyEvent e){if(e.getKeyCode() == KeyEvent.VK_DOWN&&startSign == 1)//处理下键{this.down();}if(e.getKeyCode() == KeyEvent.VK_LEFT&&startSign == 1)//处理左键{this.left();}if(e.getKeyCode() == KeyEvent.VK_RIGHT&&startSign== 1)//处理右键{this.right();}if(e.getKeyCode() == KeyEvent.VK_UP&&startSign== 1)//处理上键转换{this.change_body(blockNumber);}if(startSign == 0){infoTex.setText("游戏未开始或已结束!");}}public void keyReleased(KeyEvent e){}public void paint(Graphics g){g.setColor(Color.black);g.fill3DRect(0,0,300,450,true);for(int i = 0;i < 19;i++){for(int j = 0;j < 10;j++){if(game_body[i][j] == 1){g.setColor(Color.blue);g.fill3DRect(30*j,30*(i-4),30,30,true); }if(game_body[i][j] == 2){g.setColor(Color.magenta);g.fill3DRect(30*j,30*(i-4),30,30,true); }}}}public void left()//向左移动{int sign = 0;dingwei();for(int k = 0;k < 4;k++){if(game_sign_x[k] == 0||game_body[game_sign_y[k]][game_sign_x[k] - 1] == 2){sign = 1;}}if(sign == 0&&downSign == false){num_csh_game();for(int k = 0;k < 4;k++){game_body[game_sign_y[k]][game_sign_x[k] - 1] = 1; }infoTex.setText("向左移动!");repaint();}}public void right()//向右移动{int sign = 0;dingwei();for(int k = 0;k < 4;k++){if(game_sign_x[k] == 9||game_body[game_sign_y[k]][game_sign_x[k] + 1] == 2){sign = 1;}}if(sign == 0&&downSign == false){num_csh_game();for(int k = 0;k < 4;k++){game_body[game_sign_y[k]][game_sign_x[k] + 1] = 1; }infoTex.setText("向右移动!");repaint();}}public void down()//下落{int sign = 0;dingwei();for(int k = 0;k < 4;k++){if(game_sign_y[k] == 18||game_body[game_sign_y[k] + 1][game_sign_x[k]] == 2){sign = 1;downSign = true;changeColor();cancelDW();getScore();if(game_over() == false){rand_block();repaint();}}}if(sign == 0){num_csh_game();for(int k = 0;k < 4;k++){game_body[game_sign_y[k] + 1][game_sign_x[k]] = 1;}infoTex.setText("游戏进行中!");repaint();}}public boolean game_over()//判断游戏是否结束{int sign=0;for(int i = 0;i < 10;i++){if(game_body[4][i] == 2){sign = 1;}}if(sign == 1){infoTex.setText("游戏结束!");changeColor();repaint();startSign = 0;timer.suspend();return true;}elsereturn false;}public void getScore()//满行消除方法{for(int i = 0;i < 19;i++){int sign = 0;for(int j = 0;j < 10;j++){if(game_body[i][j] == 2){sign++;}}if(sign == 10){gameScore += 100;scoreTex.setText(gameScore+"");infoTex.setText("恭喜得分!");for(int j = i;j >= 1;j--){for(int k = 0;k < 10;k++){game_body[j][k] = game_body[j - 1][k];}}}}}public void changeColor()//给已经落下的块换色{downSign = false;for(int k = 0;k < 4;k++){game_body[game_sign_y[k]][game_sign_x[k]] = 2; }}public void dingwei()//确定其位置{int k = 0;cancelDW();for(int i = 0;i < 19;i++){for(int j = 0;j < 10;j++){if(game_body[i][j] == 1){game_sign_x[k] = j;game_sign_y[k] = i;k++;}}}}public void cancelDW()//将定位数组初始化{for(int k = 0;k < 4;k++){game_sign_x[k] = 0;game_sign_y[k] = 0;}}public void block1()//长条{game_body[0][4] = 1;game_body[1][4] = 1;game_body[2][4] = 1;game_body[3][4] = 1;}public void block2()//正方形{game_body[3][4] = 1;game_body[3][5] = 1;game_body[2][5] = 1;}public void block3()//3加1(下) {game_body[1][4] = 1;game_body[2][4] = 1;game_body[3][4] = 1;game_body[3][5] = 1;}public void block4()//3加1(中) {game_body[1][4] = 1;game_body[2][4] = 1;game_body[3][4] = 1;game_body[2][5] = 1;}public void block5()//3加1(上) {game_body[1][4] = 1;game_body[2][4] = 1;game_body[1][5] = 1;}public void block6()//转折1 {game_body[1][5] = 1;game_body[2][5] = 1;game_body[2][4] = 1;game_body[3][4] = 1;}public void block7()//转折2 {game_body[1][4] = 1;game_body[2][4] = 1;game_body[2][5] = 1;game_body[3][5] = 1;}}//定时线程class MyTimer extends Thread {ERSBlock myBlock;public MyTimer(ERSBlock myBlock){this.myBlock = myBlock;}public void run(){while(myBlock.startSign == 1){try{sleep((10-myBlock.speedMark + 1)*100);myBlock.down();}catch(InterruptedException e){}}}}。
毕业设计(论文)正文题目俄罗斯方块游戏专业班级姓名学号指导教师职称俄罗斯方块游戏摘要: 在现代信息高速发展的时代,电子游戏已经深入了人们的日常生活,成为了老少咸宜的娱乐方式,但是游戏设计结合了日新月异的技术,在一个产品中整合了复杂的艺术,设计,声音和软件,所以并不是人人皆知,直到今天,在中国从事游戏设计的人仍然很少,但是游戏行业的发展之快,远超如汽车,家电等传统行业,也正因为如此,游戏人才的教育培养远落后于行业的发展。
俄罗斯方块是一个老少咸宜的小游戏,它实现有四个正方形的色块组成,然后存储于一个数组的四个元素中,计算机随机产生七种不同类型的方块,根据计算机时钟控制它在一定的时间不停的产生,用户根据键盘的四个方向键进行向左,向右,向下,翻转操作。
然后程序根据这七种方块折叠成各种不同的类型。
论文描述了游戏开发的背景,意义,算法分析,功能实现,功能测试。
以C++为开发语言进行设计与实现。
关键词:电子游戏,算法,C++,测试The Russian square pieceAbstract :In the era of high-speed development of electronic of information, computer game has enter people’s daily life, become an amusement adapt to old and young. But game design is a combination of fast-moving technology ,the complexity of integrati ng design,art,audio and software into a single production,so this thechnology isn’t known by everyone .up-to-date,there are few people work at game design all the same,whereas,thedevelopment of game industry more faster than traditional industry as home ap pliances and automobile,by the reason of this situation,the education and training of person with ablity of game design drop behind the development of game industry.The Russian square piece is a get-away drama with all proper old young ,it carry out to be constitute by four pieces of colours of exact square piece ,then save in one four chemical elements of the piece set ,random creation dissimilarity of calculator seven the square piece of the category type ,control it according to the calculator clock in certain time continuously creation , the customer is inside out according to four directions key control of the keyboard ,to left ,rightwards and get down ,(the realization of the control key is to be carry out by the event handing of the direction key of the keyboard) Then the procedure pileds according to these seven kinds of square pieces various different model.The thesis has described the game history ,has developed this game history ,has developed this game environment, development significance of game .Knowledge abiding by a software engineering ,definition begins from software problem ,proceed to carry out feasibility study ,need analysis ,essentials design,the at last has carried out a testing on the software engineering knowledge hierarchy .The computer games design and practice are designed o eclipse developing platform with C++ developing instrument ,under Microsoft Windows XP system this time.Key Words: electronic game calculate way C++ test目录1引言 (1)1.1课题背景 (1)1.2毕设意义 (2)2需求与算法分析 (3)2.1需求分析 (3)2.1.1 游戏需求 (3)2.1.2游戏界面需求 (4)2.1.3 游戏形状(方块)需求 (4)2.2算法分析 (5)2.2.1定义方块的数据结构 (5)2.2.2俄罗斯方块流程 (6)3系统功能实现 (8)3.1产生主窗口 (8)3.2定义俄罗斯方块数据结构 (9)3.3游戏的主逻辑 (10)3.4销行功能实现 (12)3.5中断操作流程的实现 (14)3.6变形的实现 (16)3.7 游戏区域绘图的实现 (17)3.8 游戏方块绘制 (21)3.9 烟花燃放功能 (23)4功能测试 (27)4.1测试环境 (27)4.2图像功能测试 (27)4.3销行和计分功能测试 (30)4.4速度功能测试 (32)5总结 (34)[参考文献] (35)致谢 (36)┊┊┊┊┊┊┊┊┊┊┊┊┊装┊┊┊┊┊订┊┊┊┊┊线┊┊┊┊┊┊┊┊┊┊┊┊┊俄罗斯方块的程序设计1引言计算机游戏产业在随着网络的发展有了长足的发展。
java俄罗斯方块小游戏代码package keshe;import java.awt.Color; //颜色import java.awt.Font; //字体import java.awt.Graphics; //绘图import java.awt.event.ActionEvent; //动作import java.awt.event.ActionListener; //事件处理importjava.awt.event.KeyAdapter; //接受键盘信息import java.awt.event.KeyEvent; //键盘信息处理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 ddddddd extends JFrame{public static void main(String[] args) {ddddddd te = new ddddddd();te.setVisible(true);}private TetrisPanel tp;JMenuItem itemPause;JMenuItem itemContinue;JMenuItem itemgnd;JMenuItem itemdnd;public ddddddd() {this.setDefaultCloseOperation(EXIT_ON_CLOSE);this.setLocation(500, 200);this.setSize(220, 275);this.setResizable(false);tp = new TetrisPanel();this.getContentPane().add(tp);//添加菜单JMenuBar menubar = new JMenuBar();this.setJMenuBar(menubar);JMenu menuGame = new JMenu("游戏");menubar.add(menuGame);JMenuItem itemNew = new JMenuItem("新游戏"); itemNew.setActionCommand("new");itemgnd = new JMenuItem("提高难度");itemgnd.setActionCommand("gnd"); itemdnd = new JMenuItem("降低难度");itemdnd.setActionCommand("dnd");itemPause = new JMenuItem("暂停");itemPause.setActionCommand("pause");itemContinue = new JMenuItem("继续");itemContinue.setActionCommand("continue");itemContinue.setEnabled(false);itemdnd.setEnabled(false);menuGame.add(itemNew);menuGame.add(itemPause);menuGame.add(itemContinue);menuGame.add(itemgnd);menuGame.add(itemdnd);MenuListener menuListener = new MenuListener();itemNew.addActionListener(menuListener);itemPause.addActionListener(menuListener);itemContinue.addActionListener(menuListener);itemgnd.addActionListener(menuListener);itemdnd.addActionListener(menuListener);//让整个JFrame添加键盘监听this.addKeyListener( tp.listener );}class MenuListener implements ActionListener{ public void actionPerformed(ActionEvent e) { //玩新游戏if(e.getActionCommand().equals("new")){tp.newGame();}if(e.getActionCommand().equals("pause")){ timer.stop();itemContinue.setEnabled(true);itemPause.setEnabled(false);}if(e.getActionCommand().equals("continue")){ timer.restart(); itemContinue.setEnabled(false);itemPause.setEnabled(true);}if(tp.nandu==1){itemdnd.setEnabled(false);}else itemdnd.setEnabled(true);if(tp.nandu==9)itemgnd.setEnabled(false);else itemgnd.setEnabled(true);if(e.getActionCommand().equals("gnd")){ tp.delay/=1.3;tp.nandu+=1;timer.setDelay(tp.delay);}if(e.getActionCommand().equals("dnd")){ tp.delay*=1.3;tp.nandu-=1;timer.setDelay(tp.delay);}}}private Timer timer; class TetrisPanel extends JPanel{// 方块的形状:// 第一维代表方块类型(包括7种:S、Z、L、J、I、O、T)// 第二维代表旋转次数// 第三四维代表方块矩阵// shapes[type][turnState][i] i--> block[i/4][i%4]int shapes[][][] = new int[][][] {// I (※把版本1中的横条从第1行换到第2行){ { 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, 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 } }, // 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 } } };private int blockType;//方块类型private int turnState;//旋转状态private int dierkuai;private int dierState;private int x;//方块的位置x--列的位置--列号private int y;//方块的位置y--行的位置--行号private int map[][]=new int[13][23];//地图:12列22行。
基于Java的游戏“俄罗斯方块”的设计与实现毕业设计目录第1章绪论 (1)1.1 本设计的目的和意义 (1)1.2 国内外研究现状 (1)1.3 设计研究的主要内容、目标与工具 (2)1.3.1 设计的主要内容 (2)1.3.2 设计要达到的目标 (3)1.3.3 设计方法介绍 (3)第2章可行性分析 (7)2.1 可行性分析 (7)2.1.1 可行性研究的前提 (7)2.1.1.1 要求 (7)2.1.1.2 目标 (8)2.1.1.3 条件、假定和限制 (8)2.1.2 对现有软件的分析 (8)2.2 所建议的软件 (9)2.2.1 游戏处理流程 (9)2.2.2 社会可行性分析 (10)2.2.3 技术可行性分析 (11)2.2.3.1 执行平台方面 (11)2.2.3.2 执行速度方面 (12)2.2.3.3 语言特性与功能方面 (12)2.2.3.4 开发环境方面 (13)2.2.3.5 JBuilder开发工具 (13)2.2.4 经济可行性 (13)第3章需求分析 (14)3.1 任务概述 (14)3.1.1 目标 (14)3.1.2 用户的特点 (14)3.2 具体需求 (14)3.2.1 功能需求 (14)3.2.1.1 游戏主界面模块需求 (14)3.2.1.2 游戏图形区域界面的需求 (15)3.2.1.3 方块下落速度需求 (15)3.2.1.4 游戏分数需求 (15)3.2.1.5 游戏排行榜需求 (15)3.2.2 属性需求 (15)第4章概要设计 (16)4.1 游戏功能 (16)4.2 功能模块设计 (16)4.2.1 总设计模块的划分 (16)4.2.2 游戏主界面模块 (17)4.2.3 游戏控制模块 (17)4.2.4 游戏设置模块 (18)4.2.5 游戏排行榜模块 (18)4.3 类模块设计 (19)4.3.1 类模块之间关系 (19)4.3.2 各类模块设计概要 (19)4.3.3 类和Java源文件对应关系 (20)4.4 程序设计的重点 (21)4.4.1 游戏图形界面的图形显示更新功能 (21)4.4.2 游戏方块的设计 (21)4.5 接口设计 (22)4.5.1 外部接口 (22)4.5.2 外部接口 (22)4.6 维护设计 (22)4.7 故障处理 (22)第5章详细设计 (24)5.1 程序主结构 (24)5.2 开发环境配置 (24)5.2.1 Java2的标准运行环境 (24)5.2.1.1 J2SE SDK (24)5.2.1.2 J2SE JRE (25)5.2.1.3 J2SE Documentation (25)5.2.2 J2SE的安装与配置 (25)5.2.2.1安装过程 (25)5.2.2.2 配置环境变量 (28)5.3 类模块程序流程图 (31)5.3.1 BlockGame类 (31)5.3.2 BlockFrame类 (32)5.3.3 Square类 (32)5.3.4 LevelDialog类 (32)5.3.5 Game类 (33)5.3.6 Score类 (35)5.3.7 SaveScoreDialog类 (35)5.3.8 ReportDialog类 (36)5.3.9 AboutDialog类 (36)5.4 类模块具体设计 (36)5.4.1 BlockGame.java程序 (36)5.4.2 BlockFrame.java程序 (37)5.4.2.1 BlockFrame类程序 (37)5.4.2.2 Game类程序 (38)5.4.2.3 LevelDialog类程序 (41)5.4.2.4 BlockFrame.java的UML图 (41)5.4.3 Score.java程序 (43)5.4.4 SaveScoreDialog.java程序 (44)5.4.5 Reportdialog.java程序 (47)5.4.6 AboutDialog.java程序 (49)5.4.7 Square.java程序 (50)5.5 安装文件的生成 (51)5.5.1 inno setup简介 (51)5.5.2 安装文件制作步骤 (51)5.6 游戏界面展示 (55)第6章软件测试 (59)6.1 程序代码调试 (59)6.1.1 用正常数据调试 (59)6.1.2 异常数据调试 (59)6.1.3 用错误数据调试 (59)6.2 程序功能测试 (59)6.2.1 模块功能测试 (60)6.2.2 确认测试 (61)第7章软件维护 (62)结论 (63)致谢 (64)参考文献 (65)附录“俄罗斯方块游戏”程序源代码 (66)第1章绪论1.1 本设计的目的和意义俄罗斯方块游戏具有广泛的用户群,因为它比较简单有趣,无论老少都比较适合。
java实现俄罗斯⽅块⼩游戏本⽂实例为⼤家分享了java实现俄罗斯⽅块的具体代码,供⼤家参考,具体内容如下使⽤⼀个⼆维数组保存游戏的地图:// 游戏地图格⼦,每个格⼦保存⼀个⽅块,数组纪录⽅块的状态private State map[][] = new State[rows][columns];游戏前先将所有地图中的格⼦初始化为空:/* 初始化所有的⽅块为空 */for (int i = 0; i < map.length; i++) {for (int j = 0; j < map[i].length; j++) {map[i][j] = State.EMPTY;}}玩游戏过程中,我们能够看到界⾯上的⽅块,那么就得将地图中所有的⽅块绘制出来,当然,除了需要绘制⽅块外,游戏积分和游戏结束的字符串在必要的时候也需要绘制:/*** 绘制窗体内容,包括游戏⽅块,游戏积分或结束字符串*/@Overridepublic void paint(Graphics g) {super.paint(g);for (int i = 0; i < rows; i++) {for (int j = 0; j < columns; j++) {if (map[i][j] == State.ACTIVE) { // 绘制活动块g.setColor(activeColor);g.fillRoundRect(j * BLOCK_SIZE, i * BLOCK_SIZE + 25,BLOCK_SIZE - 1, BLOCK_SIZE - 1, BLOCK_SIZE / 5,BLOCK_SIZE / 5);} else if (map[i][j] == State.STOPED) { // 绘制静⽌块g.setColor(stopedColor);g.fillRoundRect(j * BLOCK_SIZE, i * BLOCK_SIZE + 25,BLOCK_SIZE - 1, BLOCK_SIZE - 1, BLOCK_SIZE / 5,BLOCK_SIZE / 5);}}}/* 打印得分 */g.setColor(scoreColor);g.setFont(new Font("Times New Roman", Font.BOLD, 30));g.drawString("SCORE : " + totalScore, 5, 70);// 游戏结束,打印结束字符串if (!isGoingOn) {g.setColor(Color.RED);g.setFont(new Font("Times New Roman", Font.BOLD, 40));g.drawString("GAME OVER !", this.getWidth() / 2 - 140,this.getHeight() / 2);}}通过随机数的⽅式产⽣⽅块所组成的⼏种图形,⼀般七种图形:条形、⽥形、正7形、反7形、T形、Z形和反Z形,如⽣成条形:map[0][randPos] = map[0][randPos - 1] = map[0][randPos + 1]= map[0][randPos + 2] = State.ACTIVE;⽣成图形后,实现下落的操作。
Java程序课程设计任务书俄罗斯方块游戏的开发1、主要内容:俄罗斯方块游戏具有广泛的游戏人群,因为它比较简单有趣,无论老少都比较适合。
俄罗斯方块游戏的设计对于每一个Java语言设计者进行语言提高和进阶都是一个很好的锻炼机会。
俄罗斯方块游戏的设计工作是非常复杂和重要的,它涉及面逛,牵涉面多,如果不好好考虑和设计,将难以成功开发出这个游戏。
在这个游戏的设计中,将牵涉到图形界面的显示与更新,数据的收集与更新并且在这个游戏的开发中还会应用类的继承机制以及一些设计模式。
因此,如何设计和开发好这个俄罗斯方块游戏,对于提高Java开发水平和系统的设计能力有极大的帮助。
在设计开发过程中,开发者需要处理好各个类之间的集成关系,还要处理各个类的相应的封装,并且还要协调好各个模块之间的逻辑依赖关系和数据通信关系。
2、具体要求(包括技术要求等):系统的功能设计要求:本课程设计将实现以下几种功能。
1.游戏界面主框架游戏界面主框架主要包括游戏图形区域界面,游戏速度的选择更新界面,,游戏分数的显示更新界面,下一个图形方块的显示更新区域,开始游戏按钮,重新开始游戏按钮以及退出游戏按钮游戏界面主框架的主要结构如下图所示。
2.游戏图形区域界面的显示更新功能游戏图形区域界面主要是一个图形显示更新区域,主要包括游戏方块显示更新,整行方块的删除和更新,进行中和游戏结束时的分数更新和游戏图形区域界面的清除。
在这个游戏图形区域界面中,主要是一个表格,根据相应格子的设置标志来显示相应的图形图片,这样就实现了俄罗斯方块的实时显示。
3.游戏方块的设计在俄罗斯方块游戏中,具体的游戏方块图形的设计是比较重要的一个方面。
因为俄罗斯方块游戏中主要的动作就是控制游戏方块的移动和翻转,以便于组成一行行连续的方块从而增加游的分数。
由于主要的游戏动作都集中在这个游戏方块上,因此游戏方块的设计就显得格外重要了。
为了增加程序的可扩展性,这里设计一个游戏方块的基类,各个具体的游戏方块都从这个基类开始继承。
**** 届毕业设计Java小游戏俄罗斯方块┊┊┊┊┊┊┊┊┊┊┊┊┊装┊┊┊┊┊订┊┊┊┊┊线┊┊┊┊┊┊┊┊┊┊┊┊┊摘要在现今电子信息高速发展的时代,电子游戏已经深入人们的日常生活,成为老少皆宜的娱乐方式。
但是游戏设计结合了日新月异的技术,在一个产品中整合了复杂的设计、艺术、声音和软件,所以并不是人人皆知。
直到今天,在中国从事游戏设计的人仍然很少,但是游戏行业的发展之快,远超如家电、汽车等传统行业,也正因为如此,游戏人才的教育、培养远落后于产业的发展。
俄罗斯方块是个老幼皆宜的小游戏,它实现由四块正方形的色块组成,然后存储在一个数组的四个元素中,计算机随机产生不同七种类型的方块,根据计算机时钟控制它在一定的时间不停的产生,用户根据键盘的四个方向键控制翻转、向左、向右和向下操作,(控制键的实现是由键盘的方向键的事件处理实现)。
然后程序根据这七种方块堆叠成各种不同的模型。
论文描述了游戏的历史,开发此游戏的环境,游戏开发的意义。
遵循软件工程的知识,从软件问题定义开始,接着进行可行性研究、需求分析、概要设计、详细设计,最后对软件进行了测试,整个开发过程贯穿软件工程的知识体系。
此次设计在Microsoft Windows 7系统下,以Java为开发语言,在eclipse开发平台上进行游戏的设计与实践。
从游戏的基本玩法出发,主要就是俄罗斯方块的形状和旋转,我在设计中在一个图片框中构造了一些的网状小块,由这些小块组合成新的形状,每四个小块连接在一起就可以构造出一种造型,因此我总共设计了7中造型,每种造型又可以通过旋转而变化出2到4种形状,利用随机函数在一个欲览窗体中提前展示形状供用户参考,在游戏窗体中用户就可以使用键盘的方向键来控制方块的运动,然后利用递归语句对每一行进行判断,如果有某行的方块是满的,则消除这行的方块,并且使上面的方块自由下落,最后就可以得出用户的分数。
关键词:游戏设计,算法,数组,事件┊┊┊┊┊┊┊┊┊┊┊┊┊装┊┊┊┊┊订┊┊┊┊┊线┊┊┊┊┊┊┊┊┊┊┊┊┊ABSTRACTIn today's era of rapid develop ment of electronic information, electronic games has penetrated people's daily lives,become enter tain ment for all ages. But, art, sound and software, not known.Until today, the Chinese people in the game design is still very small, but the game industry's rapid development, far more than such as house hold appliances, auto mobiles and other traditional industries, but also because of this,the game personnel education and training is far behind the develop ment of the industry. Te tris is a game for all ages,it is achieved by the fours quare blocks of color, and then stored in an array off our elements, the computer randomly generated seven types of boxes, according to the computer clock to control it in a certain time to stop the generation of the user according to the four key board arrow keys to control the flip, left, right and down operation(control key implementation is provided by the arrow keys on the keyboard event hand ling to achieve).Then the program under these seven boxes stacked into a variety of different models.Paper describes the history of the game, the develop ment environ ment for this game, game develop ment significance.Follow software engineering knowledge,from a software problem definition, followed by feasibility studies,needs analysis, out line design, detailed design, and finally the software has been tested throughout the development process through out the software engineering body of knowledge. The design in Microsoft Windows XP system,for the development of the ,in eclipse develop ment platform for game design and practice.The design in Microsoft Windows 7 system, for the development of the Java language, in eclipse development platform for game design and practice. Starting from the basic game play, mainly the shape and rotation of Tetris, I have a picture box in the design of some mesh pieces constructed from a combination of these pieces into a new shape, connected to each of the four pieces together, they can construct a kind of shape, so I designed a total of 7 shapes, each shape and can be varied by rotating the 2-4 kinds of shapes, using the random function in a form previewed to browse shapes for users reference in the game form the user can use the keyboard arrow keys to control the movement of boxes, and then use the recursive statement for each line judge, if there is a line in the box is full, then remove this line in the box, and make the box above free-fall, and finally can be drawn on the user's score.Keywords:game design,algorithm,arrays,event┊┊┊┊┊┊┊┊┊┊┊┊┊装┊┊┊┊┊订┊┊┊┊┊线┊┊┊┊┊┊┊┊┊┊┊┊┊目录第一章绪论 (1)1.1游戏的历史 (1)1.1.1 从头谈起 (1)1.1.2 图形硬件的革命 (2)1.2游戏的意义与内涵 (2)1.2.1 游戏的组成要素 (2)1.3 Java的定义 (3)1.4 Applet的定义 (8)1.5 JDK简介 (9)第二章可行性研究 (11)2.1 设计目的 (11)2.2 可行性研究前提 (11)2.3 可行性分析 (11)2.4 结论意见 (12)第三章需求分析 (13)3.1 引言 (13)3.2 游戏需求 (13)3.3 软硬件需求 (13)3.4 接口控制 (14)3.5 方案论证 (14)3.5.1 VB的优点 (14)3.5.2 C++的优点 (14)3.5.3 Java的优点 (14)3.5.4方案选择 (16)第四章概要设计 (17)4.1 游戏设计分析 (17)4.2 注意事项 (18)4.3 游戏流程图 (18)第五章详细设计 (20)5.1 总体设计 (20)5.2 屏幕信息初始化(paint()) (21)5.3 方块的装载(begin()) (23)5.4 处理键盘事件 (25)5.5 方块变化(change()) (26)5.6 控制游戏速度与自动下降(run()) (28)5.7 处理到达事件 (30)5.8 判断满行及消行 (32)5.9 显示控制 (34)5.10 保存方块坐标(save()) (34)┊┊┊┊┊┊┊┊┊┊┊┊┊装┊┊┊┊┊订┊┊┊┊┊线┊┊┊┊┊┊┊┊┊┊┊┊┊5.11 处理游戏结束 (34)第六章总结 (35)致谢 (36)参考文献 (37)附录主要代码展示及详解 (38)第一章绪论1.1游戏的历史游戏开发至今已经有30多年,在这个短暂的时期里,随着硬件水平的提高,游戏开发新技术层出不穷,经典游戏比比皆是。
电子游戏,也就是运行在家用电脑、家用电子游戏机或是掌中宝游戏机及街机上的电子游戏程序。
电子游戏是一种结合剧情故事、美术、音乐、动画、程序等技术于一身的互动型娱乐软件,涉及到多个行业。
从电子游戏的分类来看,有着多种分类方式。
传统的游戏分类是按照游戏类型,将其分为即时战略游戏、第一人称射击游戏、角色扮演游戏、策略型游戏等类别。