Results 1 to 5 of 5
  1. #1
    isnie is offline Member
    Join Date
    May 2011
    Location
    Brussels
    Posts
    2
    Rep Power
    0

    Default Learning to use classes - breakout game

    Hello everyone

    I'm learning Java through the stanford CS106a course online.
    I've currently arrived at doing the breakout exersize.

    at first I finished the game to about 70% in one huge class. it got very messy codewise.

    what I want to do now is remake the game using different classes for each element.
    one for brick, wall, ball, paddle so I can clearly analyse and refine the code if needed.

    though I'm somewhat stuck with classes since I don't know exactly how to create and use them properly. I've searched some examples of other breakout code made in java but I don't quite get it.

    I'm using the ACM library for this exersize. Java Task Force Home Page

    Here what I got so far:

    one brick class

    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class Brick extends GRect {
    
    	public Brick(double w, double h, double x, double y) {
    		super(w, h, x, y);
    
    	}
    
    	private static final int APPLICATION_WIDTH = 400;
    	private static final int APPLICATION_HEIGHT = 600;
    	private static final int WIDTH = APPLICATION_WIDTH;
    	private static final int NBRICKS_PER_ROW = 10;
    	private static final int BRICK_SEP = 4;
    	private static final int BRICK_WIDTH = (WIDTH - (NBRICKS_PER_ROW - 1)
    			* BRICK_SEP)
    			/ NBRICKS_PER_ROW;
    	private static final int BRICK_HEIGHT = 8;
    
    	public void oneBrick(double x, double y) {
    		GRect brick = new GRect(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    
    	}
    }

    now I'm trying to use this brick in a different class called "world"
    to create a brick wall.

    what should this class extend? because if it doesn't extend GraphicsProgram
    I can't use add(brick); for example.

    I'm also not quite sure about the whole superclass constructor
    when to use it and when not.


    Java Code:
    import acm.graphics.*;
    import acm.program.*;
    import acm.util.*;
    import java.applet.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class world extends GraphicsProgram {
    
    	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;
    	/** 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 */
    	/** Radius of the ball in pixels */
    	private static final int BRICK_Y_OFFSET = 70;
    	private static final int BRICK_SEP = 4;
    	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;
    
    	/** Number of turns */
    
    	public world() {
    		brickWall();
    
    	}
    
    	private void brickRow(double y, Color c) {
    		double x = BRICK_SEP;
    		for (int i = 0; i < NBRICKS_PER_ROW; i++) {
    			Brick brick = new Brick(x, y, BRICK_WIDTH, BRICK_HEIGHT);
    			brick.setColor(c);
    			brick.setFilled(true);
    			add(brick);
    			x += BRICK_WIDTH + BRICK_SEP;
    
    		}
    	}
    
    	private void brickWall() {
    		double y = BRICK_Y_OFFSET;
    		Color c = Color.red;
    
    		for (int i = 0; i < NBRICK_ROWS; i++) {
    			if (i < 1) {
    				c = Color.red;
    			}
    			if (i > 1 && i < 4) {
    				c = Color.YELLOW;
    			}
    			if (i > 3 && i < 5) {
    				c = Color.ORANGE;
    			}
    			if (i > 5 && i < 8) {
    				c = Color.GREEN;
    			}
    
    			brickRow(y, c);
    			y += BRICK_HEIGHT + BRICK_SEP;
    		}
    
    	}
    
    }
    If I replace "public world() {" with a run method and use the brickWall();
    it works. however I want to use the world class in my main breakout class

    I tryed calling it in my main class like I did with the brick class

    Java Code:
    world world1 = new world();
    however I get an error that local variable is never read.
    if I then put "add (world1);" the error is gone but when I run
    my main class there is nothing to see.


    if it's just using variables in classes I somewhat get it. but when
    there is graphics involved I get very confused. I'm going to do some more reading on classes.
    but if anyone give some more info it would be very welcome.

    cheers

  2. #2
    Jodokus's Avatar
    Jodokus is online now Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    Hello Isnie,

    Let say first that I'd never heard of this course, and others here may disagree, but I'm flabbergasted. Not about your question, but about the course.
    My first reaction on reading this post was: what are these superclasses you're extending? It seems very difficult to help you when you don't use the normal Java-idiom to get things done. Then I searched the forum on "Stanford" (thought I remembered similar posts), saw some more excercises, doing things in very complicated ways. Then I googled the course: very big (282 MB and free to download, not sure if it's just this CS106a) and saw a very ADHD-professor on You-Tube telling nothing in 45 minutes (lesson 1). I read an ACM-tutorial.
    As said, I may be mistaken, but this doesn't look good. (Correct me if I'm wrong: how is your experience with this course?) Making-up your own graphics-classes (an ACM-library) and letting your students do things in a very not standard- but stanford way looks like making up your own JAVA-version, for what?
    If you make an JAVA-extension like turtle-LOGO to teach children how to draw on a computer, without the complicating things of JAVA: OK, that's reasonable. But students? Do you have to shield them from regular JAVA because it's too complicated? I don't think so. Some convenience classes OK, but extending everything from your own library, teaching a non-standard-Java approach?
    I would use this course only to get ideas on exercises, but that's all.
    Are there other people here, maybe with very positive idea's or experiences on this course?
    I may be reacting too impulsive. My opinion for a better one...
    No bug ever had to calculate its fitnessfunction.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by isnie View Post
    what I want to do now is remake the game using different classes for each element.
    one for brick, wall, ball, paddle so I can clearly analyse and refine the code if needed.
    This is a worthy goal, and I agree that you should continue to pursue this.

    Here what I got so far:

    one brick class
    ...
    Suggestions:
    • Put your class constants at the top of the class.
    • Only give Brick constants that are relevant to Brick. So for instance, it makes sense to give it a BRICK_WIDTH constant as this relates to each Brick object, but it doesn't make sense to give this class an APPLICATION_WIDTH constant as that has little to do with an individual Brick and all to do with the application which holds the Bricks.
    • Only extend a class, such as GRect, if you're going to modify one or more of its behaviors. So far your Brick class doesn't really introduce any new behaviors or states for this class, and right now you'd do just as well creating a bunch of GRect objects for your wall rather than Brick objects.
    • Your oneBrick method doesn't do anything at all. Yes it creates a brick object, but to what purpose? The object is only local to the method and thus is simply discarded never to be used as soon as the method ends.



    now I'm trying to use this brick in a different class called "world"
    to create a brick wall.
    You should adhere to naming conventions, and thus your class names, including world, should be capitalized, or "World".

    what should this class extend? because if it doesn't extend GraphicsProgram
    I can't use add(brick); for example.
    It's hard to say because most of us are not familiar with the ACM library. I suppose GraphicsProgram could work OK here, but don't know for sure.


    I'm also not quite sure about the whole superclass constructor
    when to use it and when not.
    Keep studying the tutorials and good example code to learn more about this. But in brief you need to explicitly call the super constructor if you need to call a non-default super constructor, one that needs to have parameters passed into it.

    If I replace "public world() {" with a run method and use the brickWall();
    it works. however I want to use the world class in my main breakout class
    What is a "run method"? This may be a particularity of the ACM library (or maybe not). Please clarify.


    I tryed calling it in my main class like I did with the brick class

    Java Code:
    world world1 = new world();
    however I get an error that local variable is never read.
    if I then put "add (world1);" the error is gone but when I run
    my main class there is nothing to see.
    This is not an error, but a warning. You are creating and initializing a world1 variable but never calling any methods on it. You may wish to simply do this instead:

    Java Code:
    // World world1 = new World(); // delete this
    new World(); // again World should be capitalized

  4. #4
    isnie is offline Member
    Join Date
    May 2011
    Location
    Brussels
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Jodokus View Post
    Hello Isnie,

    Let say first that I'd never heard of this course, and others here may disagree, but I'm flabbergasted. Not about your question, but about the course.
    My first reaction on reading this post was: what are these superclasses you're extending? It seems very difficult to help you when you don't use the normal Java-idiom to get things done. Then I searched the forum on "Stanford" (thought I remembered similar posts), saw some more excercises, doing things in very complicated ways. Then I googled the course: very big (282 MB and free to download, not sure if it's just this CS106a) and saw a very ADHD-professor on You-Tube telling nothing in 45 minutes (lesson 1). I read an ACM-tutorial.
    As said, I may be mistaken, but this doesn't look good. (Correct me if I'm wrong: how is your experience with this course?) Making-up your own graphics-classes (an ACM-library) and letting your students do things in a very not standard- but stanford way looks like making up your own JAVA-version, for what?
    If you make an JAVA-extension like turtle-LOGO to teach children how to draw on a computer, without the complicating things of JAVA: OK, that's reasonable. But students? Do you have to shield them from regular JAVA because it's too complicated? I don't think so. Some convenience classes OK, but extending everything from your own library, teaching a non-standard-Java approach?
    I would use this course only to get ideas on exercises, but that's all.
    Are there other people here, maybe with very positive idea's or experiences on this course?
    I may be reacting too impulsive. My opinion for a better one...

    I appreaciate your comments Jodokus. But from my personal experience with the course I can say that I'm quite happy with the way it is thought. The first lesson doesn't explain anything, but once you go onwards it gradually does take you step by step and learns you new things.
    I bought several books for learning at home but I never managed to get very far. But with this online course I stay engaged and commited. Nothing can replace a professor explaining something to you in person. And this is the closest thing that I can get to right now since I have a day job and I'm learning this purely out of intrest.

    Though I must add that the course isn't pure Java programming. It's called "programming methodology", which learns you the basic principles of OOP through Java. learning Java is almost a by product. So I think it might be understandable that in some cases you don't dive in to pure Java and they use custom libraries.

    I thought this course would be a good basis for learning OOP and then I could easily study things myself once I get the basics of OOP down.

    However, I wasn't aware that the ACM library they use is a very non standard way of programming in Java.

  5. #5
    Jodokus's Avatar
    Jodokus is online now Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    I must say I hesitated to comment this way: I don't know the course as a cursist, and you didn't ask for commenting your course. Quite possible that you learn valuable things like OOP by it, and it is good if this works for you.
    When I looked at some code on the forum and on the net I found it very confusing to see the normal Java-options that you can see shine through, being obfuscated by this shielding layer. I can tell they have put a lot of work in it, but I don't believe in the philosophy.

    Thanks for replying and much success on your JAVA-endeavours.
    No bug ever had to calculate its fitnessfunction.

Similar Threads

  1. Breakout game in the assignment 3 cs106
    By dage in forum New To Java
    Replies: 5
    Last Post: 03-16-2011, 05:07 PM
  2. Making a breakout Game
    By JavaIsSoEasy?? in forum Java Gaming
    Replies: 0
    Last Post: 03-05-2011, 10:52 AM
  3. Breaking out of Breakout! making new classes
    By sonny in forum New To Java
    Replies: 21
    Last Post: 05-05-2010, 03:29 AM
  4. Need help with Breakout game
    By tfitz666 in forum New To Java
    Replies: 9
    Last Post: 03-22-2010, 05:26 AM
  5. Breakout Game code help.
    By Ceasar in forum New To Java
    Replies: 6
    Last Post: 10-10-2009, 01:30 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •