Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This file is BrickBreaker.java, the main file.

Java
import javax.swing.JFrame;		/*necessary to add a obj in which game will run.*/
public class BrickBreaker
{
	public static void main(String[] args)
	{
		JFrame obj=new JFrame();
		GamePlay gp=new GamePlay();
		obj.setSize(700,600);		//750,560
		obj.setTitle("Generic Classic BrickBreaker");
		obj.setResizable(true);
		obj.setVisible(true);
		obj.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		obj.add(gp);
	}
}


Below is the second file GamePlay.java
Java
import javax.swing.JPanel;									//Lets you create GUI components like buttons,scroll bars,etc.
import javax.swing.Timer;
import java.awt.event.KeyListener;							//detect key presses*/
import java.awt.event.ActionListener;						//performing actions on objects of game*/
import java.awt.event.ActionEvent;							//awt is Abstract Window Toolkit, used to develop GUI
import java.awt.event.KeyEvent;
import java.awt.Graphics;
import java.awt.Color;

public class GamePlay extends JPanel implements KeyListener, ActionListener
{
	boolean play=true;										//so that game doesn't start by itself
	int score=0;											//the methods from KeyListener and ActionListener 
	int totalBricks=21;										//listed here are compulsory for the code to run
	Timer time;												//Timer is a module, allows timecheck
	int delay=8;											//speed of timer
	int playerX=310;										//starting for slider
	int ballX=120;											//starting X co-ord of ball
	int ballY=350;											//starting Y co-ord of ball
	int ballXdir=-1;										//direction of ball?
	int ballYdir=-2;

	public static void main(String[] args){}

	public void GamePlay()									//Constructor:(Similar to a method)Block of code, that's called when an
	{														//instance of the method is created.
		this.addKeyListener(this);							//to work with KL, need this statement
		setFocusable(true);									//allows object to be focused.
		setFocusTraversalKeysEnabled(false);				//allows keys to be used
		time = new Timer(delay, this);						//initialize timer
		time.start();
	}

	public void paint(Graphics g)
	{
		//background details
		g.setColor(Color.black);
		g.fillRect(1,1,692,592);

		//borders
		g.setColor(Color.blue);
		g.fillRect(0,0,3,592);
		g.fillRect(0,0,692,3);
		g.fillRect(691,0,3,592);

		//user bar
		g.setColor(Color.blue);
		g.fillRect(playerX,550,100,8);

		//ball
		g.setColor(Color.green);
		g.fillOval(ballX,ballY,10,10);

		g.dispose();
	}

	@Override
	public void actionPerformed(ActionEvent e){				//method from ActionListener
		time.start();
	}

	@Override
	public void keyTyped(KeyEvent e){						//method from KeyListener
		repaint();
	}					

	@Override
	public void keyReleased(KeyEvent e){}					//method from KeyListener

	@Override
	public void keyPressed(KeyEvent e)						//method from KeyListener
	{
		if(e.getKeyCode() == KeyEvent.VK_RIGHT)
		{
			if(playerX>=600)
			{
				playerX=600;
			}
			else
			{
				moveRight();
			}
		}

		if(e.getKeyCode() == KeyEvent.VK_LEFT)
		{
			if(playerX<10)
			{
				playerX=10;
			}
			else
			{
				moveLeft();
			}
		}

		repaint();
	}

	public void moveRight()
	{
		play = true;									//need to change as it was set to false initially
		playerX+=100;									//when right key is pressed, move 20 pixels to the right
	}

	public void moveLeft()
	{
		play = true;									//need to change as it was set to false initially
		playerX-=100;									//when right key is pressed, move 20 pixels to the right
	}
}


What I have tried:

I have tried a lot of combinations and different placement of my repaint() function. I have also tried to make changes to the time.start() or the timer in general. I thought maybe that was causing the problem. It is a BrickBreaker game, I'm learning Java. The slider doesn't move when I press arrow keys and I can't seem to figure out what is wrong. I also checked if I'm updating the write variables and I think I am. Please help me, this is driving me crazy.
Posted
Updated 19-Oct-17 20:19pm
v6
Comments
Richard MacCutchan 19-Oct-17 9:44am    
Please edit your question and explain what the problem is. And kindly do not shout, i.e. type in capitals, as it is considered rude.
Member 13474025 19-Oct-17 10:07am    
I'm very sorry :) I have fixed the What I have tried part, could you help me please?
Richard MacCutchan 19-Oct-17 10:35am    
There is something missing from your code - it never starts.
Member 13474025 20-Oct-17 2:03am    
I already had it in my code, what I think you're referring to .. I mentioned it that I had a JFrame. I just added it here. Kindly have a look.
Richard MacCutchan 20-Oct-17 4:02am    
OK, I cannot quite figure out what is happening, but none of your key handling events are being fired. Take a look at the sample code at Java Tutorials Sample Code[^] to see what you may be doing differently.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900