Results 1 to 6 of 6
  1. #1
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Exclamation Gridworld based guitar hero, user part not working

    Java Code:
    /*names *Computer Science 1 AP
    *4/7/14
    *Task: Proj
    */
    
    import info.gridworld.actor.*;
    import info.gridworld.grid.*;
    import info.gridworld.gui.*;
    import info.gridworld.world.World;
    import java.awt.*; 
    import java.util.*;
    import javax.imageio.*;
    import java.awt.image.*;
    import java.io.*;
    import java.applet.*;//rec added
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    
    
    public class Runner
    {
    Score gif = new Score();
    Toolkit toolkit;
    Scanner sc =new Scanner(System.in);
    KeyListenerForGame x=new KeyListenerForGame();
    private BufferedImage img;
    Timer timer;
    String s;
    static newWorld world = new newWorld();
    public Runner() {
    toolkit = Toolkit.getDefaultToolkit();
    timer = new Timer();
    
    timer.schedule(new RemindTask(), 5000, //initial delay
    1 * 1000); //subsequent rate
    }
    
    
    class RemindTask extends TimerTask {
    int numWarningBeeps = 20;
    
    
    public void run() {
    
    try {
    img = ImageIO.read(new File(
    "Background.PNG"));
    } catch (IOException e) {
    e.printStackTrace();
    } 
    
    if (numWarningBeeps > 0) {
    toolkit.beep();
    //System.out.println("Beep!");
    numWarningBeeps--;
    
    if(numWarningBeeps %1==0)
    {
    world.add(new Location(8,0), new Red());
    //System.out.println("red Added");
    }
    
    if(numWarningBeeps%2==0)
    {
    //System.out.println("green Added");
    world.add(new Location(8,1), new Green());
    }
    
    if(numWarningBeeps%5==0)
    {
    //System.out.println("yellow Added");
    world.add(new Location(8,2), new Yellow());
    }
    
    if(numWarningBeeps%10==0)
    {
    world.add(new Location(8,3), new Blue());
    //System.out.println("blue Added");
    }
    System.out.println("Please enter A,S,D,F");
    s=sc.nextLine();
    s.toUpperCase();
    world.keyPressed(s);
    /////////////////////////////////////////////////////////
    //f.addScore(50);// only added because it i was testing the key listener
    //System.out.println("Score:"+ gif.getScore());// it dosnt work
    world.show();
    /////////////////////////////////////////////////////////
    }
    else {
    toolkit.beep();
    System.out.println("Time's up!");
    System.out.println("Score:"+ gif.getScore());
    //timer.cancel(); //Not necessary because we call System.exit
    System.exit(0); //Stops the AWT thread (and everything else)
    }
    }
    }
    
    
    public static void main(String args[]) {
    System.out.println("About to schedule task.");
    
    Runner r = new Runner();
    System.out.println("Task scheduled.");
    world.show();
    }
    }
    
    
    class newWorld extends ActorWorld
    {
    Scanner sc =new Scanner(System.in);
    //String s;
    public void keyPressed(String s)
    { 
    Score f = new Score();
    
    //the string s will give you the letter or key pressed
    
    
    Red r = new Red();
    Green g = new Green();
    Yellow y = new Yellow();
    Blue b = new Blue(); 
    
    Location red = new Location (9,0);
    Location green = new Location (9,1);
    Location yellow = new Location (9,2); 
    Location blue = new Location (9,3);
    //if(s.equals("A"))
    //System.out.println((r.getLocation()).equals(red));
    if(s.equals("A") && (r.getLocation()).equals(red))
    {
    f.addScore(25); 
    System.out.println("****0 ");
    } 
    if (s.equals("S") && (g.getLocation()).equals(green))
    {
    f.addScore(50);
    System.out.println("****1 ");
    }
    if(s.equals("D") && (y.getLocation()).equals(yellow))
    {
    f.addScore(100);
    System.out.println("****2 "+f.getScore());
    }
    if (s.equals("F") && (b.getLocation()).equals(blue))
    {
    f.addScore(150);
    System.out.println("****3 "+f.getScore());
    }
    
    //return true;
    }
    }
    Java Code:
    /*Computer Science 1 AP *4/7/14
    *Task: Proj
    */
    
    
    import info.gridworld.actor.*;
    import info.gridworld.grid.*;
    import java.awt.*; 
    import java.util.*;
    
    
    public class Blue extends Bug
    {
    private int counter;
    public Blue() 
    {
    counter=0;
    setColor(Color.BLUE);
    setDirection(Location.SOUTH); 
    }
    public void act()
    {
    int d=getGrid().getNumRows();
    int c=0;
    if(c<d)
    move();
    else 
    removeSelfFromGrid(); 
    }
    public void move() 
    { 
    Grid<Actor> gr = getGrid(); 
    if (gr == null) 
    return; 
    Location loc = getLocation(); 
    Location next = loc.getAdjacentLocation(getDirection()); 
    if (gr.isValid(next)) 
    moveTo(next); 
    else 
    removeSelfFromGrid(); 
    } 
    }
    Java Code:
    /*Computer Science 1 AP *4/7/14
    *Task: Proj
    */
    
    
    import info.gridworld.actor.*;
    import info.gridworld.grid.*;
    import java.awt.*; 
    import java.util.*;
    
    
    public class Green extends Bug
    {
    private int counter;
    public Green() 
    {
    counter=0;
    setColor(Color.GREEN);
    setDirection(Location.SOUTH); 
    }
    public void act()
    {
    int d=getGrid().getNumRows();
    int c=0;
    if(c<d)
    move();
    else 
    removeSelfFromGrid();
    
    }
    public void move() 
    { 
    Grid<Actor> gr = getGrid(); 
    if (gr == null) 
    return; 
    Location loc = getLocation(); 
    Location next = loc.getAdjacentLocation(getDirection()); 
    if (gr.isValid(next)) 
    moveTo(next); 
    else 
    removeSelfFromGrid(); 
    
    } 
    }
    Java Code:
    /*Computer Science 1 AP *4/7/14
    *Task: Proj
    */
    
    
    import info.gridworld.actor.*;
    import info.gridworld.grid.*;
    import java.awt.*; 
    import java.util.*;
    
    
    public class Red extends Bug 
    {
    private int counter;
    
    public Red()
    {
    setColor(Color.RED);
    setDirection(Location.SOUTH); 
    }
    public void act()
    {
    int d=getGrid().getNumRows();
    int c=0;
    if(c<d)
    move();
    else 
    removeSelfFromGrid();
    
    }
    public void move() 
    { 
    Grid<Actor> gr = getGrid(); 
    if (gr == null) 
    return; 
    
    Location loc = getLocation(); 
    Location next = loc.getAdjacentLocation(getDirection()); 
    
    if (gr.isValid(next)) 
    moveTo(next); 
    else 
    removeSelfFromGrid(); 
    } 
    }
    Java Code:
    /*Computer Science 1 AP *4/7/14
    *Task: Proj
    */
    
    
    import info.gridworld.actor.*;
    import info.gridworld.grid.*;
    import java.awt.*; 
    import java.util.*;
    
    
    public class Yellow extends Bug
    {
    private int counter;
    public Yellow() 
    {
    setColor(Color.YELLOW);
    setDirection(Location.SOUTH); 
    }
    public void act()
    {
    int d=getGrid().getNumRows();
    int c=0;
    if(c<d)
    move();
    else 
    removeSelfFromGrid();
    }
    public void move() 
    { 
    Grid<Actor> gr = getGrid(); 
    if (gr == null) 
    return; 
    Location loc = getLocation(); 
    Location next = loc.getAdjacentLocation(getDirection()); 
    if (gr.isValid(next)) 
    moveTo(next); 
    else 
    removeSelfFromGrid(); 
    
    } 
    }
    Thank you so much for helping!
    Even though I enter a,s,d, or f when the tile is at that location the the score is not updated. I tried using == and .equals for the locations.

    (download the zip and add the jar file into the file directory)
    http://apcentral.collegeboard.com/ap...dWorldCode.zip
    Last edited by JosAH; 06-06-2014 at 07:58 PM. Reason: changed <code> to [code] tags

  2. #2
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Default Re: Gridworld based guitar hero, user part not working

    I tried using keylistner but that is not working, i spent about 5 hours fiddling with the code with no results
    Last edited by JosAH; 06-06-2014 at 07:56 PM.

  3. #3
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,365
    Rep Power
    6

    Default Re: Gridworld based guitar hero, user part not working

    Instead of fiddling (which is the exact opposite of what you should be doing, no wonder you get nowhere), do the most easy debugging trick in the book; pepper your code with System.out.println() statements. Print out the values of the variables you're using, see which code is executed and which code is not; and in which order. Try to reason where things go wrong - it is the point where you expect A to happen, but instead B happens.

    Also a little tip: don't be typing-lazy. Your code is written to be a huge bug-fest. The main culprit is your tendency to not write accolades; always write them. So not this:

    Java Code:
    if (gr == null) 
         return; 
         Location loc = getLocation(); 
         Location next = loc.getAdjacentLocation(getDirection());
    but this:

    Java Code:
       if (gr == null) {
           return; 
       }
    
       Location loc = getLocation(); 
       Location next = loc.getAdjacentLocation(getDirection());
    I hope you agree that is much easier to read and thus also easier to judge on its correctness.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  4. #4
    765891 is offline Member
    Join Date
    Jan 2013
    Location
    'Murica
    Posts
    25
    Rep Power
    0

    Default Re: Gridworld based guitar hero, user part not working

    Quote Originally Posted by gimbal2 View Post
    Instead of fiddling (which is the exact opposite of what you should be doing, no wonder you get nowhere), do the most easy debugging trick in the book; pepper your code with System.out.println() statements. Print out the values of the variables you're using, see which code is executed and which code is not; and in which order. Try to reason where things go wrong - it is the point where you expect A to happen, but instead B happens.

    Also a little tip: don't be typing-lazy. Your code is written to be a huge bug-fest. The main culprit is your tendency to not write accolades; always write them. So not this:

    Java Code:
    if (gr == null) 
    return; 
    Location loc = getLocation(); 
    Location next = loc.getAdjacentLocation(getDirection());
    but this:

    Java Code:
    if (gr == null) {
    return; 
    }
    
    Location loc = getLocation(); 
    Location next = loc.getAdjacentLocation(getDirection());
    I hope you agree that is much easier to read and thus also easier to judge on its correctness.

    Hello Kind sir,
    I do agree, this was a a group project (I didn't do that part)
    I'm having a issue with the if statements on the first part, the keylistener is working but code past the && conditon is not working


    C:\Users\Owner\Downloads\roland\MRGUERRAGARAGEBAND Updated\Runner.java:119: error: illegal start of expression
    if(s.equals("A")) && Red().getLocation().equals(red))
    ^
    C:\Users\Owner\Downloads\roland\MRGUERRAGARAGEBAND Updated\Runner.java:119: error: ';' expected
    if(s.equals("A")) && Red().getLocation().equals(red))
    ^
    Last edited by JosAH; 06-06-2014 at 07:56 PM.

  5. #5
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,095
    Rep Power
    4

    Default Re: Gridworld based guitar hero, user part not working

    Don't delete your posts. It doesn't make sense for people who get here via Google or the likes. If you don't want your code to be public, then don't come to a public Java forum. Otherwise, leave it as it is.
    Last edited by JosAH; 06-06-2014 at 08:05 PM.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Gridworld based guitar hero, user part not working

    I put back the original contents of all the OPs posts in this thread for reasons you already mentioned. Some people don't realize they're communicating with other people on 'teh net' and it isn't just them against some computer. Maybe even more important, they don't realize they leave a mess behind if they wipe their part from the communication ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Timed object generation/ for guitar hero game
    By 765891 in forum New To Java
    Replies: 4
    Last Post: 05-22-2014, 02:52 PM
  2. Replies: 0
    Last Post: 12-14-2011, 10:08 PM
  3. Replies: 3
    Last Post: 08-29-2011, 02:54 AM
  4. Replies: 2
    Last Post: 08-05-2010, 05:49 AM

Tags for this Thread

Posting Permissions

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