Discuss BreakOut the game in the Java Help forum on Dev Shed. BreakOut the game Java Help forum discussing all Java platforms - J2ME, J2SE and J2EE - as well as relevant standards, APIs and frameworks such as Swing, Servlets, JSPs, Applets, Struts, Spring, Hibernate, ANT, EJB, and other Java-related topics.
Receive the tools necessary to be the rock star of your field. Our 12-month program teaches you the evolving world of multi-channel marketing as well as the complex issues and opportunities found in the industry.
ASP Free and Iron Speed Designer are giving away $5,500+ in FREE licenses. Iron Speed's RAD CASE toolset can save up to 80% of your coding time. One free license per week, one perpetual license per month! Download and Activate to enter!
Web development can be a daunting task, even for specialists. There is a lot of information to absorb and a lot of technologies to learn in order to manage a superior website. When trying to learn the ropes, developers need a reliable source to introduce new ideas that can be easily implemented. When working on large projects, even web veterans may run into a technology or an aspect of a technology that they are unfamiliar with.
Posts: 10
Time spent in forums: 1 h 38 m 33 sec
Reputation Power: 0
Homework - BreakOut the game
Hi. I'm at the first stage of making the game BreakOut. This is what I have so far. I was wondering if there is a more efficient way to write this? Thanks
Code:
import java.awt.Color;
import acm.program.*;
import acm.graphics.*;
public class BreakOut extends GraphicsProgram
{
public static final int APP_WIDTH = 400;
public static final int APP_HEIGHT = 600;
private static final int PADDLE_Y_OFFSET = 30;
private static final int PADDLE_WIDTH = 60;
private static final int PADDLE_HEIGHT = 10;
private static final int PADDLE_STARTX = (APP_WIDTH / 2) - (PADDLE_WIDTH / 2);
private static final int PADDLE_STARTY = APP_HEIGHT - PADDLE_Y_OFFSET;
private static final int BRICK_Y_OFFSET = 70;
private static final int BRICK_ROWS = 10;
private static final int BRICK_COLUMNS = 10;
private static final int BRICK_SEPARATION = 4;
private static final int BRICK_HEIGHT = 8;
private static final int BRICK_WIDTH = (APP_WIDTH - (BRICK_COLUMNS - 1) * BRICK_SEPARATION) / BRICK_COLUMNS;
private static final int BALL_RADIUS = 10;
private static final int BALL_STARTX = (APP_WIDTH / 2) - (BALL_RADIUS / 2);
private static final int BALL_STARTY = APP_HEIGHT / 2;
private GOval ball;
private GRect paddle;
public void run()
{
addBricks();
addBall();
addPaddle();
}
private void addBricks()
{
int brickY = BRICK_Y_OFFSET;
GRect brick;
for (int i = 0; i < BRICK_ROWS; i++)
{
int brickX = BRICK_SEPARATION;
for (int j = 0; j < BRICK_COLUMNS; j++)
{
brick = new GRect(brickX, brickY, BRICK_WIDTH, BRICK_HEIGHT);
add(brick);
colorBricks(brick, Color.RED, 0, 1);
colorBricks(brick, Color.ORANGE, 2, 3);
colorBricks(brick, Color.YELLOW, 4, 5);
colorBricks(brick, Color.GREEN, 6, 7);
colorBricks(brick, Color.CYAN, 8, 9);
brickX += BRICK_WIDTH + BRICK_SEPARATION;
}
brickY += BRICK_HEIGHT + BRICK_SEPARATION;
}
}
private void colorBricks(GRect brick, Color color, int firstRow, int lastRow)
{
if (brick.getY() >= BRICK_Y_OFFSET + firstRow * (BRICK_HEIGHT + BRICK_SEPARATION) && brick.getY() <= BRICK_Y_OFFSET + lastRow * (BRICK_HEIGHT + BRICK_SEPARATION))
{
brick.setColor(color);
brick.setFilled(true);
}
}
private void addBall()
{
ball = new GOval(BALL_STARTX, BALL_STARTY, BALL_RADIUS, BALL_RADIUS);
ball.setFilled(true);
add(ball);
}
private void addPaddle()
{
paddle = new GRect(PADDLE_STARTX, PADDLE_STARTY, PADDLE_WIDTH, PADDLE_HEIGHT);
paddle.setFilled(true);
add(paddle);
}
}