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

2016/3/16

How do I get a score when the ball goes off the screen on one side?

tn13673 tn13673

2016/3/16

#
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 int xspeed = 4;
    public int yspeed = 4;
    public static int BallX, BallY;
    /**     * Act - do whatever the paddle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
         
        setLocation(getX() + xspeed, getY()+yspeed);
        bounce();
        movement();
        BallX = getX ();
        BallY = getY ();
    }

    public void movement()
    {
        int worldWidth = getWorld().getWidth();
        int worldHeight = getWorld().getHeight();
        int spriteWidth = getImage().getWidth()/2;
        int spriteHieght = getImage().getHeight()/2;
        if(getX() <= spriteWidth)
        xspeed = xspeed * -1;
        
        if(getX() >= worldWidth - spriteWidth)
         xspeed = xspeed * -1;
      
    }
    
    
      public void bounce ()
   {
        Actor mypaddle = getOneIntersectingObject (mypaddle.class) ;
        if(mypaddle != null)
        {
            yspeed = yspeed * -1;
           
        }
        
        
        Actor paddle = getOneIntersectingObject (paddle.class) ;
        if(paddle != null)
        {
            yspeed = yspeed * -1;
           
        }
}
}
danpost danpost

2016/3/16

#
How do I get a score when the ball goes off the screen on one side?
Why would you even ask this when lines 35 through 39 appear to prevent the ball from going off the sides?
tn13673 tn13673

2016/3/17

#
That's what my teacher had me do, I really don't understand coding.
tn13673 tn13673

2016/3/17

#
I deleted that, but now the computer paddle doesn't move.
Super_Hippo Super_Hippo

2016/3/17

#
The lines 35..39 don't do anything with the paddle. Removing only these lines couldn't have any effect to the paddle.
tn13673 tn13673

2016/3/18

#
Well it did.
danpost danpost

2016/3/18

#
Okay -- I see. The problem is not with lines 35 thru 39 at all. Apparently, your paddles are at top and bottom of the window and lines 35 thru 39 make the ball bounce off the sides. Your title to this discussion used the word "side", which is misleading (incorrect for what you are talking about). What you meant was "top or bottom edge" where lines 35 thru 39 deal with the "left and right edges" or what I would infer "sides" as. The 'if' conditions used within those lines, 35 thru 39, are similar to those you need for scoring; except that the expressional values compared will be those related to the position of the ball vertically, not horizontally.
tn13673 tn13673

2016/3/21

#
I got it fixed.
You need to login to post a reply.