I almost have the health bar down, but need help to set my greep hp value to be set as the health value of the health bar. So far, I have made the creating method of the health bar in my world class and have referenced it through all my greep classes. Now, I need to set the health bar value to match the hp of the greep. (srry if I don't make any sense).
Health bar Class:
Stage 1 World Class:
Greeps super class:
Small SubClass that extends Greeps:
There are two other subclasses that extend Greeps that are exactly the same as the Small subclass
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class HealthBar here. * * @author (your name) * @version (a version number or a date) */ public class HealthBar extends Actor { int health= int width= 80 ; int height= 15 ; int healthPerPixel=( int )width/health; public HealthBar() { update(); } public void act() { update(); } public void update() { setImage( new GreenfootImage(width+ 2 ,height+ 2 )); GreenfootImage hb=getImage(); hb.setColor(Color.BLACK); hb.drawRect( 0 , 0 ,width+ 1 ,height+ 1 ); hb.setColor(Color.GREEN); hb.fillRect( 1 , 1 ,health*healthPerPixel,height); } public void loseHealth() { // health=health-Towers(); } } |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Stage1 here. * * @author (your name) * @version (a version number or a date) */ public class Stage1 extends World { int []WayX; int []WayY; int index= 0 ; private int counter= 0 ; private static final int max= 1000000 ; private int spawned= 0 ; private static int level = 1 ; private Counter lives; HealthBar healthbar; /** * Constructor for objects of class Stage1. * */ public Stage1() { // Create a new world with 600x425 cells with a cell size of 2x2 pixels. super ( 600 , 425 , 2 , false ); setBackground( "Stage1f.png" ); prepare(); } public int getLevel() { return level; } public HealthBar getHealthBar() { return healthbar; } public void act() { counter++; if ((counter>= 50 )&&(spawned<=max)) { spawn(); spawned++; waypoints(); counter= 0 ; } } public void spawn() { int random= ( int )((Math.random()* 3 ) + 1 ); if (random== 1 ) { Small small = new Small(level,lives,healthbar); addObject(small, 0 , 108 ); addObject( new HealthBar(),small.getX(),small.getY()); } if (random== 2 ) { Medium medium = new Medium(level,lives, healthbar); addObject(medium, 0 , 108 ); addObject( new HealthBar(),medium.getX(),medium.getY()); } if (random== 3 ) { Heavy heavy = new Heavy(level,lives,healthbar); addObject(heavy, 0 , 108 ); addObject( new HealthBar(),heavy.getX(),heavy.getY()); } } public void waypoints() { WayX= new int []{ 94 , 94 , 188 , 188 , 281 , 281 , 374 , 374 , 467 , 467 , 563 , 563 , 620 }; WayY= new int []{ 108 , 223 , 223 , 28 , 28 , 372 , 372 , 83 , 83 , 314 , 314 , 140 , 140 }; index++; } public void prepare() { Counter lives = new Counter(); addObject(lives, 123 , 416 ); lives.setLocation( 120 , 415 ); this .lives = lives; Counter2 money = new Counter2(); addObject(money, 240 , 415 ); money.setLocation( 237 , 414 ); NormalButton nbutton = new NormalButton(money); addObject(nbutton, 15 , 416 ); nbutton.setLocation( 12 , 414 ); IceButton ibutton = new IceButton(money); addObject(ibutton, 26 , 409 ); ibutton.setLocation( 49 , 413 ); ibutton.setLocation( 51 , 413 ); ibutton.setLocation( 48 , 413 ); FireButton fbutton = new FireButton(money); addObject(fbutton, 31 , 415 ); fbutton.setLocation( 30 , 414 ); fbutton.setLocation( 31 , 414 ); fbutton.setLocation( 31 , 414 ); SniperButton sbutton = new SniperButton(money); addObject(sbutton, 70 , 415 ); sbutton.setLocation( 66 , 414 ); sbutton.setLocation( 69 , 414 ); BombButton bbutton = new BombButton(money); addObject(bbutton, 89 , 417 ); bbutton.setLocation( 85 , 414 ); //All these objects are just text objects. Money moneytext = new Money(); addObject(moneytext, 195 , 409 ); moneytext.setLocation( 193 , 414 ); Lives livestext = new Lives(); addObject(livestext, 424 , 395 ); livestext.setLocation( 112 , 415 ); lives.setLocation( 145 , 414 ); lives.setLocation( 149 , 414 ); livestext.setLocation( 117 , 414 ); lives.setLocation( 151 , 414 ); money.setLocation( 237 , 413 ); } } |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Creepers here. * * @author (your name) * @version (a version number or a date) */ public class Greeps extends Actor { public int type; boolean touchingBullet = false ; int []WayX; int []WayY; int index= 0 ; double distance= 150 ; public int hp; public int speed; private int level; public Counter liveCounter; public Greeps( int hp, int speed, int lv,Counter lives) { level = lv; this .hp=hp; this .speed=speed; liveCounter=lives; setRotation( 90 ); if (type== 1 ) setImage( "Enemy-small.png" ); if (type== 2 ) setImage( "Enemy-medium.png" ); if (type== 3 ) setImage( "Enemy-heavy.png" ); waypoints(); } public void act() { movement(); gameOver(); } /** * Sets the points where the Greeps will turn * */ public void waypoints() { if (level== 1 ) { WayX= new int []{ 94 , 94 , 188 , 188 , 281 , 281 , 374 , 374 , 467 , 467 , 563 , 563 , 620 }; WayY= new int []{ 108 , 223 , 223 , 28 , 28 , 372 , 372 , 83 , 83 , 314 , 314 , 140 , 140 }; } if (level == 2 ){ WayX= new int [] { 478 , 478 , 272 , 272 , 599 }; WayY= new int []{ 171 , 380 , 380 , 19 , 19 }; } if (level == 3 ){ WayX= new int [] { 355 , 246 , 246 , 463 , 463 , 134 , 134 , 572 , 572 , 27 , 27 }; WayY= new int [] { 248 , 248 , 115 , 115 , 314 , 314 , 49 , 49 , 381 , 381 , 10 }; } if (level == 4 ){ WayX= new int []{ 319 , 319 , 56 , 56 , 431 , 431 , 525 , 525 , 599 }; WayY= new int []{ 376 , 21 , 21 , 288 , 288 , 21 , 21 , 375 , 375 }; } } /** * Sets the speed of the Greeps and determins when to move onto the next waypoint * */ public void movement() { move( 1 ); int theDistance = ( int )(Math.hypot(WayX[index] - getX(), WayY[index] - getY())); if (theDistance < 1 ) { if (index<WayX.length- 1 ) { index++; } else { getWorld().removeObject( this ); return ; } } turnTowards(WayX[index], WayY[index]); } public void decreaseHealth() { Actor b1 = getOneIntersectingObject(Bullets. class ); Bullets b2 = (Bullets)b1; if (isTouching(Bullets. class )) { World world =getWorld(); if (b2.bulletType== 1 ) { hp=hp- 20 ; world.removeObject(b1); if (hp <= 0 ) world.removeObject( this ); } if (b2.bulletType== 2 ) { hp=hp- 10 ; world.removeObject(b1); if (hp <= 0 ) world.removeObject( this ); } if (b2.bulletType== 3 ) { hp=hp- 10 ; world.removeObject(b1); if (hp <= 0 ) world.removeObject( this ); } if (b2.bulletType== 4 ) { hp=hp- 45 ; world.removeObject(b1); if (hp <= 0 ) world.removeObject( this ); } if (b2.bulletType== 5 ) { hp=hp- 35 ; world.removeObject(b1); if (hp <= 0 ) world.removeObject( this ); } } return ; } public int loseALife() { if ((level== 1 )&&(index== 13 )) { liveCounter.add(- 1 ); getWorld().removeObject( this ); } if ((level== 2 )&&(index>= 5 )) { liveCounter.add(- 1 ); getWorld().removeObject( this ); } if ((level== 3 )&&(index>= 11 )) { liveCounter.add(- 1 ); getWorld().removeObject( this ); } if ((level== 4 )&&(index>= 9 )) { liveCounter.add(- 1 ); getWorld().removeObject( this ); } return index; } public void gameOver() { if (liveCounter.getLives()<= 0 ) { Greenfoot.setWorld( new GameOver()); getWorld().addObject( new MainMenuButton(), 300 , 225 ); } } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Medium here. * * @author (your name) * @version (a version number or a date) */ public class Small extends Greeps { private int level; public Small( int level,Counter lives, HealthBar healthbar) { super ( 25 , 3 , level,lives); liveCounter=lives; super .waypoints(); type= 1 ; } /** * Act - do whatever the Medium wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { movement(); super .decreaseHealth(); super .loseALife(); } } |