public class Player_1 extends Players { /** * Act - do whatever the Player_1 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Move(); Hit(); wrapAtEdge(); } public void Move() { if (Greenfoot.isKeyDown("w")) { move(3); setRotation(-90); } if (Greenfoot.isKeyDown("a")) { move(3); setRotation(180); } if (Greenfoot.isKeyDown("s")) { move(3); setRotation(90); } if (Greenfoot.isKeyDown("d")) { move(3); setRotation(0); } } public void Hit() { removeTouching(BlueCircle.class); OnePlayer oneplayer = (OnePlayer)getWorld(); Counter counter = oneplayer.getCounter(); counter.addScore(); } }
public class OnePlayer extends World { Counter counter = new Counter(); public OnePlayer() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(600, 400, 1); prepare(); } public Counter getCounter() { return counter; } /** * Prepare the world for the start of the program. That is: create the initial * objects and add them to the world. */ private void prepare() { Player_1 player_1 = new Player_1(); addObject(player_1, getWidth()/2, getHeight()/2); addObject(counter, 0, 0); BlueCircle bluecircle = new BlueCircle(); addObject(bluecircle, 494, 308); } }
public class Counter extends Score { int score = 0; public Counter() { setImage(new GreenfootImage("Score; " + score, 40, Color.BLACK, Color.WHITE)); } public void act() { setImage(new GreenfootImage("Score; " + score, 40, Color.BLACK, Color.WHITE)); } public void addScore() { score++; } }