stillpatheticpong.java

来自「书籍"Java_面向事件编程"的附带光盘代码」· Java 代码 · 共 67 行

JAVA
67
字号
import objectdraw.*;// plays a slightly more advanced version of pong where the ball// can bounce off of the paddlepublic class StillPatheticPong extends WindowController{        // Margin between court and window boundaries    private static final int        COURT_MARGIN = 50;        // dimensions of the paddle    private static final int        PADDLE_WIDTH = 50,        PADDLE_HEIGHT = 20;    FilledRect paddle;    FramedRect boundary;        // the boundary of the playing area.        public void begin()    {        // make the playing area        boundary = new FramedRect(COURT_MARGIN, COURT_MARGIN,                                  canvas.getWidth() - 2*COURT_MARGIN,                                  canvas.getHeight() - 2*COURT_MARGIN,                                  canvas);                // make the paddle        paddle =            new FilledRect((canvas.getWidth()-PADDLE_WIDTH)/2,                           boundary.getY() + boundary.getHeight() - PADDLE_HEIGHT -1,                           PADDLE_WIDTH, PADDLE_HEIGHT,                           canvas);    }        public void onMouseClick(Location point)    {        // make a new ball when the player clicks        new BouncingBall(boundary.getX() + boundary.getWidth()/2,                         boundary.getY(),                          paddle,                         canvas).start();    }        public void onMouseMove(Location point)    {        if ( point.getX() < boundary.getX() )            {                // place paddle at left edge of the court                paddle.moveTo( boundary.getX(),                               paddle.getY() );            }        else if ( point.getX() > boundary.getX() + boundary.getWidth() - PADDLE_WIDTH)            {                // place paddle at right edge of the court                paddle.moveTo( boundary.getX() + boundary.getWidth() - PADDLE_WIDTH,                               paddle.getY());            }        else            {                // keep the edge of the paddle lined up with the mouse                paddle.moveTo( point.getX(),                               paddle.getY());            }    }    }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?