This site requires JavaScript, please enable it in your browser!
Greenfoot back
badatcoding1325
badatcoding1325 wrote ...

2020/1/7

Activate Gif when Timer Over

badatcoding1325 badatcoding1325

2020/1/7

#
Hey! I want to know how I can display a gif to the screen when a timer I made is over.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
 *
 * <p>
 * <p>
 *
 */
/**
 * Declare Instance Variables
 */
public class Timer extends Actor
{
    //Maximum time the Timer can run.
    private int maxTime;
    //
    private int currTime = 0;
    //The rate of which the bar how fast the bar needs to drain
    private double currPercentTime;
    //The size of the blue bar
    private int blueBarSize;
    //The bar image
    private GreenfootImage bar;
    //The black bar image
    private GreenfootImage blackBar;
     
    //Sets the bar width, must be greater than or equal to 100
    private final int TIMER_BAR_WIDTH = 255;
    //Sets bar height, must be greater than or equal to 20
    private final int TIMER_BAR_HEIGHT = 20;
     
    //Initalize colours red and blue
    private int red;
    private int blue;
     
    //Value can be changed from 0 to 255
    private final int GREEN_COLOUR = 255;
     
    // the first colours
    private Color myBlue = new Color (0, 255, 255);
    private Color myBlack = new Color (51, 64, 69);
     
    //Initializing the text colour
    private Color foreground;
    //Ititializing the String text
    private String text;
     
    private final int NUM_ZEROS = 5;
   
    /**Constructor creates the bar image according to size and height.
     * Fills bar so it can start full and start to drain as the timer starts.
        * Also displays "Time" text on screen.
        */
    public Timer()
    {
        blueBarSize = TIMER_BAR_WIDTH;
        red = 0;
         
        //Creates the black bar that is to be under/behind the coloured bar as reference to how much time has passed.
        bar = new GreenfootImage(TIMER_BAR_WIDTH, TIMER_BAR_HEIGHT);
        blackBar = new GreenfootImage(100,100);
        //Sets the colour, fills the bar, sets the overall image.
        bar.setColor(myBlack);
        bar.fill();
        this.setImage(bar);
        //Sets  and displays text
        foreground = new Color (255, 255, 255);
        text = "Time:";
        this.update(text);
    }
     
     
        /**
     * Overload constructor that sets the current time to be the maximum amount of time as well as
     * the full bar(maximum time) to be blue
     * <p>
     * @param theMaxTime    The maximum amount of time in the timer
    */
    public Timer(int theMaxTime)
     
    {
        //Retrieves the values for maxTime and currTime
        this();
        maxTime = theMaxTime;
        currTime = theMaxTime;
        blue = maxTime;
    }
 
     
    public Timer(int theMaxTime, int theCurrTime)
    /**
     * Overload constructor that retrieves/sets the maxTime to theMaxtime and currTime to theCurrTime
     * <p>
     * @param theMaxTime    The maximum amount of time in the timer
     * @param theCurrTime   The current time in the timer
     */
    {
        //Retrieves the values for maxTime and currTime
        this();
        maxTime = theMaxTime;
        currTime = theCurrTime;
        blue = maxTime;
         
    }  
     
    /**
     * Displays white text
     * <p>
     * @param output    Updates the String in the Timer bar
     */
    public void update (String output)
     
    {
        bar.setColor(foreground); 
        int centeredY = (TIMER_BAR_HEIGHT/2+5);
        bar.drawString(output, 20, centeredY);
    }
     
    /**
     * Overload method update that takes in two booleans, startTime and endTime
     * Checks to see if the timer is to start/is starting or has ended.
     * If the Timer is starting the bar will decrease and the "Time" text is shown with how much time is left.
     * When the timer is over it displays text "Time Over!"
     * <p>
     * @param startTime  If Timer has started
     * @param endTime    If the timer has ended
     */
    public void update(boolean startTime, boolean endTime)
     
    {
         if(startTime)
        {
            //If the blue bar is not empty keeps decreasing the timer
            if(blue > 0)
            {
                decreaseTimer();
            }
             
            //Initializes text to be set
            String setTimeString;
     
            if(currTime > 0
            {
                //Keeps the "Time" text present the whole time the program is run, even if the timer reaches zero
                 
                setTimeString = zeroAdder(currTime, NUM_ZEROS);
                text = "Time: " + setTimeString;
                this.update(text);
                //Decreases the current time from the timer
                currTime--;
            }
        }
         
        //If the currentTime is 0 and endTime is true display new text "Timer over!"
        if(endTime && currTime == 0)
        {
            text = "TIMER OVER!";
            this.update(text);
        }
    }
     
     /**
     * Overload method update that takes in two booleans, startTime and endTime
     * Checks to see if the timer is to start/is starting or has ended.
     * If the Timer is starting the bar will decrease and the "Time" text is shown with how much time is left.
     * When the timer is over it displays text "Time Over!"
     *
     * @param startTime    Checks to see if Timer has started
     * @param endTime       Checks to see if Timer has ended
     * @param endPhrase     Message that is displayed once timer is over
     */
    public void update(boolean startTime, boolean endTime, String endPhrase)
     
    {
         if(startTime)
        {
            //If the blue bar is not empty keeps decreasing the timer
            if(blue > 0)
            {
                decreaseTimer();
            }
             
            //Initializes text to be set
            String setTimeString;
     
            if(currTime > 0
            {
                //Keeps the "Time" text present the whole time the program is run, even if the timer reaches zero
                 
                setTimeString = zeroAdder(currTime, NUM_ZEROS);
                text = "Time: " + setTimeString;
                this.update(text);
                //Decreases the current time from the timer
                currTime--;
            }
        }
         
        //If the currentTime is 0 and endTime is true display new end text message
        if(endTime && currTime == 0)
        {
            text = endPhrase;
            this.update(text);
        }
    }
         
           
    /**
     * Displays a set number of zeroes to display on the screen for the Timer.
     *
     * @param value     The amount of zeroes needed to be displayed on the screen
     * @param digits    The number of digits in the amount of time
     */
    public static String zeroAdder (int value, int digits)
     
    {
        // Figure out how many digits the number is
        int numDigits = digitCounter(value);
     
        // If not extra digits are needed
        if (numDigits >= digits)
        {
            return Integer.toString(value);
        }
            else // Build the number with zeroes for extra place values:
            {
                String zeroes = "";
                for (int i = 0; i < (digits - numDigits); i++)
                {
                    zeroes += "0";
                }
                return (zeroes + value);
            }
     
    }
         
    /**
    * Counts the number of digits in the starting amount of time
    * <p>
    * @param number Number of digits in starting time
    */
    private static int digitCounter (int number)
     
    {
            if (number < 10) {
                return 1;
            }
            int count = 0;
            while (number > 0) {
                number /= 10;
                count++;
            }
            return count;
    }
     
     
        /**
     * Decreases the timer and changes the colour simultaneously.
     */
    public void decreaseTimer()
    {
        //If the current time is not the max time, the bar is to decrease/change colour.
        if(currTime != maxTime)
        {
           currPercentTime = (double) currTime / maxTime;
           blueBarSize = (int) (currPercentTime * TIMER_BAR_WIDTH);
           blue -= 1;
            
           //Blue cannot be greater than 255 or else the colour does not exist
           if (blue > 255)
           {
               myBlue = new Color(0, 255, 255);
           }
           //Decreases the bar and changes the bar to red and "disappears"
           else if(red < 256 && blue > 0)
           {
               red += 1;
               myBlue = new Color(red, blue,GREEN_COLOUR);
           }
            
           //When the Bar is empty, the blue bar size is 0
           else
           {
                blueBarSize = 0;
           }
            
           bar.setColor(myBlue);
           bar.fillRect(0, 0, blueBarSize, TIMER_BAR_HEIGHT);
           bar.setColor(myBlack);
           bar.fillRect(blueBarSize,0, TIMER_BAR_WIDTH - blueBarSize, TIMER_BAR_HEIGHT);
           this.setImage(bar);
        }
    }
}
This is what is in the World
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MainWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MainWorld extends World
{
    private Timer timer;  
     
     
    /**
     * Constructor for objects of class MainWorld.
     *
     */
    public MainWorld()
    {   
        // Create a new world with 940x640 cells with a cell size of 1x1 pixels.
        super(1000, 640, 1);
        GreenfootImage bg = new GreenfootImage("Background.jpg");
        bg.scale(getWidth(), getHeight());
        setBackground(bg);
         
         timer = new Timer(10000,10000);
         
        //Adds object to the world while centering it on screen.
        addObject(timer, 800, 50);
        // adding grid
        for(int i = 140; i < 640; i += 105){
            for(int j = 285; j < 1000; j += 82){
                addObject(new Grass(), j, i);
            }
        }
         
         
 
    }
     
     
    public void act()
    {
         timer.update(true, true, "THE BIRDS WORK FOR THE BOURGEOISIE" );
    }
}
badatcoding1325 badatcoding1325

2020/1/7

#
oh note: The there is text that it already displays on the timer when it is over however I want to change it to activate a separate gif to appear on screen
danpost danpost

2020/1/7

#
badatcoding1325 wrote...
oh note: The there is text that it already displays on the timer when it is over however I want to change it to activate a separate gif to appear on screen
Use a separate actor to run the gif. You can adjust lines 200 and 201 to create the actor and put it in the world (before the possibility of removing the timer itself from the world.
You need to login to post a reply.