Left and right arrow keys steer the ufo. Up and down arrows make the ufo fly higher.
xxxxxxxxxx
function setup() {
createCanvas(windowWidth, windowHeight);
//background(0);
}
var raketti;
var raketti2;
var ufo;
var blueMarble;
var spacecow;
function preload() {
raketti = loadImage('raketti.png');
raketti2 = loadImage('raketti2.png');
ufo = loadImage('ufo.png');
blueMarble = loadImage('tausta.png');
spacecow = loadImage('spacecow.png');
}
var x = 0;
var y = 0;
var impulse = 0;
var speed = 0;
var score = 0;
var maxScore = 0;
var block1x = -200, block2x = -200, block3x = -20, block4x = -20;
var block1y = 0, block2y = 0, block3y = 0, block4y = 0;
var b1s = 1, b2s = 2, b3s = 5, b4s = 8;
var scorex = -20, scorey = -20;
function draw() {
background(0);
image(blueMarble,windowWidth/2,windowHeight/2);
//CONTROLS
if (keyIsDown(LEFT_ARROW)) {
x -= 10;
}
if (keyIsDown(RIGHT_ARROW)) {
x += 10;
}
if (keyIsDown(UP_ARROW)) {
impulse += 1;
}
if (keyIsDown(DOWN_ARROW)) {
impulse += 1;
//y += 5;
}
//GRAVITY STUFF
if(impulse > 0) {
impulse -= 1;
speed += 4;
}
speed -= 1;
y -= speed;//1.05*y
//player constraints
if(y + windowHeight/2 >= windowHeight- 10) {
impulse = 1;
}
if(y + windowHeight/2 < 20) {
y = -windowHeight/2+20;
impulse = 0;
speed = 0;
}
if(x > windowWidth/2) {
x = -windowWidth/2;
}
if(x < -windowWidth/2) {
x = windowWidth/2;
}
//BLOCK MOVEMENT
if(block1x < 0-200) {
block1x = windowWidth + 100;
block1y = random(windowHeight);
b1s = random(8)+4+score/3;
}
block1x -= b1s;
if(block2x < 0-200) {
block2x = windowWidth + 100;
block2y = random(windowHeight);
b2s = random(8)+4+score/3;
}
block2x -= b2s;
if(block3y < 0-200) {
block3y = windowHeight + 100;
block3x = random(windowWidth);
b3s = random(8)+4+score/3;
}
block3y -= b3s;
if(block4y < 0-200) {
block4y = windowHeight + 100;
block4x = random(windowWidth);
b4s = random(8)+4+score/3;
}
block4y -= b4s;
if(scorey < 0) {
scorex = random(windowWidth-40);
scorey = random(windowHeight-40);
}
//BLOCKS
fill(255,0,0);
image(raketti, block1x, block1y, 200, 20);
image(raketti, block2x, block2y, 200, 20);
image(raketti2, block3x, block3y, 20, 200);
image(raketti2, block4x, block4y, 20, 200);
fill(0,255,0);
image(spacecow, scorex,scorey, 80, 80);
//DRAW PLAYER
imageMode(CENTER);
image(ufo, x+windowWidth/2, (y)+windowHeight/2, 100, 100);
//BLOCK COLLISION
if((x+windowWidth/2) > block1x && (x+windowWidth/2) < block1x+200 && y+windowHeight/2 < block1y+20 && y+windowHeight/2 > block1y-20) {
x = 0;
y = 0;
maxScore = max(maxScore,score);
score = 0;
block1x = -200, block2x = -200, block3x = -20, block4x = -20;
block1y = 0, block2y = 0, block3y = 0, block4y = 0;
}
if((x+windowWidth/2) > block2x && (x+windowWidth/2) < block2x+200 && y+windowHeight/2 < block2y+20 && y+windowHeight/2 > block2y-20) {
x = 0;
y = 0;
maxScore = max(maxScore,score);
score = 0;
block1x = -200, block2x = -200, block3x = -20, block4x = -20;
block1y = 0, block2y = 0, block3y = 0, block4y = 0;
}
if((x+windowWidth/2) > block3x && (x+windowWidth/2) < block3x+20 && y+windowHeight/2 < block3y+200 && y+windowHeight/2 > block3y) {
x = 0;
y = 0;
maxScore = max(maxScore,score);
score = 0;
block1x = -200, block2x = -200, block3x = -20, block4x = -20;
block1y = 0, block2y = 0, block3y = 0, block4y = 0;
}
if((x+windowWidth/2) > block4x && (x+windowWidth/2) < block4x+20 && y+windowHeight/2 < block4y+200 && y+windowHeight/2 > block4y) {
x = 0;
y = 0;
maxScore = max(maxScore,score);
score = 0;
block1x = -200, block2x = -200, block3x = -20, block4x = -20;
block1y = 0, block2y = 0, block3y = 0, block4y = 0;
}
if((x+windowWidth/2) > scorex-80 && (x+windowWidth/2) < scorex+80 && y+windowHeight/2 < scorey+80 && y+windowHeight/2 > scorey) {
scorex = -20;
scorey = -20;
score += 1;
}
//TEXT
textSize(50);
text('Score:\t' + score, 100, 100);
text('Max Score:\t' + maxScore, 100, 160);
}