Having trouble using KeyListener and GObject to create BreakOut game

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 2431 Views - Last Post: 27 August 2014 - 04:31 PM Rate Topic: -----

#1 zk206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-May 12

Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:17 PM

I am trying to create the Breakout game and cannot figure how to get the paddle to move and make constraints to bricks disappear when the ball hits them but not the paddle. I tried this:

public void keyPressed(KeyEvent press)
	{
		obj = paddle;
		int key = press.getKeyCode();
		if(key == KeyEvent.VK_LEFT)
		{
			while(true)
			{
				paddle.move();
			}
		}
	}




but I get an error for the "paddle.move();". Below is the entire code that I have gotten so far:


import acm.program.GraphicsProgram;
import acm.graphics.*;
import java.awt.Color;
import java.awt.event.*;
import acm.util.RandomGenerator;

public class BreakOut2 extends GraphicsProgram
{
	final int APPLET_WIDTH = 400;
	final int APPLET_HEIGHT = 600;
	RandomGenerator rg = new RandomGenerator();
	int y=1;
	int x=1;
	GRect paddle, brick;
	GOval ball;
	GObject obj;

	public void init()
    {
        setSize(APPLET_WIDTH, APPLET_HEIGHT);
        addKeyListeners();
    }

	public void run()
	{

		drawBricks();
		drawPaddle();
		drawBall();

	}// end of run

	public void drawPaddle()
	{
		final int PADDLE_WIDTH = 60;
 		final int PADDLE_HEIGHT = 10;

		//Create paddle
		GRect paddle = new GRect(170,570,60,10);
		add(paddle);
		paddle.setFilled(true);
	}


	public void drawBall()
	{
		//Create ball
		GOval ball = new GOval(200,300,10,10);
		add(ball);
		ball.setFilled(true);

		//Make ball move
		int x=1;
		int y=1;

		while(true)
		{
			ball.move(x, y);
			if(ball.getX() + ball.getWidth() > APPLET_WIDTH || ball.getX() < 0)
			{
				x = -x;
			}
			if(ball.getY() + ball.getHeight() > APPLET_HEIGHT ||ball.getY() < 0)
			{
				y = -y;
			}
			pause(10);


		}
	}

	public void drawBricks()
	{
		final int BRICK_WIDTH = 32;
 		final int BRICK_HEIGHT = 8;
 		int xCoord = 10;
 		int yCoord = 70;

		//Create bricks
		for(int i=1; i<=10; i++)
		{

			for(int k=1; k<=10; k++)
		    {
		    	GRect brick = new GRect(xCoord, yCoord, BRICK_WIDTH, BRICK_HEIGHT);
		        add(brick);
				xCoord = xCoord + 38;

				brick.setFilled(true);

				//Color for rows 1 and 2
				if (i==1 || i==2)
				{

					brick.setFillColor(Color.RED);
				}

				//Colors for rows 3 and 4
				if (i==3 || i==4)
				{
					brick.setFillColor(Color.ORANGE);
				}

				//Color for rows 5 and 6
				if (i==5 || i==6)
				{
					brick.setFillColor(Color.YELLOW);
				}

				//Color for rows 7 and 8
				if (i==7 || i==8)
				{
					brick.setFillColor(Color.GREEN);
				}

				//Color for rows 9 and 10
				if (i==9 || i==10)
				{
					brick.setFillColor(Color.BLUE);
				}
			}

				yCoord = yCoord + 13;
				xCoord = xCoord - 380;
		}
	}

	public void keyPressed(KeyEvent press)
	{
		obj = paddle;
		int key = press.getKeyCode();
		if(key == KeyEvent.VK_LEFT)
		{
			while(true)
			{
				paddle.move();
			}
		}
	}

}// end of class



Is This A Good Question/Topic? 0
  • +

Replies To: Having trouble using KeyListener and GObject to create BreakOut game

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10664
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:24 PM

What specific error(s) are you encountering? Please post them exactly as they appear.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:29 PM

Are you sure the GRect class has a move() method
And your GUI will freeze if you while(true) in the EventDispatchThread

http://en.wikipedia....patching_thread
http://docs.oracle.c...cy/initial.html
Was This Post Helpful? 0
  • +
  • -

#4 zk206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-May 12

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:32 PM

View Postmacosxnerd101, on 14 May 2012 - 05:24 PM, said:

What specific error(s) are you encountering? Please post them exactly as they appear.



The error that I get is:


C:\Users\Sony\Desktop\BreakOut2.java:147: error: method move in class GObject cannot be applied to given types;
paddle.move();
^
required: double,double
found: no arguments
reason: actual and formal argument lists differ in length
1 error

Tool completed with exit code 1

I know that it needs an x and y coordinate but my problem is that i don't know how use the keylistener and GObject to get the paddle to move
Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:33 PM

View Postzk206, on 14 May 2012 - 08:30 PM, said:

C:\Users\Sony\Desktop\BreakOut2.java:147: error: method move in class GObject cannot be applied to given types;
paddle.move();
^
required: double,double

Quite clear so it is

GRect.move(double, double):

probably x and y displacement or destination as arguments
Was This Post Helpful? 0
  • +
  • -

#6 zk206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-May 12

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:33 PM

your right, GRect doesn't have a move() method
Was This Post Helpful? 0
  • +
  • -

#7 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:34 PM

View Postzk206, on 14 May 2012 - 08:33 PM, said:

your right, GRect doesn't have a move() method

Yes it has one but it expects 2 arguments
Was This Post Helpful? 0
  • +
  • -

#8 zk206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-May 12

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:35 PM

even when i do give it x and y values, it still doesn't move
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:40 PM

Sure, look at my first post

Your GUI will be refreshed when you will exit the keyPressed() method
but in that method you have an infinite loop

while(true)
paddle.move()

you can call GRect.move() millions times, the GUI will effectively be refreshed when the method will exit.
That applies to all EventDispatchThread so actionPerformed(), ListListener, ....
Was This Post Helpful? 0
  • +
  • -

#10 zk206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-May 12

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 05:59 PM

i just don't understand how to use keylisteners to make the paddle move.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 14 May 2012 - 06:20 PM

in your keyListener() you can:

paddle.move(x, y);
repaint();
}

but you can't loop in it, you have to exit
Was This Post Helpful? 0
  • +
  • -

#12 zk206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-May 12

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 15 May 2012 - 07:53 PM

I used keyListeners to set everything up, and there are no errors showing up; however, when I run the program the paddle does not move.

I was hoping I could get some help in figure out where my mistake is in order to get the game running properly.

Below is my entire code, the moving paddle part is at the bottom:



import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class BreakoutGame extends GraphicsProgram
{
	//Game Display
	final int APPLET_WIDTH = 400;
	final int APPLET_HEIGHT = 600;

	//Paddle
	final int PADDLE_WIDTH = 60;
	final int PADDLE_HEIGHT = 10;
	final int PADDLE_Y_OFFSET = 30;
	final int left = 0;
	final int right = 1;

	//Bricks
	final int NBRICK_ROWS = 10;
	final int NBRICK_PER_ROW = 10;
	final int BRICK_SEP = 4;
	final int BRICK_WIDTH = APPLET_WIDTH/NBRICK_PER_ROW - BRICK_SEP;
	final int BRICK_HEIGHT = 12;
	final int BRICK_Y_OFFSET = 70;

	//Ball
	final int BALL_RADIUS = 10;
	final int BALL_Y_START = 300;
	final int BALL_X_START = 185;


	int startX, startY;
	GObject obj;
	GRect brick, paddle;
	GOval ball;
	double vx, vy; //velocities
	final double PAUSE_TIME = 20;

 	public void run()
 	{
		setSize(APPLET_WIDTH, APPLET_HEIGHT);
		drawBricks();
		drawPaddle();
		drawBall();
		addKeyListeners();


		while(true)
		{
			advanceBall();
			pause(PAUSE_TIME);
		}

	}

	//Create bricks
	public void drawBricks()
	{


		for(int row = 0; row<NBRICK_ROWS;row++)
		{
			int x = ((APPLET_WIDTH - (NBRICK_PER_ROW * BRICK_WIDTH))/2); // x coord of first brick
			int y = (BRICK_HEIGHT*row + BRICK_Y_OFFSET); // y coordof first brick

			for(int bricks = 0; bricks < NBRICK_PER_ROW; bricks ++)
			{
				GRect brick = new GRect((x + (bricks * BRICK_WIDTH)),y,BRICK_WIDTH - BRICK_SEP,BRICK_HEIGHT - BRICK_SEP);
				brick.setFilled(true);
				add(brick);

				if(row<=1)
				{
					brick.setFillColor(Color.RED);
				}

				else if(row<=3 && row>1)
				{
					brick.setFillColor(Color.ORANGE);
				}

				else if(row<=5 && row>3)
				{
					brick.setFillColor(Color.YELLOW);
				}

				else if(row<=7 && row>5)
				{
					brick.setFillColor(Color.GREEN);
				}

				else if(row<=9 && row>7)
				{
					brick.setFillColor(Color.CYAN);
				}

			}

			y += BRICK_HEIGHT;

		}

	}
	//Create ball
	public void drawBall()
	{
		ball = new GOval(BALL_X_START,BALL_Y_START,BALL_RADIUS*2,BALL_RADIUS*2);
		ball.setFilled(true);
		add(ball);
		vx = -3;
		vy = 3;
	}

	//Make ball bounce
	public void advanceBall()
	{
		int count = 0;
		boolean haveBlocks = true;

		RandomGenerator rg = new RandomGenerator();
		vx = rg.nextDouble(1.0,3.0);
		if(rg.nextBoolean())
		{
			vx = -vx;
		}
		vy = 4.0;

		while(true)
		{
			ball.move(vx,vy);
			pause(20);

			if(ball.getX() <= 0)
			{
				vx = -vx;
			}
			if(ball.getX() >= APPLET_WIDTH)
			{
				vx = -vx;
			}
			if(ball.getY() <= 0)
			{
				vy = -vy;
			}
			if(ball.getY() >= APPLET_HEIGHT)

			{
				//Game Over statement
				GLabel loser = new GLabel(" ", 100, 10);
				loser.setLabel("GAME OVER, CLICK TO PLAY AGAIN");
				add(loser);
				remove(ball);
			}


			GObject corner1 = getElementAt(ball.getX(),ball.getY());//location of upper left corner
			GObject corner2 = getElementAt(ball.getX() + (2 * BALL_RADIUS),ball.getY());//location of upper right corner
			GObject corner3 = getElementAt(ball.getX(),ball.getY() + (2 * BALL_RADIUS));//location of lower left corner
			GObject corner4 = getElementAt(ball.getX() + (2 * BALL_RADIUS),ball.getY() + (2 * BALL_RADIUS));//location of lower right corner

			if(corner1 != null )//there is an object at this location
			{
				vy = -vy;
				vx = vx;
				ball.move(vx,vy);
				remove(corner1);
			}

			else if(corner2 != null)//there is an object at this location
			{
				vy = -vy;
				vx = vx;
				ball.move(vx,vy);
				remove(corner2);
			}

			else if(corner3 != null && corner3!=paddle)//there is an object other than the paddle at this location
			{
				vy = -vy;
				vx = vx;
				ball.move(vx,vy);
				remove(corner3);
			}

			else if(corner4 != null && corner4!=paddle)//there is an object other than the paddle at this location
			{
				vy = -vy;
				vx = vx;
				ball.move(vx,vy);
				remove(corner4);
			}

			else if(corner3==paddle||corner4==paddle)
			{
				vy = -vy;
				vx = vx;
			}

			ball.move(vx,vy);

			if(count>=(NBRICK_PER_ROW*NBRICK_ROWS))//all squares are gone
			{
				haveBlocks = false;
				GLabel winner = new GLabel("YOU WIN!!!",100,100);
				winner.setFont(new Font("Arial",Font.BOLD,18));
				add(winner);
			}
		}

	}//end advanceBall


	//Draw paddle
	public void drawPaddle()
	{
		int x = (APPLET_WIDTH - PADDLE_WIDTH)/2;
		int y = (APPLET_HEIGHT - PADDLE_HEIGHT);

		paddle = new GRect(x,y - PADDLE_Y_OFFSET,PADDLE_WIDTH,PADDLE_HEIGHT);
		paddle.setFilled(true);
		add(paddle);

	}//end drawPaddle

	public void keyTyped(KeyEvent e){}

	public void keyPressed(KeyEvent press)
	{
		int key = press.getKeyCode();
		if(key == KeyEvent.VK_LEFT)
		{
			if(obj == paddle)
			{
				paddleMove(left);
				
			}
		}

		if(key==KeyEvent.VK_RIGHT)
		{
			if(obj==paddle)
			{
				paddleMove(right);
			}
		}
	}

	public void keyReleased(KeyEvent e){}

	public void paddleMove(int move)
	{
		//Move paddle
		int x_left = -30;
		int x_right = 30;

		if(move == left)
		{
			if(paddle.getX() <= 0) return;
			paddle.move(x_left,0);
			return;
		}

		if(move == right)
		{
			if(paddle.getX() >= APPLET_WIDTH-PADDLE_WIDTH) return;
			paddle.move(x_right,0);
			return;
		}

	}






}//end class



*Edited: remove wrong code posted

This post has been edited by pbl: 15 May 2012 - 07:59 PM

Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 15 May 2012 - 08:01 PM

you have to call repaint() after having changed your paddle coordinates
Was This Post Helpful? 0
  • +
  • -

#14 zk206  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 14-May 12

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 15 May 2012 - 08:13 PM

I wrote return(); though...shouldn't that do the job?
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Having trouble using KeyListener and GObject to create BreakOut game

Posted 15 May 2012 - 08:18 PM

No !

If your first version you call repaint() but never return; from the method so your repiant() was useless.

Now you corrected your problem and return; from the method but you still have to call repaint() before exiting. This repaint() will be executed because you return;
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2