its long so i didnt , but here it is. the right two ones are the ones should be able after switching
thanx for taking the time!
main
Block block,block1;
NightBlock nBlock,nBlock1;
float screenLeftX, screenTopY;
float screenBlockX, screenBlockY;
float screenNightBlockX, screenNightBlockY;
// Position of player center in level coordinates
float playerX, playerY, playerR=10;
float playerVX = 0, playerVY = 0;
float speed = 200;
float blockX, blockY;
float nBlockX, nBlockY;
int schritt = 1;
float radius;
void setup() {
size (800, 600);
restart ();
screenNightBlockX = nBlockX - width/2;
screenNightBlockY = (nBlockY - height)/2;
nBlockX = nBlockX - screenNightBlockX;
nBlockY = nBlockY - screenNightBlockY;
screenBlockX = blockX - width/2;
screenBlockY = (blockY - height)/2;
blockX = blockX - screenBlockX;
blockY = blockY - screenBlockY;
}
void restart () {
block = new Block(200 - screenLeftX, 150 - screenTopY);
block1 = new Block(200- screenLeftX,100- screenTopY);
nBlock = new NightBlock(250- screenLeftX,150- screenTopY);
nBlock1 = new NightBlock(250- screenLeftX,100- screenTopY);}
void playerControl () {
if ( keyPressed ) {
if (keyCode==UP) {
playerVY = -speed;
playerVX = 0;
}
if (keyCode==DOWN) {
playerVY = speed;
playerVX = 0;
}
if (keyCode==LEFT ) {
playerVX = -speed;
playerVY = 0;
}
if (keyCode==RIGHT ) {
playerVX = speed;
playerVY = 0;
}
}}
void draw () {
background (0);
block.display(screenLeftX, screenTopY, playerX, playerY, playerR);
block1.display(screenLeftX, screenTopY, playerX, playerY, playerR);
nBlock.display(screenLeftX, screenTopY, playerX, playerY, playerR);
nBlock1.display(screenLeftX, screenTopY, playerX, playerY, playerR);
if (keyPressed) {
playerX = playerX+playerVX/frameRate;
playerY = playerY+playerVY/frameRate;
playerControl ();
}
screenLeftX = playerX - width/2;
screenTopY = (playerY - height)/2;
rect (playerX - screenLeftX, playerY - screenTopY, 2*playerR, 2*playerR );
}
class one
class Block {
float x, y, r;
float vx, vy;
boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2) {
return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}
//Konstruktor
Block (float tempx, float tempy) {
x = tempx;
y = tempy;
r = 50.0;
}
void display (float display_x, float display_y, float playerX, float playerY, float playerR) {
rect (x - display_x, y - display_y, r, r);
x = x+vx;
y = y+vy;
if (rectangle_collision (x-display_x, y-display_y, r, r, playerX-display_x, playerY-display_y, playerR, playerR)) {
float dist_left = x - playerX - playerR;
float dist_right = playerX - x - r;
float dist_top = y - playerY - playerR;
float dist_bottom = playerY - y - r;
float dist_x = min(dist_left, dist_right);
float dist_y = min(dist_top, dist_bottom);
if (dist_x < dist_y) {
if (dist_left < dist_right) {
vx = -1;
vy = 0;
}
else {
vx = 1;
vy = 0;
}
}
else {
if (dist_top < dist_bottom) {
vx = 0;
vy = -1;
}
else {
vx = 0;
vy = 1;
}
}
}
else {
vx=0;
vy=0;
}
}
}
class two
class NightBlock {
float xb, yb, rb;
float vxb, vyb;
boolean rectangle_collision(float x_1, float y_1, float width_1, float height_1, float x_2, float y_2, float width_2, float height_2) {
return !(x_1 > x_2+width_2 || x_1+width_1 < x_2 || y_1 > y_2+height_2 || y_1+height_1 < y_2);
}
//Konstruktor
NightBlock (float tempx, float tempy) {
xb = tempx;
yb = tempy;
rb = 50.0;
}
void display (float display_x, float display_y, float playerX, float playerY, float playerR) {
rect (xb - display_x, yb - display_y, rb, rb);
xb = xb+vxb;
yb = yb+vyb;
if (rectangle_collision (xb-display_x, yb-display_y, rb, rb, playerX-display_x, playerY-display_y, playerR, playerR)&&
(keyPressed)) {
if (key == ' ' )
{
float dist_left = xb - playerX - playerR;
float dist_right = playerX - xb - rb;
float dist_top = yb - playerY - playerR;
float dist_bottom = playerY - yb - rb;
float dist_x = min(dist_left, dist_right);
float dist_y = min(dist_top, dist_bottom);
if (dist_x < dist_y) {
if (dist_left < dist_right) {
vxb = -1;
vyb = 0;
}
else {
vxb = 1;
vyb = 0;
}
}
else {
if (dist_top < dist_bottom) {
vxb = 0;
vyb = -1;
}
else {
vxb = 0;
vyb = 1;
}
}
}
else {
vxb=0;
vyb=0;
}
}
}
}