1 Replies - 532 Views - Last Post: 08 February 2017 - 11:56 AM Rate Topic: -----

#1 dshane  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-February 17

Problem with my brickbreaker code

Posted 08 February 2017 - 10:57 AM

The problem with my code is when the paddle intersects with the ball sometimes the ball will get stuck and "roll off".
the rest is fully working. I have the other classes of the code that corresponds with this to animate, create the JFrame, and Blocks. i will attach a Zip of my code also.

 
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Random;

import javax.swing.JPanel; //create a loop for the powerup
public class BreakoutPanel extends JPanel implements KeyListener{ //KeyListener allows the game to recognize when a key on the keyboard is pressed
//extends makes more or extends the panel?
	ArrayList<Block> blocks = new ArrayList<Block>();
	ArrayList<Block> ball = new ArrayList<Block>(); //making the ball
	ArrayList<Block> powerup = new ArrayList<Block>();
	
	Block paddle;
	Thread thread;
	Animate animate;
	int size =25;
	//to make the rectangle
	BreakoutPanel(){  //this is making the all the blocks and creating them all // I am copying and pasting the for loop and setting all the different block colors
		paddle = new Block(175,480,150,25, "paddle.png");
		for(int i = 0; i<8; i++){ //for loop is using i's instead of j's for some reason
			blocks.add(new Block((i*60+2), 0, 60, 25, "blue.png"));  //Blue
		}
		for(int i = 0; i<8; i++){ //Red
			blocks.add(new Block((i*60+2), 25, 60, 25, "red.png"));  
		}
		for(int i = 0; i<8; i++){ //Green
			blocks.add(new Block((i*60+2), 50, 60, 25, "Green.png"));  
		}
		for(int i = 0; i<8; i++){ //yellow
			blocks.add(new Block((i*60+2), 75, 60, 25, "Yellow.png"));  
		}
		Random random = new Random();
		blocks.get(random.nextInt(32)).powerup = true;
		blocks.get(random.nextInt(32)).powerup = true;
		blocks.get(random.nextInt(32)).powerup = true;
		blocks.get(random.nextInt(32)).powerup = true;
		blocks.get(random.nextInt(32)).powerup = true;
		blocks.get(random.nextInt(32)).powerup = true;

		ball.add(new Block(237,437,26,26, "ball.png")); //creating the ball
		addKeyListener(this);
		setFocusable(true);
	}
	public void paintComponent(Graphics g){ //drawing the blocks
		super.paintComponent(g);
		for(Block b : blocks) //i'm not sure what all of this does. 
			b.draw(g, this);
		for(Block b : ball)
			b.draw(g, this);
		for(Block p : powerup)
			p.draw(g, this);
		paddle.draw(g, this);
	}
	public void update(){  //for the ball to move
		for(Block p : powerup){ //this is telling the powerup what to do
			p.y+=1;
			if(p.intersects(paddle) && !p.destroyed){ //if the ball is not destroyed
				p.destroyed = true;
				ball.add(new Block(paddle.dx + 75, 437, 26, 26, "ball.png")); //adds a new ball when the powerup intersects with the paddle
			}
		}
		for(Block ba : ball){ 
			ba.x+= ba.dx;
			if(ba.x > (getWidth() - size) && ba.dx>0 || ba.x<0)
				ba.dx*=-1;
			if(ba.y < 0 || ba.intersects(paddle))
			ba.dy*=-1;
			ba.y+= ba.dy;//parameters for the ball so it bounces off the wall
			for(Block b : blocks){
				if((b.left.intersects(ba) || b.right.intersects(ba)) && !b.destroyed){ // to destroy the blocks
					ba.dx*=-1;
					b.destroyed = true;
					if(b.powerup)
						powerup.add(new Block(b.x, b.y, 25, 19, "extra.png")); // this is setting the image of the powerups
				}
				else if(ba.intersects(B)/>/> && !b.destroyed){
					b.destroyed = true;
					ba.dy*=-1;
					if(b.powerup)
						powerup.add(new Block(b.x, b.y, 25, 19, "extra.png")); 			
					}
				}
			
					}
		repaint();
	}
	@Override
	public void keyTyped(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ENTER){ //if the enter key is pressed start the game
			animate = new Animate (this);
			thread = new Thread(animate);
			thread.start();
		}
		if(e.getKeyCode() == KeyEvent.VK_LEFT && paddle.x > 0){  //if the left arrow key is pressed
			paddle.x-=15; //speed of the movement of the paddle in the left direction
		}
		if(e.getKeyCode() == KeyEvent.VK_RIGHT && paddle.x < (getWidth()-paddle.width)){  //if the right arrow key is pressed //this also sets limits to where the paddle can go
			paddle.x+=15; //speed of the movement of the paddle in the right direction
		}
		
	}
	@Override
	public void keyReleased(KeyEvent e) {
		// TODO Auto-generated method stub
		
	}
	}
	



  


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with my brickbreaker code

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1575
  • View blog
  • Posts: 3,541
  • Joined: 05-April 11

Re: Problem with my brickbreaker code

Posted 08 February 2017 - 11:56 AM

You need to properly indent your code. If you indent your code, it will be a lot easier for yourself and everyone else to read it and spot any potential errors.

Did you forget to add curly brackets here or is it intended?
if(ba.y < 0 || ba.intersects(paddle))
ba.dy*=-1;
ba.y+= ba.dy;//parameters for the ball so it bounces off the wall



I don't see anywhere in your code where you try and move the ball "out of" the paddle it intersects with. When the ball intersects with a paddle, you should first be moving the ball so it no longer intersects the paddle.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1