import objectdraw.*; // program for two players to play basketball. public class TeamBasketballGame extends FrameWindowController { // Location of the display private static final int DISPLAY_X = 150; private static final int DISPLAY_Y = 200; private static final int DISPLAYSIZE = 16; // in points // Location and dimensions of the hoop private static final int HOOPTOP = 50; private static final int HOOPLEFT = 160; private static final int HOOPWIDTH = 80; private static final int HOOPHEIGHT = 35; // Initial locations and dimensions of the balls private static final int BALLX = 110; private static final int BALLY = 250; private static final int BALLSIZE = 40; private static final int BALLSPACING = 160; // Distance between balls and score displays private static final int SCOREDOWNSET = BALLSIZE + 25; // the Text objects which display the scores private Text homeDisplay, visitorDisplay; // the oval that represent the hoop private FramedOval hoop; // the two balls private Basketball homeBall, visitorBall; // the scores for each player private int homeScore = 0, visitorScore = 0; // the last previous known location of the mouse private Location lastMouse; // the ball currently being dragged private Basketball ballInPlay; // initialize the counter and the text message public void begin() { hoop = new FramedOval(HOOPLEFT, HOOPTOP, HOOPWIDTH, HOOPHEIGHT, canvas); homeBall = new Basketball(BALLX, BALLY, BALLSIZE, canvas); visitorBall = new Basketball(BALLX + BALLSPACING, BALLY, BALLSIZE, canvas); homeDisplay = new Text("HOME 0", BALLX + BALLSIZE/2, BALLY + SCOREDOWNSET, canvas); visitorDisplay = new Text("VISITORS 0", BALLX + + BALLSPACING + BALLSIZE/2, BALLY + SCOREDOWNSET, canvas); homeDisplay.setFontSize(DISPLAYSIZE); homeDisplay.move(-homeDisplay.getWidth()/2,0); visitorDisplay.setFontSize(DISPLAYSIZE); visitorDisplay.move(-visitorDisplay.getWidth()/2,0); } // Note where mouse is depressed public void onMousePress(Location point) { lastMouse = point; if (homeBall.contains(point)) { ballInPlay = homeBall; } else if (visitorBall.contains(point)) { ballInPlay = visitorBall; } } // Move the ball as the mouse is dragged public void onMouseDrag(Location point) { if (ballInPlay != null) { ballInPlay.move(point.getX() - lastMouse.getX(), point.getY() - lastMouse.getY()); lastMouse = point; } } // increment the counter and update the text public void onMouseRelease(Location point) { if (hoop.contains(point)) { if (ballInPlay == homeBall) { homeScore = homeScore + 2; homeDisplay.setText("HOME " + homeScore); } else if (ballInPlay == visitorBall) { visitorScore = visitorScore + 2; visitorDisplay.setText("VISITORS " + visitorScore); } } if (ballInPlay != null) { ballInPlay.returnToStart(); ballInPlay = null; } } }