pong.java

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

JAVA
68
字号
import objectdraw.*;import java.awt.*;// plays a classic version of single player pongpublic class Pong extends WindowController{   private static final int   // Dimensions of the court   courtLeft = 50,   courtTop = 50,   courtHeight = 300,   courtWidth = 250,   // Dimensions of the paddle   paddleWidth = 50,   paddleHeight = 20;   private FilledRect paddle;    // the paddle   private FramedRect boundary; // the boundary of the playing area.   public void begin()   {      // make the playing area      boundary = new FramedRect(courtLeft,courtTop,                                courtWidth, courtHeight,                                canvas);      // make the paddle      paddle =            new FilledRect(courtLeft + (courtWidth-paddleWidth)/2,            courtTop + courtHeight - paddleHeight -1,            paddleWidth, paddleHeight,            canvas);   }   public void onMouseClick(Location point)   {      // make a new ball when the player clicks      new MovingBall(canvas, paddle, boundary);   }   public void onMouseMove(Location point)   {      if ( point.getX() < courtLeft )      {         // place paddle at left edge of the court         paddle.moveTo( courtLeft,                        courtTop + courtHeight - paddleHeight -1);      }      else if ( point.getX() > courtLeft + courtWidth - paddleWidth)      {         // place paddle at right edge of the court         paddle.moveTo( courtLeft + courtWidth - paddleWidth,                        courtTop + courtHeight - paddleHeight -1);      }      else      {         // keep the edge of the paddle lined up with the mouse         paddle.moveTo( point.getX(),                        courtTop + courtHeight - paddleHeight -1);      }   }}

⌨️ 快捷键说明

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