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

2023/5/31

Help with game

Lilia Lilia

2023/5/31

#
Hey I really could use some help! I am trying to program a new game that is similar to Mario Kart and I am almost finished. but I am experiencing some problems and troubles. firstly, I want that whenever a player arrives at the finishing line it stops the movement and a little sound plays in the background. Honestly, I am very new to programming and I don't really know much about it yet. though I still have to complete this school project... Oh and don't mind my English I am not a native :) Here is what I have now: public void act() { // Ergänzen Sie Ihren Quelltext hier... int lDirection = this.getRotation(); if (this.canMove()) { switch(lDirection) { case 0 : this.setLocation(getX()+ 1, getY()); break; case 90 : this.setLocation(getX(), getY()+1); break; case 180 : this.setLocation(getX()-1, getY()); break; case 270 : this.setLocation(getX(), getY()-1); break; } Greenfoot.delay(1); } else { System.out.println("Vorbei :("); } } public boolean canMove() { if (this.getRotation() == 0 && this.getX() == this.getWorld().getWidth()-1) { return false; } if (this.getRotation() == 90 && this.getY() == this.getWorld().getHeight()-1) { return false; } if (this.getRotation() == 180 && this.getX() == 0) { return false; } if (this.getRotation() == 270 && this.getY() == 0) { return false; } return true; } public void drive(){ while (this.canMove()){ act(); race(); } } public void race(){ if (Greenfoot.isKeyDown("Left")) { setRotation(180); drive(); } if (Greenfoot.isKeyDown("Down")) { setRotation(90); drive(); } if (Greenfoot.isKeyDown("Up")) { setRotation(270); drive(); } if (Greenfoot.isKeyDown("Right")) { setRotation(0); drive(); } } }
Lilia Lilia

2023/5/31

#
oh and secondly I want that whenever a player doesn't hit the road he gets to start from the beginning ( so that the car goes back to the beginning and that a little text appears that shows something like game over)
Lilia Lilia

2023/5/31

#
could somebody please help me? this is very urgent I am literally very close to breaking down in tears because I have no idea about how I could solve this problem... it really doesn't have to be something extravagant, it only is supposed to work
danpost danpost

2023/5/31

#
Lilia wrote...
I want that whenever a player arrives at the finishing line it stops the movement and a little sound plays in the background.
When the player arrives at the finish line, just play the sound and stop the program using:
Greenfoot.playSound(/** sound file name here */);
Greenfoot.stop();
Some text can be displayed at the same time, if desired.
Lilia wrote...
I want that whenever a player doesn't hit the road he gets to start from the beginning ( so that the car goes back to the beginning and that a little text appears that shows something like game over)
Not sure what you want -- game over or restart. You could just show game over and stop the program (and maybe play a little sound). Looking over your code, I do see some things that could be issues. As written, the race and drive methods will never be reached, which is a good thing as they call each other causing an infinite loop (not good in programming). However, it may not be infinite as the condition of being able to move (canMove) will probably change at some point. Still, the while loop is definitely not what you are looking for. Maybe the problem is more general. Looks like you tried to compensate for the while loop with a Greenfoot.delay call in the act method. You are doing things that greenfoot does on its own, complicating matters. The canMove method is a bit bulky (lots of conditions). The order of operations can often simplify things. Here, you check if move is possible, then move. You could just move, or attempt to move, and if it did not move, you must be at an edge. So, the moving code would then be:
private void move()
{
    int latestX = getX();
    int latestY = getY();
    move(1);
    if (getX() == latestX && getY() == latestY)
    {
        System.out.println("Vorbei :(");
        Greenfoot.stop();
    }
}
Now, obviously, you want key control for driving directions (as per your race method code). So, the direction setting code would be:
private boolean setDirection()
{
    int dx = 0;
    if (Greenfoot.isKeyDown("right")) dx++;
    if (Greenfoot.isKeyDown("left")) dx--;
    int dy = 0;
    if (Greenfoot.isKeyDown("down")) dy++;
    if (Greenfoot.isKeyDown("up")) dy--;
    if (dx*dy != 0 || dx+dy == 0) return false;
    setRotation(90*((dx*dx)*(1-dx)+(dy*dy)*(2-dy)));
    return true;
}
I took the liberty of returning a boolean, as you may want to move only when a key is down. The act method can now simply be:
public void act()
{
    setDirection();
    move();
}
or:
public void act()
{
    if (setDirection()) move();
}
In the first way, the actor will continue to move, even if no key is pressed. In the second, the actor will only move when a key is down. No while statements; no delay statements; no switch statements; no more if statements than necessary. Just simply do what needs done.
Lilia Lilia

2023/5/31

#
Thank you very much! it really helped me. I tried to make the actor stop by pressing the space bar but he just doesn't. here is what I thought of. Could you help me with that one too? if (Greenfoot.isKeyDown("spacebar")) Greenfoot.stop(); I added it into the private boolean setDirection right under the last isKeyDown
danpost danpost

2023/5/31

#
Lilia wrote...
I tried to make the actor stop by pressing the space bar but he just doesn't. here is what I thought of. ... if (Greenfoot.isKeyDown("spacebar")) Greenfoot.stop(); I added it into the private boolean setDirection right under the last isKeyDown
It is probably better place as the last statement in the act method, as it has nothing to do with setting direction, and the key is "space" -- not "spacebar".
Lilia Lilia

2023/5/31

#
I hope that you have time and that I am not asking for too much. earlier the main problem with playing the sound is that I know how to play a sound but I am not quite sure how to make the sound play on one certain point. I know the x and y coordinates but I don't know how to put that into words... Also, I added a line that should disappear when the car hits it to conclude I want an image to disappear and a sound to be played, which has to be triggered by an actor touching it. I would be very thankful for your help.
Lilia Lilia

2023/5/31

#
danpost wrote...
It is probably better place as the last statement in the act method, as it has nothing to do with setting direction, and the key is "space" -- not "spacebar".
haha, thanks that indeed did work.
danpost danpost

2023/5/31

#
Lilia wrote...
how to make the sound play on one certain point. I know the x and y coordinates but I don't know how to put that into words... Also, I added a line that should disappear when the car hits it to conclude I want an image to disappear and a sound to be played, which has to be triggered by an actor touching it.
if (isTouching(Line.class))
{
    removeTouching(Line.class);
    Greenfoot.playSound(/** sound filename here */);
}
This could be put in a separate method (private void finish() ) and called from the act method. Again, you may want to stop the program at that point also (and/or display some game over text ("You Win", for example).
Lilia Lilia

2023/5/31

#
danpost wrote...
if (isTouching(Line.class))
{
    removeTouching(Line.class);
    Greenfoot.playSound(/** sound filename here */);
    
}
This could be put in a separate method (private void finish() ) and called from the act method. Again, you may want to stop the program at that point also (and/or display some game over text ("You Win", for example).
I copied it from here and I entered the name of the file but somehow it says error: undeclared variable: balloon (that's how I called the sound) here is the code if it helps you understand the problem better:
if (isTouching(goal.class))
       {
          removeTouching(goal.class);
          Greenfoot.playSound(balloon);
          System.out.println("YOU WIN :)");
       }            
I also tried looking in the Greenfoot API but there was nothing that could really help me I have a wav file because our teacher told us to use a wav file. could that be the problem?
Lilia Lilia

2023/5/31

#
YAY! I just figured it out myself! Thank you very much for your help though @danpost; you really made my day. Without you, i probably would have really panicked very hard!
You need to login to post a reply.