sketches | 3D creative code sketches with Three.js | Graphics library
kandi X-RAY | sketches Summary
kandi X-RAY | sketches Summary
Three.js sketches using topics from codevember.xyz. Also using as a model file host for codepen
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of sketches
sketches Key Features
sketches Examples and Code Snippets
Community Discussions
Trending Discussions on sketches
QUESTION
I started several attempts to get this complex working. As mentioned in so many other discussions the micropython modules are not recognized, e.g. machine. Python modules like numpy were also not found.
I think, the python environment is not working correctly and the modules are there but not found. But, there is no recommendation or tutorial that really solves this. How can I set this up?
What I did so far:
manually installed all components according to tutorials
another way: installed the pything coding pack which contains a lot of stuff.
The Windows paths have the correct folder paths to the components.
I set the obviously correct python interpreter in vscode
connection/communication with board is working. I can set up codes which dont contain micropython modules.
in other IDE's like thonny/mu the modules are found.
I also installed a python venv: I could install numpy inside this venv and later it was found in vscode (wasn't found before) when I used the venv python as interpreter in vscode. But I wasn't succesful with micropython in venv.
PS: I can use the micropython modules like machine or network and upload the sketch to the esp32 board. It is working on the board. But I cant run any of the sketches in vscode. I think that Vscode uses cpython instead of micropython but shouldn't this be working after the installations I mentioned?
...ANSWER
Answered 2021-May-24 at 00:00It sounds like you're confusing modules you install on the machine running Visual Studio Code and modules you install in Micropython on the ESP32.
They're totally separate.
Python on your Windows machine can use venv.
MicroPython doesn't use venv at all (there apparently is a clone of venv for MicroPython but it's not readily apparent what it does or why or how you'd use it). It is a completely separate instance of Python from the one on your Windows machine, and it doesn't operate the same way. Modules you install under venv won't be visible or usable by MicroPython. Numpy in particular is not available for MicroPython.
Many modules need to be written specially to work with MicroPython. MicroPython isn't running in a powerful operating system like Windows, MacOS or Linux. It's running in a highly constrained environment that lacks much of the functionality of those operating systems, and that has extremely little memory and storage compared to them. You can't expect that a module written for regular Python will just work on MicroPython (and likewise, many MicroPython modules use hardware features like I2C or SPI access that may not be available on more powerful, general purpose computers).
Only modules available with upip
will be available for MicroPython. They'll need to be installed in the instance of MicroPython running on the ESP32, not in the instance of Python running under Windows. They're two, totally separate instances of Python.
QUESTION
I am searching for a method (package) that enables me to 'drop' a row from one table on a row in another table. The server-side functionality that I am envisioning with it is that I can create some logic that will update the destination table. Unfortunately, I have not been successful prototyping this with the packages with the available shiny packages I could find.
The idea of the MVP concept in the code below is to assign (with drag 'n drop on) one of the callers in the top table to a row in the second table.
The closes I have come to it, is the following:
...ANSWER
Answered 2021-Jun-06 at 19:48You can make an interface using {shinyjqui}
that allows you to drag cells from some
table, drop them into a different table, and have shiny update the
underlying data frame of the table the draggable was dropped in.
First we need to define our draggable and droppable in our server function.
QUESTION
I have a project that is divided into two sections each one of them requires an independent sketch, I wanna upload the two sketches into the same Arduino card without one of them ruining the other's work, is that possible? if yes how to please if no any other alternatives
...ANSWER
Answered 2021-May-25 at 16:15No.
But you could combine both rather independent parts and add some logic to find out which part should be active, currently.
E.g. When a button (-combination) is pressed at restart, Arduino runs the test mode, else it runs in standard mode.
You can have multiple tabs in the Arduino IDE to handle the modes more independently, but all code is coexisting in flash memory after upload.
QUESTION
I've been working for some time on making a 3D first person camera in p5.js for games and random projects, but I've been having some trouble.
For some time now I've been using a single y-rotation matrix with my projects to allow the player to look around, but I've felt like having an upgrade recently, so I decided to use x and y rotation matrices for my camera code. I was able to botch together a system that kind of worked by dividing both calculated z values, but there were some issues, not to mention that's not how rotation matrices work. I recently tried doing a proper implementation, but I've come across some issues.
I've been using this: camera(0, 0, 0, -200*sin(rot.y), -200*sin(rot.x), (200*cos(rot.x)) + (200*cos(rot.y)), 0, 1, 0);
as my test camera code, which in theory would work, but in an actual setting, it doesn't for some reason, as you can see here. Right now if you look around too far it will randomly spaz out and mess up the way you're looking.
I also can confirm that I am using the correct formulas as here. I used the (almost) exact same code for calculating the values, and it looks completely fine.
Is there any weird trick to using the p5.js camera or is this some error that needs to be fixed?
...ANSWER
Answered 2021-May-22 at 04:31You actually don't have the correct formulas. The example you showed uses orbitControl()
, not camera
. It also doesn't have two different angles it's rotating through.
The middle 3 coordinates of camera()
define the point toward which the camera is pointing. That means that you want that point to move the same way you want the focus of the camera to move. It might help to draw a box at that point like this (in your original):
QUESTION
I am trying to use a glsl shader with p5js to create a simulation like the game of life. To do that I want to create a shader which will take a texture as uniform and which will draw a new texture based on this previous texture. In a next iteration this new texture will be used as uniform and that should allow me create a simulation following the idea exposed here. I am experienced with p5.js but I'm completely new to shader programming so I'm probably missing something.
For now my code is as straightforward as possible:
- In the
preload()
function, I create a texture using thecreateImage()
function and setup some pixels to be white and the others to be black. - In the
setup()
function I use this texture to run the shader a first time to create a new texture. I also set a timer to run the shader at regular intervals and draw the result in a buffer. - In the
draw()
function I draw the buffer in the canvas. - To keep things simple I keep the canvas and the texture the same size.
My issue is that at some point the y
coordinates in my code seems to get inverted and I don't understand why. My understanding is that my code should show a still image but each time I run the shader the image is inverted. Here is what I mean:
I am not sure if my issue comes from how I use glsl or how I use p5 or a mix of both. Can someone explain to me where this weird y
inversion comes from?
Here is my minimal reproducible example (which is also in the p5 editor here):
The sketch file:
...ANSWER
Answered 2021-May-18 at 08:52Long story short: the texture coordinates for a rectangle or a plane drawn with p5.js are (0, 0) in the bottom left, and (1, 1) in the top right, where as the coordinate system for sampling values from a texture are (0, 0) in the top left and (1, 1) in the bottom right. You can verify this by commenting out your color sampling code in your fragment shader and using the following:
QUESTION
let playerX;
let playerY;
let playerSize = 15;
let playerSpeed = (playerSize / Math.pow(playerSize, 1.44)) * 10;
let Newcelltimer = 0;
let cell = []
let zoom = 1;
let n = 0;
let Xgrid = 600;
let Ygrid = 600;
let cpu = [];
let x = 0;
let y = 10;
let OffSetX = [];
let OffSetY = [];
let CPUteam = 2;
function setup() {
smooth();
frameRate(999)
createCanvas(600, 450);
playerX = 500;
playerY = 100;
}
function draw() {
let cellDist = [];
let cpuDist = [];
background(220);
push();
//Changes the FOV depending on your size
let Newzoom = 10 / playerSize
let newnewZoom = 1.3 * lerp(0.9, 10 / playerSize, 0.5)
Newzoom = lerp(zoom, Newzoom, 0.3)
translate(Xgrid / 2, Ygrid / 2 - 50);
scale(newnewZoom)
translate(-playerX, -playerY);
// Player's speed
playerSpeed = round((playerSize / Math.pow(playerSize, 1.44)) * 10000) / 1000
Newcelltimer++;
//Adds in new cells
if (Newcelltimer % 40 == 0) {
cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
ceil(random(0 - Ygrid, 2 * Ygrid)))
}
//Adds in new AI's
if (Newcelltimer % 200 == 0) {
cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
OffSetX.push(ceil(random(0, 1000000)))
OffSetY.push(ceil(random(1000000, 2000000)))
CPUteam++;
}
//Creates AI when you start playing
if (Newcelltimer == 1 || Newcelltimer == 2) {
cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
OffSetX.push(ceil(random(0, 1000000)))
OffSetY.push(ceil(random(1000000, 2000000)))
cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
ceil(random(0 - Ygrid, 2 * Ygrid)))
CPUteam++;
}
//Checks if cell is eaten
for (let i = 0; i < cell.length / 2; i++) {
let d = int(dist(cell[i * 2], cell[i * 2 + 1], playerX, playerY))
if (d <= playerSize / 2) {
playerSize += 5;
cell.splice(i * 2, 2)
celliseaten = true;
}
else{
fill(0,255,255,180);
circle(i*2,i*2+1,8);
}
}
//The cells disappear after a bit
if (Newcelltimer % 200 == 0) {
cell.splice(0, 2)
}
//Everything to do with the AI system
for (let j = 0; j < (cpu.length / 4); j++) {
let cpuDist = [];
let distance = int(dist(cpu[j * 4], cpu[j * 4 + 1], playerX, playerY)) //Distance between AI and player
if (distance <= playerSize / 2 && cpu[j * 4 + 2] < playerSize) {
playerSize += floor(cpu[j * 4 + 2]);
cpu.splice(j * 4, 4)
}
else if(distance<=cpu[j*4+2]/2&&cpu[j*4+2] cpu[j * 4 + 2]) ? cpu[m * 4 + 2] : cpu[j * 4 + 2];
if (higher > distant) {
if (cpu[m * 4 + 2] > cpu[j * 4 + 2]) {
cpu[m * 4 + 2] += cpu[j * 4 + 2]
cpu.splice(j * 4, 4)
} else {
cpu[j * 4 + 2] += cpu[m * 4 + 2]
cpu.splice(m * 4, 4)
} //Else
} //If
//If AI didn't eat another AI, return the distance
else {
cpuDist.push(distant);
} //Else
} //If
} //If
} //For
let ClosestCpu = min(cpuDist);
let ClosestCpupos;
//Index value of the closest cpu (NOT WORKINGS)
for (var q = 0; q < cpu.length / 4; q++) {
if (ClosestCpu == int(dist(cpu[q * 4], cpu[q * 4 + 1], cpu[j * 4], cpu[j * 4 + 1]))) {
ClosestCpupos = q;
break;
}
}
//Checks if AI ate cell
for (let n = 0; n < cell.length / 2; n++) {
let dis = int(dist(cell[n * 2], cell[n * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))
if (dis <= cpu[j * 4 + 2] / 2) {
cpu[j * 4 + 2] += 5;
cell.splice(n * 2, 2)
} else {
//If it didn't eat the cell, does the same idea with the AI from before
cellDist.push(dis)
}
}
let ClosestCell = min(cellDist);
let ClosestCellpos;
for (let r = 0; r < cell.length / 2; r++) {
if (ClosestCell == int(dist(cell[r * 2], cell[r * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))) {
ClosestCellpos = r;
break;
}
}
//AI sppeed
let amp = round((cpu[j * 4 + 2] / Math.pow(cpu[j * 4 + 2], 1.44)) * 70000) / 7000;
if (dist(playerX, playerY, cpu[j * 4], cpu[j * 4 + 1]) < 150 && playerSize > cpu[j * 4 + 2]) {
let distXpos = cpu[j * 4]-playerX;
let distYpos = cpu[j * 4 + 1]-playerY;
let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
let MultVal = 150/abs(higherVal);
distXpos*=MultVal;
distYpos*=MultVal;
distXpos = map(distXpos, -150, 150, -1, 1);
distYpos = map(distYpos, -150, 150, -1, 1);
cpu[j * 4] += distXpos * amp;
cpu[j * 4 + 1] += distYpos * amp;
} else if (dist(playerX, playerY, cpu[j * 4], cpu[j * 4 + 1]) < 150 && playerSize < cpu[j * 4 + 2]) {
let distXpos = cpu[j * 4]-playerX;
let distYpos = cpu[j * 4 + 1]-playerY;
let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
let MultVal = 150/abs(higherVal);
distXpos*=MultVal;
distYpos*=MultVal;
distXpos = map(distXpos, -150, 150, -1, 1);
distYpos = map(distYpos, -150, 150, -1, 1);
cpu[j * 4] -= distXpos * amp;
cpu[j * 4 + 1] -= distYpos * amp;
}
else if (dist(cpu[ClosestCpupos * 4], cpu[ClosestCpupos * 4 + 1], cpu[j * 4], cpu[j * 4 + 1]) < 150 && cpu[j * 4 + 2] > cpu[ClosestCpupos * 4 + 2]) {
let distXpos = cpu[ClosestCpupos * 4] - cpu[j * 4];
let distYpos = cpu[ClosestCpupos * 4 + 1] - cpu[j * 4 + 1];
let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
let MultVal = 150/abs(higherVal);
distXpos*=MultVal;
distYpos*=MultVal;
distXpos = map(distXpos, -150, 150, -1, 1);
distYpos = map(distYpos, -150, 150, -1, 1);
cpu[j * 4] += distXpos * amp;
cpu[j * 4 + 1] += distYpos * amp;
} else if (dist(cpu[ClosestCpupos * 4], cpu[ClosestCpupos * 4 + 1], cpu[j * 4], cpu[j * 4 + 1]) < 150 && cpu[j * 4 + 2] > cpu[ClosestCpupos * 4 + 2]) {
let distXpos = cpu[ClosestCpupos * 4] - cpu[j * 4];
let distYpos = cpu[ClosestCpupos * 4 + 1] - cpu[j * 4 + 1];
let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
let MultVal = 150/abs(higherVal);
distXpos*=MultVal;
distYpos*=MultVal;
distXpos = map(distXpos, -150, 150, -1, 1);
distYpos = map(distYpos, -150, 150, -1, 1);
cpu[j * 4] -= distXpos * amp;
cpu[j * 4 + 1] -= distYpos * amp;
} else if (dist(cell[ClosestCellpos * 2], cell[ClosestCellpos * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]) < 150) {
let distXpos = cell[ClosestCellpos * 2] - cpu[j * 4];
let distYpos = cell[ClosestCellpos * 2 + 1] - cpu[j * 4 + 1];
let higherVal = (abs(distXpos) > abs(distYpos)) ? distXpos : distYpos;
let MultVal = 150/abs(higherVal);
distXpos*=MultVal;
distYpos*=MultVal;
distXpos = map(distXpos, -150, 150, -1, 1);
distYpos = map(distYpos, -150, 150, -1, 1);
cpu[j * 4] += distXpos * amp;
cpu[j * 4 + 1] += distYpos * amp;
} else {
x += 0.003;
y += 0.003;
let offsetX = map(noise(x + OffSetX[j]), 0, 1, -1, 1) * amp;
let offsetY = map(noise(y + OffSetY[j]), 0, 1, -1, 1) * amp;
cpu[j * 4] += offsetX;
cpu[j * 4 + 1] += offsetY;
}
if (cpu[j * 4 + 3] % 2 == 0) {
fill(0, 0, 255)
circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2]);
} else {
fill(255, 0, 0)
circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2])
} // Else
} //Cpu for
//Draws player
fill(255, 255, 0)
circle(playerX, playerY, playerSize)
pop();
}
...ANSWER
Answered 2021-May-12 at 19:02This is happening because the AI moves more quickly and more efficiently than the player. Suppose the CPU moves 3 pixels per frame and the player moves 1. Now consider the following:
- The CPU is 149 pixels away from the player, so it moves 3 pixels away from the player and the player moves 1 pixel toward the CPU
- Next frame: CPU is 151 pixels away, so it reverts to its normal behavior.
- Next frame: the player has moved closer, so it reverts to being chased.
- repeat
This alternating between states is what causes the vibration you see. How do you solve this? One solution is to give each CPU a boolean value for being chased. You can set this to true when the CPU comes within 150 of the player, and then set it back to false when it gets to be 200 away. That way, it won't oscillate because it needs to move 50 pixels in order to change behavior. In order to do this, you need to change all of the instances of j * 4
to j * 5
and give cpu[j * 5 + 4]
an initial boolean value. Here's my solution for testing:
QUESTION
I have three images of different sizes which I would like to layout side-by-side within a responsive container that has a max-width. e.g.
The HTML for the images is as follows:
...ANSWER
Answered 2021-May-12 at 06:18I got closer to what I was trying to achieve using the following. However I'm hoping someone could suggest a more elegant solution.
QUESTION
I'm trying to make a final sketch which final goal is basically to have an input box, submitting a word in it, and getting the spelling of the word, letter by letter. I have both sketches made, the input box one, and the spelling one, but I have tried multiple times and I can't seem to combine both sketches. For all it's worth here's the code.
...ANSWER
Answered 2021-May-09 at 16:20Instead of using two canvases, it might be a good idea to create graphics buffers! The code below will creates only one canvas with two buffers of width = canvas_width
and height = canvas_height/2
! These buffurs are drawn as image objects!
Also please notice that in the setup function I use noLoop
which prevents the draw()
function to be executed 60 times per second. By using noLoop() the draw()
function will be executed only once! Is good practice to use the noLoop()
function when you are working with static canvases. In your case you wanted the input to be inside of the canvas, therefore you must use this to prevent reload on the canvas and allow the system to capture your input.
The rectangle with the button stop loop
is for testing purposes only! You may remove it, or experiment and understand how it works! Remember the draw()
function is called only once, therefore when the program is executed the rectangle has a random color.
Now, the visualize()
function uses the loop() function which enables the draw()
function to be executed on 60FPS by default! This is required because you want to update the canvas once a new letter is added! You may choose your own algorithm to implement this but that the main idea of how to solve your problem! This function will use setInterval() & clearInterval() and it's controlled by milliseconds which are defined at the top of the code!
PS:
- Consider large text when you implement this because it will go off the canvas. You can check if text position is larger than canvas_width and prevent this from happening (i.e. Reset text position to new line)
- Consider invalid input
- Consider that a space character is not added in the display text. You may use
\s
to solve this. - ...and more
QUESTION
In the course of assessing a trained model synthesized for the regression problem below, I have some confusion in plotting the resulting history
. In particular, when I don't consider any metrics
ANSWER
Answered 2021-May-06 at 12:18Your loss is the mean squared error and your metric is the mean squared error, which is exactly the same. It means they are overlapping when you plot them !
QUESTION
var playerX;
var playerY;
var playerSize = 15;
var playerSpeed = (playerSize / Math.pow(playerSize, 1.44)) * 10;
var Newcelltimer = 0;
var cell = []
var cellEaten = 0;
var celliseaten = false;
var eaten = false;
var zoom = 1;
var n = 0;
var Xgrid = 600;
var Ygrid = 600;
var cpu = [];
var x = 0;
var y = 10;
var z = 100;
var OffSetX = [];
var OffSetY = [];
var lost = false;
var CPUteam = 2;
function setup() {
smooth();
frameRate(999)
createCanvas(600, 450);
playerX = 500;
playerY = 100;
}
function draw() {
var TotalBlue = 0;
var TotalRed = 0;
var Total = 0;
var cellDist = [];
if (lost) {
noLoop()
fill(255)
textSize(40)
background(0)
text("You lost", width / 2.5, height / 2)
textSize(20)
text("Final Board: ", 350, 60)
} else {
background(220);
push();
var Newzoom = 10 / playerSize
let newnewZoom = 1.3 * lerp(0.9, 10 / playerSize, 0.5)
Newzoom = lerp(zoom, Newzoom, 0.3)
translate(Xgrid / 2, Ygrid / 2 - 50);
scale(newnewZoom)
translate(-playerX, -playerY);
playerSpeed = round((playerSize / Math.pow(playerSize, 1.44)) * 10000) / 1000
Newcelltimer++;
if (Newcelltimer % 40 == 0) {
cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
ceil(random(0 - Ygrid, 2 * Ygrid)))
}
if (Newcelltimer % 200 == 0) {
cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
OffSetX.push(ceil(random(0, 1000000)))
OffSetY.push(ceil(random(1000000, 2000000)))
CPUteam++;
}
if (Newcelltimer == 1 || Newcelltimer == 2) {
cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
OffSetX.push(ceil(random(0, 1000000)))
OffSetY.push(ceil(random(1000000, 2000000)))
CPUteam++;
}
//Checks if player Ate cell
for (var i = 0; i < cell.length / 2; i++) {
let d = int(dist(cell[i * 2], cell[i * 2 + 1], playerX, playerY))
if (d <= playerSize / 2) {
playerSize += 5;
cell.splice(i * 2, 2)
celliseaten = true;
}
}
if (keyIsDown(UP_ARROW) && playerY > 0 - Ygrid) {
playerY -= playerSpeed;
}
if (keyIsDown(RIGHT_ARROW) && playerX < 2 * Xgrid) {
playerX += playerSpeed;
}
if (keyIsDown(DOWN_ARROW) && playerY < 2 * Ygrid) {
playerY += playerSpeed;
}
if (keyIsDown(LEFT_ARROW) && playerX > 0 - Xgrid) {
playerX -= playerSpeed;
}
if (Newcelltimer % 200 == 0) {
cell.splice(0, 2)
}
for (var j = 0; j < (cpu.length / 4); j++) {
var cpuDist = [];
//Checks if AI ate cell
for (var n = 0; n < cell.length / 2; n++) {
let dis = int(dist(cell[n * 2], cell[n * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))
if (dis <= cpu[j * 4 + 2] / 2) {
cpu[j * 4 + 2] += 5;
cell.splice(n * 2, 2)
} else {
cellDist.push(dis)
}
}
var ClosestCell = min(cellDist);
var ClosestCellpos;
for (var r = 0; r < cell.length / 2; r++) {
if (ClosestCell == dist(cpu[j * 4], cpu[j * 4 + 1], cell[r * 2], cell[r * 2 + 1])) {
ClosestCellpos = r;
break;
}
}
let amp = round((cpu[j * 4 + 2] / Math.pow(cpu[j * 4 + 2], 1.44)) * 10000) / 1000;
if (r < 100 && dist(playerX, playerY, cpu[j * 4], cpu[j * 4 + 1]) > 100 || cpu[j * 4 + 2] > playerSize) {
var disXpos = cell[r * 2] - cpu[j * 4];
var disYpos = cell[r * 2 + 1] - cpu[j * 4 + 1];
disXpos = map(disXpos, -100, 100, -1, 1);
disYpos = map(disYpos, -100, 100, -1, 1);
cpu[j * 4] += disXpos * amp;
cpu[j * 4 + 1] += disYpos * amp;
} else {
x += 0.003;
y += 0.003;
let offsetX = map(noise(x + OffSetX[j]), 0, 1, -1, 1) * amp;
let offsetY = map(noise(y + OffSetY[j]), 0, 1, -1, 1) * amp;
cpu[j * 4] += offsetX;
cpu[j * 4 + 1] += offsetY;
}
//Draws the AI's
if (cpu[j * 4 + 3] % 2 == 0) {
fill(0, 0, 255)
circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2]);
} else {
fill(255, 0, 0)
circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2])
}
//Checks if player ate AI or vice versa
let distance = int(dist(cpu[j * 4], cpu[j * 4 + 1], playerX, playerY))
if (distance <= playerSize / 2 && cpu[j * 4 + 2] < playerSize) {
playerSize += floor(cpu[j * 4 + 2]);
cpu.splice(j * 4, 4)
}
if (distance <= cpu[j * 4 + 2] && cpu[j * 4 + 2] > playerSize) {
lost = true;
}
//Checks if one AI eats another
for (var m = 0; m < cpu.length / 4; m++) {
if (m != j) {
if (cpu[j * 4 + 3] % 2 != cpu[m * 4 + 3] % 2) {
distant = dist(cpu[m * 4], cpu[m * 4 + 1], cpu[j * 4], cpu[j * 4 + 1])
let higher = (cpu[m * 4 + 2] > cpu[j * 4 + 2]) ? cpu[m * 4 + 2] : cpu[j * 4 + 2]
if (higher > distant) {
if (cpu[m * 4 + 2] > cpu[j * 4 + 2]) {
cpu[m * 4 + 2] += cpu[j * 4 + 2]
cpu.splice(j * 4, 4)
} else {
cpu[j * 4 + 2] += cpu[m * 4 + 2]
cpu.splice(m * 4, 4)
} //Else
} // If
} //If
} //If
} //If
} //Nested For
} // For
//Draws all the "cells"
for (var k = 0; k < cell.length / 2; k++) {
fill(0, 255, 255, 100)
circle(cell[k * 2], cell[k * 2 + 1], 7)
}
//Nothing important, drawing the smiley face character
fill(255, 255, 0)
circle(playerX, playerY, playerSize)
fill(0)
ellipse(playerX - playerSize / 5, playerY - playerSize / 10, playerSize / 5, playerSize / 3)
ellipse(playerX + playerSize / 5, playerY - playerSize / 10, playerSize / 5, playerSize / 3)
arc(playerX, playerY + playerSize / 5, playerSize / 2, playerSize / 5, 0, 3.2, PIE)
fill(255, 255, 0)
noStroke()
arc(playerX, (playerY + playerSize / 5) - 1, playerSize / 2, playerSize / 5, 0, 3.2, PIE)
stroke(0)
//Limits the max player size
if (playerSize >= 3 * Xgrid | playerSize >= 3 * Ygrid) {
if (Xgrid > Ygrid) {
playerSize = 3 * Ygrid;
} else {
playerSize = 3 * Xgrid;
}
}
}
VS
var playerX;
var playerY;
var playerSize = 15;
var playerSpeed = (playerSize / Math.pow(playerSize, 1.44)) * 10;
var Newcelltimer = 0;
var cell = []
var cellEaten = 0;
var celliseaten = false;
var eaten = false;
var zoom = 1;
var n = 0;
var Xgrid = 600;
var Ygrid = 600;
var cpu = [];
var x = 0;
var y = 10;
var z = 100;
var OffSetX = [];
var OffSetY = [];
var lost = false;
var CPUteam = 2;
function setup() {
smooth();
frameRate(999)
createCanvas(600, 450);
playerX = 500;
playerY = 100;
}
function draw() {
var TotalBlue = 0;
var TotalRed = 0;
var Total = 0;
var cellDist = [];
if (lost) {
noLoop()
fill(255)
textSize(40)
background(0)
text("You lost", width / 2.5, height / 2)
textSize(20)
text("Final Board: ", 350, 60)
} else {
background(220);
push();
var Newzoom = 10 / playerSize
let newnewZoom = 1.3 * lerp(0.9, 10 / playerSize, 0.5)
Newzoom = lerp(zoom, Newzoom, 0.3)
translate(Xgrid / 2, Ygrid / 2 - 50);
scale(newnewZoom)
translate(-playerX, -playerY);
playerSpeed = round((playerSize / Math.pow(playerSize, 1.44)) * 10000) / 1000
Newcelltimer++;
if (Newcelltimer % 40 == 0) {
cell.push(ceil(random(0 - Xgrid, 2 * Xgrid)),
ceil(random(0 - Ygrid, 2 * Ygrid)))
}
if (Newcelltimer % 200 == 0) {
cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
OffSetX.push(ceil(random(0, 1000000)))
OffSetY.push(ceil(random(1000000, 2000000)))
CPUteam++;
}
if (Newcelltimer == 1 || Newcelltimer == 2) {
cpu.push(ceil(random(-Xgrid, 2 * Xgrid)), ceil(random(-Ygrid, 2 * Ygrid)), 20, CPUteam)
OffSetX.push(ceil(random(0, 1000000)))
OffSetY.push(ceil(random(1000000, 2000000)))
CPUteam++;
}
//Checks if player Ate cell
for (var i = 0; i < cell.length / 2; i++) {
let d = int(dist(cell[i * 2], cell[i * 2 + 1], playerX, playerY))
if (d <= playerSize / 2) {
playerSize += 5;
cell.splice(i * 2, 2)
celliseaten = true;
}
}
if (keyIsDown(UP_ARROW) && playerY > 0 - Ygrid) {
playerY -= playerSpeed;
}
if (keyIsDown(RIGHT_ARROW) && playerX < 2 * Xgrid) {
playerX += playerSpeed;
}
if (keyIsDown(DOWN_ARROW) && playerY < 2 * Ygrid) {
playerY += playerSpeed;
}
if (keyIsDown(LEFT_ARROW) && playerX > 0 - Xgrid) {
playerX -= playerSpeed;
}
if (Newcelltimer % 200 == 0) {
cell.splice(0, 2)
}
for (var j = 0; j < (cpu.length / 4); j++) {
var cpuDist = [];
//Checks if AI ate cell
for (var n = 0; n < cell.length / 2; n++) {
let dis = int(dist(cell[n * 2], cell[n * 2 + 1], cpu[j * 4], cpu[j * 4 + 1]))
if (dis <= cpu[j * 4 + 2] / 2) {
cpu[j * 4 + 2] += 5;
cell.splice(n * 2, 2)
}
}
let amp = round((cpu[j * 4 + 2] / Math.pow(cpu[j * 4 + 2], 1.44)) * 10000) / 1000;
x += 0.003;
y += 0.003;
let offsetX = map(noise(x + OffSetX[j]), 0, 1, -1, 1) * amp;
let offsetY = map(noise(y + OffSetY[j]), 0, 1, -1, 1) * amp;
cpu[j * 4] += offsetX;
cpu[j * 4 + 1] += offsetY;
//Draws the AI's
if (cpu[j * 4 + 3] % 2 == 0) {
fill(0, 0, 255)
circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2]);
} else {
fill(255, 0, 0)
circle(cpu[j * 4], cpu[j * 4 + 1], cpu[j * 4 + 2])
}
//Checks if player ate AI or vice versa
let distance = int(dist(cpu[j * 4], cpu[j * 4 + 1], playerX, playerY))
if (distance <= playerSize / 2 && cpu[j * 4 + 2] < playerSize) {
playerSize += floor(cpu[j * 4 + 2]);
cpu.splice(j * 4, 4)
}
if (distance <= cpu[j * 4 + 2] && cpu[j * 4 + 2] > playerSize) {
lost = true;
}
//Checks if one AI eats another
for (var m = 0; m < cpu.length / 4; m++) {
if (m != j) {
if (cpu[j * 4 + 3] % 2 != cpu[m * 4 + 3] % 2) {
distant = dist(cpu[m * 4], cpu[m * 4 + 1], cpu[j * 4], cpu[j * 4 + 1])
let higher = (cpu[m * 4 + 2] > cpu[j * 4 + 2]) ? cpu[m * 4 + 2] : cpu[j * 4 + 2]
if (higher > distant) {
if (cpu[m * 4 + 2] > cpu[j * 4 + 2]) {
cpu[m * 4 + 2] += cpu[j * 4 + 2]
cpu.splice(j * 4, 4)
} else {
cpu[j * 4 + 2] += cpu[m * 4 + 2]
cpu.splice(m * 4, 4)
} //Else
} // If
} //If
} //If
} //If
} //Nested For
} // For
//Draws all the "cells"
for (var k = 0; k < cell.length / 2; k++) {
fill(0, 255, 255, 100)
circle(cell[k * 2], cell[k * 2 + 1], 7)
}
//Nothing important, drawing the smiley face character
fill(255, 255, 0)
circle(playerX, playerY, playerSize)
fill(0)
ellipse(playerX - playerSize / 5, playerY - playerSize / 10, playerSize / 5, playerSize / 3)
ellipse(playerX + playerSize / 5, playerY - playerSize / 10, playerSize / 5, playerSize / 3)
arc(playerX, playerY + playerSize / 5, playerSize / 2, playerSize / 5, 0, 3.2, PIE)
fill(255, 255, 0)
noStroke()
arc(playerX, (playerY + playerSize / 5) - 1, playerSize / 2, playerSize / 5, 0, 3.2, PIE)
stroke(0)
//Limits the max player size
if (playerSize >= 3 * Xgrid | playerSize >= 3 * Ygrid) {
if (Xgrid > Ygrid) {
playerSize = 3 * Ygrid;
} else {
playerSize = 3 * Xgrid;
}
}
}
...ANSWER
Answered 2021-May-04 at 01:49The reason the player instantly loses is because the code that the code that checks AI "cpu" distance to player doesn't work correctly.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install sketches
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page