In my soccer game, I have a bug where the ball keeps getting caught in the edges of the world. I would like to add a reset command that would basically involve moving the ball to the middle of the pitch by pressing 'R' WITHOUT resetting the score counters. How can I do this? Below is my current code for the ball.
Thanks in advance
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.*; /** * Write a description of class ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Actor { private int direction; private int deltaX; // change in x direction private int deltaY; // change in y direction private LeftCounter leftCounterPoints; private RightCounter rightCounterPoints; private int leftCounterPointsAdd; private int rightCounterPointsAdd; public Ball(LeftCounter leftcounter,RightCounter rightcounter) { leftCounterPoints = leftcounter; rightCounterPoints = rightcounter; leftCounterPointsAdd = 0 ; rightCounterPointsAdd = 0 ; do { direction = Greenfoot.getRandomNumber( 6 ); } while (direction == 3 ); deltaX = direction - 3 ; deltaY = direction - 3 ; } /** * Act - do whatever the ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { detectPaddle(); ballMove(); if (leftCounterPointsAdd == 10 ) { leftGameWon(); } if (rightCounterPointsAdd == 10 ) { rightGameWon(); } } /** * move the ball */ public void ballMove() { int xCoordinate = getX() + deltaX; int yCoordinate = getY() + deltaY; setLocation(xCoordinate, yCoordinate); int direction; if (atTopOrBottom()) { deltaY = deltaY * (- 1 ); } if (atSides() == true ) { setLocation( 300 , 215 ); do { direction = Greenfoot.getRandomNumber( 20 ); } while (direction == 3 ); deltaX = direction - 3 ; deltaY = direction - 3 ; } } /** * test if the ball is at the top or the bottom */ public boolean atTopOrBottom() { if (getY() < 63 || getY() > getWorld().getHeight() - 33 ) { return true ; } else { return false ; } } public void detectPaddle() { Actor rightPaddle = getOneIntersectingObject(Player2. class ); Actor leftPaddle = getOneIntersectingObject(Player1. class ); if (rightPaddle!= null ) { deltaX = -deltaX; int offset = getY() - rightPaddle.getY(); deltaY = deltaY + offset/ 10 ; Greenfoot.playSound( "oof.wav" ); } if (leftPaddle!= null ) { deltaX = -deltaX; int offset = getY() - leftPaddle.getY(); deltaY = deltaY + offset/ 10 ; } } public boolean atSides() { if (getX() < 50 ) { rightCounterPoints.add( 1 ); rightCounterPointsAdd = rightCounterPointsAdd + 1 ; return true ; } if (getX() > getWorld().getWidth() - 50 ) { leftCounterPoints.add( 1 ); leftCounterPointsAdd = leftCounterPointsAdd + 1 ; return true ; } else return false ; } public void rightGameWon() { World world = getWorld(); world.removeObjects(world.getObjects( null )); Greenfoot.setWorld( new GameOverScreen2()); } public void leftGameWon() { World world = getWorld(); world.removeObjects(world.getObjects( null )); Greenfoot.setWorld( new GameOverScreen1()); } } |