This site requires JavaScript, please enable it in your browser!
Greenfoot back
faze_bleach
faze_bleach wrote ...

2018/9/6

Piano Tiles - Continuous Tiles

faze_bleach faze_bleach

2018/9/6

#
Hello, I am developing a piano tiles game, and have successfully managed to code a basic piano tiles simulator. However, I have a few issues that currently require help: 1) Currently my score counter does not work. The code for the Score counter is posted below:
public class Score extends Actor
{
    /**
     * Act - do whatever the Score wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public int points = 0;
    public Score(){
        GreenfootImage scr = new GreenfootImage("Score: "+points,40,Color.BLACK,new Color(0,0,0,0));
        setImage(scr);
    }
    
    public void addScore(int newPoints){
        points += newPoints;
        GreenfootImage scr = new GreenfootImage("Score: "+points,40,Color.BLACK,new Color(0,0,0,0));
        setImage(scr);
    }
    public void setScore(int zero){
        points = 0;
        GreenfootImage nil = new GreenfootImage("Score: "+points,40,Color.BLACK,new Color(0,0,0,0));
        setImage(nil);
    }
}
The Score class is executed in my Greenfoot scenario, however, the score does now increment every time I successfully press a key, when it should have. My take on this is that the Score class is working perfectly, the exception is that the world class Piano() has some limitations. Below is the code for the aforementioned world class which includes the Score class:
public Piano() //declares a public method Piano
    {
        super(900, 600, 1, false); //sets the dimensions for the world
        setPaintOrder(Score.class);
        Score score = new Score();
        addObject(score, 787, 52);
        makeKeys(); //calls the makeKeys method
        act(); //calls the makeBars method
        prepare();
    }
 public void scoreboard(){
         score.addScore(1);
    }
And below is my code for if the correct key is pressed, along with the execution if the correct key is not pressed and if the piano tile reaches the bottom edge of the world:
public void act() 
    {
        // Add your action code here.
        atWorldEdge();
        fall(4);
        if (getWorld() != null && isDown == false && Greenfoot.isKeyDown(bar)){
            Piano piano = (Piano)getWorld();
            GreenfootImage downImage = new GreenfootImage("images/bars-down.PNG");
            piano.scoreboard();
            piano.removeObject(this);
        }
        else if (isDown == true && !Greenfoot.isKeyDown(bar)){
            GreenfootImage upImage = new GreenfootImage("images/bars.PNG");
            setImage(upImage);
            isDown = false;
        }
        else{
            GreenfootImage upImage = new GreenfootImage("images/bars.PNG");
            setImage(upImage);
            isDown = false;
        }
    }
I am not sure why this is happening, but help would be greatly appreciated. If I missed anything just ask me to add in more information. 2) It would also be a great help if I could implement continuous piano tiles into my game, where the player has to hold the key down for an extended period of time for the continuous bar to disappear from the world, instead of simply pressing the key.
Super_Hippo Super_Hippo

2018/9/6

#
1) Remove 'Score' (beginning of line 5) in the Piano-constructor. You probably have a 'private Score score;' line somewhere and you create a new variable (with the same name) for the method if you start with a variable type (here the class). If the other line is 'private Score score = new Score();', then you can completely remove line 5. 2) Usually, games like these require you to hit the key at the correct moment. Right now, it seems like you just have to press any key (if there are several Tiles in the world). Not exactly sure how this works (or should work) at all for you so far. In general, to check for a continuous key press, you need a timer variable which increments as long as you hold down the key.
faze_bleach faze_bleach

2018/9/11

#
Thanks Super Hippo for the code - it works! However I have some other problems in my code. First of all, I need to make it so that if you press the wrong key, you lose a life. In coding terms, if Greenfoot.isKeyDown() is not the correct key, the lives counter decrements by 1. And I am not sure if this is possible but how do you make it so that the tiles that appear in the World class align with the notes in an imported song?
Super_Hippo Super_Hippo

2018/9/11

#
It's quite hard to do that with the isKeyDown-method because you can't check for any key. If you have let's say ten different keys and you only lose a life if you press one of the wrong ones, you can do that. If you want to check for any other key, you should use the getKey-method which behaves a bit different though and generally should only be used for typing in text. For the second thing, importing a song and use the notes… I have no clue how to do that. If the notes are in some form in a text file, you might be able to read them.
faze_bleach faze_bleach

2018/9/13

#
How should I use the getKey() method? I'm not sure where to put that in my code.
danpost danpost

2018/9/13

#
faze_bleach wrote...
How should I use the getKey() method? I'm not sure where to put that in my code.
I believe you would use something like the following somewhere in the act method in your Piano class:
String key = Greenfoot.getKey(); // to fetch any key typed in
if (key == << expected key >>)
{
    // correct key typed in (do whatever)
}
else if (key != null)
{
    // wrong key typed in (lose a life here)
}
faze_bleach faze_bleach

8 hours ago

#
Thanks very much danpost, sorry for the late reply.
You need to login to post a reply.