khoalabear wrote...
Just realised * are already in there. So only the ones which come after 100.for (int i=0; i<2; i++) images[i].scale(images[i].getWidth()*size/100, images[i].getHeight()*size/100);
for (int i=0; i<2; i++) images[i].scale(images[i].getWidth()*size/100, images[i].getHeight()*size/100);
public void eat()
{
AI_right f = (AI_right) getOneIntersectingObject(AI_right.class);
if (f != null)
{
if (size > f.size)
{
getWorld().removeObject(f);
size++;
updateImages();
}
else if (size < f.size) //idk how you want to handle same size
{
getWorld().removeObject(this);
Greenfoot.stop(); //or something
}
}
}
private int direction = 0; //+1=right, -1=left
public AI(int dir)
{
direction = dir;
//if they have different images
if (direction==0) setImage(/**...*/);
else setImage(/**...*/);
}
public void act()
{
setLocation(getX()+direction, getY());
}new AI(1) //for a fish moving from left to right new AI(-1) //for a fish moving from right to left //So for example: addObject(new AI(1), 0, 100);
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Pond_world extends World
{
public Pond_world()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1440, 810, 1);
prepare();
}
public void prepare()
{
User_fish user_fish = new User_fish();
addObject(user_fish,1152,313);
user_fish.setLocation(770,405);
}
public void act()
{
Right_spawn();
Left_spawn();
}
public boolean canAddRight()
{
// in the world class, use this line
return getObjects(AI_right.class).size() < 7;
}
public boolean canAddLeft()
{
// in the world class, use this line
return getObjects(AI_left.class).size() < 7;
}
private int spawnCounter = 0;
public void Right_spawn()
{
if (canAddRight())
{
if (spawnCounter > 50)
{
spawnCounter = 0;
addObject(new AI_right(), 0, Greenfoot.getRandomNumber(getHeight()));
}
spawnCounter++;
}
}
public void Left_spawn()
{
if (canAddLeft())
{
if (spawnCounter > 50)
{
spawnCounter = 0;
addObject(new AI_left(), 1440, Greenfoot.getRandomNumber(getHeight()));
}
spawnCounter++;
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class AI_right extends Actor
{
private int movementspeed = 5;
public int size = 70;
private int direction = 0;
public AI_right(int dir)
{
if (direction==0) setImage("AI_fish-right.png");
else setImage("AI_fish-left.png");
}
public void act()
{
setLocation(getX()+direction, getY());
AI_right();
remove();
}
public void AI_right()
{
setLocation(getX() + movementspeed, getY());
}
public void remove()
{
if (getX() >= getWorld().getWidth() -1)
getWorld().removeObject(this);
else if (getX() <=1)
getWorld().removeObject(this);
else if (getY() >= getWorld().getHeight() -1)
getWorld().removeObject(this);
else if (getY() <=1)
getWorld().removeObject(this);
}
public AI_right()
{
GreenfootImage img = new GreenfootImage("AI_fish-right.png");
double scaleFactor = (Greenfoot.getRandomNumber(13)+1) / 10.0; //random number between 0.1 and 4.0
img.scale( (int)(img.getWidth()*scaleFactor), (int)(img.getHeight()*scaleFactor));
setImage(img);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class User_fish extends Actor
{
private int movementspeed = 6;
private GreenfootImage[] images = {new GreenfootImage("User_fish-left.png"), new GreenfootImage("User_fish-right.png")};
private int size = 80;
public void act()
{
checkKeys();
eat();
}
public void checkKeys()
{
int dx = 0, dy = 0;
if (Greenfoot.isKeyDown("up")) dy--;
if (Greenfoot.isKeyDown("down")) dy++;
if (Greenfoot.isKeyDown("left")) dx--;
if (Greenfoot.isKeyDown("right")) dx++;
if (dx != 0 || dy != 0)
{
setLocation(getX()+dx*movementspeed, getY()+dy*movementspeed);
if (dx < 0 && getImage() != images[0]) setImage(images[0]);
else if (dx > 0 && getImage() != images[1]) setImage(images[1]);
}
}
public void eat()
{
AI_right f = (AI_right) getOneIntersectingObject(AI_right.class);
if (f != null)
{
if (size > f.size)
{
getWorld().removeObject(f);
size++;
updateImages();
}
else if (size < f.size) //idk how you want to handle same size
{
getWorld().removeObject(this);
Greenfoot.stop(); //or something
}
}
}
private void updateImages()
{
images[0] = new GreenfootImage("User_fish-left.png");
images[1] = new GreenfootImage("User_fish-right.png");
for (int i=0; i<2; i++) images[i].scale(images[i].getWidth()*size/100, images[i].getHeight()*size/100);
}
}
setLocation(getX()+direction*movementspeed, getY()); if (getX() < 1 || getX() > getWorld().getWidth()-2) getWorld().removeObject(this);
int r = Greenfoot.getRandomNumber(2); //0 or 1 addObject(new AI(r*2-1), 1440-1440*r, Greenfoot.getRandomNumber(getHeight()));
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Pond_world extends World
{
public Pond_world()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1440, 810, 1);
prepare();
}
public void prepare()
{
User_fish user_fish = new User_fish();
addObject(user_fish,770,405);
}
public void act()
{
Right_spawn();
}
public boolean canAdd()
{
// in the world class, use this line
return getObjects(AI.class).size() < 7;
}
private int spawnCounter = 0;
int r = Greenfoot.getRandomNumber(2);
public void Right_spawn()
{
if (canAdd())
{
if (spawnCounter > 50)
{
spawnCounter = 0;
addObject(new AI(r*2-1), 0, Greenfoot.getRandomNumber(getHeight()));
}
spawnCounter++;
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class AI extends Actor
{
private int movementspeed = 5;
public int size = 70;
private int direction = 0;
public AI(int dir)
{
if (direction==1) setImage("AI_fish-right.png");
else setImage("AI_fish-left.png");
}
public void act()
{
setLocation(getX()+direction*movementspeed, getY());
if (getX() < 1 || getX() > getWorld().getWidth()-2) getWorld().removeObject(this);
}
}direction = dir;
public void moveRight()
{
setImage("User_fish-right.png");
int worldWidth = getWorld().getWidth();
int x = (getX()+movementspeed)%worldWidth;
setLocation(x, getY());
}public void moveLeft()
{
setImage ("User_fish-left.png");
int worldWidth = getWorld().getWidth();
int x = (getX()-movementspeed+worldWidth)%worldWidth;
setLocation(x, getY());
}