I'm trying to make a cookie clicker game and made it so that when you hit one of the smaller cookies, it subtracts points from your score. I was wondering if its possible to prevent it from going below zero and just stop there instead.
This is my counter code:
And this is the smaller cookie code that will subtract points:
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 | import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) import java.awt.Graphics; /** * Counter that displays a number with a text prefix. * * @author Michael Kolling * @version 1.0.2 */ public class Counter extends Actor { private static final Color textColor = new Color( 255 , 180 , 150 ); private int value = 0 ; private int target = 0 ; private String text; private int stringLength; private int totalCount = 100 ; public Counter() { setImage( new GreenfootImage( "0" , 20 , Color.WHITE, Color.BLUE)); } public void updateImage() { setImage( new GreenfootImage( "" + value, 20 , Color.WHITE, Color.BLUE)); } public void act() { if (value < target) { value++; updateImage(); } else if (value > target) { value--; updateImage(); } } public void add( int score) { target += score; } public void subtract( int score) { target -= score; } public int getValue() { return value; } } |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class smallCookie here. * * @author (your name) * @version (a version number or a date) */ public class smallCookie extends Actor { public smallCookie() { GreenfootImage image = getImage(); image.scale(image.getWidth() - 425 , image.getHeight() - 425 ); setImage(image); turn( 90 ); } /** * Act - do whatever the smallCookie wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move( 4 ); if (getY()>= 598 ){ getWorld().removeObject( this ); } if (Greenfoot.mouseClicked( this )== true ) { BackGround theWorld = (BackGround) getWorld(); // get a reference to the world Counter counter = theWorld.getCounter(); // get a reference to the counter counter.add(- 5 ); } } } |