"Original" Pure oo: Write a flappybird from design to coding (ii)

Source: Internet
Author: User

The first part please click here.

This article will implement the game class before continuing.

The first is the instance variable. As can be seen from the last class diagram, the game class must first have the following instance variables:

0, Judge Judge;
1, Simpledraw Draw; Why not an interface type? Because Addmouselistener is required, the Drawboard interface does not have this method: Expect improvements for the next release
2, Bing Fang;
3, Obstacle obs;

We need to control the speed of the frame transformation in the game, so we need to add a variable to indicate the speed: (What is the speed of the frame transformation?). See this article)

0, private static final int time = 10;

Finally, we need a variable to mark the score:

0, private int score;

Because the Drawboard implementation class needs to notify the game class when a mouse event occurs, the game class should also implement the MouseListener interface.

We also need a Setup method to initialize all the variables. At the same time a start method is required to start the game, and finally an end method is required to print the score after the game.

Next, we talk about the Start method and the implementation of the Mousepressed method.

Start method:

 public  void   start () { int  flag = 1;  while  (Flag > 0) {Fang.act            Ion ();            Obs.move ();            Flag  = Judge.isgameover (fang,obs);  if  (flag = = 2) score  ++
   
    ;            Draw.draw ();  
    try  
     {Thread.Sleep (TI            ME);     
    catch  
     (Exception e) {}} }
   

Very simple, typical game logic. The classes implemented by the judge interface provide a way to determine whether the game is over, when it returns 1 o'clock, not dead, no score, when it returns to 2 o'clock, and when it returns to 0 o'clock, the square sir. The

Fang.action (); Let the bird begin to fall down (by pressing it up). Obs.move (); Let the barrier move itself, Draw.draw (); The updated image is redrawn. That try is actually a pause to run the program.

Then the Mousepressed method:

     Public void mousepressed (MouseEvent e)     {        fang.beginup ();    }

Simpler, better understood. Just a minute, sir.

All the code for the game class:

Importjavax.swing.*;Importjava.awt.*;Importjava.awt.event.*; Public classGameImplementsMouseListener {Judge Judge;    Simpledraw Draw;    Bing Fang;    Obstacle Obs; Private Static Final intTime = 10; Private intscore;  PublicGame () {score= 0; }         Public Static voidmain (String [] args) {Game game=NewGame ();        Game.setup ();        Game.start ();    Game.end (); }         Public voidSetup () {JFrame frame=NewJFrame (); Fang=NewSimplebing (200,200); Obs=NewSimpleobstacle (600); Judge=NewSimplejudge (fang,obs); Draw=NewSimpledraw (fang,obs);                Frame.getcontentpane (). Add (Draw);        Frame.setdefaultcloseoperation (Jframe.exit_on_close); Draw.addmouselistener ( This); Frame.setsize (600,600); Frame.setvisible (true); }         Public voidstart () {intFlag = 1;  while(Flag > 0) {fang.action ();            Obs.move (); Flag=Judge.isgameover (fang,obs); if(Flag = = 2) score++;            Draw.draw (); Try{thread.sleep (time); } Catch(Exception e) {}}}  Public voidEnd () {System.out.println ("Your score is" +score); }         Public voidmouseclicked (MouseEvent e) {} Public voidmouseentered (MouseEvent e) {} Public voidmouseexited (MouseEvent e) {} Public voidmousepressed (MouseEvent e) {fang.beginup (); }     Public voidmousereleased (MouseEvent e) {}}

"Original" Pure oo: Write a flappybird from design to coding (ii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.