So this is my code<3 So my question is how can the main character not be able to go through the “obstacles” as you want to call them. Yes, that, I need collision or something I do not know:( thanks a lot<3
Face f1;
Enemy e1,e2,e3,e4;
Message m1, m2, m3, m4;
int currentTime;
void setup()
{
size (600,600);
f1=new Face(50,200,300,0,255,0);
e1 = new Enemy(200,300,30,-5,255,0,0);
e2=new Enemy(400,200,40,-8,255,0,0);
e3=new Enemy(100,500,50,-7,255,0,0);
e4=new Enemy(550,400,30,-6,255,0,0);
m1 = new Message(width/2, height/2, 1000,4000, "wear a mask");
m2 = new Message(width/2, height/2, 5000,9000, "avoid contacts");
m3 = new Message(width/2, height/2, 10000,14000, "wash your hands");
m4 = new Message(width/2, height/2, 15000,19000, "stay safe");
}
void draw()
{
background (0);
currentTime = millis();
f1.display();
e1.move();
e1.display();
e2.display();
e2.move();
e3.display();
e3.move();
e4.display();
e4.move();
m1.setTime(currentTime);
m2.setTime(currentTime);
m3.setTime(currentTime);
m4.setTime(currentTime);
m1.display();
m2.display();
m3.display();
m4.display();
fill(35, 20, 219);
rect(200, 240, 180, 30);
rect(0, 0, 600, 10);
rect(0, 0, 10, 600);
rect(0, 590, 600, 10);
rect(590, 0, 10, 600);
rect(40, 50, 50, 20);
rect(140, 50, 50, 20);
rect(0, 220, 40, 140);
rect(0, 400, 40, 20);
rect(80, 580, 200, 20);
rect(340, 580, 200, 20);
rect(240, 500, 150, 20);
rect(300, 500, 20, 50);
rect(240, 380, 150, 20);
rect(300, 380, 20, 70);
rect(390, 450, 60, 15);
rect(500, 450, 90, 15);
rect(500, 480, 20, 50);
rect(120, 420, 20, 80);
rect(90, 500, 90, 20);
rect(280, 0, 20, 60);
rect(380, 50, 40, 20);
rect(480, 50, 40, 20);
rect(480, 280, 20, 100);
rect(450, 260, 80, 20);
rect(150, 120, 300, 20);
rect(275, 140, 25, 40);
rect(100, 270, 20, 100);fill(35, 20, 219);
rect(200, 240, 180, 30);
rect(0, 0, 600, 10);
rect(0, 0, 10, 600);
rect(0, 590, 600, 10);
rect(590, 0, 10, 600);
rect(40, 50, 50, 20);
rect(140, 50, 50, 20);
rect(0, 220, 40, 140);
rect(0, 400, 40, 20);
rect(80, 580, 200, 20);
rect(340, 580, 200, 20);
rect(240, 500, 150, 20);
rect(300, 500, 20, 50);
rect(240, 380, 150, 20);
rect(300, 380, 20, 70);
rect(390, 450, 60, 15);
rect(500, 450, 90, 15);
rect(500, 480, 20, 50);
rect(120, 420, 20, 80);
rect(90, 500, 90, 20);
rect(280, 0, 20, 60);
rect(380, 50, 40, 20);
rect(480, 50, 40, 20);
rect(480, 280, 20, 100);
rect(450, 260, 80, 20);
rect(150, 120, 300, 20);
rect(275, 140, 25, 40);
rect(100, 270, 20, 100);
if(dist(f1.x, f1.y, e1.x, e1.y) < ( (f1.diameter + e1.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e2.x, e2.y) < ( (f1.diameter + e2.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e3.x, e3.y) < ( (f1.diameter + e3.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e4.x, e4.y) < ( (f1.diameter + e4.diameter) / 2) ){
noLoop();
}
if(dist(f1.x, f1.y, e4.x, e4.y) < ( (f1.diameter + e4.diameter) / 2) ){
noLoop();
}
}
}
void keyPressed()
{
if(key==CODED)
{
if(keyCode==LEFT)
{
f1.moveLeft();
}
if(keyCode==RIGHT)
{
f1.moveRight();
}
if(keyCode==UP)
{
f1.moveUp();
}
if(keyCode==DOWN)
{
f1.moveDown();
}
}
}
----------------------------------------------------------------------------------------
class Eye
{
int diameter;
int x;
int y;
int grey;
Eye(int d, int xcor, int ycor, int g)
{
grey=g;
diameter=d;
x=xcor;
y=ycor;
}
void display()
{
fill(grey);
ellipse(x,y,diameter,diameter);
}
int getX()
{
return x;
}
void setX(int xx)
{
x=xx;
}
int getY()
{
return y;
}
void setY(int yy)
{
y=yy;
}
}
-------------------------------------------------------------------------------------------
class Enemy
{
int x;
int y;
int diameter;
int step;
int r;
int g;
int b;
Enemy(int a,int b,int c,int d,int rc,int gc,int bc)
{
x=a;
y=a;
diameter=c;
step=d;
r=rc;
g=gc;
b=bc;
}
void display()
{
fill(r, g, b);
stroke(0,0,255);
ellipse(x,y,diameter,diameter);
}
void move()
{
y=y+step;
if(y<diameter)
step=-step;
if(y>600)
step=-step;
}
void moveUp()
{
if(y>=5+diameter/2)
{
y=y-5;
}
}
void moveDown()
{
if(y<=height-5-diameter/2)
{
y=y+5;
}
}
void changeColor()
{
g=(g+20)%255;
}
}
------------------------------------------------------------
class Face
{
int diameter;
int x;
int y;
int r;
int g;
int b;
Eye leftEye;
Eye rightEye;
Face(int d, int xcor, int ycor,int rc,int gc,int bc)
{
diameter=d;
y=ycor;
x=xcor;
r=rc;
g=gc;
b=bc;
leftEye=new Eye(10,x-10,y-10,255);
rightEye=new Eye(10,x+10,y-10,250);
}
void display()
{
fill(255, g, b);
stroke(255,0,0);
ellipse(x,y,diameter,diameter);
leftEye.display();
rightEye.display();
}
void moveLeft()
{
if(x>diameter/2)
{
x=x-10;
leftEye.setX(leftEye.getX()-10);
rightEye.setX(rightEye.getX()-10);
}
}
void moveRight()
{
if(x<width-diameter/2)
{
x=x+10;
leftEye.setX(leftEye.getX()+10);
rightEye.setX(rightEye.getX()+10);
}
}
void moveUp()
{
if(y>=5+diameter/2)
{
y=y-10;
leftEye.setY(leftEye.getY()-10);
rightEye.setY(rightEye.getY()-10);
}
}
void moveDown()
{
if(y<=height-5-diameter/2)
{
y=y+10;
leftEye.setY(leftEye.getY()+10);
rightEye.setY(rightEye.getY()+10);
}
}
}
-----------------------------------------------------------------------------
class Message
{
int startTime;
int stopTime;
String str;
int x;
int y;
boolean visible;
Message(int xcor, int ycor, int t1,int t2, String s)
{
x = xcor;
y = ycor;
startTime = t1;
stopTime = t2;
str = s;
visible = false;
}
void display()
{
textSize(24);
if(visible == true)
{
text(str, x, y);
}
}
void setTime(int time)
{
if(time>=startTime && time<= stopTime)
{
visible = true;
}
else
{
visible = false;
}
}
}
-------------------------------------------------------------------