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

2022/5/26

NewPong

1
2
danpost danpost

2022/5/31

#
Your HealthBarTime class above still has too many fields that it makes it difficult to deal with -- frames, value and timer. All of three of these fields are essentially trying to do the same work. Your line 79 should be:
int percentage = timer*maxValue/3600; // where 3600 is timer maximum
or, simply:
int percentage = timer/36;
which takes the value of timer, with the range (0, 3600), and reduces it to the range (0, 100), which the bar uses.
ronald ronald

2022/5/31

#
I was content to find a formula for adjustvalue but I thought that the percentage should not be touched Yes, it's true it is difficult to manage all these fields
ronald ronald

2022/6/1

#
public void act()
    {
        GreenfootImage paddle1 = new GreenfootImage(20, 200);
        paddle1 = getImage();
        int worldHeight = getWorld().getHeight();
        int topLeftHalfHeight = getImage().getHeight()+290;
        int bottomLeftHalfHeight = getImage().getHeight()-90;
        
        if (Greenfoot.isKeyDown("a") && getY() > worldHeight-topLeftHalfHeight) {
            setLocation(getX(), getY()-10);
        }
              
        if (Greenfoot.isKeyDown("z") && getY() < worldHeight-bottomLeftHalfHeight) {
            setLocation(getX(), getY()+10);
        }
        
        if (isTouching(Ball.class)) {
            ((Counter1) ((Background) getWorld()).getCounter1()).addScore1();
        }
        
    }
I just finished my paddles, that's it the paddles no longer come out of the frame here is a piece of code thank you danpost and spock47
Spock47 Spock47

2022/6/1

#
ronald wrote...
I just finished my paddles
Well done.
ronald wrote...
for the progress bar, I replaced minValue by value and frames by timer, suddenly, both work but the progress bar does not follow time, both must be synchronized, in my opinion, I should review adjustvalue?
Yes, especially the line
value += amount;
Since the time components use the value of the timer, and the adjustValue method instead adds the value to the current value. I guess, just replacing it with
value = amount;
would probably solve it. But in the interest of simplicity and understandability, I think danpost's advice to reduce the number of fields is the best way to go. Note, that you can have just one attribute timer and still get transformed values at any time by using methods that compute it on the fly:
private int timer = 0;

public int getTimerValue() {
    return minValue + (maxValue - minValue) * timer / 3600;
}

public int getTimerPercentage() {
    return timer / 36;
}
That way, you can be sure that it will be consistent, easy to read and you still have the same values as before, if you think that you need them.
ronald ronald

2022/6/1

#
public int getTimerValue() {
        return minValue + (maxValue - minValue) * timer / 3600;
        }
 
    public int getTimerPercentage() {
        return timer*100 / 3600;
    }
private int timerValue;
    private int timerPercentage;
    
    static final Color TRANS = new Color(0,0,0,0);
     
    public HealthBarTime(int value, int timer) {
        this.timerValue = value;
        this.timerPercentage = timer;
        valueDisplay = new SimpleActor();
        updateValueDisplay();
    }
HealthBarTime hbt = new HealthBarTime(3600, 36);
        int hbtValue = hbt.getTimerValue();
        int hbtTimer = hbt.getTimerPercentage();
        addObject(hbt, 1025, 400);
Hello again here is what i did following your code I'm not used to this get method yet I show you my code I also deleted adjustvalue in the act method what do you think ?
Spock47 Spock47

2022/6/1

#
Yes, you can even simplify it to:
    private int timer = 0;
     
    static final Color TRANS = new Color(0,0,0,0);
      
    public HealthBarTime() {
        valueDisplay = new SimpleActor();
        updateValueDisplay();
    }
and you can use the get-methods wherever you need the value, e.g. in updateValueDisplay:
    private void updateValueDisplay() {
        ...
        int time = timer/60; // number of seconds of running time
        String minutes = "0"+(time/60); //  long text of minutes
        ...
        GreenfootImage colorBar = new GreenfootImage(wide,high);
        colorBar.drawImage(img, getTimerPercentage()-wide, 0);
        ...
    }
So, as you can see: you have only one attribute (timer) that makes sure that your data is stored correctly. But you can still use the stored data in two different "formats" (either using the timer-attribute directly, or as a percentage value by calling the getTimerPercentage-method).
ronald ronald

2022/6/1

#
OK if I understood correctly, I have the choice between two get methods the first method gettimervalue, I can use it in adjustvalue and the second method gettimerpercentage in int percentage without adding attributes to the constructor and in world background if not i can also use both get methods, or either one with constructor modification and add variables in world background I try to see clearly, am I right or not? correct me if i'm wrong, thanks
Spock47 Spock47

2022/6/1

#
Yes, you can use both get-methods without the need for any additional attributes or constructor parameters. Note: Since you also don't need the adjustValue method anymore, you probably don't even need the second get-method (getTimerValue). But any way, it does not have any additional "cost" - just writing down the get-method and it will return the right value already.
ronald ronald

2022/6/1

#
I understand I find that the progress bar works better with get than adjustvalue, it follows seconds well, one hit for one hit for get method and two hits for one hit for adjustvalue, i mean two small bars for one second, I don't know if you noticed if you have other explanations or subjects to advise me, I am taking Otherwise I am looking for how to make a smoke of bullets let's say 4 or 5 bullets following each other like a trail of smoke I think like code 5 + getRandomNumber(getWidth()) 5 + getRandomNumber(getHeight()) I don't know where I saw that thank you again spock47
Spock47 Spock47

2022/6/2

#
As soon as the trigger is pulled, you can set a timer and then use the timer to release bullets at defined times (e.g. every 30 seconds) until a defined number of bullets (e.g. 5) have been fired/launched.
public class Shooter extends Actor
{
    private static final int BULLETS_IN_SMOKE = 5; // number of bullets within a smoke.
    private static final int FRAMES_BETWEEN_BULLETS = 30;
    private int shootTimer = 0;
    
    public void act()
    {
        if (Greenfoot.isKeyDown("f") && shootTimer <= 0) {
            shootTimer = BULLETS_IN_SMOKE * FRAMES_BETWEEN_BULLETS;
        }
        if (shootTimer > 0) {
            if (shootTimer % FRAMES_BETWEEN_BULLETS == 0) {
                final Bullet bullet = new Bullet();
                getWorld().addObject(bullet, getX(), getY());
                bullet.setRotation(getRotation());
            }
            shootTimer--;
        }
    }
}
Make sure that the bullet has a move-call in its act-method (otherwise the bullets will just add up at the position of the shooter).
ronald ronald

2022/6/2

#
thank you spock47 for this code I also had the idea of a snake Anyway, I'm going to try your code and another from snake I am also working on another project I am looking for how to access from an actor class a void method of another actor class, there are several ways to achieve this I had a little trouble understanding Thanks for your help
You need to login to post a reply.
1
2