Results 1 to 9 of 9
Thread: probably a no-brainer..
- 11-28-2010, 09:03 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
probably a no-brainer..
Hi all,
Total newbee...New to java and new to the forum. Going through the stanford cs106a itunes U (fantastic) to one day reach their iphone programming course. In an assignment I wish to do a "cascading if" as the professor has shown in the slide attached, but it will not work for me. Eclipse wants me to remove the second and third "else" and then if I do that is says the method has to return an object i.e. the null statement is missing. Can someone point out the error(s)? (the relevant section is in red font section close to the end):
Thanks a lot
Jesper
ps. the program is work in progress, so there are still missing sections and draft examples from other methods pasted in it
Here's the code:
/*
* 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;
/** Regulate Game Difficulty */
private static final int DELAY = 10;
/** Runs the Breakout program. */
public void run() {
// Run game for the number of turns defined by NTURNS
points=0; //initialize score
for(int i=1; i < NTURNS; i++){
setUpGame(); // Setup fresh board, paddle and ball
// Set random direction for ball when starting new game
vx = rgen.nextDouble(1.0, 3.0);
if (rgen.nextBoolean(0.5)) vx=-vx;
// println("Size of Canvas" + getWidth() + " , "+ getHeight());
// Run game until a gameover condition occurs
while (!gameOver()) {
moveBall();
checkForWallCollision();
checkForGameCollision();
pause(DELAY);
}
}
// Number of turns exhausted game ends
GLabel gameover = new GLabel("Game over",0,0);
double GAMEOVERWIDTH = gameover.getWidth();
add(gameover,WIDTH/2-GAMEOVERWIDTH/2,HEIGHT/2);
}
private void setUpGame(){
setUpCanvas();
setUpBricks();
setUpPaddle();
SetUpBall();
addMouseListeners();
startGame();
}
private void setUpCanvas(){
getGCanvas().setSize(WIDTH, HEIGHT);;
}
private void setUpBricks(){
double StartPosX = BRICK_SEP;
double StartPosY = BRICK_Y_OFFSET;
int linecount=0;
for (int i = 0;i<NBRICK_ROWS;i++){
for (int j = 0;j<NBRICKS_PER_ROW;j++){
if(linecount<2){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.RED);
Brick.setFilled(true);
add (Brick);
}
else if(linecount<4){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.ORANGE);
Brick.setFilled(true);
add (Brick);
}
else if(linecount<6){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.YELLOW);
Brick.setFilled(true);
add (Brick);
}
else if(linecount<8){
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.GREEN);
Brick.setFilled(true);
add (Brick);
}
else {
GRect Brick = new GRect (StartPosX+j*(BRICK_WIDTH+BRICK_SEP),StartPosY+i*( BRICK_HEIGHT+BRICK_SEP),BRICK_WIDTH,BRICK_HEIGHT);
Brick.setColor(Color.CYAN);
Brick.setFilled(true);
add (Brick);
}
}
linecount++;
}
}
private void setUpPaddle(){
paddle = new GRect(100,HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT,PADDLE_WIDTH,PADDLE_HEIGHT);
paddle.setFillColor(Color.BLACK);
paddle.setFilled(true);
add (paddle,100,HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT);
}
private void SetUpBall(){
ball = new GOval(WIDTH/2-BALL_RADIUS,HEIGHT/2-BALL_RADIUS,BALL_RADIUS*2,BALL_RADIUS*2);
ball.setFillColor(Color.RED);
ball.setFilled(true);
add (ball);
}
private void startGame(){
// Ask user to start game
GLabel gamestart = new GLabel("Click to Start",0,0);
gamestart.setFont("Helvetica-24");
double gamestartwidth = gamestart.getWidth();
println("width" + gamestart.getWidth() + " ,X= "+ (WIDTH/2-gamestartwidth/2));
add(gamestart,(WIDTH/2)-(gamestartwidth/2),HEIGHT/2*0.8);
waitForClick();
remove(gamestart);
}
public void mouseMoved(MouseEvent e)
{
double mouseXPos = e.getX();
if (mouseXPos < (WIDTH- PADDLE_WIDTH))
paddle.setLocation(mouseXPos,HEIGHT - (PADDLE_Y_OFFSET + PADDLE_HEIGHT));
}
private void moveBall() {
ball.move(vx, vy);
}
private void checkForWallCollision() {
// determine if ball has dropped beyond the boundries
//Did ball hit the bottom?
if (ball.getY() > (HEIGHT - BALL_RADIUS*2)) {
vy = -vy;
double diff = (ball.getY() - (HEIGHT - BALL_RADIUS*2));
ball.move(vx, -2 * diff);
}
//Did ball hit the top?
else if(ball.getY() < 0) {
vy = -vy;
// assume bounce will move ball an amount above the
// floor equal to the amount it would have dropped
// below the floor.
double diff = (ball.getY());
ball.move(vx, -2 * diff);
}
//Did ball hit the left wall?
else if(ball.getX() < 0) {
// change ball's Y velocity to now bounce upwards
vx = -vx;
// assume bounce will move ball an amount above the
// floor equal to the amount it would have dropped
// below the floor.
double diff = (ball.getX());
ball.move(-2 * diff,vy);
}
//Did ball hit the right wall?
else if(ball.getX() > WIDTH-BALL_RADIUS*2) {
// change ball's Y velocity to now bounce upwards
vx = -vx;
// assume bounce will move ball an amount above the
// floor equal to the amount it would have dropped
// below the floor.
double diff = ball.getX() - (WIDTH - BALL_RADIUS*2);
ball.move(-2 * diff,vy);
// else proceed with next move
}
}
private void checkForGameCollision(){
getCollidingObject();
if(collider == paddle){
vy = -vy;
double diff = (ball.getY() - (HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT));
ball.move(vx, -2 * diff);
}
else {
vy = -vy;
double diff = (ball.getY() - (HEIGHT-PADDLE_Y_OFFSET-PADDLE_HEIGHT));
ball.move(vx, -2 * diff);
points++;
remove(collider);
}
}
private GObject getCollidingObject(){
if(getElementAt(ball.getX(),ball.getY())!=null){
GObject collider = getElementAt(ball.getX(),ball.getY()); }
else if(getElementAt((ball.getX()+BALL_RADIUS*2),ball.g etY())!=null);{
GObject collider = getElementAt((ball.getX()+BALL_RADIUS*2),ball.getY ());}
else if(getElementAt((ball.getX()+BALL_RADIUS*2),ball.g etY())!=null);{
GObject collider = getElementAt((ball.getX()+BALL_RADIUS*2),ball.getY ());
else if(getElementAt((ball.getX()+BALL_RADIUS*2),ball.g etY())!=null);{
GObject collider = getElementAt((ball.getX()+BALL_RADIUS*2),ball.getY ());}
else null;
}
/** determines if game is over -- true if either
* the UFO is destroyed or if the UFO lands */
private boolean gameOver() {
return false;
/* return (ufo == null) ||
(ufo.getY() >= getHeight() - UFO_HEIGHT);*/
}
private GRect paddle;
private GOval ball;
private GObject collider;
private double vx = 5;
private double vy = 3; // velocity of Ball
private RandomGenerator rgen = RandomGenerator.getInstance();
private int points;
}
- 11-28-2010, 09:28 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
There are a couple of errors in there: only if all the tests fail you reach the else part and return null; otherwise nothing is returned; that's why the Eclipse Java compiler is whining. Also, those collider objects are local to the if-blocks, so they are gone when the block is ready executing: better make that:
kind regards,Java Code:GObject collider= null; // initialize it if ( ... ) collider= ...; else if ( ... ) collider= ...; else if ( ... ) collider= ...; // no else, so collider == null return collider;
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 11-28-2010, 10:25 AM #3
Member
- Join Date
- Nov 2010
- Posts
- 3
- Rep Power
- 0
Thanks a lot Jos!
got it working :)
- 02-09-2011, 07:20 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Newbie here. I have a question about best practices in regards to writing this game. Obviously, you can get it working with everything (all the methods, etc.) in one program since the bricks and paddle are simply rectangles and the ball is simply a circle.
However, would it generally be a better idea to create separate classes for the Bricks, Paddle, and Ball objects, etc.? Or am I just trying too hard?
- 02-09-2011, 07:30 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
- 02-09-2011, 08:53 PM #6
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Thanks, that's slightly reassuring. Not sure how familiar you are with the ACM libraries, but I'll ask the question anyways. Should I make the Brick class extend the GRect class? It seems that if I do that, I may as well just make the bricks GRects in the main program. :confused:
- 02-09-2011, 09:20 PM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Yes, you should write a Brick class that extends GRect. Among the advantages is that you can allow each Brick to know its own score value (initiated in a constructor, or calculated in a getter method based on its color), and this can make your scoring code simpler and more straightforward.
-Gary-
- 02-14-2011, 09:32 AM #8
Member
- Join Date
- Feb 2011
- Posts
- 3
- Rep Power
- 0
Perhaps this depends on the individual coder, but what's the proper way to handle the collision between the ball and brick? In a perfect world, the ball would bounce differently depending on whether it hits the side of a brick or the top/bottom of a brick. While I'm sure this is possible to implement, is it too difficult to bother with (at least at this stage in my programming knowledge)? Should I just follow Jesper's example and simply reverse the vertical velocity?
Also, because we are only checking the corners of the ball's bounding box, the ball usually only removes the leftmost brick if it hits right between two bricks. Of course, this is due to the cascading if statement in which I have checked the points clockwise from the upper left. Is this something I should worry about?
- 02-14-2011, 09:55 AM #9
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,385
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks