xxxxxxxxxx
56
let countries;
let randomCountry = "";
let flag;
let chosenThree = [];
let name;
let win = "";
async function preload() {
let url = "https://restcountries.com/v3.1/all?fields=name,flags";
let response = await fetch(url);
countries = await response.json();
console.log(countries);
}
function setup() {
createCanvas(600, 400);
textAlign(CENTER);
background(0);
}
function draw() {
fill(50);
rect(width / 2 - 200, height / 2 - 150, 400, 240);
if (flag) {
image(flag, width / 2 - 150, height / 2 - 130, 300, 200);
}
}
function mousePressed() {
list = [];
randomCountry = getRandomCountry();
flag = loadImage(randomCountry.flags.png);
name = randomCountry.name.common;
list.push(name);
list.push(getRandomCountry().name.common);
list.push(getRandomCountry().name.common);
fill(50);
rect(0, 300, 600, 100);
fill(255);
text(
`Is this flag from: 1. ${list[0]} - 2.${list[1]} - 3.${list[2]}`,
width / 2,
350
);
console.log(name);
}
function getRandomCountry() {
let randy = floor(random(countries.length));
let chosenCountry = countries[randy];
return chosenCountry;
}