Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: A grid game

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Thanked 0 Times in 0 Posts

    Default A grid game

    Really confused on how to even begin this. Reply with your price please.

    In the grid game you create, the screen is divided into a grid of squares, each 20 20 pixels. The player (I will use a ladybug here as an example, but you can re-theme the game as you like) wanders around the grid collecting good things while avoiding the bad ones. If the ladybug moves onto a bad thing or off the screen, then the player loses. After collecting 5 good things, the player wins.
    The game starts when the mouse is pressed: your setUp() method can call noLoop() and you can write a mousePressed() function that calls loop() to start the game. At every point in time, the ladybug has a current direction (up, down, left, right) and moves one square in that direction every 1/3 second or so. (You can change the timing to make a more interesting game, but make sure it is easy to test). The current direction is changed by the arrow keys. Perhaps the best way to do is to check frameCount % 20 and only re-draw the screen if this is 0 (or perhaps 19, so that the ladybug doesn’t move right away). When the ladybug moves onto a square with a ”good thing” the score is increased and the ”good thing” teleports to a new square (Don’t worry if the good thing occasionally teleports onto the After collecting 5 good things (or perhaps 10 if they are worth varying amounts of points) the game ends with a “congratulation, you won!” message. If the ladybug ever moves off the screen on onto a bad thing, the game ends with a “You lost” message.
    The version you turn in should be reasonable small, say 10 squares by 10 squares (200x200 pixels). You should have about 10 bad things to run into, and their positions should be stored in an array.
    Program Specifications
    Your program must have a GridPos class. The provided sample will get you started, but you will have to add methods to the class. See the comments in the provided sample.
    The x and y instance variables should not be used directly outside of the class. Feel free to structure your program with additional classes.
    Grading: 10 pts total Required (0-8 pts)
    • (1 pt) following submission guidelines and pair programming log 1
    • (2 pts) following the program style handout, this includes: a block comment at the beginning with program description and authors, user instructions, and acknowledging any extra help; good indentation; descriptive variable names in camelCase: overall code organization and blank lines to separate different code chunks; and helpful comments indicating what each chunk is doing.
    • (3 pts) for completing and properly using the GridPos class, and using an array to hold the bad things.
    • (1 pt) for only accessing the GridPos instance variables within the GridPos class. You may use functions like verb+myPos.getX()+ that return the x-value of a GridPos object myPos.
    • (1 pt) for making sure good things don’t teleport onto either the ladybug or the bad things (use a while loop as indicated in the hints below).
    Optional parts (at most 2 optional points total)
    • (1pt)forcreatingandusingaLadybugclass(orwhateveris appropriateforyourtheme) that uses instance variables to keep track of the ladybug’s position and direction, and contains the method for printing the ladybug.
    • (1 pt) if the ladybug faces or points towards the direction it is moving.
    • (1 pt) for creating another helpful class for the program (perhaps good things and/or
    bad things).
    • (1 pt) for having the good/bad things have different positive and negative values and adjusting the score appropriately. In this case the player loses if their score drops below zero.
    • (1 pt) grader discretionary for an interesting/pleasing/fun sketch.

    This is what I have so far:

    class GridPos {
    static final int GRIDSIZE = 20;
    int x; 
    int y; 
    GridPos (int initX, int initY) {
    x = initX;
    y = initY;
    public String toString() {
    return ( "(" + x + ", " + y + ")" );
    void setTo (int xval, int yval) {
    x = xval;
    y = yval;
    boolean equals (GridPos p) {
    return (this.x == p.x && this.y == p.y);
    boolean onScreen() {
    return (x >= 0 && y >= 0 && x*GRIDSIZE < width && y*GRIDSIZE < height);
    void paint (color c) {
    GridPos myPos = new GridPos(0,0);
    void setup() {
    println("in setup");
    println("the new GridPos is: " + myPos);
    println("myPos now is; " + myPos);
    void draw() {
    println("Now in draw()");
    myPos.paint( color(0,0,255) );
    GridPos yourPos = new GridPos(1,1);
    GridPos theirPos = yourPos; 
    if (theirPos == yourPos) {
    println("TheirPos and yourPos are the same object");
    if ( myPos.equals(yourPos) ) {
    println("myPos is:" + myPos + " and yourPos is:" + yourPos);
    println(" myPos and yourPos have the same values");
    if (myPos == yourPos) {
    println("this if is false -- myPos and yourPos refer to different objects");
    GridPos[] manyPos = new GridPos[5];
    for (int i=0; i < manyPos.length; i++) {
    println("manyPos[" +i+ "] is: " + manyPos[i]);
    for (int i=0; i< manyPos.length; i++) {
    manyPos[i] = new GridPos(i,i);
    for (int i=0; i < manyPos.length; i++) {
    println("now manyPos[" +i+ "] is: " + manyPos[i]);
    for (int i=0; i < manyPos.length; i++) {
    manyPos[i].paint( color(255,0,0) );
    Last edited by helloworld922; March 5th, 2012 at 12:27 AM. Reason: please use [code] tags

  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Thanked 619 Times in 561 Posts
    Blog Entries

    Default Re: A grid game

    What issue are you running into? If you post the details of your problem (not just a homework dump), we will help you for free (as in free food). However, we will not complete your assignment for you.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Thanked 0 Times in 0 Posts

    Default Re: A grid game

    Ok. I can't get the grid to appear. I only have like 5 squares appear, and I need an entire grid to appear. Also, any tips as to how to have the lady bug appear would help.

  4. #4
    Junior Member
    Join Date
    Sep 2011
    My Mood
    Thanked 0 Times in 0 Posts

    Default Re: A grid game

    Do you still need help? I would be willing to assist you.

Similar Threads

  1. Java TV Board Game/Grid + Moving objects
    By Massaslayer in forum Java Theory & Questions
    Replies: 6
    Last Post: December 13th, 2011, 08:03 AM
  2. Grid bag layout inside grid bag layout
    By kiddkoder in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 29th, 2011, 08:07 AM
  3. [SOLVED] Grid Pattern problem:
    By mick.bhattarai in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 30th, 2010, 07:45 AM
  4. Randomizing cells in a grid
    By Flowbs in forum AWT / Java Swing
    Replies: 3
    Last Post: December 18th, 2010, 09:53 PM
  5. Grid GUI Library
    By aussiemcgr in forum Java Theory & Questions
    Replies: 7
    Last Post: September 15th, 2010, 03:30 PM

Tags for this Thread