Awesome! won't i be needing one for right and down too?
private boolean canPlace(Actor actor, int x, int y) { if (actor == null) return false; // other checks returning false if actor cannot be placed return true; if (y > 1 && getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass()) { return false; } }
private boolean canPlace(Actor actor, int x, int y) { if (actor == null) return false; // other checks returning false if actor cannot be placed return true; if (y > 1 && getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass() && getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass()) { return false; } }
public int state = 0; public void act() { if (state == 0) { if (Greenfoot.mousePressed(this)) { state = 1; } } else if (state == 1) { movement(); } } public void movement() { if (Greenfoot.isKeyDown("w") && Greenfoot.isKeyDown("a") && Greenfoot.isKeyDown("s") && Greenfoot.isKeyDown("d")) { setLocation(getX(), getY() - 1); state = 0; } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import javax.swing.*; /** * */ public class Bejeweled_World extends World { public Bejeweled_World() { super(20, 10, 2); GreenfootImage bg = new GreenfootImage("Bejeweled_bg.jpg"); bg.scale(getCellSize(), getCellSize()); setBackground(bg); prepare(); } private boolean canPlace(Actor actor, int x, int y) { if (actor == null) return false; if (y > 1) // zero and one are the first two rows if (getObjectsAt(x, y-2, Actor.class).get(0).getClass() == actor.getClass()) { return false; } if (getObjectsAt(x, y-1, Actor.class).get(0).getClass() == actor.getClass()) { return false; } // other checks returning false if actor cannot be placed return true; } public void prepare() { for (int row=0; row<getHeight(); row++) for (int col=0; col<getWidth(); col++) { Actor actor = null; while (!canPlace(actor, col, row)) { int which = Greenfoot.getRandomNumber(3); if(which == 0) actor = new Sword(); if(which == 1) actor = new Coin(); if(which == 2) actor = new Torch(); } addObject(actor, col, row); } } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * This class makes up the background of the game and generates the other PIP 'worlds' in the game * such as the Bejeweled part and the adventure part while creating the menu at the left. * * @author Simon Nanoq Callisen * @version 28-04-2014 */ public class Main_Control extends World { PIP pip; PIP pip2; boolean wDown; boolean sDown; Actor mouseActor; int mouseOffX, mouseOffY; public Main_Control() { super(1675, 910, 1); //paint the background blue GreenfootImage background = getBackground(); background.setColor(Color.blue); background.fill(); //spawn the Bejeweled/puzzle world World minor = new Bejeweled_World(); Class[] order = { Sword.class }; Class[] order2 = { Torch.class }; Class[] order3 = { Coin.class }; pip = new PIP(minor, order); addObject(pip, 1070, 605); } }