Part Three, please click here.
Here to implement the obstacle class. In fact, the essence of Flappybird is the bird in situ drop, and then a few pillars are walking. This is also the reason why the game logic is implemented with Obs.move () in the Games class.
We must first determine a few data.
0, the gap between the pillars
1. Minimum and maximum value of pillars
2, the spacing between the pillars
3, the width of the column
With the height of the screen determined, as long as we determine the height of the upper part of the column, according to the Gap, you can draw two pillars. (Tip: do a subtraction)
Therefore, an obstacle requires two data to indicate:
0, x value
1. Height
The implementation of move is the decrement of X.
Also need to implement an important function is to add and delete pillars. we have to decide when to delete an existing pillar and when to add a new one.
But before that, we had to decide what data structure to use to store the pillars.
It is easy to find that the dynamic additions and deletions of pillars have the properties of FIFO, then the LinkedList of the queue interface is realized naturally.
So when do we add a pillar?
Answer: When the last pillar, and the right distance difference between a pillar gap between.
When to delete a pillar:
A: When the X-value of the first column is less than 0 o'clock width.
Note: Judge class needs and obstacle public linkedlist, otherwise cannot calculate cent. This coupling will have to be resolved in the next refactoring.
All the code for the Obstacle class:
Importjavax.swing.*;Importjava.util.LinkedList;Importjava.awt.*;Importjava.awt.event.*; Public InterfaceObstacle { Public voidmove (); PublicLinkedlist<pillar>getobstacles ();}classSimpleobstacleImplementsObstacle {Private Static Final intSpeed = 2; PublicLinkedlist<pillar> Pillar =NewLinkedlist<pillar> (); intborder; Simpleobstacle (intborder) { This. border =border; Init (); } Private Static intgetrandomheight () {intres = (int) (Math.random () * (PILLAR.GETHEILMT ()-100) + 100); returnRes; } Private voidinit () {Pillar.add (NewPillar ( This. Border,getrandomheight ())); } PublicLinkedlist<pillar>Getobstacles () {returnPillar; } Public voidMove () {Booleandel =false; for(Pillar p:pillar) {p.setx (P.getx ()-Speed ); if(P.getx () + PILLAR.GETWIDLMT () <= 0) del=true; } if(Del) pillar.remove (); Pillar tmp=Pillar.getlast (); if(Tmp.getx () + PILLAR.GETWIDLMT () + pillar.getwidgap () <=border) Pillar.add (NewPillar ( This. Border,getrandomheight ())); }}classPillar {intheight; intx; Private Static Final intWIDGAP = 200; Private Static Final intHeigap = 150; Private Static Final intWIDLMT = 100; Private Static Final intHEILMT = 300; Pillar (intXintheight) { This. x =x; This. Height =height; } Public voidSetX (intx) { This. x =x; } Public intGetX () {returnx; } Public intgetheight () {returnheight; } Public Static intGetwidgap () {returnWidgap; } Public Static intGetheigap () {returnHeigap; } Public Static intGETWIDLMT () {returnWIDLMT; } Public Static intGETHEILMT () {returnHEILMT; }}
"Original" Pure oo: Write a flappybird from design to coding (iv)