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

New Topic/Question
Reply



MultiQuote







|