This site requires JavaScript, please enable it in your browser!
Greenfoot back
CardJitsu5
CardJitsu5 wrote ...

2020/5/4

Reset command

CardJitsu5 CardJitsu5

2020/5/4

#
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.
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());
     

    }
    
    
}
Thanks in advance
Super_Hippo Super_Hippo

2020/5/4

#
You can put this into your act method. (Somewhere before line 46.)
if (Greenfoot.isKeyDown("r")) setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2);
You need to login to post a reply.