📄 superpongball.java
字号:
import objectdraw.*;// Implements animated balls for variants of Pong with// multiple obstacles in the playing areapublic class SuperPongBall extends ActiveObject { // Ball's initial speed private final static double INITY = 100; private static final double XSPEED_RATIO = 1.5; // Size of ball private static final double BALLSIZE = 10; // delay between moves private static final double DELAY= 30; // the ball itself private FilledOval theBall; // the bricks ball might bounce off private RectCollection bricks; // the boundary within which the ball should stay private FramedRect boundary; // the paddle trying to hit the ball private FilledRect paddle; // current ball speed private double xSpeed; private double ySpeed = -INITY; // y coordinate of bottom of screen private double offScreen; // create a new ball. remember all the things it might hit public SuperPongBall( RectCollection theBricks, FramedRect theBoundary, FilledRect thePaddle, DrawingCanvas theCanvas) { // set the speed RandomDoubleGenerator xGen = new RandomDoubleGenerator( -XSPEED_RATIO*INITY, XSPEED_RATIO*INITY); xSpeed = xGen.nextValue(); // draw the ball theBall = new FilledOval( thePaddle.getX()+thePaddle.getWidth()/2, thePaddle.getY() - 2*BALLSIZE, BALLSIZE, BALLSIZE, theCanvas); // remember the obstacles bricks = theBricks; boundary = theBoundary; paddle = thePaddle; offScreen = theCanvas.getHeight(); start(); } // bounce the ball around the playing area public void run(){ double start = System.currentTimeMillis(); while ( theBall.getY() < offScreen ) { pause(DELAY); double elapsedTime = System.currentTimeMillis() - start; start = start + elapsedTime; theBall.move( elapsedTime*xSpeed/1000, elapsedTime*ySpeed/1000); // see if you need to bounce off one of the boundary's sides if ( theBall.getX() < boundary.getX() ) { xSpeed = -xSpeed; theBall.move( 2* (boundary.getX()- theBall.getX()),0 ); } else if ( theBall.getX() > boundary.getX() +boundary.getWidth() - BALLSIZE) { xSpeed = -xSpeed; theBall.move( 2*(boundary.getX() + boundary.getWidth() - BALLSIZE - theBall.getX()),0); } // see if you hit the top of the boundary if ( theBall.getY() < boundary.getY() ) { ySpeed = -ySpeed; theBall.move(0, 2*(boundary.getY()-theBall.getY())); } // see if you hit a brick FilledRect hitBrick = bricks.findFirstOverlap(theBall); if ( hitBrick != null){ bounceOffRect( hitBrick); } // check the paddle if ( theBall.overlaps(paddle) ) { bounceOffRect(paddle); } } } // the following implements a crude approximation of the phsyical process // of a ball bouncing off a brick (or the paddle). Anomalies will occur! private void bounceOffRect(FilledRect box){ double centerX = theBall.getX()+BALLSIZE/2; double centerY = theBall.getY()+BALLSIZE/2; if ( centerX > box.getX() && centerX < box.getX()+box.getWidth()){ if (ySpeed > 0) { theBall.move( 0, 2*(box.getY() - (theBall.getY() + theBall.getHeight()))); } else { theBall.move( 0, 2*(box.getY()+box.getHeight() - theBall.getY() )); } ySpeed = -ySpeed; } else if (centerY > box.getY() && centerY < box.getY()+box.getHeight()) { if (xSpeed > 0) { theBall.move( 2*(box.getX() - (theBall.getX() + theBall.getWidth())), 0); } else { theBall.move( 2*(box.getX()+box.getWidth() - theBall.getX() ), 0); } xSpeed = -xSpeed; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -