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

2022/5/26

NewPong

1
2
ronald ronald

2022/5/26

#
hello I just noticed my paddles go over the edges I would like to know how to stop them just before they hit Thank you for your help
danpost danpost

2022/5/26

#
ronald wrote...
I just noticed my paddles go over the edges I would like to know how to stop them just before they hit
At end of act method (or after all movement has been completed), test the paddle's location. If the paddle is within half its image's width from an edge, reset its location so that it is AT half its image's width from the edge.
ronald ronald

2022/5/29

#
Hello I added a progress bar I'm trying to create a progress bar that follows time as the time is in string and the progress bar in int I made some code drafts but nothing more, I find it difficult to reconcile the two also I looked at scenarios on the pongs, all the paddles cross the background, maybe it's because of the isKeyDown key, in any case, there is no way to stop the paddles just before I don't see where you are coming from with the reset Thank you for your reply
ronald ronald

2022/5/29

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Test2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Test2 extends Actor
{
    private int minValue = 0;
    private int maxValue = 100;
    private int value = 0;
     
    static final Color TRANS = new Color(0,0,0,0);
    
    private final int FPS = 60;
    private int[] time = {0, 0};
    private int timer = 0;
     
    public Test2() {
        updateImage();
    }
    
    public void act() {
        if (++timer == FPS) {
            timer = 0;
               if (++time[1] == 60) {
                   if (++time[0] == 24) {
                       time[0] = 0;
                   }
                   time[1] = 0;
               }
               updateImage();
        }
        setValue(value*maxValue/FPS);
    }
    
    public void setValue(int amount) {
        value = amount;
        if(amount < 0) amount = 0;
        if(amount > maxValue) amount = maxValue; {
            value = amount;
            updateImage();
        }
    }
    
    public void updateImage() {
        String h = time[0]<10 ? "0"+time[0] : ""+time[0], min=time[1]<10 ? "0"+time[1] : ""+time[1];
        
        GreenfootImage img = new GreenfootImage(200,40);
        img.fill();
        img.setColor(Color.BLUE);
        img.fillRect(3,3,194,34);
        
        GreenfootImage text = new GreenfootImage(h + ":" + min,40,Color.WHITE,TRANS);
        img.drawImage(text,100-text.getWidth()/2,20-text.getHeight()/2);
        img.setFont(new Font("Calibri", 40));
        setImage(img);
    }
    
}
I managed to integrate the time inside the progress bar but for the moment it does not advance So a miscalculation I think Thank you for your help
danpost danpost

2022/5/29

#
ronald wrote...
I don't see where you are coming from with the reset
As an example:
int worldHeight = getWorld().getHeight();
int halfMyHeight = getImage().getHeight()/2;
if (getY() > worldHeight-halfMyHeight)
{
    setLocation(getX(), worldHeight-halfMyHeight);
}
Lines 1 and 2 just get the values needed (world height and half the height of the image of the actor). Line 3 checks the location of the actor with respect to one of its limits (bottom of screen). Line 5 resets the location of the actor so that it is not passed its limit (the one that was checked). Other if clauses should be added to check for other limits.
danpost danpost

2022/5/29

#
ronald wrote...
I added a progress bar I'm trying to create a progress bar that follows time as the time is in string and the progress bar in int I made some code drafts but nothing more, I find it difficult to reconcile the two
There is a problem with your naming of variables. You are dealing with minutes and seconds here, not hours and minutes. What might help would be to run everything off of a single timer, not multiple timers. That way, there is less to deal with -- less to incorporate together. By using one timer that goes to a maximum value of 60*60*24 = 86400, you can run with the following:
timer = (timer+1)%86400; // auto resets to zero when maxed
if (timer%60 == 0) updateImage(); // updates image every second
using:
private void updateImage()
{
    int time = frames/60;; // number of seconds of running time
    String minutes = "0"+(time/60); //  long text of minutes
    String seconds = "0"+(time%60); // long text of seconds
    String mins = minutes.substring(minutes.length()-2); // 2-digit text of minutes
    String secs = seconds.substring(seconds.length()-2); // 2-digit text of seconds
    String text = mins+":"+secs;
    GreenfootImage image = new GreenfootImage(text, 40, Color.WHITE, TRANS);
    GreenfootImage img = new GreenfootImage(200, 40);
    img.fill();
    img.setColor(Color.BLUE);
    img.fillRect(3, 3, 194, 34);
    int imageH = image.getHeight();
    int imageW = image.getWidth();
    img.drawImage(image, 100-imageW/2, 20-imageH/2);
    setImage(img);
}
Please note that setting the font of an image only sets it for future draws (using the drawString method). It does nothing to previously drawn text or images with text..
ronald ronald

2022/5/29

#
thank you Danpost for these code ideas I will test them thanks again
ronald ronald

2022/5/30

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Test2 here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class HealthBarTime extends Actor
{
    private Actor valueDisplay = new SimpleActor();
    private int value = 0;
    private int minValue = 0;
    private int maxValue = 100;
    private int frames = 0;
     
    static final Color TRANS = new Color(0,0,0,0);
    
    //private final int FPS = 60;
    //private int[] time = {0, 0};
    private int timer = 0;
    
    public HealthBarTime() {
        updateValueDisplay();
    }
    
    protected void addedToWorld(World world) {
        updateValueDisplay();
        world.addObject(valueDisplay, 1025, 400);
    }
    
    public void act() {
        /*if (++timer == FPS) {
            timer = 0;
               if (++time[1] == 60) {
                   if (++time[0] == 60) {
                       time[0] = 0;
                   }
                   time[1] = 0;
               }
        }
        updateValueDisplay();
        adjustValue(minValue*FPS/maxValue);
        */
       
        timer = (timer+1)%3600; // auto resets to zero when maxed
        if (timer%60 == 0) updateValueDisplay(); // updates image every second 
        //updateValueDisplay();
        adjustValue(minValue*frames/maxValue);
    }
    
    public void adjustValue(int amount) {
        value += amount;
        if(value < 0)               value = 0;
        if(value > maxValue)        value = maxValue; {
            updateValueDisplay();
        }
    }
    
    private void updateValueDisplay() {
        int wide = 200;
        int high = 40;
        
        //String minutes = time[0]<10 ? "0"+time[0] : ""+time[0];
        //String seconds = time[1]<10 ? "0"+time[1] : ""+time[1];
        
        int time = frames/60; // number of seconds of running time
        String minutes = "0"+(time/60); //  long text of minutes
        String seconds = "0"+(time%60); // long text of seconds
        String mins = minutes.substring(minutes.length()-2); // 2-digit text of minutes
        String secs = seconds.substring(seconds.length()-2); // 2-digit text of seconds
        String text = mins+":"+secs;
        
        GreenfootImage img = new GreenfootImage(wide, high);
        img.setColor(Color.GREEN);
        img.fill();
        
        GreenfootImage colorBar = new GreenfootImage(wide,high);
        int percentage = wide*minValue/maxValue;
        colorBar.drawImage(img,percentage-wide,0);
        
        GreenfootImage border = new GreenfootImage(wide+4,high+4);
        border.setColor(Color.WHITE);
        border.fill();
        border.setColor(Color.BLACK);
        border.drawRect(0,0,wide+3,high+3);
        border.drawImage(colorBar,2,2);
        
        GreenfootImage image = new GreenfootImage(text, 40, Color.BLUE, TRANS);
        int imageH = image.getHeight();
        int imageW = image.getWidth();
        border.drawImage(image, 100-imageW/2, 20-imageH/2);
        valueDisplay.setImage(border);
    }
}
Hello again I modified the code I don't know what the problem is the seconds and the minutes do not advance and likewise the progress bar which must advance at the same time as the seconds and the minutes Thank you for your help
Spock47 Spock47

2022/5/30

#
int percentage = wide*minValue/maxValue;
I guess minValue does not change throughout the run, so it always stays 0, so percentage always stays 0. I haven't checked in detail, but looking at the name of the variable, I guess when computing percentage, "value" is needed instead of "minValue". Live long and prosper, Spock47
ronald ronald

2022/5/31

#
I replaced minValue with value, it still remains 0 for me, value and minValue, I have the impression that it is the same thing, the same value I ask myself a question, is it possible that two images one on top of the other does not work??? I tried to separate them, for example by removing the progress bar, the second ones don't work thank you
Spock47 Spock47

2022/5/31

#
As I understand it, value is the current value and minValue and maxValue are the lower and upper bound of possible values. Now, the place where value is changed is the method adjustValue, but the only place where this method is called from is in line 49:
adjustValue(minValue*frames/maxValue);
Here, again, minValue is 0, so each time this call is effectively "adjustValue(0)". So, you have to think about the argument here. You surely want something that is different from 0, because with 0 there will be no change at all. Note: Is "value" supposed to be the normalized form of "timer"? I.e. if timer increases from 0 to 3600, value is supposed to increase from minValue (0) to maxValue (100)? If so, then you could use
adjustValue(minValue + (maxValue - minValue) * timer / 3600);
ronald ronald

2022/5/31

#
OK I now understand the value the value is like the percentage that advances between minvalue and maxvalue I didn't think about that, I realize that you have to detail everything with programming languages as they are mathematics too it doesn't work with your code either. I will think about that
Spock47 Spock47

2022/5/31

#
So, let's look at the two problems independently: the progress bar (filling green) and the shown time. 1. Progress bar. Well, the progress bar moves up correctly now with the given source code (and the two fixes above) on my computer. Please clean up the source code (removing unused variables and outcommented source code). This way it can be prevented that one accidentally replaces the wrong source code line (e.g. if the modified adjustValue line replaces the one in line 43 instead of the one in line 49, it would look like it would actually not fix the problem). Now, please check whether the progress bar (slowly filling green from the left) works then on your computer. 2. The shown time is computed from the value of variable "frames". But the variable frames is actually not being updated anymore (it is always 0). It looks to me that it was replaced in its core functionality by the variable "timer". So, just remove the variable "frames" and replace it by "timer" where needed (e.g. "int time = frames/60;" -> "int time = timer/60;". Tip: When you remove the variable declaration, the compiler will show you to all places where the variable still needs to be replaced. Live long and prosper, Spock47
ronald ronald

2022/5/31

#
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? I'm looking for thank you
danpost danpost

2022/5/31

#
danpost wrote...
using:
private void updateImage()
{
    int time = frames/60;;
Line 3 should have been:
int time = timer/60;
There are more replies on the next page.
1
2