java - Stanford cs106a - Breakout exersize -
i'm trying learn java following cs106a course online. i've arrived @ breakout exersize , i'm having trouble bugs
i haven't finished want solves these issues first before go further.
problem 1:
when ball collides bricks don't seem removed canvas. brick removed on second time collides. there 1 row (of yellow bricks) doesn't respond @ ball collisions.
problem 2:
my paddle can moved dragging mouse. problem is. bricks can moved paddle.
i know has piece of code
gobj = getelementat(lastx, lasty);
if remove altogether bricks aren't moveable anymore. good. i'm still able move paddle no matter click , drag.
can give me hint can correct mistakes?
here's code below. thanks
/* * file: breakout.java * ------------------- * file implement game of breakout. */ import acm.graphics.*; import acm.program.*; import acm.util.*; import java.applet.*; import java.awt.*; import java.awt.event.*; import org.omg.corba.public_member; public class breakout extends graphicsprogram { /** width , 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 same) */ private static final int width = application_width; private static final int height = application_height; /** dimensions of paddle */ private static final int paddle_width = 60; private static final int paddle_height = 10; /** offset of paddle 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 = 8; /** separation between bricks */ private static final int brick_sep = 4; /** width of brick */ private static final int brick_width = (width - (nbricks_per_row - 1)* brick_sep)/ nbricks_per_row; /** height of brick */ private static final int brick_height = 8; /** radius of ball in pixels */ private static final int ball_radius = 10; /** offset of top brick row top */ private static final int brick_y_offset = 70; /** number of turns */ private static final int nturns = 3; private static final int delay = 50; private static final double x_start = width / 2; private static final double y_start = 450; public void run() { world(); play(); } private void ball() { ball = new goval(x_start, y_start, ball_radius, ball_radius); ball.setfillcolor(color.black); ball.setfilled(true); add(ball); } private void paddle() { paddle = new grect(100, 500, paddle_width, paddle_height); paddle.setcolor(color.black); paddle.setfilled(true); add(paddle); } private void brick(int x, int y, color c) { grect brick = new grect(x, y, brick_width, brick_height); brick.setcolor(getbackground()); brick.setfillcolor(c); brick.setfilled(true); add(brick); } private void brickrow(int x, int y, color c) { x = brick_sep / 2; y += brick_y_offset; (int = 0; < nbricks_per_row; i++) { brick(x, y, c); x += brick_width + brick_sep; } } private void world() { //initialize x , y position rows of bricks int x = 0; int y = 0; // set starting color first row color c = color.red; paddle(); //create 2 rows of bricks , switch colors (int = 0; < nbrick_rows; i++) { if (i <= 1) { c = color.orange; } else if (i > 1 && <= 3) { c = color.yellow; } else if (i > 3 && <= 5) { c = color.green; } else if (i > 5 && <= 7) { c = color.cyan; } brickrow(x, y, c); y += brick_height + brick_sep; } } private void moveball() { ball.move(xvel, yvel); } public void mousepressed(mouseevent e) { lastx = e.getx(); lasty = e.gety(); gobj = getelementat(lastx, lasty); } public void mousedragged(mouseevent e) { if (paddle != null) { paddle.move(e.getx() - lastx, gety()); lastx = e.getx(); lasty = e.gety(); } //constrain paddle movement if (paddle.getx() < 0){ paddle.setlocation(0, 500); }else if (paddle.getx()+brick_width > width ){ paddle.setlocation(width-brick_width, 500); } } private void checkforcollision() { // ball collission walls if (ball.gety() > getheight() - ball_radius || ball.gety() < 0 + ball_radius) { yvel = -yvel; } else if (ball.getx() > getwidth() - ball_radius || ball.getx() < 1 + ball_radius) { xvel = -xvel; // ball collission paddle } else if (getelementat(ball.getx() + ball_radius, ball.gety() + ball_radius) == paddle) { yvel = -yvel; // check collision bricks remove them ignore collision paddle } else if (getelementat(ball.getx(), ball.gety()) != null && getelementat(ball.getx(), ball.gety()) != paddle) { remove(getcollidingobject(ball.getx(), ball.gety())); yvel = -yvel; } else if (getelementat(ball.getx() + ball_radius, ball.gety() + ball_radius) != null && getelementat(ball.getx() + ball_radius, ball.gety() + ball_radius) != paddle) { remove(getcollidingobject(ball.getx() + ball_radius, ball.gety() + ball_radius)); yvel = -yvel; } else if (getelementat(ball.getx() + ball_radius, ball.gety() + ball_radius) != null && getelementat(ball.getx() + ball_radius, ball.gety() + ball_radius) != paddle) { remove(getcollidingobject(ball.getx() + ball_radius, ball.gety() + ball_radius)); yvel = -yvel; } else if (getelementat(ball.getx(), ball.gety() + ball_radius) != null && getelementat(ball.getx(), ball.gety() + ball_radius) != paddle) { remove(getcollidingobject(ball.getx(), ball.gety() + ball_radius)); yvel = -yvel; } } private void play() { addmouselisteners(); ball(); while (true) { checkforcollision(); moveball(); pause(delay); } } private gobject getcollidingobject(double x, double y) { return getelementat(x, y); } private double lastx; private double lasty; private double xvel = 5; private double yvel = 15; private goval ball; private grect paddle; private gobject gobj; }
why don't use suggested method in handout 19:
private gobject getcollidingobject()
use collider:
gobject collider = getcollidingobject();
first of separate conditions walls collisions , (paddle bricks) game elements collisions 2 separate methods cause simplify else if
cascading (decomposition think know means). have 2 objects should worry paddle
, bricks
, need compare on (collider == paddle)
, collider != null
. use radius in comparisons should use diameter (2 * radius). have fun :)
Comments
Post a Comment