第二部分请点这里

下面首先来实现Bing接口!

实现Bing接口的类取名SimpleBing。

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

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

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

下面是类图:

接口State:

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

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

首先必须明确,当向上执行到一定程度时,需要转换为向下。我采用的方案是,设定一个距离阀值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;
}
}

【原创】纯OO:从设计到编码写一个FlappyBird (三)的更多相关文章

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

    说起来,自学计算机也有2年多的时间了,自己还没有从设计到编码,完完整整的设计一个基于面向对象的软件的经历..囧 于是,就有了这个系列.首先选用的语言是Java,没别的原因,HeadFirst设计模式是 ...

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

    第五部分请看这里 终于到了最后一个部分了! 这里使用SimpleJudge类来实现Judge接口. 首先是SimpleJudge需要的实例变量: 0.final LinkedList<Pilla ...

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

    第四部分请点这里 本文将实现DrawBoard. 如前文所述,Obstacle类和Bing类仅仅提供给DrawBoard必要的信息,如何绘制则完全委托给了DrawBoard,也就是说游戏关键类的细节和 ...

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

    第三部分请点这里 这里来实现Obstacle类.其实flappybird的本质就是小鸟原地掉,然后几根柱子在走.这也是在Game类里,用obs.move()来实现游戏逻辑的原因. 我们首先必须确定几个 ...

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

    第一部分请点这里. 续结前文,本文将实现Game类. 首先是实例变量.由上次的类图可以看出,Game类首先得具有如下实例变量: 0.Judge judge;1.SimpleDraw draw; // ...

  6. Qt 利用XML文档,写一个程序集合 三

    接上一篇https://www.cnblogs.com/DreamDog/p/9214052.html 滚动区域实现, 滚动区域可以三成分层 第一层,显示内容 中间层,滚动层 第三层,爸爸层 把我们要 ...

  7. 后端开发实践系列之二——领域驱动设计(DDD)编码实践

    Martin Fowler在<企业应用架构模式>一书中写道: I found this(business logic) a curious term because there are f ...

  8. 领域驱动设计(DDD)编码实践

    写在前面 Martin Fowler在<企业应用架构模式>一书中写道: I found this(business logic) a curious term because there ...

  9. 【设计模式系列】之OO面向对象设计七大原则

    1  概述 本章叙述面向向对象设计的七大原则,七大原则分为:单一职责原则.开闭原则.里氏替换原则.依赖倒置原则.接口隔离原则.合成/聚合复用原则.迪米特法则. 2  七大OO面向对象设计 2.1 单一 ...

随机推荐

  1. Codechef Not a Triangle题解

    找出一个数组中的三个数,三个数不能组成三角形. 三个数不能组成三角形的条件是:a + b < c 两边和小于第三边. 这个问题属于三个数的组合问题了.暴力法可解,可是时间效率就是O(n*n*n) ...

  2. C#反射Assembly 具体说明

    1.对C#反射机制的理解 2.概念理解后,必须找到方法去完毕,给出管理的主要语法 3.终于给出有用的样例,反射出来dll中的方法 反射是一个程序集发现及执行的过程,通过反射能够得到*.exe或*.dl ...

  3. win7+vs2008+opencv

    1.下载安装VS2008,然后直接下载opencv的windows的安装版, 2.把opencv解压出来,我的路径为:D:\Program\opencv 3.配置PATH:电脑--属性--高级系统设置 ...

  4. 三框架:使用数据源dbcp注意

    使用spring整合hibernate时间,需要使用该数据源,数据源使用apache的dbcp,使用dbcp当需要依靠pool的jar包.选择dbcp和pool当你需要注意. DBCP 2 compi ...

  5. Android actionbar 搜索框

    就是实如今顶部这种搜索框. 一.这个搜索框是actionbar上的menu上的一个item.叫SearchView.我们能够先在menu选项里定义好: bmap_menu.xml: <?xml ...

  6. Android-1-电话拨号程序

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMjY1MTM4OQ==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  7. 基于WEB 的认证防火墙的设计

    项目要求: 1.  采用Linux iptbles作为接入防火墙,默认放行所以访问入口的80端口 2.  访问者通过http://x.x.x.x 访问防火墙的认证系统,进行账号的登陆操作,同时系统对用 ...

  8. zoj3471(状压dp)

    题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题意:不超过10种气体,两两之间相互碰撞可以产生一定的能量,如 ...

  9. Devstack: A copy of worked local.conf I&#39;m sharing with you.

    service_plugins = neutron.services.firewall.fwaas_plugin.FirewallPlugin [service_providers] service_ ...

  10. Linq 数据操作,两个数组求差、交集、并集

    int[] a = { 1, 2, 3, 4, 5, 6, 7 }; int[] b = { 4, 5, 6, 7, 8, 9, 10 }; int[] c = { 1, 2, 3, 3, 4, 1, ...