Results 1 to 3 of 3
  1. #1
    BradM313 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Greenfoot: Need help with my Pinball game

    Hello,

    I'm relatively new to Java so it might be a stupid question.

    I have to make a Pinball game for school, our teacher gave us a code for the "Ball.class" but I can't figure out how I can let it bounce back from the paddles. (and thats quite important )
    I also posted this question on the Greenfoot-forum but there aren't many people there. So I thought someone here could help me out.

    btw. I'm only allowed to use Greenfoot so I can't do anything in other programming programs.

    Thanks in advance!!!

    The "Ball" code:

    Java Code:
        import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  
        import java.awt.Color;  
           
        public class Ball extends Actor  
        {  
            double x,y,a=0,b=0,ang,angN,mag,angx,angy,magG;  
            int numAng=0;  
              
            protected void addedToWorld(World world){  
                 x=getX();  
                 y=getY();  
                 if(x>890 || x<10 || y>600 || y<10){  
                     getWorld().removeObject(this);  
                     return;  
                    }  
                 for(int f=0;f<360;f=f+5){  
                     if(!isColor(getX(),getY())){  
                         numAng++;  
                         angx=angx+getX();  
                         angy=angy+getY();  
                        }  
                    }  
                if(numAng!=0)getWorld().removeObject(this);  
            }  
             
            public void act()  
            {  
                x=x+a;  
                y=y+b;  
                 
                if(y>595){  
                    getWorld().removeObject(this);  
                    return;  
                }  
                 
                numAng=0;  
                angx=0;  
                angy=0;  
                for(int f=0;f<360;f=f+10){  
                    if(!isColor((int)(x+5*Math.cos((f/180.0)*Math.PI)),(int)(y+5*Math.sin((f/180.0)*Math.PI)))){  
                        numAng++;  
                        angx=angx+Math.cos((f/180.0)*Math.PI);  
                        angy=angy+Math.sin((f/180.0)*Math.PI);  
                    }  
                }  
                        
                for(int f=0;f<360;f=f+10){  
                    Actor actor = getOneIntersectingObject(Versnelling.class);  
                    if(actor != null){  
                        numAng++;  
                        angx=angx+Math.cos((f/180.0)*Math.PI);  
                        angy=angy+Math.sin((f/180.0)*Math.PI);  
                    }  
                }  
                             
                if(numAng>3){  
                                 
                    while(numAng>20 && !Greenfoot.isKeyDown("c")){  
                        numAng=0;  
                        x=x-.1*angx;  
                        y=y-.1*angy;  
                        angx=0;  
                        angy=0;  
                        for(int f=0;f<360;f=f+10){  
                            if(!isColor((int)(x+5*Math.cos((f/180.0)*Math.PI)),(int)(y+5*Math.sin((f/180.0)*Math.PI)))){  
                                numAng++;  
                                angx=angx+Math.cos((f/180.0)*Math.PI);  
                                angy=angy+Math.sin((f/180.0)*Math.PI);  
                            }  
                        }  
                    }  
                    while(numAng!=0 && !Greenfoot.isKeyDown("c")){  
                        numAng=0;  
                        y=y-.1*angy;  
                        x=x-.1*angx;  
                        for(int f=0;f<360;f=f+5){  
                            if(!isColor((int)(x+5*Math.cos((f/180.0)*Math.PI)),(int)(y+5*Math.sin((f/180.0)*Math.PI)))){  
                                numAng++;  
                            }  
                        }  
                    }  
                    x=x-.1*a;  
                    y=y-.1*b;  
                    angN = Math.PI/2.0 + Math.atan2(angy,angx);  
                    mag = Math.sqrt(a*a + b*b);  
                    if(mag>.7)mag=mag-.5;  
                    ang = Math.atan2(b,a);  
                    if(Math.abs(angN-ang)<.7){  
                        angN = angN-.05;  
                        if(angN<ang)angN=angN+.1;  
                    }  
                    ang = -(ang-angN)+angN;  
                    a = mag*Math.cos(ang)*(.3 + .7*Math.abs(Math.cos(angN)));  
                    b = mag*Math.sin(ang)*(.3 + .7*Math.abs(Math.sin(angN)));  
                    magG = Math.sin(angN);  
                    if(angN>Math.PI/2.0 && angN<3*Math.PI/2.0){  
                        a = a + magG*Math.cos(angN);  
                        b = b + magG*Math.sin(angN);  
                    }  
                    a= (int)(a*40)/40.0;  
                    b= (int)(b*40)/40.0;  
                    if(Math.abs(a)<.01)a=Math.random()/10.0-.05;  
                     
           
                }else {  
                    if(Math.sqrt(a*a + (b+.1)*(b+.1)) < 8)b=b+.1;  
                }  
                setLocation((int)x,(int)y);  
            }     
               public boolean isColor(int X,int Y){  
                return ((Kast)getWorld()).isColor(X,Y);  
            }  
        }
    and this is a piece of the World code:

    Java Code:
    import greenfoot.*;  
    import java.awt.Color;  
      
    /** 
     * The BrickWorld 
     */  
    public class Kast extends World {  
       
        private static int SizeX = 900;  
        private static int SizeY = 600;  
          
        GreenfootImage background = getBackground();   
        
        /** 
         * Nieuwe Kast 
         */  
        public Kast() {  
           super(SizeX, SizeY, 1);  
            GreenfootImage background = getBackground();  
            background.setColor(Color.BLACK);  
            background.fill();  
              
            createStars(500);  
            createOvals(15);  
            drawLine();  
              
            addObject(new Start(), 225, 120);  
            addObject(new Ball(), 225, 100);  
            addObject(new Links(), 360, 520);  
            addObject(new Rechts(), 550, 520);  
            addObject(new Versnelling(), 450, 300);  
        }  
         
        /** 
         * sets the background color 
         */  
        public void setColor (Color color) {  
            GreenfootImage background = new GreenfootImage(20,20);  
            background.setColor(color);  
            background.fill();  
            setBackground(background);  
        }  
          
        private void createStars(int number)  
        {  
            GreenfootImage background = getBackground();              
            for (int i = 0; i < number; i++)  
            {  
                 int x = Greenfoot.getRandomNumber( getWidth() );  
                 int y = Greenfoot.getRandomNumber( getHeight() );  
                 background.setColor(new Color(  
                        Greenfoot.getRandomNumber(255),  
                        Greenfoot.getRandomNumber(255),  
                        Greenfoot.getRandomNumber(255)  
                 ));  
                 background.fillOval(x, y, 2, 2);  
            }  
        }  
      
        /** 
         * Crete a given number of stars in space. 
         */  
        private void createOvals(int number)  
        {  
            GreenfootImage background = getBackground();              
            for (int i = 0; i < number; i++)  
            {  
                 int x = Greenfoot.getRandomNumber( getWidth() );  
                 int y = Greenfoot.getRandomNumber( getHeight() );  
                 background.setColor(new Color(255,255,255));  
                 background.fillOval(x, y, 25, 10);  
            }  
        }  
         
      //some code  
          
        public boolean isColor(int a,int b){  
            return background.getColorAt(a,b).equals(Color.BLACK);  
        }  
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,307
    Rep Power
    25

    Default Re: Greenfoot: Need help with my Pinball game

    I'm only allowed to use Greenfoot
    That will keep most people from trying to test your code.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    BradM313 is offline Member
    Join Date
    Feb 2013
    Posts
    2
    Rep Power
    0

    Default Re: Greenfoot: Need help with my Pinball game

    Quote Originally Posted by Norm View Post
    That will keep most people from trying to test your code.
    Thanks for your reply! I already tought that, that would keep people away :(

Similar Threads

  1. Greenfoot help
    By Big-D in forum New To Java
    Replies: 2
    Last Post: 05-17-2012, 03:24 PM
  2. Concentration Game in Greenfoot
    By corpen in forum New To Java
    Replies: 1
    Last Post: 10-16-2011, 06:33 PM
  3. Pinball program problem
    By trueblue in forum New To Java
    Replies: 8
    Last Post: 03-21-2009, 02:01 AM
  4. Greenfoot 1.4.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-24-2008, 06:02 PM
  5. Greenfoot 1.2.1
    By Jamie in forum Java Software
    Replies: 0
    Last Post: 06-14-2007, 02:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •