学习着传智播客的一个俄罗斯方块视频,练习的笔记如下:
首先第一步,理清楚,自己在这个游戏中要涉及到的几个类。类之间的关系。类所涉及到的动作
方块类Shape
void moveLeft()//左移
void moveRight()//右移
void moveDown()//向下
void rotate()//旋转
void drawMe()//显示
这个方块类需要能自己定时下落。即有个定时的线程
方块工厂类ShapeFactory ,用于生产不一样的方块
Shape getShape();//生产出一个方块
障碍物类Ground
void accept()//把一个方块变成障碍物
void drawMe()//显示障碍物
游戏面板类GamePanel
然后类之间的关系,如下图所示:
再然后,就可以创建一个工程,搭起整个框架。跟着噼哩啪啦的敲一敲代码。从代码中去理解,去体会
创建一个工程TetrisGame
在包hello.code.entities下创建
Ground.java,Shape.java,ShapeFactory.java
在包hello.code.view下创建
GamePanel.java
在包hello.code.controller下创建
Controller.java
再再然后,就像写作文要有个草稿一样。写程序也可以打个草稿先。如下:
Shape.java的草稿
package hello.code.entities;
public class Shape {
public void moveLeft(){
System.out.println("图形向左移");
}
public void moveRight(){
System.out.println("图形向右移");
}
public void moveDown(){
System.out.println("图形向下移");
}
public void rotate(){
System.out.println("图形旋转");
}
public void drawMe(){
System.out.println("图形画出自己");
}
private class ShapeDriver implements Runnable{
@Override
public void run() {
// 定时向下落
while(true){
moveDown();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
ShapeFactory.java草稿
public class ShapeFactory {
public Shape getShape(){
System.out.println("产生一个方块");
return new Shape();
}
}
Ground.java草稿
public class Ground {
public void accept(Shape shape){
System.out.println("把方块变成障碍物");
}
public void drawMe(){
System.out.println("画出障碍物");
}
}
GamePanel.java草稿
public class GamePanel extends JPanel{
private Ground ground;
private Shape shape;
public void display(Ground ground,Shape shape){
System.out.println("GamePanel 显示");
this.ground=ground;
this.shape=shape;
this.repaint();
}
@Override
protected void paintComponent(Graphics g) {
//重新显示
super.paintComponent(g);
if(shape!=null && ground!=null){
shape.drawMe();
ground.drawMe();
}
}
}
Controller.java草稿:
public class Controller extends KeyAdapter {
private Shape shape;
private ShapeFactory shapeFactory;
private Ground ground;
private GamePanel gamePanel;
@Override
public void keyPressed(KeyEvent e){
// TODO Auto-generated method stub
super.keyPressed(e);
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
shape.moveLeft();
break;
case KeyEvent.VK_UP:
shape.rotate();
break;
case KeyEvent.VK_RIGHT:
shape.moveRight();
break;
default:
break;
}
gamePanel.display(ground, shape);
}
}
在包hello.code.listener下
创建ShapeListener.java
public interface ShapeListener {
void shapeMoveDown(Shape shape);
}
在Shape.java下添加监听器
public class Shape {
private ShapeListener listener;// **
public void moveLeft(){
System.out.println("图形向左移");
}
public void moveRight(){
System.out.println("图形向右移");
}
public void moveDown(){
System.out.println("图形向下移");
}
public void rotate(){
System.out.println("图形旋转");
}
public void drawMe(){
System.out.println("图形画出自己");
}
private class ShapeDriver implements Runnable{
@Override
public void run() {
// 定时向下落
while(true){
moveDown();
listener.shapeMoveDown(Shape.this);//**
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public Shape(){//**
new Thread(new ShapeDriver()).start();
}
public void addShapeListener(ShapeListener l){//**
if(null!=l){
this.listener=l;
}
}
}
声明://**,表示这行以前存在过,现在被修改了
Controller类实现接口ShapeListener
public class Controller extends KeyAdapter implements ShapeListener{//**
private Shape shape;
private ShapeFactory shapeFactory;
private Ground ground;
private GamePanel gamePanel;
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
super.keyPressed(e);
switch (e.getKeyCode()) {
case KeyEvent.VK_LEFT:
shape.moveLeft();
break;
case KeyEvent.VK_UP:
shape.rotate();
break;
case KeyEvent.VK_RIGHT:
shape.moveRight();
break;
default:
break;
}
gamePanel.display(ground, shape);
}
@Override
public void shapeMoveDown(Shape shape) {//**
// TODO Auto-generated method stub
gamePanel.display(ground, shape);
}
}
为方块工厂产生的方块添加监听
public class ShapeFactory {
public Shape getShape(ShapeListener listener){//**
System.out.println("产生一个方块");
Shape shape=new Shape();
shape.addShapeListener(listener);//**
return shape;
}
}
Controller.java添加产生新游戏 的方法
public void newGame(){
shape=shapeFactory.getShape(this);
}
public Controller(ShapeFactory shapeFactory,
Ground ground,GamePanel gamePanel){
this.shapeFactory=shapeFactory;
this.ground=ground;
this.gamePanel=gamePanel;
}
设置GamePanel大小
public GamePanel(){
this.setSize(300,300);
}
测试类
在包hello.code.test下创建
GamePlay.java
public class GamePlay {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ShapeFactory shapeFactory=new ShapeFactory();
Ground ground=new Ground();
GamePanel gamePanel=new GamePanel();
Controller controller=new Controller(shapeFactory,
ground,gamePanel);
JFrame frame=new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(gamePanel.getSize().width+10,
gamePanel.getSize().height+30);
frame.add(gamePanel);
gamePanel.addKeyListener(controller);
frame.setVisible(true);
controller.newGame();
}
}
测试结果就是个空面板。然后程序一调试,游戏就开始,隐形的方块开始下落,上下左右开始方向键,然后在控制台就会输出各种相关操作的信息。
以上就是这么一个草稿,不成体统。算是简单做个笔记 了。
就当是宣传这个很好的视频了。没有废话,如画龙点睛一气呵成,高山流水,绕粱三日。
感兴趣的,可网上搜一搜咯
最后,提醒自己,未完待续,不能偷赖啊。。。