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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Timer here. * * @author (your name) * @version (a version number or a date) */ public class Timer extends Actor { /** * Act - do whatever the Timer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public int time = 20 ; public int count = 65 ; public void act() { if (time== 0 ) { addText( "Time Up" ); Greenfoot.stop(); return ; } if (counter()) { time--; count = 65 ; } display(); } public boolean counter() { if (count> 0 ) { count--; } return count== 0 ; } public void display() { setImage( new GreenfootImage( "Time left: " +time, 30 ,Color.RED,Color.BLACK)); } public void setTime() { time = 20 ; } public boolean isTimeUp() { return time == 0 ; } public void addText(String message) { addObject( new Message(message), getWidth()/ 2 , getHeight()/ 2 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; /** * Write a description of class Text here. * * @author (your name) * @version (a version number or a date) */ public class Message extends Actor { public Message(String message) { setImage( new GreenfootImage(message, 50 , Color.RED, Color.WHITE)); } /** * Act - do whatever the Text wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. } } |