Skip to content

Instantly share code, notes, and snippets.

@codetricity
Created June 28, 2018 03:38
Show Gist options
  • Select an option

  • Save codetricity/54ccdbd9fd1d8b09ab31a498f886e31c to your computer and use it in GitHub Desktop.

Select an option

Save codetricity/54ccdbd9fd1d8b09ab31a498f886e31c to your computer and use it in GitHub Desktop.
Dog class for Greenfoot lesson
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
* Write a description of class Dog here.
* Dog was made for all the cute shiba inus in the world and to gain recognition for them.
* @author (Caitlyn Oda)
* @version (6/27/18)
*/
public class Dog extends Animal
{
private int delay;
public GreenfootSound whistle = new GreenfootSound("sounds/whistle.mp3");
private List<GreenfootImage> right = new ArrayList();
private List<GreenfootImage> left = new ArrayList();
String baseName;
int maxFrame = 10;
int currentFrame = 0;
private int maxDelay = 3;
public Dog()
{
for(int x = 0; x < maxFrame; x++){
baseName = "Walk0";
if(x == maxFrame - 1){
baseName = "Walk";
}
GreenfootImage rightDog = new GreenfootImage("images/" + baseName + Integer.toString(x + 1) + ".png");
rightDog.scale(230, 200);
right.add(rightDog);
GreenfootImage leftDog = new GreenfootImage("images/" + baseName + Integer.toString(x + 1) + ".png");
leftDog.mirrorHorizontally();
leftDog.scale(230, 200);
left.add(leftDog);
};
GreenfootImage dogImage = getImage();
int height = dogImage.getHeight();
int width = dogImage.getWidth();
dogImage.scale(230, 200);
delay = 0;
}
public void act()
{
checkKeys();
moveVertical();
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("z")){
whistle.play();
}
if(Greenfoot.isKeyDown("i")){
whistle.stop();
}
if(Greenfoot.isKeyDown("right")){
move(3);
switchImage();
}
else if(Greenfoot.isKeyDown("left")){
move(-3);
switchImageLeft();
}
}
public void moveVertical()
{
int y = getY();
int x = getX();
if(Greenfoot.isKeyDown("up")){
y = y - 3;
setLocation(x, y);
switchImage();
}
if(Greenfoot.isKeyDown("down")){
y = y + 3;
setLocation(x, y);
switchImage();
}
}
public void switchImage()
{
if(delay > maxDelay){
if (currentFrame < maxFrame) {
setImage(right.get(currentFrame));
currentFrame++;
}
else {
currentFrame = 0;
}
delay = 0;
}
else{
delay = delay + 1;
}
}
public void switchImageLeft()
{
if(delay > maxDelay){
if (currentFrame < maxFrame) {
setImage(left.get(currentFrame));
currentFrame++;
}
else {
currentFrame = 0;
}
delay = 0;
}
else{
delay = delay + 1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment