Hi! We used the sample whack a mole here in greenfoot as a guide for our coding. How do we change the cell size for the world? Cause we intend to only put the actors on half of the world so how are we gonna do it??
xRand = 5+rand.nextInt(5); yRand = 5+rand.nextInt(5): setLocation(xRand, yRand);
int[] starters = { 3, 2, 1 }; // for y-values of 7, 8, and 9, respectively randY = 7+Greenfoot.getRandomNumber(3); randX = starters[randY-7]+Greenfoot.getRandomNumber(9-starters[randY-7]);
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.util.Random; public class RandomMole extends Actors { private Random rand = new Random(); private int xRand, yRand; private int timer; private GreenfootImage[] images; private int noOfImages = 2; private int currentImage = 0; private int moleTimer=103; private int moles; private int maxMoles = 8; private int bombs; /** * Constructor - creates the timer and makes the mole * animated. */ public RandomMole() { timer=0; images = new GreenfootImage[noOfImages]; for(int i=0; i < noOfImages; i++) { images[i] = new GreenfootImage("pinky" + i + ".png"); } setImage(images[currentImage]); } /** * Act - do whatever the RandomMole wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { super.act(); currentImage = (currentImage + 1) % images.length; setImage(images[currentImage]); addMole(); bonkIfHit(); addBomb smokeIfHit(); } /** * Moves the mole randomly. */ public void move() { if(timer<2) { timer++; return; } else { timer=0; } xRand = rand.nextInt(3); yRand = rand.nextInt(3); if(!canMove()) { return; } setLocation(xRand, yRand); } /** * Adds moles to the world randomlly in random places. */ public void addMole() { moles = getWorld().numberOfObjects(); if(moles < maxMoles) { if(Greenfoot.getRandomNumber(150) < 65) { getWorld().addObject(new RandomMole(), Greenfoot.getRandomNumber(2), Greenfoot.getRandomNumber(2)); } } else { return; } } public void bonkIfHit() { if(this.getOneIntersectingObject(Hammer.class) != null) { setImage("ouch.png"); } } }