import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class MyWorld extends World { /** * Constructor for objects of class MyWorld. */ public MyWorld() { super(1168, 750, 1); AttackD mainAttackD = new AttackD(); Player mainPlayer = new Player(); Player2 mainPlayer2 = new Player2(); addObject(mainPlayer, 200, 500); addObject(mainPlayer2, 1000, 500); prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { } }
import lang.stride.*; import java.util.*; import greenfoot.*; /** * */ public class Player extends Actor { private int health = 100; public int damage = 5; private int power = 0; private int attackDelay = 0; public int damage2 = getWorld().getObjects(Player2.class).get(0).damage2; /** * */ public Player() { GreenfootImage image = getImage(); image.scale(500, 500); setImage(image); } /** * Act - do whatever the Person wants to do. This method is called whenever the 'Act' or 'Run' button gets pressed in the environment. */ final public void act() { if (attackDelay > 0) { attackDelay = attackDelay - 1; return; } int dx = 0; if (Greenfoot.isKeyDown("right")) { dx = dx + 1; } if (Greenfoot.isKeyDown("left")) { dx = dx - 1; } setLocation(getX() + 5 * dx, getY()); if ("x".equals(Greenfoot.getKey())) { if ( ! Greenfoot.isKeyDown("down")) { atac(); } if (Greenfoot.isKeyDown("down")) { atacDown(); } } } /** * */ private void atac() { attackDelay = 20; getWorld().addObject( new Attack(), getX() + 50, getY()); } /** * */ private void atacDown() { attackDelay = 20; getWorld().addObject( new AttackD(), getX() + 50, getY() + 50); } /** * */ public void getDamaged() { if (isTouching(Attack.class)) { health = health - damage2; } } }