【原创】纯OO:从设计到编码写一个FlappyBird (三)

第二部分请点这里

下面首先来实现Bing接口!

实现Bing接口的类取名SimpleBing。

容易发现,SimpleBing类总的来说要向下,但点击一下又得向上,向上到了一定界限又得向下,但我们又只有一个action方法供Game调用。。怎么办??

又向上,又向下的,还互相转换,有没有让你想到什么?

Bingo!状态!这里将使用状态模式来实现SimpleBing类的行为。

下面是类图:

【原创】纯OO:从设计到编码写一个FlappyBird (三)

接口State:

interface State {
public void move();
public void changeState();
public void setInit(int speed);
}

move方法好说,你是往上,就往上走;你是往下,就往下走。changeState就是切换状态,下图是状态图:

【原创】纯OO:从设计到编码写一个FlappyBird (三)

首先必须明确,当向上执行到一定程度时,需要转换为向下。我采用的方案是,设定一个距离阀值upSpeed,当向上移动了upSpeed的位移之后自动转化为向下状态。

当在Game类中执行fang.beginUp()的时候,状态转移;如果当前状态就是向上,那么重新计算upSpeed(就好像在原位置重新向上一次。);如果当前状态是向下,那么转化为向上。

SimpleBing类需要的实例变量:

0、private static final int WIDTH = 30; // 小鸟的宽
1、private static final int HEIGHT = 30;// 小鸟的高
2、private static final int UP_SPEED = 30;// 往上最多走的位移
3、private int x; // 标记位置的x
4、private int y; // 标记位置的y
5、State nowState; // 当前状态
6、DownState down; // 向下状态
7、UpState up; // 向上状态

action方法和beginUp方法的实现,就比较简单了。

    public void action()
{
nowState.move();
if (this.getY() < 0)
this.setY(0);
} public void beginUp()
{
up.setInit(UP_SPEED);
nowState = up;
}

最后是该类的全部代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.*; public interface Bing {
public void beginUp();
public void action();
public int getX();
public int getY();
public void setX(int x);
public void setY(int y);
public int getWidth();
public int getHeight();
} interface State {
public void move();
public void changeState();
public void setInit(int speed);
} class DownState implements State {
SimpleBing bing;
private static final int DOWN_SPEED = 3;
private int downSpeed = 3; DownState(SimpleBing bing)
{
this.bing = bing;
} public void move()
{
bing.setY(bing.getY() + downSpeed);
//downSpeed ++;
//if (downSpeed > DOWN_SPEED)
//downSpeed = DOWN_SPEED;
} public void changeState()
{
bing.setState(bing.getUpState());
} public void setInit(int speed)
{
this.downSpeed = speed;
}
} class UpState implements State {
SimpleBing bing;
private int upSpeed = 30;
private static final int SPEED = 3;
private static final int DES = 1; UpState(SimpleBing bing)
{
this.bing = bing;
} public void move()
{
bing.setY(bing.getY() - SPEED);
upSpeed -= DES;
if (upSpeed <= 0)
this.changeState();
} public void changeState()
{
bing.setState(bing.getDownState());
//bing.getDownState().setInit(0);
} public void setInit(int speed)
{
this.upSpeed = speed;
}
} class SimpleBing extends JPanel implements Bing { private static final int WIDTH = 30;
private static final int HEIGHT = 30;
private static final int UP_SPEED = 30;
private int x;
private int y;
State nowState;
DownState down;
UpState up;
SimpleBing(int x,int y)
{
this.x = x;
this.y = y;
down = new DownState(this);
up = new UpState(this);
nowState = down;
//nowState.setInit(0);
} public void setState(State state)
{
nowState = state;
} public void action()
{
nowState.move();
if (this.getY() < 0)
this.setY(0);
} public void beginUp()
{
up.setInit(UP_SPEED);
nowState = up;
} public State getDownState()
{
return down;
} public State getUpState()
{
return up;
} public int getX()
{
return x;
} public int getY()
{
return y;
} public void setX(int x)
{
this.x = x;
} public void setY(int y)
{
this.y = y;
} public int getWidth()
{
return WIDTH;
} public int getHeight()
{
return HEIGHT;
}
}
上一篇:ios开发--KVO浅析


下一篇:PNPOLY - Point Inclusion in Polygon W. Randolph Franklin