patternjavaMinor
Building a Breakout game from scratch
Viewed 0 times
scratchbuildinggamefrombreakout
Problem
I'm a beginner, currently taking the Stanford's free CS106A course which teaches Java. The second assignment has you build Breakout from scratch using the ACM programs and graphics packages.
Since it's an online course and I don't have anyone who can review my code, I thought I might ask you what newbie mistakes I'm making. I'm sure there are a lot.
I had some trouble figuring out where to stick some of the behavior. I have a
I also could manage to get the main program's instance from anywhere, only its class, which wasn't what I needed. Any ideas on how to do this? Other instances were problems, too. For instance, is there any way for me to get the
I wasn't sure about where to use private/public and static. I feel as though a lot of my uses were workarounds.
How would you have built this differently? What huge mistakes should I stop making? Is it organized awfully?
Main program:
```
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Breakout extends GraphicsProgram {
public void init() {
setBricks();
add(paddle);
add(ball,WIDTH/2,500);
lifeBar.setColor(Color.WHITE);
add(lifeBar,20,60);
addMouseListeners();
setBackground(Color.BLACK);
}
public void run() {
while(gameOver != true) {
pause(20);
checkCollision(ball);
ball.move();
if (prize != null) {
prize.drop();
if (prize.leftScreen()) remove(prize);
}
}
addMouseListeners();
}
private void setBricks() {
for (int row = 0; row = WIDTH) {
ball.setLocation(WIDTH-r,y
Since it's an online course and I don't have anyone who can review my code, I thought I might ask you what newbie mistakes I'm making. I'm sure there are a lot.
I had some trouble figuring out where to stick some of the behavior. I have a
Ball class within which I wanted to put the collision detection and reaction, but ended up having to put it in the main program, because ACM wouldn't let me check the canvas around the ball from within the ball. I also could manage to get the main program's instance from anywhere, only its class, which wasn't what I needed. Any ideas on how to do this? Other instances were problems, too. For instance, is there any way for me to get the
Paddle's instance within the Ball without passing the paddle into the constructor? Is there any way to call it?I wasn't sure about where to use private/public and static. I feel as though a lot of my uses were workarounds.
How would you have built this differently? What huge mistakes should I stop making? Is it organized awfully?
Main program:
```
import acm.graphics.*;
import acm.program.*;
import acm.util.*;
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Breakout extends GraphicsProgram {
public void init() {
setBricks();
add(paddle);
add(ball,WIDTH/2,500);
lifeBar.setColor(Color.WHITE);
add(lifeBar,20,60);
addMouseListeners();
setBackground(Color.BLACK);
}
public void run() {
while(gameOver != true) {
pause(20);
checkCollision(ball);
ball.move();
if (prize != null) {
prize.drop();
if (prize.leftScreen()) remove(prize);
}
}
addMouseListeners();
}
private void setBricks() {
for (int row = 0; row = WIDTH) {
ball.setLocation(WIDTH-r,y
Solution
I haven't tried to get into the high level design of your game but here are a few details you might want to fix.
-
-
You can use the ternary operator to use setLabel only once :
-
I guess the code to check elements can be factorised.
-
Using
-
You probably want to store your magic numbers in constant variables to make things easier to understand.
-
The way you initialise direction in the Ball constructor is weird (and the way you change direction is pretty awkward as well)
-
There's probably a problem in the
-
The
-
if (won == true) can be written if (won)-
You can use the ternary operator to use setLabel only once :
gameOverLabel.setLabel((lives == 3)?"HOLY SHIT PERFECT SCORE!!!":"WOO YOU WON!!!");-
I guess the code to check elements can be factorised.
-
Using
instanceof is usually a bad thing.-
You probably want to store your magic numbers in constant variables to make things easier to understand.
-
The way you initialise direction in the Ball constructor is weird (and the way you change direction is pretty awkward as well)
-
There's probably a problem in the
decreaseBricks() method : you might want to do the test in the other order and separated by else and you can simply return (bricksRemaining > 0) (maybe returning bricksRemaining would work but I just can't remember how it works in Java)-
The
leftScreen() method could be a return ;.Context
StackExchange Code Review Q#7865, answer score: 4
Revisions (0)
No revisions yet.