Control number of PShapes in scene with key input - processing

I am trying to make a certain ammount of PShapes display in the scene, this is done by using keys from 0-9. The objects themselves do appear, but respawn endlesly. I am using an arrayList to store them. I suspect there is something wrong with the for loop that reads the shapes from the array... but I cannot figure it out...
import java.io.*;
import java.util.Random;
import java.util.ArrayList;
Random rnd = new Random();
PShape torus;
int nTorus;
ArrayList<PShape> toruses;
void settings() {
size(640, 480, P3D);
}
void setup() {
toruses = new ArrayList();
}
void draw() {
background(0);
for (PShape torus : toruses) {
pushMatrix();
translate((int)(rnd.nextDouble() * 1000-1000), (int)(rnd.nextDouble() * 1000-1000), (int)(rnd.nextDouble() * 1000-1000));
shape(torus);
popMatrix();
}
}
void keyPressed() {
if (key == '1') {
nTorus = 1;
} else if (key == '2') {
nTorus = 2;
} else if (key == '3') {
nTorus = 3;
} else if (key == '4') {
nTorus = 4;
} else if (key == '5') {
nTorus = 5;
} else if (key == '6') {
nTorus = 6;
} else if (key == '7') {
nTorus = 7;
} else if (key == '8') {
nTorus = 8;
} else if (key == '9') {
nTorus = 9;
} else if (key == '0') {
nTorus = 0;
}
toruses.clear();
for (int i = 0; i < nTorus; i++) {
PShape tShape = getTorus((int)(rnd.nextDouble() * 200+50), (int)(rnd.nextDouble() * 100+50), 32, 32);
toruses.add(tShape);
}
}

You've got a few things going on in your code:
Why are you constantly checking the key every time the draw() function is called?
You actually check it twice, since you also call the createTorus() function from draw() for some reason.
The key variable holds the last pressed key, so that big if statement in your draw() function will always be entered.
Instead of constantly checking from the draw() function, you'll be better off using the keyPressed() function. That way you can take action only when the user actually presses a key. Then from the draw() function, the only thing you need to do is actually draw what's in the ArrayList.
Here is a small example that takes that approach to draw points based on user input:
ArrayList<PVector> points = new ArrayList<PVector>();
void setup() {
size(500, 500);
}
void keyPressed() {
int pointCount = 0;
if (key == '1') {
pointCount = 1;
} else if (key == '2') {
pointCount = 2;
} else if (key == '3') {
pointCount = 3;
} else if (key == '4') {
pointCount = 4;
} else if (key == '5') {
pointCount = 5;
} else if (key == '6') {
pointCount = 6;
} else if (key == '7') {
pointCount = 7;
} else if (key == '8') {
pointCount = 8;
} else if (key == '9') {
pointCount = 9;
}
points.clear();
for (int i = 0; i < pointCount; i++) {
points.add(new PVector(random(width), random(height)));
}
}
void draw() {
background(0);
for (PVector point : points) {
ellipse(point.x, point.y, 10, 10);
}
}

Related

I'm trying to get pvectors used in the second frame, but nothing pops up anywhere for the pvector (Used circle too)

I can't see where the circle and not sure what's wrong with my code to make it so I can't see the circle with gravity that I coded. Wondering where I went wrong and what I could do to improve it. Can't seem to see what I've done wrong and I put the circle in gameState2 because I'm making the game in that gameState, gameState1 is for the introduction screen. I want to know why the circle won't appear when I use pvectors. If someone can please help me it would be greatly appreciated(this is an assignment for my class). I hope the comments help show the area in which I need help. (Also if someone could look over my code just for confirmation that I won't get bugs while trying to make the gravity.)
PImage background, backgroundGameState1, headbasketballbackground, player1, player2;
boolean moveRight, moveLeft, moveRight2, moveLeft2;
int canvasSizeX= 1000;
int canvasSizeY = 600;
int mainBackgroundX = 1000;
int mainBackgroundY = 600;
int gameState1 = 1;
int player1X = 100;
int player1Y = 200;
int player2X = 100;
int player2Y = 200;
int backgroundGameState1X= 1000;
int backgroundGameState1Y=600;
int time;
int player1MovmentX = 700;
int player2MovmentX = 100;
PVector Position = new PVector(250, 400);
PVector Velocity = new PVector(0, 0);
PVector Acceleration = new PVector(0, 5);
float elasticity = 0.8;
float airResistance = 0;
void setup() {
//size of canvas is 1000 on the X-axis by 600 on the y-axis
size(1000, 600);
//Loaded images and called them, also made sure to resize them in order to match the canvas size or to make program more asthetically pleasing
background = loadImage("headbasketballbackground.png");
background.resize(mainBackgroundX, mainBackgroundY);
backgroundGameState1 = loadImage("backgroundgamestate1.png");
backgroundGameState1.resize(backgroundGameState1X, backgroundGameState1Y);
player1 = loadImage("steph.png");
//resized image for the steph curry png
player1.resize(player1X, player1Y);
player2 = loadImage("kobe.png");
//resized the png
player2.resize(player2X, player2Y);
time=millis();
}
void draw() {
if (gameState1 == 1) {
background(backgroundGameState1);
if (millis() > time + 1000) {
text("Click On Space To Enter The Game!", 100, 100);
textSize(50);
// delay(3000); Used as a test to see how the delay would work and found it to be extremely slow so I increased it to 1000 milliseconds
}
drawGameState1();
}
if (gameState1 == 2) {
background(background);
image(player1, player1MovmentX, 300);
image(player2, player2MovmentX, 300);
}
if (player1MovmentX < 30) {
player1MovmentX = 40;
}
if (player1MovmentX > 930) {
player1MovmentX = 920;
}
if ( player2MovmentX < 30) {
player2MovmentX = 40;
}
if (player2MovmentX > 930) {
player2MovmentX = 920;
}
// if (gameState2 == 3) {
// text("Congrats you won!");
//}
}
void drawGameState1() {
}
void drawGameState2() {
drawBall();
checkIfBallHitEdge();
moveBall();
drawPlayer1Movment();
drawPlayer2Movment();
//drawBoundaries();
}
void drawGameState3() {
}
void drawPlayer1Movment() {
if (moveRight) {
player1MovmentX += 25;
}
if (moveLeft) {
player1MovmentX -= 25;
}
}
void drawPlayer2Movment() {
if (moveRight2) {
player2MovmentX += 25;
}
if (moveLeft) {
player2MovmentX -= 25;
}
}
//pvectors used here
void drawBall() {
ellipse(Position.x, Position.y, 30, 30);
}
void moveBall() {
Velocity = Velocity.add(Acceleration);
Velocity = Velocity.mult((1-airResistance)); // This slows the ball down a little bit each instant
Position = Position.add(Velocity);
}
//pvectors end here
void checkIfBallHitEdge() {
if (Position.x < 0 || Position.x > width) {
Velocity.x *= -1;
}
if (Position.y < 0) {
Velocity.y *= -1;
}
if (Position.y > height) { // Strikes the ground
Velocity.y *= -elasticity;
Position.y = height;
}
}
void keyPressed() {
if (gameState1 == 1) {
if (keyCode == 32) {
gameState1 = 2;
}
} else if (gameState1 == 2) {
if (keyCode == RIGHT) {
moveRight = true;
player1MovmentX += 25;
}
if (keyCode == LEFT) {
moveLeft = true;
player1MovmentX -= 25;
}
if (keyCode == 68) {
moveRight2 = true;
player2MovmentX += 25;
}
if (keyCode == 65) {
moveLeft2 = true;
player2MovmentX -= 25;
}
if (keyCode == UP) {
Velocity.y = -70;
}
}
}
void keyReleased() {
if (keyCode == RIGHT) {
moveRight = false;
}
if (keyCode == LEFT) {
moveLeft = false;
}
if (keyCode == 68) {
moveRight2 = false;
}
if (keyCode == 65) {
moveLeft2 = false;
}
}
I used the processing IDE for java.

Why won't image 1 move to the right?

I'm very new to coding and was wondering how I could make this image move to the right. I added all of my code so that it would be a bit more understandable for what's going on. The moveRight command will not actually move the png to the right which is why I need help fixing and understanding why it won't work. If someone can please help me it would be greatly appreciated.
PImage background, backgroundGameState1, headbasketballbackground, player1, player2;
boolean moveRight, moveLeft;
int canvasSizeX= 1000;
int canvasSizeY = 600;
int mainBackgroundX = 1000;
int mainBackgroundY = 600;
int gameState1 = 1;
int player1X = 100;
int player1Y = 200;
int player2X = 100;
int player2Y = 200;
int backgroundGameState1X= 1000;
int backgroundGameState1Y=600;
int time;
int player1MovmentX = 100;
int player2MovmentX = 700;
void setup() {
//size of canvas
size(1000, 600);
//Loaded images and called them, also made sure to resize them in order to match the canvas size or to make program more asthetically pleasing
background = loadImage("headbasketballbackground.png");
background.resize(mainBackgroundX, mainBackgroundY);
backgroundGameState1 = loadImage("backgroundgamestate1.png");
backgroundGameState1.resize(backgroundGameState1X, backgroundGameState1Y);
player1 = loadImage("steph.png");
player1.resize(player1X, player1Y);
player2 = loadImage("paul.png");
player2.resize(player2X, player2Y);
time=millis();
}
void draw() {
if (gameState1 == 1) {
background(backgroundGameState1);
if (millis() > time + 1000) {
text("Click On Space To Enter The Game!", 100, 100);
textSize(50);
// delay(3000);
}
drawGameState1();
}
if (gameState1 == 2) {
background(background);
image(player1, player1MovmentX, 300);
image(player2, player2MovmentX, 300);
}
// if (gameState2 == 3) {
// text("Congrats you won!");
//}
}
void drawGameState1() {
}
void drawGameState2() {
drawPlayer1Movment();
}
void drawPlayer1Movment(){
if(moveRight){
player1MovmentX += 25;
}
}
void drawGameState3() {
}
void keyPressed() {
if (gameState1 == 1) {
if (keyCode == 32) {
gameState1 = 2;
}
}
else if(gameState1 == 2){
if(keyCode == RIGHT){
moveRight = true;
}
}
}
void keyReleased(){
if(keyCode == RIGHT){
moveRight = false;
}
}
I don't think you ever called drawPlayer1Movement(). Add it to keyPressed() and player1 should move to the right when you hit the right arrow.
void keyPressed() {
if (gameState1 == 1) {
if (keyCode == 32) {
gameState1 = 2;
}
} else if (gameState1 == 2) {
if (keyCode == RIGHT) {
moveRight = true;
drawPlayer1Movment();
}
}
}

I was wondering if someone could help me re-format my code to fix the loop I made

I was wondering what mistake I made in the line that is formatted like this "''" because I was wondering why my code wouldn't move back to the right to bounce back and forth between walls. I put the code that I need help in with single quotations. This is my frist time on stack, so any tips would be appreciated.
PImage invader, invader2, invader3, invader4, invader5, space, tank;
int tankX = 400;
PImage [] picArray = new PImage [7];
int invaderState = 2;
int timer = 0;
int lap = 0;
int [] alienXPos = {100, 180, 260, 340, 420, 500, 580, 660, 740, 820};
//had to add a few zeros on alienYPos otherwise the loop would bug out and wouldn't work
int[] alienYPos = {40, 140, 240, 340, 0, 0, 0, 0, 0, 0};
boolean moveLeft, moveRight;
int paddleSpeed = 3;
int gameState = 1;
String message1 = "Welcome to space invaders!";
String message2 = "Click space to start!";
boolean movingLeft, movingRight;
void setup() {
size(1000, 1000);
invader = loadImage("invader.jpg");
invader.resize(70, 50);
invader2 = loadImage("invader2.png");
invader2.resize(70, 50);
space = loadImage("space.png");
space.resize(1000, 1000);
tank = loadImage("tank.png");
tank.resize(150, 100);
}
void draw() {
background(space);
timer = millis();
if (timer-lap>800) {
lap = timer;
' **for (int m=0; m <10; m++) {
if (movingRight == false) {
alienXPos[m] += 40;
} else {
alienXPos[m] -= 40;
}
if (alienXPos [m] > 900 && movingRight == false) {
movingRight = true;
for (int l=0; l<10; l++) {
alienYPos[l] += 75;
println("movingleft : " + movingLeft);
println("Moving right : " + movingRight);
}
// if(movingLeft == false){
// alienXPos[m] -= 55;
//}
//else{
// alienXPos [m] += 40;
//}
if (movingLeft == false) {
//alienXPos[m] -=55;
} /*else {
alienXPos[m] ;
}*/
if (alienXPos[m] < 100 && movingLeft == true) {
movingLeft = false;
movingRight = true;
/*for (int l=0; l<10; l++) {
alienYPos[l] += 75;
}** '
println("movingLeft : " + movingLeft);
*/
}
}
}
/* if (alienXPos[m] > 0 && movingLeft == true) {
alienXPos[m] -= 55;
}*/
if (invaderState == 1) {
invaderState = 2;
} else {
invaderState = 1;
}
}
if (tankX > width) {
tankX = 0;
}
if (tankX < 0) {
tankX = 1000;
}
if (gameState == 1) {
drawGameState1();
} else if (gameState == 2) {
drawGameState2();
}
}
void drawGameState1() {
background(#222222);
fill(#000000);
textSize(36);
fill(130, 130, 130);
text(message1, 300, 450);
textSize(20);
text(message2, 430, 600);
}
void drawGameState2() {
background(space);
drawSpaceInvader1();
drawTank();
drawTankMovement();
}
void drawSpaceInvader1() {
for (int i=0; i< 10; i++) {
if (invaderState == 1) {
image(invader, alienXPos[i], alienYPos[0]);
image(invader, alienXPos[i], alienYPos[1]);
image(invader, alienXPos[i], alienYPos[2]);
image(invader, alienXPos[i], alienYPos[3]);
} else if (invaderState == 2) {
image(invader2, alienXPos[i], alienYPos[0]);
image(invader2, alienXPos[i], alienYPos[1]);
image(invader2, alienXPos[i], alienYPos[2]);
image(invader2, alienXPos[i], alienYPos[3]);
}
}
}
void drawTank() {
image(tank, tankX, 700);
}
void drawTankMovement() {
if (moveLeft) {
tankX -= 25;
}
if (moveRight) {
tankX += 25;
}
}
void keyPressed() {
if (gameState == 1) {
if (keyCode == 32) {
gameState = 2;
}
}
if (gameState == 2) {
if (keyCode == LEFT) {
moveLeft = true;
}
if (keyCode == RIGHT) {
moveRight = true;
}
}
}
void keyReleased() {
if (keyCode == LEFT) { // Left Key
moveLeft = false;
}
if (keyCode == RIGHT) { // Right Key
moveRight = false;
}
}
A few things:
Next time, please try and provide a minimal, reproducible example, you were almost there, but you forgot to attach the image files that you use to draw your game out. Running this code gave me errors saying I was missing a few files. I took the liberty of importing some nonsense image and reusing that as your missing assets.
Also missing from your answer is a more implicit title. I think you misunderstood the term "reformatting" code, as that generally refers to the formatting of code. To make your answer likely to be found by others with the same problem, please consider changing the title to what your question is actually about: You can't figure out why your code doesn't reverse the movement of your row of space invaders. Next time, please read this guide on how to write a proper question.
Now to answer your question:
I ran your code and noticed that movingLeft was false whilst movingRight was true. Yet your invaders seem to be running towards the left side of the screen. One of your mistakes was probably getting confused by these variables.
In your code you do
if (movingRight == false) {
alienXPos[m] += 40;
} else {
alienXPos[m] -= 40;
}
Which effectively does the opposite of what you want. In coordinates are oriented like this, assuming we have a 800x400 canvas:
So if you want your invaders to move right, you should be adding to the coordinates when the bool is true, not when it's false. Your logic which flips the invaders direction should be sound:
if (alienXPos[m] < 100 && movingLeft == true) {
movingLeft = false;
movingRight = true;
}
But since in your program due to the above logic error movingLeft is never true, this statement will never run.
One suggestion is that in the simple case of space invaders you don't really need to keep track of two seperate moving variables for each direction, after all, invaders either:
Move to the right
or
Move to the left
Thus you could ditch having two variables and just check, for example, movingRight. If that is true, the invaders should be moving to the right, and if it's false, they should be moving to the left.

Processing-A shape is selected by either pressing a key ‘1’ or or key ‘2’ so that shape 1 or shape 2 can be picked up respectively

Draw two shapes on the sketch (e.g. a rectangle and a circle). Use the UP, DOWN, LEFT and RIGHT keys to control the movement of the selected shape. A shape is selected by either pressing a key ‘1’ or or key ‘2’ so that shape 1 or shape 2 can be picked up respectively.I want to select the shape by pressing the key "1" or "2", but they cannot run.`
int x = 0;
int y = 0;
int ex= 0;
int ey= 0;
void setup(){
size (400, 400);
}
void draw(){
background(80);
rect(x, y, 25,25);
ellipse(50, 50, 50, 50);
}
void keyPresse() {
if ( (key == '1')) {
if (keyCode == UP) {
y -= 10;
} else if (keyCode == DOWN) {
y += 10;
} else if (keyCode == LEFT) {
x -= 10;
} else if (keyCode == RIGHT) {
x += 10;
}
} else if ((key == '2')){
if (keyCode == UP) {
ey -= 10;
} else if (keyCode == DOWN) {
ey += 10;
} else if (keyCode == LEFT) {
ex -= 10;
} else if (keyCode == RIGHT) {
ex += 10;
}
}
}
There is a typo. The name of the key board callback is keyPressed. However, there are also some logical issues.
Crate an array for the x and y coordinates. And an index variabel (shape_i):
int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0;
Draw the shapes on its position. (x[0], y[0]) is the position of the rectangle and (x[1], y[1]) is the position of the ellipse:
void draw(){
background(80);
rect(x[0], y[0], 25, 25);
ellipse(x[1], y[1], 50, 50);
}
Change the index (shape_i) when 1 or 2 is pressed. Change (x[shape_i], y[shape_i]) when an arrow key is pressed:
void keyPressed() {
if (key == '1') {
shape_i = 0;
} else if (key == '2') {
shape_i = 1;
} else if (keyCode == UP) {
y[shape_i] -= 10;
} else if (keyCode == DOWN) {
y[shape_i] += 10;
} else if (keyCode == LEFT) {
x[shape_i] -= 10;
} else if (keyCode == RIGHT) {
x[shape_i] += 10;
}
}
Complete example:
int x[] = new int[]{100, 100};
int y[] = new int[]{200, 100};
int shape_i = 0;
void setup(){
size (400, 400);
}
void draw(){
background(80);
rect(x[0], y[0], 25, 25);
ellipse(x[1], y[1], 50, 50);
}
void keyPressed() {
if (key == '1') {
shape_i = 0;
} else if (key == '2') {
shape_i = 1;
} else if (keyCode == UP) {
y[shape_i] -= 10;
} else if (keyCode == DOWN) {
y[shape_i] += 10;
} else if (keyCode == LEFT) {
x[shape_i] -= 10;
} else if (keyCode == RIGHT) {
x[shape_i] += 10;
}
}

Processing generating a random amount of text

I am trying to generate some random text using processing, what I want is that everytime I press the mouse new text is generated and is displayed on the screen. As of now the text is simply generated than it gets removed due to the looping of draw() any way to fix this?
int click = 0;
void setup() {
String alfabet = "abcdefghijklmnopqrstuvw";
size(1000,1000);
textSize(64);
textAlign(CENTER);
}
void draw() {
background(0);
if(click==1) {
click = 0;
genereren();
}
}
void genereren() {
String alfabet = "abcdefghijklmnopqrstuvw";
int x = 10;
for(int i = 0; i < 15; i = i+1) {
float r = random(24);
if(r < 1) {
r = r+1;
}
int d = int(r);
String EersteLetter = alfabet.substring(d-1,d);
if ( i <= 4) {
text(EersteLetter, 60+(x*3*i), 80);
}
if ( i <= 8) {
text(EersteLetter, 60+(x*3*i), 120);
}
if ( i <= 12) {
text(EersteLetter, 60+(x*3*i), 160);
}
if ( i <= 16) {
text(EersteLetter, 60+(x*3*i), 200);
}
}
}
void mouseClicked() {
click = 1;
}
try this example, if you click the mouse it will display or not, if you hold the mouse button you will freeze the current text.
boolean click = false;
void setup() {
String alfabet = "abcdefghijklmnopqrstuvw";
size(1000, 1000);
textSize(64);
textAlign(CENTER);
background(0);
}
void draw() {
if (click) {
genereren();
}
}
void mousePressed() {
if (mouseButton == LEFT) {
genereren();
}
}
void genereren() {
background(0);
String alfabet = "abcdefghijklmnopqrstuvw";
int x = 10;
for (int i = 0; i < 15; i = i+1) {
float r = random(24);
if (r < 1) {
r = r+1;
}
int d = int(r);
String EersteLetter = alfabet.substring(d-1, d);
if ( i <= 4) {
text(EersteLetter, 60+(x*3*i), 80);
}
if ( i <= 8) {
text(EersteLetter, 60+(x*3*i), 120);
}
if ( i <= 12) {
text(EersteLetter, 60+(x*3*i), 160);
}
if ( i <= 16) {
text(EersteLetter, 60+(x*3*i), 200);
}
}
}
void mouseReleased() {
clear();
}
void mouseClicked() {
click=!click;
}
The easiest way to do this would be just by not using the "background(0);", this way the text would stay for ever. Then you could add a button that runs a method whit the "background(0);" to erase all text.

Resources