I'm not sure how to write my code so that when i'm moving to the right and i press 'a' to go to the left, i don't crash into myself. Thanks.
int x=50;
int y=200;
int direction = RIGHT;
void setup()
{
size(400,400);
background(0);
noFill();
stroke(0,150,150);
rect(20,20,360,360);
}
void draw()
{
human();
}
void human()
{
stroke(255,255,255);
if(get(x,y) != color(0,0,0))
{
fill(255,0,0);
ellipse(150,150,200,200);
stroke(0,255,0);
}
else
{
point(x,y);
if(direction == RIGHT)
{
x++;
}
else if(direction == LEFT)
{
x--;
}
else if(direction == UP)
{
y--;
}
else if( direction == DOWN)
{
y++;
}
}
}
void keyPressed()
{
if (key == 'a')
{
direction= LEFT;
}
if (key == 'd')
{
direction= RIGHT;
}
if (key == 'w')
{
direction= UP;
}
if (key == 's')
{
direction= DOWN;
}
}
int x=50;
int y=200;
int direction = RIGHT;
void setup()
{
size(400,400);
background(0);
noFill();
stroke(0,150,150);
rect(20,20,360,360);
}
void draw()
{
human();
}
void human()
{
stroke(255,255,255);
if(get(x,y) != color(0,0,0))
{
fill(255,0,0);
ellipse(150,150,200,200);
stroke(0,255,0);
}
else
{
point(x,y);
if(direction == RIGHT)
{
x++;
}
else if(direction == LEFT)
{
x--;
}
else if(direction == UP)
{
y--;
}
else if( direction == DOWN)
{
y++;
}
}
}
void keyPressed()
{
if (key == 'a')
{
direction= LEFT;
}
if (key == 'd')
{
direction= RIGHT;
}
if (key == 'w')
{
direction= UP;
}
if (key == 's')
{
direction= DOWN;
}
}
1
