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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Player extends Actor { int speed = 5 ; int moveX; int moveY; public void act() { Input(); if (moveX != 0 && moveY != 0 ) { setLocation(getX() + moveX * speed * Math.sqrt( 2 ), getY() + moveY * speed * Math.sqrt( 2 )); } else { setLocation(getX() + moveX * speed, getY() + moveY * speed); } } public void Input() { moveX = 0 ; moveY = 0 ; if (Greenfoot.isKeyDown( "a" )) { moveX = - 1 ; } else if (Greenfoot.isKeyDown( "d" )) { moveX = 1 ; } if (Greenfoot.isKeyDown( "w" )) { moveY = - 1 ; } else if (Greenfoot.isKeyDown( "s" )) { moveY = 1 ; } } } |

