I want to make the pacman in my game turn the way im walking. This is my code:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Paggaman here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Paggaman extends Actor
{
int score = 0;
public void act(){
foundCoin();
Lost();
if (canmove() && WallUp() && Greenfoot.isKeyDown("up")){
setLocation(getX(), getY() - 1);
}else{
if (canmove() && WallRight() && Greenfoot.isKeyDown("right")){
setLocation(getX() + 1, getY());
}else{
if (canmove() && WallDown() && Greenfoot.isKeyDown("down")){
setLocation(getX(), getY() + 1);
}else{
if (canmove() && WallLeft() && Greenfoot.isKeyDown("left")){
setLocation(getX() - 1, getY());
}
}
}
}
}
public boolean canmove(){
World myWorld = getWorld();
int x = getX();
int y = getY();
if (x >= myWorld.getWidth() || y >= myWorld.getHeight() || x < 0 || y < 0) {
return false;
}
return true;
}
public boolean WallUp(){
Actor Around = getOneObjectAtOffset(0, -15, Around.class);
Actor WallIn = getOneObjectAtOffset(0, -15, WallIn.class);
if(Around != null){
return false;
}
else if (WallIn != null){
return false;
}
else{
return true;
}
}
public boolean WallRight(){
Actor Around = getOneObjectAtOffset(+15, 0, Around.class);
Actor WallIn = getOneObjectAtOffset(+15, 0, WallIn.class);
if(Around != null){
return false;
}
else if (WallIn != null){
return false;
}
else{
return true;
}
}
public boolean WallDown(){
Actor Around = getOneObjectAtOffset(0, +15, Around.class);
Actor WallIn = getOneObjectAtOffset(0, +15, WallIn.class);
if(Around != null){
return false;
}
else if (WallIn != null){
return false;
}
else{
return true;
}
}
public boolean WallLeft(){
Actor Around = getOneObjectAtOffset(-15, 0, Around.class);
Actor WallIn = getOneObjectAtOffset(-15, 0, WallIn.class);
if(Around != null){
return false;
}
else if (WallIn != null){
return false;
}
else{
return true;
}
}
public boolean foundCoin(){
Actor Coin = getOneObjectAtOffset(0, 0, Coin.class);
if(Coin != null){
getWorld().removeObject(Coin);
score = score + 1;
}
else{
return false;
}
return false;
}
public boolean getLost()
{
Actor Ghost = getOneObjectAtOffset( 15, 15, Ghost.class);
return Ghost != null;
}
public void Lost()
{
if(getLost())
{
Greenfoot.setSpeed(0);
}
}
}
