This week's book giveaway is in the NodeJS forum.
We're giving away four copies of Serverless Applications with Node.js and have Slobodan Stojanovic & Aleksandar Simovic on-line!
See this thread for details.
Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

Early stages of a working version of Breakout - where to go from here?  RSS feed

 
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, again.

I've watched 11 lessons of the Stanford CS106A online now and, with the help of the course book, I was able to produce a rudimentary working version of the Breakout game, similar to what is stated in the assignment paper.

Searching through the JavaRanch forums, I find some older posts reffering to this particular course and assignement, but none of them relates to my "existential" pains and aches.

I still have to go through the code in order to clean it up and make it less of a mess readable, but I have a lot of concepts that I would like to implement that go beyond the initial stated assignement.  

Being the purpose of the assignement teaching basic coding design patterns and techniques (rather than teaching how to program Breakout), I would like to ask for your input on the following matters:

The game is coded using a single class with multiple methods inside. Each method is dedicated to a single unitary purpose in the game (1 method to setup the board, 1 method to draw a brick, 1 method to set a line of bricks, 1 method to manage movement, etc).

1 - If I was to add additional levels (with different brick layouts) to the program, what would be a good aproach? 1 method per level? 1 class per level? Probaby, some of these levels would have to be almost built brick-by-brick, without using the "line-of-bricks" method.

2 - The same for bricks. If I was to add different kinds of bricks (in size, in shape - squares, tetris forms etc), how/where should I code them? A method? A class?

3... and 4 - The ball is round. Or is it? What we see is a circle, but the coordinate mechanism that orients the instantiation of an Oval object uses a rectangle placed around the Oval. All of the collision mechanism used to detect contact between ball and bricks is based on the upper, lower, left and right sides of that rectangle and on the illusion provided by movement that the ball is in fact hitting the bricks. However, if, say, I was to put two or more balls bouncing around (or, even better, if I wanted to code a snooker game), and if I wanted to manage the collisions between balls, this implemetations would fall short, because of all the new possible angles of contact not being taken into account (ex: one ball colliding with another at a 45 (first ball) to 225 (second ball) angle degree). This allready happens in part with a single ball and a brick, but it is not an important issue to correct at this stage - I mean, the game works fine as it is.

Given that some of the libraries (acm) used in this course and examples are confined to teaching purposes, and that the object library available might be a little bit old, which libraries/techologies/tools would now be a good choice as to "update" this example? JavaFX? Is there a "round ball" (hehe) available somewhere inside those libraries, with ready methods to getting angles and so?  

Rephrasing: If I was to build such a game from scratch using nowadays Java tools, where would I want to go?

Thanks in advance for any insight/tip.
 
Ranch Hand
Posts: 1325
3
Java Netbeans IDE Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

If I was to add additional levels (with different brick layouts) to the program, what would be a good aproach? 1 method per level?



In my opinion, one unique method addBrick() should be correct in the case that all the bricks equal.

The same for bricks. If I was to add different kinds of bricks (in size, in shape - squares, tetris forms, etc), how/where should I code them? A method? A class?



In this case, you could add to the previous method several parameters of (size, shape) and call it with different values when needed. If you don't know in advance the type and number of the parameters, you could overload the method. This allows you to add a different number and type of parameters to your method. A Brick Class should contain the method addBrick(), and another class should behave as a runnable containing a run() method. The  "BrickRunable" class makes an instance of the Brick Class and calls the addBrick() method.
 
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ricky,

my very first thoughts (I've played the game, but never programmed it):

1)  I would use an array[][] (or a List<List>) for this purpose. An element at x, y of 0 or false or whatever would indicate no brick. That makes for an easy level layout.

2) I would most certainly create a Brick class, with fields Color, nrOfHitsBeforeDisappearing, width and heigth, isDestroyed, et cetera. Giving bricks different sizes might complicate  1) a little, but a simple approach could be to display a brick only if its location on screen is not yet already taken by some other brick. Wonder if that would leave some unacceptible holes in the layout, though.

3) Sofar I've used the bounding box method myself. If that is too course however, then use the distance, radius and angle of approach of the ball(s) to determine whether there is a collision between balls or ball/brick. That complicates the formulas, so, as you said, if it is not a big problem, I would leave that for now.

I once had a version that was written in Basic (BBC Basic V, with a 5 byte floating point), and it suffered sometimes from getting into an infinite loop, when the ball was rapidly bouncing between two close bricks.

Edit: I forgot: I only know Swing a little, but if I was to do it all over again, I would certainly go for JavaFX, with its much nicer effects and possibilities.

 
Ricky Bee
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, Piet and Angus.


Thank your for your time and insights.

I will certainly give some thought to your suggestions. I will probably go for a multiple class approach, thinking about different kinds of bricks, layouts, levels, balls, paddles, etc. I'll try and see how it goes - if anything, I'll learn something in the process.  

3) Sofar I've used the bounding box method myself. If that is too course however, then use the distance, radius and angle of approach of the ball(s) to determine whether there is a collision between balls or ball/brick. That complicates the formulas, so, as you said, if it is not a big problem, I would leave that for now.  



Yes, formulas will get from simple and straightforward to something resembling rocket-science. Specialy the ones related to the physics of motion (that is, what happens to objects after collisions). I guess I'll put this one on hold for now.  
 
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do rewrite your code in JavaFX, you can use this as a way of detecting shape collision, not just bounds collision:
Perhaps Swing has something similar?
 
Ricky Bee
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey, Knute.

Thanks for the tip. That was exactly the sort of thing I was asking about. I'm still in the fisrt steps of a long Java-learning process and I know nothing at all about JavaFX and Swing. In a not much distant future (I hope), maybe I will come back to this topic to read again your post.

This Breakout-type app was coded with resources available in both the awt and acm libraries, the latter of which is said to be used mainly in teaching contexts. It even runs inside an Applet...



"The thing" presently looks like this:  

 
Knute Snortum
Sheriff
Posts: 5813
150
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An Applet, huh?  Those aren't used outside of the classroom, so redoing the code in Swing or JavaFX would make sense.  I'd vote for JavaFX, but there is some differing opinions on that.
 
Piet Souris
Rancher
Posts: 3127
110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks nice! Is it true that the ball slows down (to us, spectators), when it approaches the event horizon?   ;)

Swing has a Shape.intersects method, but that only considers rectangles. For instance:

JavaFX has more options. I'll give it a look if I can get javaFX running.
 
Ricky Bee
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:An Applet, huh?  Those aren't used outside of the classroom, so redoing the code in Swing or JavaFX would make sense.  I'd vote for JavaFX, but there is some differing opinions on that.



Yes, indeed. I was fully aware of the Applet "limitation" before I started the lectures. My main goal regarding this course is to learn how to think in a OO-logic coding environment, and the fact that I got to code a Breakout-like little game was a value-added bonus (Arkanoid II was the first Spectrum game I ever owned, so this project has also a kind of emotional value attached).

But yes - I intend to recode this in Swing or JavaFX somewhere along the path. It still does make sense to learn both these technologies, right? (instead of putting my money in only one of them - which would by JavaFX, in case of choice).


 
Ricky Bee
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Piet Souris wrote:Looks nice! Is it true that the ball slows down (to us, spectators), when it approaches the event horizon?   ;)



The ball does slow down sometimes, but I suspect it is due both to processor occupancy and poor coding skills...    


Thank you for your interest in the matter.     I will look into your code, study it to figure stuff out, but a good portion of it it still "out of my reach".
 
No matter. Try again. Fail again. Fail better. This time, do it with this tiny ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!