This is my code of my health bar and I;m trying to make it decrease when every time an enemy bullet collides with my spaceship. Im not sure should I include my bullet code or player code, so if its necessary tell me and I'll upload it.
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 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; import java.awt.Font; /** * The StatBar class let's you display any stat with a little amount of code required. * * @author Zamoht * @version January 2014 */ public class StatBar extends Actor { public static enum Style {RECT, ROUND, SMOOTH}; private int max; private double value; private int target; private double temp; private double speed; private GreenfootImage frame; private GreenfootImage fullBar; private Style barStyle; private int fontSize; private int letterWidth; private int stringWidth; private Color color; /** * Updates the stat bar every act. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (target > value) { value+=speed*(( double )max/ 1000.0 ); if (target < value) { value = target; } } else if (target < value) { value-=speed*(( double )max/ 1000.0 ); if (target > value) { value = target; } } if (value != temp) { updateImage(); } } /** * Creates a stat bar using the given color, width, height, style, maximum value and current value. * * @param color the color of the stat bar. Transparent colors should be avoided. * @param width the width of the stat bar. * @param height the height of the stat bar. * @param style the style of the edges. * @param max the maximum value of the shown stat. * @param current the current value of the shown stat. */ public StatBar(Color color, int width, int height, Style style, int max, int current) { barStyle = style; int edge = getEdge(width, height); if (current > max) current = max; frame = new GreenfootImage(width, height); frame.setColor(Color.gray); frame.fillOval( 0 , 0 , edge, height); frame.fillRect(edge/ 2 , 1 , width-edge, height); frame.fillOval(width-edge, 0 , edge, height); fullBar = new GreenfootImage(width, height); setColor(color, edge, false ); setMax(max, true ); this .value = current; target = ( int )value; speed = 5 ; updateImage(); } /** * Changes the color of the stat bar. Should not be used in the updateColor() method due to * bad performance. * * @param color a new color */ public void setColor(Color color) { setColor(color, true ); } private void setColor(Color color, int edge, boolean update) { if (fullBar == null || color.equals( this .color)) { return ; } this .color = color; int width = fullBar.getWidth(); int height = fullBar.getHeight(); fullBar.clear(); fullBar.setColor(color); fullBar.fillOval( 0 , 0 , edge, height); fullBar.fillRect(edge/ 2 , 1 , width-edge, height); fullBar.fillOval(width-edge, 0 , edge, height); if (update)updateImage(); } /** * Changes the color of the stat bar. Use this in the updateColor() method with the update parameter * set to false. * * @param color a new color. * @param update a boolean which should only be true if an instant update is needed. */ public void setColor(Color color, boolean update) { int width; int height; if (fullBar != null ) { width = fullBar.getWidth(); height = fullBar.getHeight(); } else { return ; } setColor(color, getEdge(width, height), update); } /** * updateColor is called everytime the stat bar is updated. It should be implemented in * a subclass for changing the color of the stat bar. * */ public void updateColor() { //Used by subclasses to update the color of the bar } /** * Adds a given amount to the value target of the stat bar. * * @param change the amount which the target should be changed. This value can be negative when * subtraction is needed instead of addition. */ public void add( int change) { target += change; if (target > max) target = max; if (target < 0 ) target = 0 ; updateImage(); } /** * Sets the target which the stat bar's current value will change to over time. * * @param target the value target */ public void setTarget( int target) { this .target = target; } /** * Instantly changes the value and updates the bar with this new value. * * @param value a new value. */ public void setValue( int value) { this .value = value; updateImage(); } /** * Sets the maximum value the shown stat can reach. This does not fix the font size. * * @param max the maximum value. */ public void setMax( int max) { setMax(max, false ); } /** * Sets the maximum value the shown stat can reach. * * @param max the maximum value. * @param fixFont a boolean which determines wether the font size should be adjusted. If set * to true and the new maximum value is too large for the stat bar, the font size will be asjusted. */ public void setMax( int max, boolean fixFont) { this .max = max; if (fixFont) { int width = frame.getWidth(); int height = frame.getHeight(); int digits = getDigits(max); fontSize = 1 ; GreenfootImage label = new GreenfootImage( "0" , fontSize, Color.black, null ); while (label.getHeight() < height-height/ 5 && label.getWidth()*(digits* 2 + 1 ) < width-width/ 5 ) { letterWidth = label.getWidth(); fontSize++; label = new GreenfootImage( "0" , fontSize, Color.black, null ); } fontSize--; label = new GreenfootImage( "" +max+ "/" +max, fontSize, Color.black, null ); stringWidth = label.getWidth()/ 2 ; } updateImage(); } /** * Sets the update speed of the statbar. This should not be changed due to a higher maximum value * since the update works with a percentage of the maximum value. * * @param speed the update speed. Setting this to 1 will change the current value with getMax()/1000 * every act till the target is reached. Setting this to 2 will change the curren value with getMax()/500 and so on. Remember that the * speed is a double. */ public void setSpeed( double speed) { this .speed = speed; } private void updateImage() { updateColor(); GreenfootImage image = new GreenfootImage(frame); double ratio = ( double )value/( double )max; int width = ( int )Math.round(ratio*frame.getWidth()); if (width > 0 ) { GreenfootImage part = new GreenfootImage(width, frame.getHeight()); part.drawImage(fullBar, 0 , 0 ); image.drawImage(part, 0 , 0 ); } GreenfootImage label = new GreenfootImage( "" +( int )value+ "/" +max, fontSize, Color.black, null ); int modifier = 0 ; if (label.getHeight()% 2 == 0 || (label.getHeight()% 2 == 1 && image.getHeight()% 2 == 1 )) modifier = 1 ; image.drawImage(label, image.getWidth()/ 2 - stringWidth + letterWidth * (getDigits(max) - getDigits(( int )value)), image.getHeight()/ 2 - label.getHeight()/ 2 + modifier); //image.getWidth()/2 - letterWidth*(getDigits(max)*2+1)/2 + letterWidth*(getDigits(max)-getDigits((int)value)) setImage(image); temp = value; } private int getDigits( int value) { int digits = 0 ; do { digits++; value/= 10 ; } while (value > 0 ); return digits; } private int getEdge( int width, int height) { int edge; switch (barStyle) { case RECT: edge = 0 ; break ; case ROUND: edge = height; break ; case SMOOTH: edge = height/ 2 ; break ; default : edge = 0 ; break ; } return edge; } /** * Returns the maximum value. * */ public int getMax() { return max; } /** * Returns the current value. * */ public int getValue() { return ( int )value; } } |