Let me start fresh,
below is my coding for a breakout game, i have become stuck on the code that allows the ball to hit the bat and go back up, the ball will hit where the mouse is, not on the bat this bit of coding is:
PHP Code:
private void checkCollision(){
Graphics myPen = paper.getGraphics();
// ball hitting bat!
if ((ballY + ballX) >= 30 && ballY >= batX && ballY <= (batX + 60)){
yChange = -yChange;
}
}
once this collision has taken place you press the button go to let another ball come down, the ball will stop at the start of the screen, if you continue to press the 'go' button it will stay at the top of the screen, the program is a single class program which is why i do not have a main class, critism and comments are all welcome, i am looking for help on the above coding and if anyone can find the problem to why the program will not continue sending more balls down.
with the program it once it has hit the bat(mouse) it dissappears at the top of the screen wen it should bounce back not sure on this which is why i am looking for help from people who understand java a great deal more than i do
thank you
:
PHP Code:
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import javax.swing.*;
public class BreakOut extends JFrame implements ActionListener,MouseListener, MouseMotionListener {
private JPanel paper;
private JButton btnInsert£, btnGo;
private JTextField txtHowManyGoes;
private int x, y, xChange, yChange, diameter, batX, batY, ballX, ballY;
private int preX=0, preY=0 ;
private int counter;
private Boolean firstTime=true, brk1;
private JLabel lblDisplay;
private Timer timer1;
private Random RandNum;
public static void main(String[] args){
BreakOut breakOut = new BreakOut();
breakOut.setSize(500, 500);
breakOut.setBackground(Color.green);
breakOut.setVisible(true);
}
public BreakOut(){
setLayout(new FlowLayout());
btnInsert£ = new JButton("Insert £");
txtHowManyGoes = new JTextField(2);
add(btnInsert£); add(txtHowManyGoes);
btnGo = new JButton("Go!..");
btnGo.setEnabled(false);
add(btnGo);
btnInsert£.addActionListener(this);
btnGo.addActionListener(this);
lblDisplay = new JLabel("....X Y Coordinates......");
paper = new JPanel();
paper.setPreferredSize(new Dimension(400, 400));
paper.setBackground(Color.green);
add(paper);
add(lblDisplay);
addMouseListener(this);
addMouseMotionListener(this);
x =0; y =0; xChange = 2; yChange =7; diameter = 11;
counter=0;
timer1 =new Timer (50, this);
}
public void moveBall(){
Graphics myPen = paper.getGraphics();
myPen.setColor(Color.GREEN);
myPen.fillOval(x, y, diameter, diameter);
x = x + xChange ;
y = y + yChange ;
if (x <= 0 || x >= paper.getWidth()){
xChange = -xChange;
}
if (y <= 0 || y >= paper.getHeight()){
timer1.stop();
counter--;
txtHowManyGoes.setText(counter+"");
}
myPen.setColor(Color.blue);
myPen.fillOval(x, y, diameter, diameter);
ballX =x; ballY = y;
}
public void actionPerformed(ActionEvent event){
//moveBall();
if (event.getSource()==btnInsert£){
btnGo.setEnabled(true);
counter = counter + 5;
txtHowManyGoes.setText(counter +"");
}
if (event.getSource()==timer1){
moveBall();
}
if (event.getSource()==btnGo){
if (counter >0){
Random randNum; randNum = new Random();
x= randNum.nextInt((paper.getWidth()));
y=0;
timer1 = new Timer(10, this);
timer1.start();
}
else btnGo.setEnabled(false);
}
}
public void drawBat(){
int batY;
Graphics myPen = paper.getGraphics();
//paint(myPen); // stops flickering!
batY = paper.getHeight()-20; // bottom of the screen!
myPen.clearRect(0, 0, paper.getWidth(),paper.getHeight());
// myPen.fillRect(batX-60,batY , 60, 10);
myPen.setColor(Color.BLACK);
batX = batX - 70; batY = batY - 30;//Co-ordinates of the batY position
//batY = paper.getHeight()-40;
myPen.fillRect(batX,batY , 70, 10); //adjusts bat size
preX = batX; preY = batY;
}
private void checkCollision(){
Graphics myPen = paper.getGraphics();
// ball hitting bat!
if ((ballY + ballX) >= 30 && ballY >= batX && ballY <= (batX + 60)){
//if((ballX >= batX)&& (ballX <= batX +30 && (ballY >= batY )))
//yChange = -5;
yChange = -yChange;
}
}
public void mouseMoved(MouseEvent event){
lblDisplay.setText("X is " +x + " and Y is " + y );
batX = event.getX();
//batY = event.getY();
drawBat();
checkCollision();
batX = batX - 70;
batY = batY - 40;
//batY = paper.getHeight()-40;
//getMousePosition();
}
public void drawBrks(){
Graphics myPen = paper.getGraphics();
myPen.setColor(Color.BLUE);
if (brk1== true){
myPen.fillRect(80, 10, 40, 10);
}
}
public void mouseClicked(MouseEvent e){
}
public void mouseEntered(MouseEvent e){
}
public void mouseExited(MouseEvent e){
}
public void mouseReleased(MouseEvent e){
}
public void mousePressed(MouseEvent e){
}
public void mouseDragged(MouseEvent e){
}
}// end of BouncingBall