# Thread: Greenfoot: Need help with my Pinball game

1. Member
Join Date
Feb 2013
Posts
2
Rep Power
0

## 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.

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;

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

}

/**
* 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. ## 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.

3. Member
Join Date
Feb 2013
Posts
2
Rep Power
0

## Re: Greenfoot: Need help with my Pinball game

Originally Posted by Norm
That will keep most people from trying to test your code.