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

2015/9/24

BrickBreaker Ball Help

ali_23 ali_23

2015/9/24

#
Hey I am currently trying to create a brickbreaker game and struggling to find a way to get the ball to bounce off the walls, panel, and bricks. Any help would be appreciated. This is my current code: import greenfoot.*;
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import greenfoot.*;
 
/**
 * Write a description of class Ball here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ball extends Actor{
 
    private int ballX;        
    private int ballY;  
    private int oldEdge;
    private int vel;
    /**
     * Move the ball. Then check what we've hit.
     */
    public void act()
    {
 
        move(5);
        setRotation(180);
 
    }
 
    public void move()
    {
        setLocation (getX() + ballX, getY() + ballY);
 
        checkWalls();
        checkBlock();
    }
  
 
      
 
     
    public void checkWalls()//public int atWorldEdge()   
    
        boolean atXEdge = getX() < 10 || getX() > getWorld().getWidth()-10;
        boolean atYEdge = getY() < 10 || getY() > getWorld().getHeight()-10;
        if (atXEdge || atYEdge) move(-5);
        if (atYEdge) setRotation(360-getRotation());
        if (atXEdge) setRotation(540-getRotation());
    }
public void turnAtFloor()
    {
        if (getY() + getImage().getHeight()/2>=getWorld().getHeight() ) {
            vel.reverseY();
        }
    }
    public void turnAtRightWall()
    {
        if (getX() + getImage().getWidth()/2>=getWorld().getWidth() ) {
            vel.reverseX();
        }
    }
    public void turnAtRoof()
    {
        if (getY() + getImage().getHeight()/2<=getWorld().getHeight() ) {
            vel.reverseY();
        }
    }
    public void turnAtLeftWall()
    {
        if (getX() + getImage().getWidth()/2<=getWorld().getWidth() ) {
            vel.reverseX();
        }
    }
    
 
    /**
     * Check whether we're out (bottom of screen).If not out at bottom
     * then check if we have completed the level.
     */
    public void checkBlock()
    {
        Actor brick = getOneIntersectingObject(Bricks.class);
 
        if (brick != null)
        {
            ballY = -ballY;
        }
    }
    //End of Borrowed
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }
 
    public void release()
    {
        ballX = Greenfoot.getRandomNumber(11) - 5;
        ballY = -5;
 
    }
 
}
danpost danpost

2015/9/25

#
As is the only thing your 'act' method is allowing the object in doing is moving to the right edge of the screen. That is, line 21 will call the 'move' method starting at line 86 which takes the given distance and adds it to the current x-coordinate of the actor. Line 22 rotates the actor to "face" left which will not influence its movement direction because of the way the movement is processed. All the other methods (other than 'act' and 'move(int)') will never be executed (unless called from another class) as this class is currently coded. I will say that all of your 'turnAt ???' methods are flawed in that since 'vel' is an 'int' value, you cannot call any methods ('reverseX', 'reverseY', or any other) on it.
ali_23 ali_23

2015/9/25

#
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
52
53
54
55
56
57
58
import greenfoot.*;
 
/**
 * Write a description of class Ball here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Ball extends Actor{
 
    private int ballX;        
    private int ballY;  
    private int oldEdge;
    private int ball;
    int move = 5;
    int rotation = 35;
 
    /**
     * Move the ball. Then check what we've hit.
     */
    public void act()
    {
        checkWalls();
        move(move);
        setRotation(rotation);
 
    }
 
    public void move()
    {
        setLocation (getX() + ballX, getY() + ballY);
 
        checkWalls();
        //checkBlock();
    }
    //Borrowed
 
     
 
    public void checkWalls()
    
        if(isAtEdge()){
 
            if(rotation >= 0 && rotation<= 90){
                rotation = 280;
            }
            else if(rotation >90 && rotation<= 180){
                rotation = 200;
            }
            else if(rotation >180 && rotation<=270){
                rotation = 130;
            }
            else if(rotation >270 && rotation<=359){
                rotation = 45;
            }
        }
    }
}
I ended up using this instead and its not getting stuck on the wall anymore but for I can't figure out what to set the angles to in order to have it bounce off the walls in a realistic manner. Thanks for all your input danpost
fejfo fejfo

2015/9/25

#
your "move()" method still doesn't get called (unless you call it for some where else . to bounce realistickly you got to know this the angle at with the ball comes in is the same as the angle at with the ball bouces out so if you have the ball bounce vertickally you can just do
1
setRotation(-getRotation());
and horizontickally you can do :
1
2
3
turn(90);
setRotation(-getRotation());
turn(-90);
danpost danpost

2015/9/25

#
fejfo wrote...
your "move()" method still doesn't get called (unless you call it for some where else .
Actually, your 'move' method (lines 29 through 35) can now be removed. Line 31 will not move the ball because both 'ballX' and 'ballY' have values of zero and line 32 is now being called from the 'act' method. Four of your fields can also be removed for lack of use (lines 11 through 14 -- 'ballX', 'ballY', 'oldEdge', and 'ball').
ali_23 ali_23

2015/9/28

#
fejfo wrote...
if you have the ball bounce vertickally you can just do setRotation(-getRotation()); and horizontickally you can do : Thanks so much that code seemed to work off the left wall but not off any of the others, I'm not sure what exactly what went wrong? Once again thanks danpost, I removed the lines and it just made it look alot less messy. Appreciate it both of you! turn(90); setRotation(-getRotation()); turn(-90);
ali_23 ali_23

2015/9/28

#
sorry about the format od my post still new to the forums and figuring it out
danpost danpost

2015/9/28

#
ali_23 wrote...
that code seemed to work off the left wall but not off any of the others, I'm not sure what exactly what went wrong?
You will need to show the revised code for the class (the entire class -- again).
ali_23 ali_23

2015/9/28

#
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
public class Ball extends Actor{
 
  
    int move = 5;
    int rotation = 45;
 
    /**
     * Move the ball. Then check what we've hit.
     */
    public void act()
    
        checkWalls();
        move(move);
        setRotation(rotation);
 
    }
 
    public void move()
    {
        setLocation (getX() + ballX, getY() + ballY);
 
        checkWalls();
         
    }
    
 
    public void checkWalls()//public int atWorldEdge()   
    {    setRotation(-getRotation());
        turn(90);
        setRotation(-getRotation());
        turn(-90);
         
 
      getRotation());
        if(isAtEdge()){
            System.out.println("changing");
            if(rotation >= 0 && rotation<= 90){
                rotation = 135;
            }
            else if( y=0(rotation >90 && rotation<=180){
                rotation =225;
            }
            else if(rotation >180 && rotation<=270){
                rotation = 270;
            }
            else if(rotation >270 && rotation<=359){
                rotation = 45;
            }
        }
    }
}
danpost danpost

2015/9/28

#
You should trace the logic of your 'checkWalls' method. You cannot just throw the code given you into the method. The snippets given you should be placed at the proper locations, replacing the problem code you already had. Also, using 'isAtEdge' will not be enough to determine which edge is being hit -- even with the rotational value). You need to check the actual x- and y-coordinate values for it being at an edge.
You need to login to post a reply.