need help making it so when the ball hits the boundaries it adds a point to the other player and resets the whole world.
Here is my game with the source code: https://www.greenfoot.org/scenarios/25316


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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. * */ public MyWorld() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); // theCounter = new Score(); // addObject(theCounter, 175,48); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { Paddle1 bar1 = new Paddle1(); addObject(bar1, 550 , 200 ); Ball ball = new Ball(); addObject(ball, 300 , 200 ); Paddle2 bar2 = new Paddle2(); addObject(bar2, 50 , 200 ); Scoreboard scoreboard = new Scoreboard( "Player One: 0" , "Player Two: 0" ); addObject(scoreboard, 200 , 0 ); } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Ball here. * * @author (your name) * @version (a version number or a date) */ public class Ball extends Actor { public Ball() { GreenfootImage img = new GreenfootImage( 15 , 15 ); img.setColor(Color.RED); img.fill(); setImage(img); } private int xSpeed = 5 , ySpeed = 7 ; public int xVector = (Greenfoot.getRandomNumber( 13 )- 6 ); public int yVector = (Greenfoot.getRandomNumber( 13 )- 6 ); public void act() { movement(); Actor paddle_1 = getOneIntersectingObject(Paddle1. class ); if (paddle_1!= null ) { xSpeed = -xSpeed; } Actor paddle_2 = getOneIntersectingObject(Paddle2. class ); if (paddle_2!= null ) { xSpeed = -xSpeed; } } public void movement() { setLocation(getX() + xSpeed, getY() + ySpeed); if (getY()< 5 || getY() > getWorld().getHeight()- 5 ) { ySpeed = -ySpeed; } } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Bar1 here. * * @author (your name) * @version (a version number or a date) */ public class Paddle1 extends Paddles { //Creates the paddle and fills it in public Paddle1() { GreenfootImage img = new GreenfootImage( 20 , 100 ); img.setColor(Color.RED); img.fill(); setImage(img); } /** * Act - do whatever the Bar1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. if (Greenfoot.isKeyDown( "up" )) { setLocation(getX(), getY()- 5 ); } if (Greenfoot.isKeyDown( "down" )) { setLocation(getX(), getY()+ 5 ); } } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Score here. * * @author (your name) * @version (a version number or a date) */ public class Score extends Actor { private int totalCount = 0 ; public void bumpCount( int amount) { totalCount += amount; setImage( new GreenfootImage( "" + totalCount)); } /** * Act - do whatever the Score wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) // import java.awt.Color; /** * Write a description of class Scoreboard here. * * @author (your name) * @version (a version number or a date) */ public class Scoreboard extends Actor { public Scoreboard( String Player1, String Player2) { //Sets the borders for where he txt will be GreenfootImage img = new GreenfootImage( 500 , 300 ); img.setColor(Color.RED); Font font = new Font( "Helvetica" , true , false , 20 ); img.drawString(Player1, 150 , 200 ); img.drawString(Player2, 425 , 200 ); setImage(img); img.setFont(font); } public void setText(String Player1, String Player2) { GreenfootImage img = getImage(); img.clear(); img.drawString(Player1, 150 , 35 ); img.drawString(Player2, 500 , 35 ); } } |
1 2 3 | private Score score1 = new Score(); private Score score2 = new Score(); |