⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 superpong.java

📁 书籍"Java_面向事件编程"的附带光盘代码
💻 JAVA
字号:
import objectdraw.*;// Play a game like Pong but with extra obstacles that// the ball can bounce off of added to the screen as// play progressespublic class SuperPong extends WindowController {        // Inset of playing area from edges of canvas    private static final double MARGIN = 50;        // dimensions of the grid in which bricks are placed    private static final int COLS= 7;    private static final int ROWS= 30;        // increments used to choose position of next brick    private static final double COLINC = 4;    private static final double ROWINC = -3;        // ratio between size of paddle and bricks    private static final double PADDLERATIO = 1.5;        // position where next brick will be placed    private double curRow = 20;    private double curCol = 1;        // dimensions of a brick    private double bWidth;    private double bHeight;        // dimensions of the paddle    private double paddleWidth;    private double paddleHeight;        // the boundary of the playing area     private FramedRect boundary;        // the paddle    private FilledRect paddle;        // the collection of bricks placed in the playing area    private RectCollection theBricks = new RectCollection(ROWS*COLS);        // has at least one ball been launched    private boolean started = false;        // create the boundary and the paddle    public void begin() {        boundary = new FramedRect( MARGIN, MARGIN,                                   canvas.getWidth() - 2*MARGIN,                                   canvas.getHeight() - 2*MARGIN,                                   canvas );                // dimensions of bricks        bWidth = boundary.getWidth()/COLS;        bHeight = boundary.getHeight()/ROWS;                // dimensions for the paddle        paddleWidth = PADDLERATIO*bWidth;        paddleHeight = 1.5*bHeight;                paddle =             new FilledRect( canvas.getWidth()/2 - paddleWidth/2,                            canvas.getHeight() - (MARGIN + paddleHeight + 2),                            paddleWidth, paddleHeight, canvas);    }        // Make a new ball whenever the user clicks    public void onMouseClick(Location point){        new SuperPongBall( theBricks, boundary, paddle, canvas);        started = true;    }        // make the paddle follow the mouse back and forth    public void onMouseMove(Location point){                if ( point.getX() < boundary.getX()){            paddle.moveTo( boundary.getX(), paddle.getY());        } else if ( point.getX() + paddleWidth > boundary.getX() + boundary.getWidth() ) {            paddle.moveTo( boundary.getX() + boundary.getWidth() - paddleWidth,                           paddle.getY());        } else {            paddle.moveTo( point.getX(), paddle.getY());        }    }        // make the game a bit harder    public void onMouseEnter(Location point){        makeItHarder();    }        // make the game a bit harder    public void onMouseExit(Location point){        makeItHarder();    }        // add new bricks until there is no more room    private void makeItHarder() {        if (started) {            if (curRow > 0 && curRow < ROWS) {                // add a new brick                theBricks.add(                              new FilledRect(                                             boundary.getX() + 2 + curCol * bWidth,                                             boundary.getY() + bHeight * curRow,                                             bWidth - 2,                                             bHeight - 2,                                             canvas));                curCol += COLINC;                if (curCol >= COLS) {                    curCol = curCol - COLS;                    curRow += ROWINC;                }            }        }    }    }

⌨️ 快捷键说明

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