diff --git a/.classpath b/.classpath index 948fbb9..573c75c 100755 --- a/.classpath +++ b/.classpath @@ -1,7 +1,7 @@ - - - - - - - + + + + + + + diff --git a/.gitignore b/.gitignore index 07b21ae..75402ef 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ -*.class -*.classpath -*.project - +*.class +*.classpath +*.project +*.html +*.au +*.applet + diff --git a/.project b/.project index 72f8601..cd8215f 100755 --- a/.project +++ b/.project @@ -1,17 +1,17 @@ - - - Assignment3 - - - - - - org.eclipse.jdt.core.javabuilder - - - - - - org.eclipse.jdt.core.javanature - - + + + Assignment3 + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs index 21ed01f..9d62186 100755 --- a/.settings/org.eclipse.jdt.core.prefs +++ b/.settings/org.eclipse.jdt.core.prefs @@ -1,52 +1,62 @@ -#Mon Oct 15 10:34:35 PDT 2007 -eclipse.preferences.version=1 -org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning -org.eclipse.jdt.core.compiler.problem.autoboxing=ignore -org.eclipse.jdt.core.compiler.problem.deprecation=warning -org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled -org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled -org.eclipse.jdt.core.compiler.problem.discouragedReference=warning -org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore -org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore -org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore -org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning -org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning -org.eclipse.jdt.core.compiler.problem.forbiddenReference=error -org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning -org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning -org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore -org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore -org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore -org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning -org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore -org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore -org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning -org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning -org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore -org.eclipse.jdt.core.compiler.problem.nullReference=ignore -org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning -org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore -org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore -org.eclipse.jdt.core.compiler.problem.rawTypeReference=ignore -org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled -org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning -org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled -org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore -org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning -org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning -org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore -org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning -org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore -org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore -org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore -org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled -org.eclipse.jdt.core.compiler.problem.unusedImport=warning -org.eclipse.jdt.core.compiler.problem.unusedLabel=warning -org.eclipse.jdt.core.compiler.problem.unusedLocal=warning -org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled -org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled -org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning -org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.6 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.autoboxing=ignore +org.eclipse.jdt.core.compiler.problem.deprecation=warning +org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled +org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=disabled +org.eclipse.jdt.core.compiler.problem.discouragedReference=warning +org.eclipse.jdt.core.compiler.problem.emptyStatement=ignore +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore +org.eclipse.jdt.core.compiler.problem.fieldHiding=ignore +org.eclipse.jdt.core.compiler.problem.finalParameterBound=warning +org.eclipse.jdt.core.compiler.problem.finallyBlockNotCompletingNormally=warning +org.eclipse.jdt.core.compiler.problem.forbiddenReference=error +org.eclipse.jdt.core.compiler.problem.hiddenCatchBlock=warning +org.eclipse.jdt.core.compiler.problem.incompatibleNonInheritedInterfaceMethod=warning +org.eclipse.jdt.core.compiler.problem.incompleteEnumSwitch=ignore +org.eclipse.jdt.core.compiler.problem.indirectStaticAccess=ignore +org.eclipse.jdt.core.compiler.problem.localVariableHiding=ignore +org.eclipse.jdt.core.compiler.problem.methodWithConstructorName=warning +org.eclipse.jdt.core.compiler.problem.missingDeprecatedAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingOverrideAnnotation=ignore +org.eclipse.jdt.core.compiler.problem.missingSerialVersion=ignore +org.eclipse.jdt.core.compiler.problem.noEffectAssignment=warning +org.eclipse.jdt.core.compiler.problem.noImplicitStringConversion=warning +org.eclipse.jdt.core.compiler.problem.nonExternalizedStringLiteral=ignore +org.eclipse.jdt.core.compiler.problem.nullReference=ignore +org.eclipse.jdt.core.compiler.problem.overridingPackageDefaultMethod=warning +org.eclipse.jdt.core.compiler.problem.parameterAssignment=ignore +org.eclipse.jdt.core.compiler.problem.possibleAccidentalBooleanAssignment=ignore +org.eclipse.jdt.core.compiler.problem.rawTypeReference=ignore +org.eclipse.jdt.core.compiler.problem.specialParameterHidingField=disabled +org.eclipse.jdt.core.compiler.problem.staticAccessReceiver=warning +org.eclipse.jdt.core.compiler.problem.suppressWarnings=enabled +org.eclipse.jdt.core.compiler.problem.syntheticAccessEmulation=ignore +org.eclipse.jdt.core.compiler.problem.typeParameterHiding=warning +org.eclipse.jdt.core.compiler.problem.uncheckedTypeOperation=warning +org.eclipse.jdt.core.compiler.problem.undocumentedEmptyBlock=ignore +org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning +org.eclipse.jdt.core.compiler.problem.unnecessaryElse=ignore +org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=ignore +org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=ignore +org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionWhenOverriding=disabled +org.eclipse.jdt.core.compiler.problem.unusedImport=warning +org.eclipse.jdt.core.compiler.problem.unusedLabel=warning +org.eclipse.jdt.core.compiler.problem.unusedLocal=warning +org.eclipse.jdt.core.compiler.problem.unusedParameter=ignore +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenImplementingAbstract=disabled +org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=disabled +org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=warning +org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning +org.eclipse.jdt.core.compiler.source=1.6 diff --git a/Breakout.java b/Breakout.java index 3f7c84d..2253c17 100755 --- a/Breakout.java +++ b/Breakout.java @@ -1,66 +1,225 @@ -/* - * File: Breakout.java - * ------------------- - * Name: - * Section Leader: - * - * This file will eventually implement the game of Breakout. - */ - -import acm.graphics.*; -import acm.program.*; -import acm.util.*; - -import java.applet.*; -import java.awt.*; -import java.awt.event.*; - -public class Breakout extends GraphicsProgram { - -/** Width and height of application window in pixels */ - public static final int APPLICATION_WIDTH = 400; - public static final int APPLICATION_HEIGHT = 600; - -/** Dimensions of game board (usually the same) */ - private static final int WIDTH = APPLICATION_WIDTH; - private static final int HEIGHT = APPLICATION_HEIGHT; - -/** Dimensions of the paddle */ - private static final int PADDLE_WIDTH = 60; - private static final int PADDLE_HEIGHT = 10; - -/** Offset of the paddle up from the bottom */ - private static final int PADDLE_Y_OFFSET = 30; - -/** Number of bricks per row */ - private static final int NBRICKS_PER_ROW = 10; - -/** Number of rows of bricks */ - private static final int NBRICK_ROWS = 10; - -/** Separation between bricks */ - private static final int BRICK_SEP = 4; - -/** Width of a brick */ - private static final int BRICK_WIDTH = - (WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW; - -/** Height of a brick */ - private static final int BRICK_HEIGHT = 8; - -/** Radius of the ball in pixels */ - private static final int BALL_RADIUS = 10; - -/** Offset of the top brick row from the top */ - private static final int BRICK_Y_OFFSET = 70; - -/** Number of turns */ - private static final int NTURNS = 3; - -/* Method: run() */ -/** Runs the Breakout program. */ - public void run() { - /* You fill this in, along with any subsidiary methods */ - } - -} + +/* + * File: Breakout.java + * ------------------- + * Name: + * Section Leader: + * + * This file will eventually implement the game of Breakout. + */ + +import acm.graphics.*; +import acm.program.*; +import acm.util.*; + +import java.applet.*; +import java.awt.*; +import java.awt.event.*; + +public class Breakout extends GraphicsProgram { + + /** APPLICATION_WIDTH and height of application window in pixels */ + public static final int APPLICATION_WIDTH = 400; + public static final int APPLICATION_HEIGHT = 600; + + /** Dimensions of the paddle */ + private static final int PADDLE_WIDTH = 60; + private static final int PADDLE_HEIGHT = 10; + + /** Offset of the paddle up from the bottom */ + private static final int PADDLE_Y_OFFSET = 30; + + /** Number of bricks per row */ + private static final int NBRICKS_PER_ROW = 1; + + /** Number of rows of bricks */ + private static final int NBRICK_ROWS = 10; + + /** Separation between bricks */ + private static final int BRICK_SEP = 4; + + /** APPLICATION_WIDTH of a brick */ + private static final int BRICK_WIDTH = (APPLICATION_WIDTH - (NBRICKS_PER_ROW - 1) * BRICK_SEP) / NBRICKS_PER_ROW; + + /** Height of a brick */ + private static final int BRICK_HEIGHT = 8; + + /** Radius of the ball in pixels */ + private static final int BALL_RADIUS = 10; + + /** Offset of the top brick row from the top */ + private static final int BRICK_Y_OFFSET = 70; + + /** Number of turns */ + private static final int NTURNS = 3; + + /** Number of turns */ + private static final int SPEED = 15; + + + /* Instance variables */ + private int lastX; /* The last mouse X position */ + private double x_offset; + private double vx, vy; + private GRect paddle; + private GOval ball; + private GLabel status_label ; + private boolean game_in_progress; + private int nbricks; + private int nturns; + private RandomGenerator rgen = RandomGenerator.getInstance(); + + /* Method: run() */ + /** Runs the Break-out program. for the number of turns allowed as long as the user keeps falling*/ + public void run() { + /* You fill this in, along with any subsidiary methods */ + + nturns = NTURNS; + game_in_progress = true; + while(game_in_progress){ + setUpGame(); + startGame(); + endGame(); + pause(2000); + nturns--; + } + + } + + /** Set up the application space. */ + private void setUpGame() { + removeAll(); + // setup label + status_label = new GLabel("..."); + status_label.setFont("Helvetica-24"); + add(status_label, 10, 50); + + //setup wall + GRect wall = new GRect(APPLICATION_WIDTH, APPLICATION_HEIGHT); + add(wall); + + // set up the rows + x_offset = (APPLICATION_WIDTH - ((BRICK_WIDTH * NBRICKS_PER_ROW) + (BRICK_SEP * (NBRICKS_PER_ROW - 1)))) / 2; + for (int i = 0; i < NBRICK_ROWS; i++) { + double y = BRICK_Y_OFFSET + (BRICK_HEIGHT * i); + Color color = getRowColor(i); + creatBrickRow(x_offset, y, color); + } + + //setup ball + ball = new GOval(2 * BALL_RADIUS, 2 * BALL_RADIUS); + ball.setFilled(true); + ball.setFillColor(Color.BLACK); + // centering the ball in the application window + add(ball, (APPLICATION_WIDTH - 2 * BALL_RADIUS) / 2 - BALL_RADIUS,(APPLICATION_HEIGHT - 2 * BALL_RADIUS) / 2 - BALL_RADIUS); + + //setup paddle + paddle = new GRect(PADDLE_WIDTH, PADDLE_HEIGHT); + paddle.setFilled(true); + paddle.setColor(Color.RED); + add(paddle, (APPLICATION_WIDTH - PADDLE_WIDTH) / 2, APPLICATION_HEIGHT - PADDLE_Y_OFFSET); + addMouseListeners(); + + } + + + /** create a row of brick + * @param x start x coordinate of the row + * @param y start y coordinate of the row + * @param color the color of the row + */ + private void creatBrickRow(double x, double y, Color color) { + for (int i = 0; i < NBRICKS_PER_ROW; i++) { + GRect brick = new GRect(BRICK_WIDTH, BRICK_HEIGHT); + brick.setFilled(true); + brick.setColor(color); + add(brick, x + (BRICK_WIDTH + BRICK_SEP) * i, y); + } + } + + /** determine the color of a row + * @param row_num th row number being created */ + private Color getRowColor(int row_num) { + Color color = Color.BLACK; + if (row_num == 0 || row_num == 1) color = Color.RED; + else if (row_num == 2 || row_num == 3) color = Color.ORANGE; + else if (row_num == 4 || row_num == 5) color = Color.YELLOW; + else if (row_num == 6 || row_num == 7) color = Color.GREEN; + else if (row_num == 8 || row_num == 9) color = Color.CYAN; + return color; + } + + /** determine the color of a row + * @param row_num th row number being created */ + private void startGame() { + vy = +3.0; + vx = rgen.nextDouble(1.0, 3.0); + nbricks = NBRICK_ROWS * NBRICKS_PER_ROW; + if (rgen.nextBoolean(0.5))vx = -vx; + while (true) { + if (ball.getY() <= 0) vy = -vy; + else if ((ball.getX() >= APPLICATION_WIDTH - 2 * BALL_RADIUS) || (ball.getX() <= 0)) vx = -vx; + else if (ball.getY() >= APPLICATION_HEIGHT || (nbricks ==0)) + {break;} + pause(SPEED); + ball.move(vx, vy); + GObject collider = getCollidingObject(); + if (collider==paddle){vy = -vy;continue;} + if (collider==null){continue;} + vy = -vy; + remove(collider); + status_label.setLabel("bricks: "+ nbricks--); + } + } + + private GObject getCollidingObject() { + GObject gobj=null; + gobj = getElementAt(ball.getX(), ball.getY()); + if (gobj != null) return gobj; + gobj = getElementAt(ball.getX()+2*BALL_RADIUS, ball.getY()); + if (gobj != null) return gobj; + gobj = getElementAt(ball.getX(), ball.getY()+2*BALL_RADIUS); + if (gobj != null) return gobj; + gobj = getElementAt(ball.getX()+2*BALL_RADIUS, ball.getY()+2*BALL_RADIUS); + if (gobj != null) return gobj; + return gobj; + + } + + /** Determine if the conditions to end game are met and set the game_in_progress flag to false. */ + private void endGame() { + String message; + if (nbricks == 0){ + message = "you won"; + game_in_progress = false; + } + else if (nturns > 0){ + message = "failed!!!: " + (nturns-1) + " more turns game reset"; + } + else{ + message = "failed!!! - no more turns game end"; + game_in_progress = false; + } + + if (message != null){ + status_label.setLabel(message); + } + } + + /** Called on mouse press to reposition the paddle */ + public void mousePressed(MouseEvent e) { + if (e.getX() >= x_offset && e.getX() <= x_offset + APPLICATION_WIDTH) { + paddle.move(e.getX() - paddle.getX(), 0); + lastX = e.getX(); + } + } + + /** Called on mouse drag to move paddle along the x axis only*/ + public void mouseDragged(MouseEvent e) { + if (e.getX() >= x_offset && e.getX() + PADDLE_WIDTH <= x_offset + APPLICATION_WIDTH) { + paddle.move(e.getX() - lastX, 0); + lastX = e.getX(); + } + + } +} diff --git a/README.md b/README.md index 9cff8ba..f4d97c1 100644 --- a/README.md +++ b/README.md @@ -1 +1 @@ -# prog-method-assignment-3 +# prog-method-assignment-3