Hello. I am to spawn in a bunch of colored balls that will fall from the top of the screen through a file dialog approach.
The file contains this code with correlates the ball's color when it will be spawned in:
10 10 10
255 0 0
0 255 0
0 0 255
125 255 125
200 10 100
Where the first line is almost black, second is red, third is green, fourth is all blue, fifth is light-greenish, and the last is a reddish-purple. Now all of these elements will be stored into a class called BallInfo which is right here:
I need to be able to spawn these balls in at the top of the screen and have them fall until they hit the bottom of the screen or a player-controlled slider that will make them disappear. When a ball hits the slider, it should be updated in the BallInfo class aforementioned. How would I go about spawning the balls in continuously and then updating the scoreboard when a ball hits the slider at the bottom of the screen?
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 | public class BallInfo extends Actor { private Color ballColor; // color of the balls being summarized private int totalCount; // number of balls of this color ever seen in the world private int caughtCount; // number of balls of this color ever "caught" /** * constructor for color and stats information about a ball type. * @param redValue the amount of red (0-255) in the RGB color to use * @param greenValue the amount of green (0-255) in the RGB color to use * @param blueValue the amount of blue (0-255) in the RGB color to use */ public BallInfo( int redValue, int greenValue, int blueValue) { // initially, all counts will be zero. totalCount= 0 ; caughtCount= 0 ; // use specified color to create (redraw) image ballColor = new Color(redValue,greenValue,blueValue); redraw(); } // private method to redraw the stats image. private void redraw(Color col) { // build a new image for this object GreenfootImage img = new GreenfootImage( 150 , 12 ); // fill in the background color. img.setColor( new Color( 200 , 200 , 200 )); // could use Color.GRAY img.fill(); // add an "icon" that shows the corresponding color GreenfootImage ball = new GreenfootImage( 10 , 10 ); ball.setColor(col); ball.fillRect( 0 , 0 , ball.getWidth(), ball.getHeight()); img.drawImage(ball, 1 , 1 ); // add in the count statistics for this ball color img.setColor( new Color( 0 , 0 , 0 ) ); // could use Color.BLACK String stats = String.format( "#:%6d" , totalCount); img.drawString(stats, 21 , 11 ); stats = String.format( "C:%6d" , caughtCount); img.drawString(stats, 85 , 11 ); setImage(img); // use the image we've built up. } // redraw the image with the current ball calor. private void redraw() { redraw(ballColor); } /** * returns the color for this ball type. Might be useful to help draw a ball in the game * @return the color for this type */ public Color getColor() { return ballColor; } /** * returns the number of times a ball of this color has been added to the world * @return the count of the number of balls of this color ever seen in the world */ public int getTotal() { return totalCount; } /** * returns the number of times a ball of this color has been caught * @return the count of the number of balls of this color ever been caught */ public int getCaught() { return caughtCount; } /** * adds one to the total number of balls of this color seen in the world. */ public void incTotal() { totalCount++; redraw(); } /** * adds one to the total number of ballos of this color that have been caught */ public void incCaught() { caughtCount++; redraw(); } |