序言

声明一下:本人是java初学者,所以这个游戏的代码大部分是由我买的<<疯狂java实战演义 >>面的, 当然也有我自己的心血在里面.当初最初自己按照书上的思路做这个游戏(没有看作者的代码),走了不少的弯路.

做完这个游戏,收获颇多,感觉有必要总结一下自己的学习心得和分享一下自己的一点儿经验.前面已经提到自己是个初学者,如果有不当之处和理解错误的地方.在所难免,真诚地希望看到此文的朋友给以指点和帮助,再此先谢过.(仅供互相交流)

整个游戏总共花费了我两个多星期的时间,"坎坷"之路很多.中间兴奋过:仅仅因为自己实现一个功能或者理解了作者设计的思路和方案.有时又充满了迷茫,郁闷甚至打击:特别是那个空指针的错误自己始终找不到改正的方法.

但不管怎么说自己是作出来了,呵呵,小有成就感(尽管90%的代码来源与书上)

闲言少叙.下面开始说我的总结吧

首先大家想必都玩过或者知道俄罗斯方块的游戏,该游戏的过程也就是几个不同形状的方块在游戏界面中实现下降, 左移, 右移,变化, 加速下降的过程(注意游戏里那个田字格大方块不能发生变化)

根据面向对象的思维方式,每个下降的物快(后面成为大方块)都应该抽象成一个对象,需要注意个是:下降的大方块是由四个小方块图片组成的,比如按个田字格的大方块,就是由四个小方块组成。这里把组成大方块的小方块图片也抽象为一个类Square.如下所示

public class Square {
 
    /**
     * 初始化带有图片的方块的位置和该方块的图片,
     * @param image
     * @param beginX
     * @param beginY
     */
 public Square(Image image, int beginX, int beginY) {
  this.image = image;
  this.beginX = beginX;
  this.beginY = beginY;
 }

//方块的图片
   private Image image;
   //方块开始出现的的横坐标
   private int beginX;
   //方块开始出现的纵坐标
   private int beginY;
    
}

大方块对象包括多个小方块,而这些小方块的不同位置使得大方块可产生不同的变化。每一个大方块都有多种变化,下面新建一个Piece表示大方块

public class Piece {
 
 /*
  * 得到装着小方块的容器,进而通过迭代得到容器的各个小方块
  */
    public List<Square> getSquares() {
       return squares;
   }

public void setSquares(List<Square> squares) {
     this.squares = squares;
  }
                                              
   
   //该打方块所包含的小方块
 private List<Square> squares;
 
 //该大方快的变化
 protected List<List<Square>> changes = new ArrayList<List<Square>>();
  
 //当前变化的索引(在changes集合中的索引
 protected int currentIndex;
 
 //每个小块的边长
 public final static int SQUARE_BORDER = 16;
  
}

真正的游戏中有七种大方块,所以就需要创建七种大方块,在这里把Piece类作为父类,当创建某一个大方块时就需要新建类来继承Piece。在构造器里设置各个小方块的位置。例如我们下面创建一个正方形的大方块类

public class Piece0 extends Piece {

public Piece0(Image image) {
  //创建各个小方块,一个集合为一种变化
  List<Square> squares = new ArrayList<Square>();
  squares.add(new Square(image, 0, 0));
  squares.add(new Square(image, 0, image.getHeight(null)));
  squares.add(new Square(image, image.getWidth(null), 0));
  squares.add(new Square(image, image.getWidth(null), image.getHeight(null)));
  //加入到变化中
  super.changes.add(squares);

//把唯一的一个变化添加到Piece对象中
  super.setSquares(squares);
 }
}

由于正方形方块是没有变化的。因此在Piece对象中changes集合中只有一个元素。表示改大方块只有一种变化。在这里需要为Piece类提供一个setSquares(List<Square> square)方法,

public void setSquares(List<Square> squares) {
  this.squares = squares;
 }

类似的接下来创建剩下的六种大方块

public class Piece1 extends Piece {

public Piece1(Image image) {
  // 第一种变化
  List<Square> squares0 = new ArrayList<Square>();
  squares0.add(new Square(image, image.getWidth(null), image
    .getHeight(null)));
  squares0.add(new Square(image, 0, image.getHeight(null) * 2));
  squares0.add(new Square(image, image.getWidth(null), image
    .getHeight(null) * 2));
  squares0.add(new Square(image, image.getWidth(null) * 2, image
    .getHeight(null) * 2));
  // 第二种变化
  List<Square> squares1 = new ArrayList<Square>();
  squares1.add(new Square(image, 0, 0));
  squares1.add(new Square(image, 0, image.getHeight(null)));
  squares1.add(new Square(image, image.getWidth(null), image
    .getHeight(null)));
  squares1.add(new Square(image, 0, image.getHeight(null) * 2));
  // 第三种变化
  List<Square> squares2 = new ArrayList<Square>();
  squares2.add(new Square(image, 0, 0));
  squares2.add(new Square(image, image.getWidth(null), 0));
  squares2.add(new Square(image, image.getWidth(null) * 2, 0));
  squares2.add(new Square(image, image.getWidth(null), image
    .getHeight(null)));
  // 第四种变化
  List<Square> squares3 = new ArrayList<Square>();
  squares3.add(new Square(image, image.getWidth(null), image
    .getHeight(null)));
  squares3.add(new Square(image, image.getWidth(null) * 2, 0));
  squares3.add(new Square(image, image.getWidth(null) * 2, image
    .getHeight(null)));
  squares3.add(new Square(image, image.getWidth(null) * 2, image
    .getHeight(null) * 2));
  super.changes.add(squares0);
  super.changes.add(squares1);
  super.changes.add(squares2);
  super.changes.add(squares3);

}

}

这是Picece1第一种变化这是Piece1的第二种变化这是Piece1第三种变化,这是Piece1的第四种变化

所以类似的可以创建剩下的五种大方块的的的类(图片就不贴了)

public class Piece2 extends Piece {

public Piece2(Image image) {
  //第一种变化
  List<Square> squares0 = new ArrayList<Square>();
  squares0.add(new Square(image, 0, 0));
  squares0.add(new Square(image, image.getWidth(null), 0));
  squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)));
  squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
  //第二种变化
  List<Square> squares1 = new ArrayList<Square>();
  squares1.add(new Square(image, image.getWidth(null)*2, 0));
  squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)));
  squares1.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
  squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
  super.changes.add(squares0);
  super.changes.add(squares1);
   }

}

public class Piece3 extends Piece {

public Piece3(Image image) {
  //第一种变化
  List<Square> squares0 = new ArrayList<Square>();
  squares0.add(new Square(image, image.getWidth(null), 0));
  squares0.add(new Square(image, image.getWidth(null)*2, 0));
  squares0.add(new Square(image, 0, image.getHeight(null)));
  squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)));
//     第二种变化
  List<Square> squares1 = new ArrayList<Square>();
  squares1.add(new Square(image, 0, 0));
  squares1.add(new Square(image, 0, image.getHeight(null)));
  squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)));
  squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
  super.changes.add(squares0);
  super.changes.add(squares1);
 }

}

public class Piece4 extends Piece {
 
 public Piece4(Image image) {
  //第一种变化
  List<Square> squares0 = new ArrayList<Square>();
  squares0.add(new Square(image, 0, image.getHeight(null)));
  squares0.add(new Square(image, 0, image.getHeight(null)*2));
  squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
  squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
  //第二种变化
  List<Square> squares1 = new ArrayList<Square>();
  squares1.add(new Square(image, 0, 0));
  squares1.add(new Square(image, image.getWidth(null), 0));
  squares1.add(new Square(image, 0, image.getHeight(null)));
  squares1.add(new Square(image, 0, image.getHeight(null)*2));
  //第三种变化
  List<Square> squares2 = new ArrayList<Square>();
  squares2.add(new Square(image, 0, 0));
  squares2.add(new Square(image, image.getWidth(null), 0));
  squares2.add(new Square(image, image.getWidth(null)*2, 0));
  squares2.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
  //第四种变化
  List<Square> squares3 = new ArrayList<Square>();
  squares3.add(new Square(image, image.getWidth(null)*2, 0));
  squares3.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
  squares3.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
  squares3.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
  super.changes.add(squares0);
  super.changes.add(squares1);
  super.changes.add(squares2);
  super.changes.add(squares3);
  }

}

public class Piece5 extends Piece {

public Piece5(Image image) {
  //第一种变化
  List<Square> squares0 = new ArrayList<Square>();
  squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
  squares0.add(new Square(image, 0, image.getHeight(null)*2));
  squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
  squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
  //第二种变化
  List<Square> squares1 = new ArrayList<Square>();
  squares1.add(new Square(image, image.getWidth(null), 0));
  squares1.add(new Square(image, image.getWidth(null)*2, 0));
  squares1.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
  squares1.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)*2));
  //第三种变化
  List<Square> squares2 = new ArrayList<Square>();
  squares2.add(new Square(image, 0, 0));
  squares2.add(new Square(image, image.getWidth(null), 0));
  squares2.add(new Square(image, image.getWidth(null)*2, 0));
  squares2.add(new Square(image, 0, image.getHeight(null)));
  //第四种变化
  List<Square> squares3 = new ArrayList<Square>();
  squares3.add(new Square(image, 0, 0));
  squares3.add(new Square(image, 0, image.getHeight(null)));
  squares3.add(new Square(image, 0, image.getHeight(null)*2));
  squares3.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
  super.changes.add(squares0);
  super.changes.add(squares1);
  super.changes.add(squares2);
  super.changes.add(squares3);
  }
}

public class Piece6 extends Piece {

public Piece6(Image image) {
  //第一种变化
  List<Square> squares0 = new ArrayList<Square>();
  squares0.add(new Square(image, 0, image.getHeight(null)));
  squares0.add(new Square(image, image.getWidth(null), image.getHeight(null)));
  squares0.add(new Square(image, image.getWidth(null)*2, image.getHeight(null)));
  squares0.add(new Square(image, image.getWidth(null)*3, image.getHeight(null)));
  //第二种变化
  List<Square> squares1 = new ArrayList<Square>();
  squares1.add(new Square(image, image.getWidth(null), 0));
  squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)));
  squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*2));
  squares1.add(new Square(image, image.getWidth(null), image.getHeight(null)*3));
  super.changes.add(squares0);
  super.changes.add(squares1);
  }

}

制作单机俄罗斯方块游戏总结(一)相关推荐

  1. python五子棋游戏from tkinter import_Python tkinter制作单机五子棋游戏

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python家庭,作者Python家庭 实战项目:使用Python ...

  2. python 单机程序_Python tkinter制作单机五子棋游戏

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python家庭,作者Python家庭 实战项目:使用Python ...

  3. 日本人制作汉字俄罗斯方块游戏,看哭一票中国人

    点击上方"蓝色字体",选择"设为星标" 做积极向上的前端人! 俄罗斯方块对于各位玩家来说一定是童年难忘的记忆,一位日本大学生ARAMA在社交媒体发布了一段演示视 ...

  4. 【牛掰】日本人制作汉字俄罗斯方块游戏,看哭一票中国人

    俄罗斯方块对于各位玩家来说一定是童年难忘的记忆,一位日本大学生ARAMA在社交媒体发布了一段演示视频"汉字版俄罗斯方块",在这个视频中,经典俄罗斯方块中的图形全部都替换成了汉字,游 ...

  5. python五子棋游戏from tkinter import_Python tkinter制作单机五子棋游戏 | 一台服务器网...

    本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理. 以下文章来源于Python家庭,作者Python家庭 实战项目:使用Python ...

  6. 500行代码写一个俄罗斯方块游戏

    导读:本文我们要制作一个俄罗斯方块游戏. 01 俄罗斯方块 Tetris 俄罗斯方块游戏是世界上最流行的游戏之一.是由一名叫Alexey Pajitnov的俄罗斯程序员在1985年制作的,从那时起,这 ...

  7. 500 行代码写一个俄罗斯方块游戏

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 导读:本文我们要制作一个俄罗斯方块游戏. 作者 | 派森 ...

  8. 用python 和pyqt5写俄罗斯方块游戏

    # 俄罗斯方块游戏 效果展示 本章我们要制作一个俄罗斯方块游戏. ## Tetris > 译注:称呼:方块是由四个小方格组成的 俄罗斯方块游戏是世界上最流行的游戏之一.是由一名叫Alexey P ...

  9. 500 行代码写一个俄罗斯方块游戏(附源码)

    点击上方[全栈开发者社区]→右上角[...]→[设为星标⭐] 导读:本文我们要制作一个俄罗斯方块游戏. 作者 | 派森学python 来源 | https://segmentfault.com/a/1 ...

  10. java俄罗斯方块ppt_java俄罗斯方块游戏毕业设计答辩.ppt

    java俄罗斯方块游戏毕业设计答辩 使用JavaGUI开发俄罗斯方块游戏 作者: 项目介绍 1.传统游戏 2.主要使用Java语言进行GUI设计.图形编程.线程程序开发.算法的实现思路 意义和研究目的 ...

最新文章

  1. transient的作用和使用
  2. mybaits八:select查询返回map集合
  3. 数据库02_字段类型
  4. 【Mac】Chrome中添加截图扩展插件FireShot方法
  5. DT-06 For MQTT
  6. c语言趣味程序设计编程100例精解,c趣味编程100例
  7. python 帮助 autocad_python 使用pyautocad操作AutoCAD
  8. 关于e的等式及相关证明
  9. php修改学生信息代码_论导师和学生关于论文写作最大的信息不对称(一):从初稿到定稿到底要修改几遍?...
  10. ENVI中利用平均波谱角的方法进行分类
  11. linux离线安装系统工具arping
  12. intouch负值显示0_intouch的若干个经典问题解答
  13. gta5因为计算机丢失xinput1,xinput1_3.dll_gta5丢失xinput1_3.dll_xinput1_3.dll win10
  14. 西工大计算机学院二级教授,计算机学院高武教授:践行科研育人,培养拔尖创新人才...
  15. CSS3之边框图片border-image
  16. 中华英才网简历筛选标准
  17. C#获取本周,上周,下周信息
  18. 科大讯飞SDK的使用
  19. 企业成长能力分析-主营业务收入增长率、主营利润增长率、净利润增长率、总资产增长率、固定资产增长率...
  20. [pytorch] PyTorch Metric Learning库代码学习二 Inference

热门文章

  1. 中科院计算机考研对外调剂吗,考研调剂信息:2020年中科院接收调剂研究生!...
  2. Linux中tmux多终端复用和Screen多窗口工具
  3. 主干网络系列(4) -ResNeXt: 批量残差网络-作用于深度神经网络的残差聚集变换
  4. Otsu算法原理及实现
  5. 致敬CondConv!Intel提出即插即用的“万金油”动态卷积ODConv
  6. 一位算法工程师从30+场秋招面试中总结出的超强面经——目标检测篇(含答案)...
  7. Ubuntu18.04下C++编译tensorflow并在QT中使用
  8. Android App Architecture使用详解
  9. STP实验(指定特定交换机为根桥)
  10. Spring的标签和验证等模块