I created a game where a seal eats all the grapes in the ocean. The seal must avoid the birds to be eaten. My concern is how can I reset the score to zero each time without clicking the compile button? I need this project finished by Tuesday.
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 | For the ocean as background: public class ocean extends World { /** * Constructor for objects of class ocean. * */ public ocean() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); addObject( new seal(), 25 , getHeight()/ 2 ); addObject( new grapes(), getWidth() - 40 , getHeight()/ 2 ); addObject( new grapes(), (Greenfoot.getRandomNumber( 450 )), (Greenfoot.getRandomNumber( 450 ))); addObject( new grapes(), (Greenfoot.getRandomNumber( 450 )), (Greenfoot.getRandomNumber( 450 ))); addObject( new score(), 40 , getHeight()/ 2 ); populateWorld(); } public void populateWorld() { for ( int i= 1 ; i< 4 ; i++) { addObject( new bird(), (Greenfoot.getRandomNumber( 450 )), (Greenfoot.getRandomNumber( 450 ))) ; } for ( int i= 1 ; i< 10 ; i++) { addObject( new grapes(), (Greenfoot.getRandomNumber( 500 )), (Greenfoot.getRandomNumber( 500 ))); } } } The Seal for the user: public static int score = 0 ; public void act() { checkForArrowKeys( 5 ); } public void checkForArrowKeys ( int adjust) { if ( Greenfoot.isKeyDown( "left" ) ) { setLocation(getX() - adjust, getY()); } if ( Greenfoot.isKeyDown( "right" ) ) { setLocation(getX() + adjust, getY()); } if ( Greenfoot.isKeyDown( "down" ) ) { setLocation(getX() , getY()+ adjust); } if ( Greenfoot.isKeyDown( "up" ) ) The Bird to eat Seal: public class bird extends Actor { /** * Act - do whatever the bird wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { randomMove( 5 ); tryToCatch( seal. class ); } public void randomMove ( int maxAmount) { setLocation(getX() + (-maxAmount + Greenfoot.getRandomNumber( 2 * maxAmount + 1 )) , getY() + (-maxAmount + Greenfoot.getRandomNumber( 2 * maxAmount + 1 )) ); } public void tryToCatch(Class clss) { Actor actor = getOneObjectAtOffset( 0 , 0 ,clss); if ( actor != null ) { getWorld().removeObject (actor); } } The Grapes: public class grapes extends Actor { /** * Act - do whatever the grapes wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private boolean flag = false ; public void act() { checkForseal(); } public void checkForseal() { seal theseal = null ; theseal = (seal)getOneIntersectingObject(seal. class ); if (theseal != null ) { if (flag == false ) { seal.score++; flag = true ; getWorld().removeObject( this ); } } } } And Finally my Score (the purple ball): import greenfoot.*; import java.awt.Color; import java.awt.Font; public class score extends Actor { /** * Act - do whatever the score wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { updatescore(); } public void updatescore() { GreenfootImage image = new GreenfootImage( 145 , 35 ); image.setColor( new Color( 0 , 0 , 0 , 160 )); image.fillRect( 0 , 0 , 145 , 35 ); Font font = new Font ( "Arial" , Font.PLAIN, 30 ); image.setFont(font); image.setColor(Color.WHITE); image.drawString( "score: " + seal.score, 5 , 27 ); setImage(image); } } |