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

2021/5/3

improvements for movement code; how to fix if you keep hold space you dont fall down

PetrusderEchte PetrusderEchte

2021/5/3

#
Hello! I have these Code:
import greenfoot.*;  
public class Jumper2 extends Actor
{
    private final int GRAVITY = 1; 
    private int velocity; 
int speed = 1;
    public Jumper2() { 
        velocity = 5; 
    }    
    public void act() 
    {
mover();
        fall(); 
        if (Greenfoot.isKeyDown("space") && getY() > getWorld().getHeight() -50) jump();
    }    
    public void fall() { 
        setLocation(getX(), getY() + velocity); 
        if (getY() > getWorld().getHeight() -50) velocity = 5; 
        else velocity += GRAVITY;
    }
    public void jump() {
          
        velocity = -20;  
    }
public void mover() {
       if (Greenfoot.isKeyDown("a"))
       setLocation(getX() - speed, getY());
       if (Greenfoot.isKeyDown("d"))
       setLocation(getX() + speed, getY());
}
}
this is my world, i dont know if you need this information:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Die einzigen aktiven Akteure in der Roboterwelt sind die Roboter.
 * Die Welt besteht aus 14 * 10 Feldern.
 */

public class SpielWelt extends World
{
    private static int zellenGroesse = 30;
    GreenfootSound backgroundMusic = new GreenfootSound("BoxCat Games - Mt Fox Shop.mp3");

    /**
     * Erschaffe eine Welt mit 14 * 10 Zellen.
     */
    public SpielWelt()
    {
        super(24, 12, zellenGroesse);
        setBackground("images/spielhintergrund.jpg");
        backgroundMusic.playLoop();

     
    }
}      


   
do you know any kinds of improvements? thx a lot!!!
RcCookie RcCookie

2021/5/3

#
You may want to specify how you want your code to be improved. Here are some semantic and naming convetion like improvements:
  • In Spielwelt, both zellenGroesse and backgroundMusic and in Jumper2 GRAVITY are never changed and not instance-dependent. Therefore, Both of them may take the modifiers
    private static final
    As they are static and final, you should name them in upper case only, so zellenGroesse -> ZELLEN_GROESSE.
  • In Jumper2, all variable names and classs names are in english. In Spielwelt however, there are german names, which will cause you several problems: You can't use all the characters you need (ä/ö/ü/ß), and most people ain't understand it. So I would reccomend you to always use english everywhere in your code. In the end, Java names are in english, too.
  • Leave more spaces and format the indents correctly. This is really just relevant for Jumper2. A non-formatted piece of code is really bad to read and makes your own program very confusing very quickly. Leave at least one empty line between your methods and also indent inline if blocks like in line27 and 29.
There's probably more to say if you really want it to be perfect, but that should already improve readability quite a bit.
You need to login to post a reply.