1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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() { } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | 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; } } } |