xxxxxxxxxx
134
let difficulty;
let x;
let place;
let score;
let streak;
let bestStreak;
let highScore;
let per;
let shots;
let misses;
let img;
let sound;
function preload() {
img = loadImage("sloth2.png");
sound = loadSound("lazer7.mp3");
}
function setup() {
createCanvas(600, 500);
difficulty = width / 200;
score = 0;
streak = 0;
bestStreak = 0;
highScore = 0;
per = 100;
shots = 0;
misses = 0;
x = width / 2;
place = random(0, width - 15);
}
function draw() {
background(255);
fill(0);
noStroke();
textSize(width / 50);
if (shots !== 0) {
per = round(score / shots * 100);
}
text("Score: " + score, 50, width / 3 * 2);
text("Streak: " + streak, 50, width / 3 * 2 + 15);
text("Longest streak: " + bestStreak, 50, width / 3 * 2 + 30);
text("High score: " + highScore, 50, width / 3 * 2 + 45);
text("Misses: " + misses, 50, width / 3 * 2 + 60);
text("Shots: " + shots, 50, width / 3 * 2 + 75);
text("Percentage: " + per + "%", 50, width / 3 * 2 + 90);
text("Are goats awesome? Yes.", width - width / 3, height / 10 * 9);
shoot();
thing();
if (x >= width) {
difficulty = -width / 200;
} else if (x <= 0) {
difficulty = width / 200;
}
x += difficulty;
}
function keyPressed() {
if (keyCode === 13) {
score = 0;
streak = 0;
misses = 0;
shots = 0;
per = 100;
}
if (keyCode === RIGHT_ARROW) {
difficulty = width / 200;
}
if (keyCode === LEFT_ARROW) {
difficulty = -width / 200;
}
if (keyCode === 32) {
sound.play();
if (check()) {
console.log('HIT!');
score += 1;
streak += 1;
place = random(0, width - width / 33);
shots += 1;
} else {
console.log('MISS');
streak = 0;
shots += 1;
misses += 1;
}
}
if (streak > bestStreak) {
bestStreak = streak;
}
if (score > highScore) {
highScore = score;
}
}
function check() {
if (x >= place && x <= place + 30) {
return true;
} else {
return false;
}
}
function shoot() {
stroke(255, 0, 0);
line(width / 2 - width / 50, height / 4 * 3 + height / 50, x, 0);
image(img, width / 2 - height / 8, height / 4 * 3, height / 4, height / 4);
}
function thing() {
strokeWeight(width / 150);
stroke(0);
line(place, 2, place + 30, 2);
}