xxxxxxxxxx
86
let torpedomap
let shotmap
name = "Dani"
mindim = 0
function setup() {
createCanvas(windowWidth, windowHeight);
generated = sendHTTPPOST(name,"generatetorpedo")
mindim = min(width/2,height)
}
function draw() {
background(220);
if(torpedomap == undefined || shotmap == undefined){
return
}
for (x = 0; x < 10; x++){
for (y = 0; y < 10; y++){
if (shotmap[x][y] == 0){
fill(255,255,255)
}else if(shotmap[x][y] == 1){
fill(100,100,100)
}else if(shotmap[x][y] == 2){
fill(255,0,0)
}
rect(x*mindim/10,y*mindim/10,mindim/10,mindim/10)
}
}
for (x = 0; x < 10; x++){
for (y = 0; y < 10; y++){
if (torpedomap[x][y] == 0){
fill(0,0,255)
}else if(torpedomap[x][y] == 1){
fill(255,0,0)
}
rect(x*mindim/10+width/2,y*mindim/10,mindim/10,mindim/10)
}
}
}
function mousePressed(){
x = floor(map(mouseX,0,mindim,0,10))
y = floor(map(mouseY,0,mindim,0,10))
if(x>9 || y>9 || shotmap[x][y] != 0){
return
}
if(torpedomap[x][y] == 0){
shotmap[x][y] = 1
}
if(torpedomap[x][y] == 1){
shotmap[x][y] = 2
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
mindim = min(width/2,height)
loop()
}
function sendHTTPPOST(data, url){
options = {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
}
fetch("http://localhost:6969/generatetorpedo", options)
.then(response => response.json())
.then(data => {console.log(data)})
.catch(error => {console.error('Errorasdas:', error);});
}