Constrain w/ an array
in
Programming Questions
•
7 months ago
Hi, I'm making microgame program where you control one square and there are other uncontrolled squares bouncing around and you have to avoid them. However, all my AI squares are listed in an array and constrain() won't allow the use of an array? Help, please!
- /* preload="grid.jpg" */
- PImage img;
- int[] rectX = {10, 36, 375, 475, 600, 680, 150, 770};
- int[] rectY = {10, 690, 165, 700, 200, 490, 400, 30};
- int rectSize = 25;
- int playerX = 400;
- int playerY = 400;
- int edge = 2;
- int inner = edge;
- int aiX =
- int aiY
- void setup() {
- img = loadImage("grid.jpg");
- size(800,800);
- image(img, 0, 0);
- }
- void draw() {
- fill(255); //player avatar & glow effect
- strokeWeight(2);
- stroke(134,250,255);
- filter(BLUR);
- rect(playerX,playerY,20,20);
- playerX = constrain(playerX, inner, width - inner);
- playerY = constrain(playerY, inner, height - inner);
- fill(0); //AI avatars
- strokeWeight(3);
- stroke(255,145,6);
- for(int i = 0; i < rectX.length; i++){
- rect(rectX[i], rectY[i], rectSize, rectSize);
- rectX = constrain(rectX, inner, width - inner);
- rectY = constrain(rectY, inner, height - inner);
- }
- }
- void keyPressed() {
- if (keyCode == UP) {
- playerY = playerY - 3;
- }
- if (keyCode == RIGHT) {
- playerX = playerX + 3;
- }
- if (keyCode == LEFT) {
- playerX = playerX - 3;
- }
- if (keyCode == DOWN) {
- playerY = playerY + 3;
- }
- }
1