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
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);
}
}
int worldHeight = getWorld().getHeight();
int halfMyHeight = getImage().getHeight()/2;
if (getY() > worldHeight-halfMyHeight)
{
setLocation(getX(), worldHeight-halfMyHeight);
}timer = (timer+1)%86400; // auto resets to zero when maxed if (timer%60 == 0) updateImage(); // updates image every second
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);
}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);
}
}
int percentage = wide*minValue/maxValue;
adjustValue(minValue*frames/maxValue);
adjustValue(minValue + (maxValue - minValue) * timer / 3600);