Skip to content

Instantly share code, notes, and snippets.

@codetricity
Last active June 5, 2018 04:30
Show Gist options
  • Select an option

  • Save codetricity/2ea7479fab564f7c6e3cd2fc7c882bd1 to your computer and use it in GitHub Desktop.

Select an option

Save codetricity/2ea7479fab564f7c6e3cd2fc7c882bd1 to your computer and use it in GitHub Desktop.
Greenfoot Newtons Lab3
private void bounceAtEdge()
{
if (getX() == 0 || getX() == getWorld().getWidth()-1) {
setLocation((double)getX(), (double)getY());
getMovement().revertHorizontal();
accelerate(0.9);
// getImage() is part of the Actor class
GreenfootImage currentImage = getImage();
// private method that returns a color
Color randomColor = generateRandomColor();
currentImage.setColor(randomColor);
// evem after we set the color, we still need to fill the image
// if we just use currentImage.fill(), it will fill a square
int imageHeight = getImage().getHeight();
currentImage.fillOval(0, 0, imageHeight, imageHeight);
}
else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
setLocation((double)getX(), (double)getY());
getMovement().revertVertical();
accelerate(0.9);
}
}
private Color generateRandomColor() {
int r = Greenfoot.getRandomNumber(256);
int g = Greenfoot.getRandomNumber(256);
int b = Greenfoot.getRandomNumber(256);
Color randomColor = new Color(r, g, b);
return randomColor;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment