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

4 days ago

My breakout scenario is not removing the first row of my bricks

SportingLife7 SportingLife7

4 days ago

#
when i first run the game, unless it hits one of the above row bricks first, the ball does not break the bricks in the first row. can anybody help? public class Board extends World { private Paddle paddle; private int startStars = 2500; /** * Constructor for objects of class Board. * adds in the paddle, the counters etc. * */ public Board() { super(460, 520, 1); // all of these will be put in this order the first will be the top layer of the screen, the following will be under it like the asteroid going on top of the healthbar GreenfootImage background = getBackground();//use this to add text, the first line before the equal sign background.setColor(Color.BLACK); background.fill(); createStars(startStars); setPaintOrder ( Ball.class, Smoke.class, PowerUps.class ); paddle = new Paddle(); addObject ( paddle, getWidth() / 2, getHeight() - 40); ballIsOut(); buildBricks(1); } /** * Add a given number of stars to our world. */ 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()); int r = Greenfoot.getRandomNumber(256); int g = Greenfoot.getRandomNumber(256); int b = Greenfoot.getRandomNumber(256); background.setColor(new Color(r,g,b)); background.fillOval(x, y, 4 , 4); //the two two controls the size of my stars! //we add the same r value to make it gray if we would have many then we would get rainbow! } } /** * checks to see what the score, is * checks to see what level it is * checks to see if cheat keys are pressed * checks to see if it is time to advance to a new level * picks random numbers and if it is a certain range it drops a power up */ public void act() { } /** * checks to see if the ball is out */ public void ballIsOut() { paddle.newBall(); } // /** // * this is to get our brick info back! // */ // public Brick getBrick() // { // return brick; // } /** * checks to see what level it currently is and if it needs to advance. */ public void checkLevel() { } /** * creates the scoreboard for between levels. */ public void levelBoard() { } /** * initializes the construction of levels */ public void levelAdvance() { } /** * builds the bricks 1 by 1 for each of the levels */ public void buildBricks (int numberOfRows) //public void buildBricks (int numberOfRows) { for (int i = 0; i < 7; i++) for (int b = 0; b < 3; b++) addObject (new Brick (), 75 + i*50, 175 + 40*b); //Greenfoot.delay(1); // this is not the most effective way to create the block by blco effect we need // also need to figure out how to make rows of bricks (ask the guys) // this one(above)is specifically for the first row implemented } /** * separate method for a special level design */ public void levelSix() { } /** * separate method for a level design with bricks that need to be hit twice. */ public void levelSeven() { } /** * cheat keys to advance levels. */ public void cheats() { } /** * resets all factors involved with level advance. */ public void reset() { } /** * This method is called when the game is over to display the final score. */ public void gameOver() { } }
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The ball of the game. It moves and bounces off the walls and the paddle.
 * 
 * @author mik
 * @version 1.0
 */
public class Ball extends Actor
{
    private int deltaX;         // x movement speed
    private int deltaY;         // y movement speed
    private int count = 2;
    public static int BALL_COUNT = 0;
    public  static int score = 0;
    public static int levelScore = 0;
    private Counter scoreKeeper;
    private boolean stuck = true;   // stuck to paddle
    public static boolean brokenBrick = false;
    public static boolean ultimateBall = false;
    public static int timer = 251;
    public int timerReset = 250;

    /**
     * Act. Move if we're not stuck.
     */
    public void act() 
    {
        if (!stuck) 
        {
            move();
            makeSmoke();
            checkOut();
            
        }
    }

    /**
     * Move the ball. Then check what we've hit.
     */
    public void move()
    {
        setLocation (getX() + deltaX, getY() + deltaY);
        checkPaddle();
        checkWalls();
        checkBricks();
    }

    /**
     * Check whether we've hit one of the three walls. Reverse direction if necessary.
     */
    private void checkWalls()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            deltaX = -deltaX;
        }
        if (getY() == 0) {
            deltaY = -deltaY;
            turn(180);
        }
    }

    /**
     * Check whether we're out (bottom of screen). If we are, spawn a new ball on the paddle assuming we have lives left.
     */
    private void checkOut()
    {
        if (getY() == getWorld().getHeight()-1) {
            ((Board) getWorld()).ballIsOut();
            getWorld().removeObject(this);
            //Ball.BALL_COUNT--;
        }
    }

    /**
     * Check whether we are touching the paddle, and bounce off of it if we are.
     */
    private void checkPaddle()
    {
        Actor paddle = getOneIntersectingObject(Paddle.class);
        if (paddle != null) {
            deltaY = -deltaY;//flips the direction on the y-axis
            turn (180);
            int offset = getX() - paddle.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX > 7) 
            {
                deltaX = 7;
            }
            if (deltaX < -7) 
            {
                deltaX = -7;
            }
        }            
    }

    /**
     * check whether we are touching a brick, and bounce off it if we are (and remove the brick we touched)
     * adds score if we remove a brick or hit a brick
     */

    private void checkBricks()
    {
        Actor brick = getOneIntersectingObject(Brick.class);
             if (brick != null) {
            deltaY = -deltaY;
            turn(180);
            int offset = getX() - brick.getX();
            deltaX = deltaX + (offset/10);
            if (deltaX > 7) {
                deltaX = 7;
            }
            if (deltaX < -7) {
                deltaX = -7;
            }
            
    }
}
    /**
     * check whether we are touching a brick of the other brick class (this was for making bricks that require 2 hits to destroy.
     * One hit changed the image, the second hit removed it.
     */    
    private void checkBricks2()
    {

    }

    /**
     * Move the ball a given distance sideways.
     */
    public void move(int dist)
    {
        setLocation (getX() + dist, getY());
    }

    /**
     * Put out a puff of smoke (only on every second call).
     */
    private void makeSmoke()
    {
        count--;
        if (count == 0) {
            getWorld().addObject ( new Smoke(), getX(), getY());
            count = 2;
        }
    }

    /**
     * Release the ball from the paddle. 
     */
    public void release()
    {
        deltaX = Greenfoot.getRandomNumber(11) - 5; //sets the intial angle when released from paddle
        deltaY = -5;  // controls the speed of the ball
        stuck = false;
    }
}
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Brick extends Actor
{
    public Board world = (Board) getWorld();
    public boolean removed = false;
    /**
     * bricks gonna be bricks, nothing is really done in this class becuase they just have an image set for them.
     * But you may do more in here.,
     */
    public Brick()
    {
        
    }
    
        /**
     * Act - do whatever the Brick wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    
    public void act() 
    {
        if (isTouching(Ball.class))
        {
            this.removed = true;
            getWorld().removeObject(this);
            //Greenfoot.playSound();
            
        }// Add your action code here.
    }    
    

}
Super_Hippo Super_Hippo

4 days ago

#
Try to remove the brick from the ball class and remove all code inside the brick class. It’s never good to have intersection checks in the classes of both objects which intersect with each other.
You need to login to post a reply.