int rectposy = 0;
int rectposx = 0;
int rad = 20;
float xpos, ypos;
float xspeed = 2.6;
float yspeed = 2.2;
int xdirection = 5;
int ydirection = 1;
void setup()
{
size (800, 800);
background(0);
ellipseMode(RADIUS);
xpos = width/2;
ypos = height/2;
}
void draw()
{
background(0);
rect(rectposx, rectposy, 50, 100);
ellipse(xpos, ypos, rad, rad);
if(rectposy > 800)
{
rectposy = 0;
}
if(rectposy < 0)
{
rectposy = 700;
}
xpos = xpos + (xdirection);
ypos = ypos + (ydirection);
//test width & height
//collide with right
if (xpos > 800)
{
xdirection *=-1;
}
//collide with left
//if(xpos < 0)
//{
//xdirection *= -1;
//}
//collide with top and bottom
if (ypos > height-rad || ypos < rad) {
ydirection *= -1;
}
//collide with the rectangle
if(xpos < rectposx + 60 && ypos > rectposy && ypos < rectposy + 100)
{
xdirection *= -1;
}
}
void keyPressed()
{
if (keyCode == 40)
{
rectposy = rectposy + 10;
}
if (keyCode == 38)
{
rectposy = rectposy - 10;
}
}