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 | int r, g, b, player; int speed = 3 ; int count = 0 ; public Player( int player, int r, int g, int b) { setRotation( 270 ); this .r = r; this .g = g; this .b = b; this .player = player; getImage().setColor( new Color(r,g,b)); getImage().fillRect( 0 , 0 , 40 , 40 ); } public void act() { count++; getWorld().addObject( new Tail( r, g, b)) move(speed); moveAround(); eatFood(); } public void moveAround() { if ( this .player == 0 ){ if (Greenfoot.isKeyDown( "right" )) setRotation( 0 ); if (Greenfoot.isKeyDown( "left" )) setRotation( 180 ); if (Greenfoot.isKeyDown( "up" )) setRotation( 270 ); if (Greenfoot.isKeyDown( "down" )) setRotation( 90 ); } if ( this .player == 4 ){ if (Greenfoot.isKeyDown( "d" )) setRotation( 0 ); if (Greenfoot.isKeyDown( "a" )) setRotation( 180 ); if (Greenfoot.isKeyDown( "w" )) setRotation( 270 ); if (Greenfoot.isKeyDown( "s" )) setRotation( 90 ); } // Add your action code here. } public void eatFood() { if (isTouching(Food. class ) && player == 0 ) { MyWorld myWorld = (MyWorld) getWorld(); myWorld.blueCounter.addScore(); } if (isTouching(Food. class ) && player == 4 ) { MyWorld myWorld = (MyWorld) getWorld(); myWorld.greenCounter.addScore(); } } } |

