-
Help With Arkanoid
So far, I have the paddle and the ball moving, and the ball bounces off the paddle, but I'm having trouble thinking of a way to get the bricks to disappear when hit and if the paddle misses the ball it's game over. I'm a total noob to Java, so forgive my questions. :P
Here's what I have:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
import java.awt.Color;
public class FullBall2 extends Applet implements Runnable
{
//position of the ball
int x_pos = 12;
int y_pos = 12;
//ball size
int radius = 5;
//ball direction
int x_speed = 6;
int y_speed = 8;
//position of the slider
int sliderx_pos = 50;
int slidery_pos = 190;
//size of slider
int swidth = 40;
int sheight = 3;
Rectangle box = new Rectangle (swidth, sheight);
//slider direcion
int sliderx_speed = 0;
//applet size
int appletsize_top = //getHeight();
0;
// bottom would be 200 px below bottom of component.
int appletsize_bottom = //appletsize_top + 20 * 10;
200;
int appletsize_left = //getWidth();
0;
// right would be 350 past right edge of component.
int appletsize_right = //appletsize_left + 35 * 10;
300;
//level stuff
int lx_pos = 100;
int ly_pos = appletsize_top + 20;
int lwidth = 50;
int lheight = 1;
Rectangle brick = new Rectangle (lx_pos, ly_pos, lwidth, lheight);
//random movement
Random rnd = new Random ();
//Drawing image off screen
private Image dbImage;
private Graphics dbg;
Thread thread;
boolean running = false;
public void init ()
{
enableEvents (AWTEvent.KEY_EVENT_MASK);
setFocusable (true);
}
public void start ()
{
// define a new thread
running = true;
thread = new Thread (this);
// start this thread
thread.start ();
}
public void stop ()
{
running = false;
if (thread != null)
thread.interrupt ();
thread = null;
}
public void run ()
{
while (running)
{
//change direct if the ball hits the left or right wall
if (x_pos - radius + x_speed < appletsize_left ||
x_pos + radius + x_speed > appletsize_right)
{
x_speed *= -1;
}
if (y_pos - radius + y_speed < appletsize_top)
{
y_speed *= -1;
}
if (y_pos + radius + y_speed > appletsize_bottom)
{
// Reposition ball to avoid overshooting boundry
y_pos = appletsize_bottom - radius;
y_speed *= -1;
// stop();
}
if (x_pos + radius + x_speed > sliderx_pos &&
x_pos - radius + x_speed < sliderx_pos + swidth)
{
// Ball is in vertical plane of slider.
// Check for possible contact.
if (y_pos + radius + y_speed > slidery_pos)
{
// to bounce
y_speed *= -1;
}
}
if (x_pos + radius + x_speed > lx_pos &&
x_pos - radius + x_speed < lx_pos + lwidth)
{
// Ball is in vertical plane of brick.
if (y_pos - radius /*+ y_speed*/< ly_pos + brick.height)
{
// We have an imminent collision -> bounce!
y_speed *= -1;
lx_pos = 10000;
ly_pos = 10000;
brick.setLocation (lx_pos, lx_pos);
}
}
x_pos += x_speed;
y_pos += y_speed;
//repaint applet
repaint ();
try
{
// Stop thread for the speed
Thread.sleep (90);
}
catch (InterruptedException ex)
{
running = false;
break;
}
}
}
//create an image off screen and loads it into the applet
public void update (Graphics g)
{
if (dbImage == null)
{
dbImage = createImage (this.getSize ().width, this.getSize ().height);
dbg = dbImage.getGraphics ();
}
dbg.setColor (getBackground ());
dbg.fillRect (0, 0, this.getSize ().width, this.getSize ().height);
dbg.setColor (getForeground ());
paint (dbg);
// draw image on the screen
g.drawImage (dbImage, 0, 0, this);
}
public void paint (Graphics g)
{
g.drawLine (appletsize_right, appletsize_top,
appletsize_right, appletsize_bottom);
g.drawLine (appletsize_left, appletsize_bottom,
appletsize_right, appletsize_bottom);
//background
g.setColor (Color.black);
g.fillRect (1,1, 300,200);
//DRAWING THE BALL
Graphics2D g2 = (Graphics2D) g;
g.setColor (Color.orange);
g.fillOval (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
//DRAWING THE SLIDER
box.setLocation (sliderx_pos, slidery_pos);
g2.setColor (Color.red);
g2.fill (box);
//DRAWING THE BRICKS
// Red row of bricks
g.setColor (Color.red);
g.fillRect (10, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (41, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (72, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (103, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (134, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (165, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (196, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (227, 20, 30, 13);
g.setColor (Color.red);
g.fillRect (258, 20, 30, 13);
// Second row of blue bricks
g.setColor (Color.blue);
g.fillRect (10, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (41, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (72, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (103, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (134, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (165, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (196, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (227, 34, 30, 13);
g.setColor (Color.blue);
g.fillRect (258, 34, 30, 13);
// Third row, yellow bricks
g.setColor (Color.yellow);
g.fillRect (10, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (41, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (72, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (103, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (134, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (165, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (196, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (227, 48, 30, 13);
g.setColor (Color.yellow);
g.fillRect (258, 48, 30, 13);
// Fourth row, green bricks
g.setColor (Color.green);
g.fillRect (10, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (41, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (72, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (103, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (134, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (165, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (196, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (227, 62, 30, 13);
g.setColor (Color.green);
g.fillRect (258, 62, 30, 13);
//Fifth row, purple
g.setColor (Color.magenta);
g.fillRect (10, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (41, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (72, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (103, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (134, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (165, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (196, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (227, 76, 30, 13);
g.setColor (Color.magenta);
g.fillRect (258, 76, 30, 13);
}
protected void processKeyEvent (KeyEvent e)
{
int keyCode = e.getKeyCode ();
//move if left key is pressed
if (keyCode == KeyEvent.VK_LEFT)
{
if (sliderx_pos - 10 > appletsize_left)
sliderx_pos = sliderx_pos - 20;
}
//move if right key is pressed
else if (keyCode == KeyEvent.VK_RIGHT)
{
if (sliderx_pos + swidth + 10 < appletsize_right)
sliderx_pos = sliderx_pos + 20;
}
}
}
-
To get the balls to disappear when hit you first have to check if a brick was hit. Use the size and coordinates of the ball and check if they intersect with an area the brick occupies by calculating the coordinates of each pixel of the brick. If the ball intersects a coordinate in the brick then repaint the bricks making sure to not redraw the brick that existed over the intersection coords.
As for ending the game if the ball is missed use the same coord check but compare it to the paddle.
-
Okay...
Right, (again, total noob here)
so would I use something like I did for the slider? I've tried something like that, but I got stuck trying get the ball to disappear AND bounce back towards the slider. @_@