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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 13 of 13

Thread: Fixing of bug for simple GreenFoot game.

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Fixing of bug for simple GreenFoot game.

    Hey guys,

    I'm new here and require your expertise. Hopefully somebody can help me.

    I am programming a very simple game in a program called GreenFoot. Basically, a bug can fall down platforms and must reach the end to win the game. It can jump over flames but if it gets eaten by a flame (touches a flame) then the game will be over. Very simple.

    The only controls the user can use to move the bug are UP, RIGHT and LEFT.

    Problem
    The problem i am having is that, i am trying to program my game so that if ANY key other than the RIGHT, LEFT or UP key are pushed, i want a sound to play.

    Here is the code used for getting a key that the user has pressed:

       [COLOR=Red] private boolean keyPressed() {                                     
            String key = Greenfoot.getKey();                               
     
            if (key=="right") {                      
                if (direction==LEFT) {                                     
                    setRotation(0);
                }
                direction=RIGHT;                                            
                vertdirection=RIGHT;
                return true;
            }
     
            else if (key=="left") {
                if (direction==RIGHT) {                                     
                    setRotation(180);
                }
                direction=LEFT;                                             
                vertdirection=LEFT;
                return true;
     
            }
     
            else if (key=="up") {
                if (direction==UP) {                                     
                    setRotation(0);
                }
     
                direction=UP;                                             
                return true;
            }
     
            else  {
                return false;
            }
        }[/COLOR]

    I'm fairly sure it will go in there somewhere most likely in the else part of this method.

    I'm really new to Java and have no idea so if anyone could help me, i'd appreciate it extremely!

    Thanks very much for your time and thank you in advance.
    Last edited by Deep_4; November 7th, 2012 at 01:24 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Little problem

    Hello Nkay11 and welcome to the Java programming forums

    Are you looking to find out how to play a sound? Or are you trying to figure out where abouts in the code to insert this?

    I think the code will need to go here:

        private boolean keyPressed() {                                     
            String key = Greenfoot.getKey();                               
     
            if (key=="right") {                      
                if (direction==LEFT) {                                     
                    setRotation(0);
                }
                direction=RIGHT;                                            
                vertdirection=RIGHT;
                return true;
            }
     
            else if (key=="left") {
                if (direction==RIGHT) {                                     
                    setRotation(180);
                }
                direction=LEFT;                                             
                vertdirection=LEFT;
                return true;
     
            }
     
            else if (key=="up") {
                if (direction==UP) {                                     
                    setRotation(0);
                }
     
                direction=UP;                                             
                return true;
            }
     
            else  {
         [B]//Play sound code here[/B]      
         return false;
            }
        }

    This part of the code is executed when 'key' doesn't equal UP, LEFT or RIGHT.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Little problem

    Thanks for replying, i tried this but the beep just constantly plays. I now have the beep playing where it should play but the bug constantly moves :S Here is the code so far.

    private boolean keyPressed() {                                      // this is the method for moving the Bug when the key is pressed
            String key = Greenfoot.getKey();                                //check whether a key has been pressed
     
            if (key=="right") {                      
                if (direction==LEFT) {                                      // if the key is pressed right, check if the direction is left and if it is, set the rotation to 0
                    setRotation(0);
                }
                direction=RIGHT;                                            // if the direction is right, return true
                vertdirection=RIGHT;
                return true;
            }
     
            else if (key=="left") {
                if (direction==RIGHT) {                                     
                    setRotation(180);
                }
                direction=LEFT;                                             
                vertdirection=LEFT;
                return true;
     
            }
     
            else if (key=="up") {
                if (direction==UP) {                                     
                    setRotation(0);
                }
     
                direction=UP;                                             
                return true;
            }
     
            else  {
               if(key == null) {                                        // this checks to see if no key has been pressed
               return false;
            }
     
            Greenfoot.playSound("beep.wav");
            return true; 
          }
        }

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Little problem

    I'm not sure if this will work because obviousally I can't compile it but try:

    private boolean keyPressed() {
            String key = Greenfoot.getKey();
     
            if (key=="right") {
                if (direction==LEFT) {
                    setRotation(0);
                }
                direction=RIGHT;                                            
                vertdirection=RIGHT;
                return true;
            }
     
            else if (key=="left") {
                if (direction==RIGHT) {                                     
                    setRotation(180);
                }
                direction=LEFT;                                             
                vertdirection=LEFT;
                return true;
     
            }
     
            else if (key=="up") {
                if (direction==UP) {                                     
                    setRotation(0);
                }
     
                direction=UP;                                             
                return true;
            }
     
            else  {
               if(key == null) {                                       
               return false;
            }
     
       [B] else{[/B]
     
            Greenfoot.playSound("beep.wav");
       [B] }[/B]
            return true; 
     
          }
        }
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  5. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Little problem

    Hey,

    Thanks for replying again Really appreciate the help your giving me.

    The code you just gave me does exactly the same as the code i had before. It works in just the same way.
    It's also very interesting to note that when the game is run. If you press a key which is NOT UP, LEFT or RIGHT, it will NOT MOVE but after you have pressed an arrow key and then press another key, that is when the bug begins to move no matter which key is pressed :/

    It seems to me as though the bug isn't remembering it's state after an arrow key has been pressed because everything works fine until you press an arrow :/

    Any ideas?

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: Little problem

    Hmm.. its really hard for me to guess the problem without seeing the entire code.

    Can you post it here so I can attempt to compile it?
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  7. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Little problem

    Sure!

    This is the code for one of the objects. The bug.

    import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
     
    import java.util.List;
    import java.util.ArrayList;
     
     
    /**
     * This is the Bug.
     * It is controlled by the user and must reach the exit by falling down platforms avoiding flames by jumping over them or missing them.
     * 
     * @author Nigel Kay 
     * @version 1.0
     */
     
     
    public class Bug extends Actor
    {
     
        private static final int RIGHT = 1;
        private static final int LEFT = -1;
        private static final int UP = 2;
        private static final int DOWN = 3;
     
        int direction = 0;
        int jumpCount = 0;
        int vertdirection = 0;
        boolean jumping = false;
     
     
        public Bug() {
     
        }
     
        /**
         * Act - This is the method which is run constantly when the environment is run.
         * It checks to see if the Bug is not on a platform. If it is not then it will fall.
         * It then checks to see if the bug has reached the end of the game and if it has, a message will display and a sound will play. The simulation will also stop.
         * If it has found a flame, a sound will play and the simulation will stop.
         * If none of the above, it checks to see if a key has been pressed and if it has, if the direction is up, it will change jumping to true.
         * If a key has been pressed and it is not up then it will move using the move method.
         */
     
        public void act() 
        {
           if(!onPlatform()) {                               // if the bug is not on a platform or not at the bottom then it should fall
                fall();          
            }
     
           else if(atEnd()) {
            Message m1 = new Message();
            getWorld().addObject(m1, 8, 7);
            Greenfoot.playSound("completed.wav");
            removeBug();
            Greenfoot.stopSimulation();
            }                    
     
           else if(foundFlame()) {
               Message2 m2 = new Message2();
               getWorld().addObject(m2, 8, 7);
               Greenfoot.playSound("gameover.wav"); 
               removeBug();
               Greenfoot.stopSimulation();
            }
     
           else {
                if(keyPressed()){                                           // if a key is pressed and it is the up key then jump but if not, move
                    if(direction == UP) {       
                        jumping=true;
                    }
                    move();
                }
            }
        }
     
        /**
         * onPlatform - This is the method that checks to see if the flame is on a platform.
         * It gets the current co-ordinates of the Bug and checks below it one space to see if the Bug is on a platform.
         * If there is a no block there, it returns false, if not it returns true.
         */
     
        private boolean onPlatform() {
            World myWorld = getWorld();
            int x = getX();                                                 
            int y = getY();                                                 
            if(myWorld.getObjectsAt(x, y+1, Block.class).isEmpty()) {       // check to see if there is a block below the bug
                return false;
            }
            else {
                return true;
            }
        }
     
        /**
         * canMove - This is the method that checks to see if the bug can move.
         * It checks to see whether the bug has reached the outside borders and if it has, return false to stop it from moving.
         * If the direction is down and the bug is on a platform, return false. (This is also for the end of the jump)
         */
     
         private boolean canMove()
         {
            World myWorld = getWorld();
            int x = getX();
            int y = getY();
            if ((x > myWorld.getWidth()) || y > myWorld.getHeight()) {
                return false;
            }
            else if (x < 0 || y < 0) {
                return false;
            }
            else if (direction==DOWN && onPlatform()){                     //  is it at end of jump and on platform
                return false;
            }
            else {
                return true;
            }
        }
     
        /**
         * atEnd - This method simply checks to see if the Bug has reached the co-ordinates 15, 15 and if it is, return true. (This is the exit square)
         */
     
        private boolean atEnd()
        {
            int x = getX();             
            int y = getY();             
            if(x == 15 && y == 15){       
                return true;                 
            }
            else {                           
                return false;             
            }
        }
     
        /**
         * Move - This method checks to see if the Bug is jumping and if it is, it will prepare the jump using the prepareJump method.
         * If then checks to see if it can move and if it can, it will get the current direction of the Bug and move it in the current direction one space.
         */
     
        private void move(){
     
            if(jumping){
                 prepareJump();
            }
     
            if(canMove()) {
     
                switch(direction) {
                case DOWN :
                    setLocation(getX(), getY() + 1);
                    break;
                case RIGHT :
                    setLocation(getX() + 1, getY());
                    break;
                case UP :
                    setLocation(getX() + vertdirection, getY() - 1);
                    break;
                case LEFT :
                    setLocation(getX() - 1, getY());
                    break;
            }
        }
        }
     
        /**
         * keyPressed - This method checks to see if a key has been pressed.
         * It will firstly, get the key which has been pressed and check the current direction of it. If it needs rotating to change its direction, it will also set the direction.
         * It also sets the vertical direction so that the jump method functions correctly.
         */
     
        private boolean keyPressed() {                                      // this is the method for moving the Bug when the key is pressed
            String key = Greenfoot.getKey();                                //check whether a key has been pressed
     
            if (key=="right") {                      
                if (direction==LEFT) {                                      // if the key is pressed right, check if the direction is left and if it is, set the rotation to 0
                    setRotation(0);
                }
                direction=RIGHT;                                            // if the direction is right, return true
                vertdirection=RIGHT;
                return true;
            }
     
            else if (key=="left") {
                if (direction==RIGHT) {                                     
                    setRotation(180);
                }
                direction=LEFT;                                             
                vertdirection=LEFT;
                return true;
     
            }
     
            else if (key=="up") {
                if (direction==UP) {                                     
                    setRotation(0);
                }
     
                direction=UP;                                             
                return true;
            }
     
            else if(key == null) {                                        // this checks to see if no key has been pressed
               return false;
            }
     
            else {
            Greenfoot.playSound("beep.wav");
            return true; 
          }
     
        }
     
        /**
         * Fall - This method checks to see if the Bug can move and if it can move, it adds one to the y axis allowing the bug to move down to the next platform.
         */
     
        private void fall(){
            World myWorld = getWorld();
            int x = getX();                                                 
            int y = getY();                                                
            if (canMove()) {                                 
                y++;                                                        // if the bug can move, allow it to fall on the y axis and then set its location based on its current co-orindates
                setLocation(x,y);
            }
        }
     
        /**
         * prepareJump - This method describes how the Bug will jump.
         * If the jumpCount is between 0 and 1, it will go up and when the jump count reaches between 3 and 4 it will come back down.
         * After the jump has been generated and the Bug is back down, the jumpcount is set back to 0 and jumping is set back to false.
         */
     
        private void prepareJump(){
            if (jumpCount >=0 && jumpCount <=1){
                direction=UP;
            }
            else if (jumpCount >=3 && jumpCount <=4){
                direction=DOWN;
            }        
            else {
                vertdirection=0;
            }
            if(jumpCount==4){
                jumpCount=0;
                vertdirection=0;
            }
            else{
                jumpCount++;
                jumpCount=0;
                jumping=false;
            }
        }
     
        /**
         * foundFlame - This method checks to see if the Bug is in the same square as a flame and if it is, it will return true but false if not.
         */
     
        public boolean foundFlame()
        {
            Actor flame = getOneObjectAtOffset(0, 0, Flame.class);
            if(flame != null) {
                return true;
            }
            else {
                return false;
            }
        }
     
        /**
         * removeBug - This method simply removes the Bug from the playing world.
         */
     
       public void removeBug()
       {
            World myWorld = getWorld();
            myWorld.removeObject(this);
        }
     
     
     
    }

  8. #8
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Little problem

    I don't know how this function has been implemented
    Greenfoot.getKey();
    But you are checking to see if it's null. So depending how that function returns it might not be null.

    Also when checking a String against another String it is better to use,
    String.equals("astring");

    Cheers,
    Chris

  9. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Little problem

    Quote Originally Posted by Freaky Chris View Post
    I don't know how this function has been implemented
    Greenfoot.getKey();
    But you are checking to see if it's null. So depending how that function returns it might not be null.

    Also when checking a String against another String it is better to use,
    String.equals("astring");

    Cheers,
    Chris
    Greenfoot.getKey() is a command for checking to see if a key has been pressed. I am using a program called Greenfoot which is a java environment.

    I checked for null because unless that is in there, it constantly beeps whether a key has been pressed or not

    Thanks for your input. Appreciate it.

    I really need to try and get the bug to stop moving when a key other than UP, LEFT or RIGHT is pressed, i just really can't work out how to do it.

  10. #10
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Little problem

    Oh sorry I thought that was what you were having problems with. I though you ment even with that check it was still bleeping, I do apologise.

    To fix your problem you want this,
    else {
    Greenfoot.playSound("beep.wav");
    direction = 0;
    return true; 
    }

    I think anyways.

    Cheers,
    Chris

  11. #11
    Junior Member
    Join Date
    Apr 2009
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Little problem

    Quote Originally Posted by Freaky Chris View Post
    Oh sorry I thought that was what you were having problems with. I though you ment even with that check it was still bleeping, I do apologise.

    To fix your problem you want this,
    else {
    Greenfoot.playSound("beep.wav");
    direction = 0;
    return true; 
    }

    I think anyways.

    Cheers,
    Chris
    That works a treat!
    Thank you so much, i really do appreciate it loads!

    I'll be using this forum more often me thinks

    Cheers guys!

  12. #12
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Little problem

    Glad we could help, hope you stick around.

    We'll always be here.
    Could I kindly ask you to mark this thread as solved, if you are unsure how to check the very bottom forum it has a post explaining it. Thanks.

    Regards,
    Chris
    Last edited by Freaky Chris; April 1st, 2009 at 01:53 PM.

  13. #13
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Thumbs up Re: [SOLVED] Little problem

    Once again Chris saves the day! Nice work.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

Similar Threads

  1. Display output from a file using regular expression
    By jazz2k8 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: May 29th, 2008, 09:33 AM