We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hey y'all, hope you've been well. Well, I've been working on a Pong Game and am close to finishing it; I just need sound and secondly I'd like to have a window pop-up before the game to allow the user to choose a level (Difficulty; and whether they want sound or not. This is my code:
//int creates variables
//boolean creates false/true option
//color sets color for item
int x, y, w, h, speedX, speedY;
int paddleXL, paddleYL, paddleW, paddleH, paddleS;
int paddleXR, paddleYR;
boolean upL, downL;
boolean upR, downR;
//colorL and colorR are not needed, but it can be helpful
color colorL = color(255,0,0);
color colorR = color(0, 255, 0, 50);
//scoreL is for left score and scoreR is for the right score
int scoreL = 0;
int scoreR = 0;
//this sets the final winScore to a count of 10
int winScore = 10;
//Sets the game and window size up, along with the background and other important things
//happens only once; at the beginning of the game
void setup() {
//this sets the window size
size(500, 500);
//the x,y,w,h get the ball to begin at the center of the game
x = width/2;
y = height/2;
w = 50;
h = 50;
//this allows the ball to bounce
speedX = 4;
speedY = 4;
//this creates a text
textSize(30);
textAlign(CENTER, CENTER); //this centers the height and width of the text
rectMode(CENTER); //this allows both paddles to be centered
//This sets the paddles to a certain place on the window, and makes them visible
paddleXL = 40;
paddleYL = height/2;
paddleXR = width-40;
paddleYR = height/2;
paddleW = 30;
paddleH = 100;
paddleS = 5;
}
//Draws the canvas for the game
void draw() {
background(0);
drawCircle();
moveCircle();
bounceOff();
drawPaddles();
movePaddle();
restrictPaddle();
contactPaddle();
scores();
gameOver();
}
//creates paddles
void drawPaddles() {
fill(colorL);
rect(paddleXL, paddleYL, paddleW, paddleH);
fill(colorR);
rect(paddleXR, paddleYR, paddleW, paddleH);
}
//Makes both paddles move
void movePaddle() {
if (upL) {
paddleYL = paddleYL - paddleS;
}
if (downL) {
paddleYL = paddleYL + paddleS;
}
if (upR) {
paddleYR = paddleYR - paddleS;
}
if (downR) {
paddleYR = paddleYR + paddleS;
}
}
//detects when a paddle has hit the wall
void restrictPaddle() {
if (paddleYL - paddleH/2 < 0) {
paddleYL = paddleYL + paddleS;
}
if (paddleYL + paddleH/2 > height) {
paddleYL = paddleYL - paddleS;
}
if (paddleYR - paddleH/2 < 0) {
paddleYR = paddleYR + paddleS;
}
if (paddleYR + paddleH/2 > height) {
paddleYR = paddleYR - paddleS;
}
}
//This detects left and right collision; respectively
void contactPaddle() {
if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) {
if (speedX < 0) {
speedX = -speedX*1;
}
}
else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) {
if (speedX > 0) {
speedX = -speedX*1;
}
}
}
//creates circle
void drawCircle() {
fill(0,0,255);
ellipse(x, y, w, h);
}
//this allows the circle to bounce
void moveCircle() {
x = x + speedX*2;
y = y + speedY*2;
}
//This detects if the ball has bounced or not
void bounceOff() {
if ( x > width - w/2) {
setup();
speedX = -speedX;
scoreL = scoreL + 1;
} else if ( x < 0 + w/2) {
setup();
scoreR = scoreR + 1;
}
if ( y > height - h/2) {
speedY = -speedY;
} else if ( y < 0 + h/2) {
speedY = -speedY;
}
}
//This provides the user to see the score
void scores() {
fill(255);
text(scoreL, 100, 50);
text(scoreR, width-100, 50);
}
//This determines who wins
void gameOver() {
if(scoreL == winScore) {
gameOverPage("Red wins!", colorL);
}
if(scoreR == winScore) {
gameOverPage("Dark-Green wins!", colorR);
}
}
//This stops the ball from bouncing around, gets the user to know that the game is over
void gameOverPage(String text, color c) {
speedX = 0;
speedY = 0;
fill(255);
text("Game over", width/2, height/3 - 40);
text("Click to play again", width/2, height/3 + 40);
fill(c);
text(text, width/2, height/3);
//this gets the user to click on the screen to play again
if(mousePressed) {
//this sets the score to zero
scoreR = 0;
scoreL = 0;
//this gets the speed to a 1
speedX = 1;
speedY = 1;
}
}
//this gets the user to press a key and allow the paddle(s) to move
void keyPressed() {
if (key == 'w' || key == 'W') {
upL = true;
}
if (key == 's' || key == 'S') {
downL = true;
}
if (keyCode == UP) {
upR = true;
}
if (keyCode == DOWN) {
downR = true;
}
}
//this creates a false key interaction
void keyReleased() {
if (key == 'w' || key == 'W') {
upL = false;
}
if (key == 's' || key == 'S') {
downL = false;
}
if (keyCode == UP) {
upR = false;
}
if (keyCode == DOWN) {
downR = false;
}
}
Also, if there is a way to increase the speed of the ball with every added score.
Answers
Look below
I am going to do this in steps, starting with sound:
Nvm, found that out.
But I'm receiving a, null pointer exception error for this code (with sound):
//import processing.sound.*; //AudioDevice myAudioServer; //int creates variables //boolean creates false/true option //color sets color for item import ddf.minim.*; import ddf.minim.ugens.*; Minim minim; MultiChannelBuffer sampleBuffer; AudioOutput output; Sampler sampler; int x, y, w, h, speedX, speedY; int paddleXL, paddleYL, paddleW, paddleH, paddleS; int paddleXR, paddleYR; boolean upL, downL; boolean upR, downR; //colorL and colorR are not needed, but it can be helpful color colorL = color(255,0,0); color colorR = color(0, 255, 0, 50); //scoreL is for left score and scoreR is for the right score int scoreL = 0; int scoreR = 0; //this sets the final winScore to a count of 10 int winScore = 10; //Sets the game and window size up, along with the background and other important things //happens only once; at the beginning of the game void setup() { //this sets the window size size(500, 500,P3D); // create Minim and an AudioOutput minim = new Minim(this); output = minim.getLineOut(); // construct a new MultiChannelBuffer with 2 channels and 1024 sample frames. // in our particular case, it doesn't really matter what we choose for these // two values because loadFileIntoBuffer will reconfigure the buffer // to match the channel count and length of the file. sampleBuffer = new MultiChannelBuffer( 1, 1024 ); // we pass the buffer to the method and Minim will reconfigure it to match // the file. if the file doesn't exist, or there is some other problen with // loading it, the function will return 0 as the sample rate. float sampleRate = minim.loadFileIntoBuffer( "SD.wav", sampleBuffer ); // make sure the file load worked if ( sampleRate > 0 ) { // double the size of the buffer to give ourselves some silence to play with int originalBufferSize = sampleBuffer.getBufferSize(); sampleBuffer.setBufferSize( originalBufferSize * 2 ); // go through first half of the buffer, which contains the original sample, // and add a delayed version of each sample at some random position. // we happen to know that the source file is only one channel // but in general you'd want to iterate over all channels when doing something like this for( int s = 0; s < originalBufferSize; ++s ) { int delayIndex = s + int( random( 0, originalBufferSize ) ); float sampleValue = sampleBuffer.getSample( 0, s ); float destValue = sampleBuffer.getSample( 0, delayIndex ); sampleBuffer.setSample( 0, // channel delayIndex, // sample frame to set sampleValue + destValue // the value to set ); } // create a sampler that will use our buffer to generate audio. // we must provide the sample rate of the audio and the number of voices. sampler = new Sampler( sampleBuffer, sampleRate, 1 ); // and finally, connect to the output so we can hear it sampler.patch( output ); // myAudioServer = new AudioDevice (this, 44100, 128) ; //the x,y,w,h get the ball to begin at the center of the game x = width/2; y = height/2; w = 50; h = 50; //this allows the ball to bounce speedX = 4; speedY = 4; //this creates a text textSize(30); textAlign(CENTER, CENTER); //this centers the height and width of the text rectMode(CENTER); //this allows both paddles to be centered //This sets the paddles to a certain place on the window, and makes them visible paddleXL = 40; paddleYL = height/2; paddleXR = width-40; paddleYR = height/2; paddleW = 30; paddleH = 100; paddleS = 5; } } //Draws the canvas for the game void draw() { background(0); //stroke(255); drawCircle(); moveCircle(); bounceOff(); drawPaddles(); movePaddle(); restrictPaddle(); contactPaddle(); scores(); gameOver(); } //creates paddles void drawPaddles() { fill(colorL); rect(paddleXL, paddleYL, paddleW, paddleH); fill(colorR); rect(paddleXR, paddleYR, paddleW, paddleH); } //Makes both paddles move void movePaddle() { if (upL) { paddleYL = paddleYL - paddleS; } if (downL) { paddleYL = paddleYL + paddleS; } if (upR) { paddleYR = paddleYR - paddleS; } if (downR) { paddleYR = paddleYR + paddleS; } } //detects when a paddle has hit the wall void restrictPaddle() { if (paddleYL - paddleH/2 < 0) { paddleYL = paddleYL + paddleS; } if (paddleYL + paddleH/2 > height) { paddleYL = paddleYL - paddleS; } if (paddleYR - paddleH/2 < 0) { paddleYR = paddleYR + paddleS; } if (paddleYR + paddleH/2 > height) { paddleYR = paddleYR - paddleS; } } //This detects left and right collision; respectively void contactPaddle() { if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) { if (speedX < 0) { speedX = -speedX*1; } } else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) { if (speedX > 0) { speedX = -speedX*1; } } } //creates circle void drawCircle() { fill(0,0,255); ellipse(x, y, w, h); } //this allows the circle to bounce void moveCircle() { x = x + speedX*2; y = y + speedY*2; } //This detects if the ball has bounced or not void bounceOff() { if ( x > width - w/2) { setup(); speedX = -speedX; scoreL = scoreL + 1; } else if ( x < 0 + w/2) { setup(); scoreR = scoreR + 1; } if ( y > height - h/2) { speedY = -speedY; } else if ( y < 0 + h/2) { speedY = -speedY; } } //This provides the user to see the score void scores() { fill(colorL); text("Score: " + scoreL, 100, 50); fill(colorR); text("Score: " + scoreR, width-100, 50); } //This determines who wins void gameOver() { if(scoreL == winScore) { gameOverPage("Red wins!", colorL); } if(scoreR == winScore) { gameOverPage("Dark-Green wins!", colorR); } } //This stops the ball from bouncing around, gets the user to know that the game is over void gameOverPage(String text, color c) { speedX = 0; speedY = 0; fill(255); text("Game over", width/2, height/3 - 40); text("Click to play again", width/2, height/3 + 40); sampler.trigger() ; fill(c); text(text, width/2, height/3); //this gets the user to click on the screen to play again if(mousePressed) { //this sets the score to zero scoreR = 0; scoreL = 0; //this gets the speed to a 1 speedX = 1; speedY = 1; } } //this gets the user to press a key and allow the paddle(s) to move void keyPressed() { if (key == 'w' || key == 'W') { upL = true; } if (key == 's' || key == 'S') { downL = true; } if (keyCode == UP) { upR = true; } if (keyCode == DOWN) { downR = true; } } //this creates a false key interaction void keyReleased() { if (key == 'w' || key == 'W') { upL = false; } if (key == 's' || key == 'S') { downL = false; } if (keyCode == UP) { upR = false; } if (keyCode == DOWN) { downR = false; } }Where is the error and how can I fix it?
Well, now it's giving me a 'could not run sketch' error, [target VM failed to initialize]...cannot seem to be able to find that out, help would be appreciated. Thanks.
well, you are using not sound library but minim - nvm
look at the minim examples - you took the wrong one
Ok, I just need the background music; that's all that is left.
(I decided to skip the level thing and just have one window where (as the game progresses) the game gets harder.)
I would like to have sound effects for: - ball hitting the paddle - paddle dodges ball - an added score (for this I'd like two different sound effects to know who got an added score without looking at the score board) -and one final one for a game over
Thanks.
yes
look at the minim examples
load it in setup()
play it
rewind it
;-)
I cannot seem to find a minim example that would work in my code....
Look at TriggerASample.
I tried entering it into my code but it gave me a Null Pointer Exception.
show your code then
import ddf.minim.*; Minim minim; AudioSample kick; AudioSample snare; PImage bg ; int num; int time = 1; int t ; int x, y, w, h, speedX, speedY; int paddleXL, paddleYL, paddleW, paddleH, paddleS; int paddleXR, paddleYR; boolean upL, downL; boolean upR, downR; //colorL and colorR are not needed, but it can be helpful color colorL = color(225, 115,0); color colorR = color(0, 255, 0); //scoreL is for left score and scoreR is for the right score int scoreL = 0; int scoreR = 0; //this sets the final winScore to a count of 10 int winScore = 1; //Sets the game and window size up, along with the background and other important things //happens only once; at the beginning of the game void setup() { //this sets the window size size(500, 500); minim = new Minim(this); // load BD.wav from the data folder kick = minim.loadSample( "BD.mp3", // filename 512 // buffer size ); // An AudioSample will spawn its own audio processing Thread, // and since audio processing works by generating one buffer // of samples at a time, we can specify how big we want that // buffer to be in the call to loadSample. // above, we requested a buffer size of 512 because // this will make the triggering of the samples sound more responsive. // on some systems, this might be too small and the audio // will sound corrupted, in that case, you can just increase // the buffer size. // if a file doesn't exist, loadSample will return null if ( kick == null ) println("Didn't get kick!"); // load SD.wav from the data folder snare = minim.loadSample("SD.wav", 512); if ( snare == null ) println("Didn't get snare!"); //the x,y,w,h get the ball to begin at the center of the game x = width/2; y = height/2; w = 50; h = 50; // //this allows the ball to bounce // speedX = 4; // speedY = 4; //this creates a text textSize(30); textAlign(CENTER, CENTER); //this centers the height and width of the text rectMode(CENTER); //this allows both paddles to be centered //this sets up text for the beginning of the game //fill(120,6,152) ; text("Press the enter bar to start the game, press the 'p' key to pause the game. Good luck! " + "Go!", 10,350) ; //This sets the paddles to a certain place on the window, and makes them visible paddleXL = 17; paddleYL = height/2; paddleXR = width-17; paddleYR = height/2; paddleW = 30; paddleH = 100; paddleS = 5; bg = loadImage("pp.jpg") ; } //Draws the canvas for the game void draw() { background(bg); t++; if (t > height) { t = 0; } drawCircle(); moveCircle(); bounceOff(); drawPaddles(); movePaddle(); restrictPaddle(); contactPaddle(); scores(); gameOver(); if (keyPressed) { if (key=='p' || key=='P') { speedX = 0 ; speedY = 0 ; } } } //creates paddles void drawPaddles() { fill(colorL); rect(paddleXL, paddleYL, paddleW, paddleH); fill(colorR); rect(paddleXR, paddleYR, paddleW, paddleH); } //Makes both paddles move void movePaddle() { if (upL) { paddleYL = paddleYL - paddleS; } if (downL) { paddleYL = paddleYL + paddleS; } if (upR) { paddleYR = paddleYR - paddleS; } if (downR) { paddleYR = paddleYR + paddleS; } } //detects when a paddle has hit the wall void restrictPaddle() { if (paddleYL - paddleH/2 < 0) { paddleYL = paddleYL + paddleS; } if (paddleYL + paddleH/2 > height) { paddleYL = paddleYL - paddleS; } if (paddleYR - paddleH/2 < 0) { paddleYR = paddleYR + paddleS; } if (paddleYR + paddleH/2 > height) { paddleYR = paddleYR - paddleS; } } //This detects left and right collision; respectively void contactPaddle() { if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) { if (speedX < 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } } else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) { if (speedX > 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } } } //creates circle void drawCircle() { fill(0,0,255); ellipse(x, y, w, h); } //this allows the circle to bounce void moveCircle() { x = x + speedX*2; y = y + speedY*2; } //This detects if the ball has bounced or not void bounceOff() { if ( x > width - w/2) { setup(); speedX = -speedX*1; scoreL = scoreL + 1; time = time + 1 ; } else if ( x < 0 + w/2) { setup(); scoreR = scoreR + 1; time = time + 1 ; } if ( y > height - h/2) { speedY = -speedY; } else if ( y < 0 + h/2) { speedY = -speedY*1; } } //This provides the user to see the score void scores() { fill(colorL); text("Score: " + scoreL, 100, 50); fill(colorR); text("Score: " + scoreR, width-100, 50); } //This determines who wins void gameOver() { if(scoreL == winScore) { gameOverPage("Orange wins!", colorL); } if(scoreR == winScore) { gameOverPage("Dark-Green wins!", colorR); } } //This stops the ball from bouncing around, gets the user to know that the game is over void gameOverPage(String text, color c) { speedX = 0; speedY = 0; fill(255); text("Game over", width/2, height/3 - 40); text("Press enter to play again", width/2, height/3 + 40); fill(c); text(text, width/2, height/3); //this gets the user to click on the screen to play again if(keyPressed) { if (key==ENTER) { //this sets the score to zero scoreR = 0; scoreL = 0; //this gets the speed to a 1 speedX = 1; speedY = 1; winScore = winScore + 1 ; } } } //this gets the user to press a key and allow the paddle(s) to move void keyPressed() { if (key == 'w' || key == 'W') { upL = true; } if (key == 's' || key == 'S') { downL = true; } if (keyCode == UP) { upR = true; } if (keyCode == DOWN) { downR = true; } if (keyCode== ENTER) { speedX = 1 ; speedY = 1 ; } } //this creates a false key interaction void keyReleased() { if (key == 'w' || key == 'W') { upL = false; } if (key == 's' || key == 'S') { downL = false; } if (keyCode == UP) { upR = false; } if (keyCode == DOWN) { downR = false; } }I notice that the comment says use file BD.wav but the code says BD.mp3. Could this be a somewhat opaque way of saying file not found?
the code that appears on my window screen is a Null Pointer Exception error, though...
yes, could be caused by that
file name must match exactly
please change it
I don't have a file...
???
BD.wav is your file?
you must have an wave file?
https://www.freesound.org/browse/tags/sound-effects/
Please wait until I download a file
I've downloaded the file, 'fatbass' (just to try out) and it's still giving me the Null Pointer Exception:
import ddf.minim.*; Minim minim; AudioSample kick; AudioSample snare; PImage bg ; int num; int time = 1; int t ; int x, y, w, h, speedX, speedY; int paddleXL, paddleYL, paddleW, paddleH, paddleS; int paddleXR, paddleYR; boolean upL, downL; boolean upR, downR; //colorL and colorR are not needed, but it can be helpful color colorL = color(225, 115,0); color colorR = color(0, 255, 0); //scoreL is for left score and scoreR is for the right score int scoreL = 0; int scoreR = 0; //this sets the final winScore to a count of 10 int winScore = 1; //Sets the game and window size up, along with the background and other important things //happens only once; at the beginning of the game void setup() { //this sets the window size size(500, 500); minim = new Minim(this); // load BD.wav from the data folder kick = minim.loadSample( "219343_fatbass.cd"); // filenam // An AudioSample will spawn its own audio processing Thread, // and since audio processing works by generating one buffer // of samples at a time, we can specify how big we want that // buffer to be in the call to loadSample. // above, we requested a buffer size of 512 because // this will make the triggering of the samples sound more responsive. // on some systems, this might be too small and the audio // will sound corrupted, in that case, you can just increase // the buffer size. // if a file doesn't exist, loadSample will return null if ( kick == null ) println("Didn't get kick!"); // load SD.wav from the data folder snare = minim.loadSample("SD.wav", 512); if ( snare == null ) println("Didn't get snare!"); //the x,y,w,h get the ball to begin at the center of the game x = width/2; y = height/2; w = 50; h = 50; // //this allows the ball to bounce // speedX = 4; // speedY = 4; //this creates a text textSize(30); textAlign(CENTER, CENTER); //this centers the height and width of the text rectMode(CENTER); //this allows both paddles to be centered //this sets up text for the beginning of the game //fill(120,6,152) ; text("Press the enter bar to start the game, press the 'p' key to pause the game. Good luck! " + "Go!", 10,350) ; //This sets the paddles to a certain place on the window, and makes them visible paddleXL = 17; paddleYL = height/2; paddleXR = width-17; paddleYR = height/2; paddleW = 30; paddleH = 100; paddleS = 5; bg = loadImage("pp.jpg") ; } //Draws the canvas for the game void draw() { background(bg); t++; if (t > height) { t = 0; } drawCircle(); moveCircle(); bounceOff(); drawPaddles(); movePaddle(); restrictPaddle(); contactPaddle(); scores(); gameOver(); if (keyPressed) { if (key=='p' || key=='P') { speedX = 0 ; speedY = 0 ; } } } //creates paddles void drawPaddles() { fill(colorL); rect(paddleXL, paddleYL, paddleW, paddleH); fill(colorR); rect(paddleXR, paddleYR, paddleW, paddleH); } //Makes both paddles move void movePaddle() { if (upL) { paddleYL = paddleYL - paddleS; } if (downL) { paddleYL = paddleYL + paddleS; } if (upR) { paddleYR = paddleYR - paddleS; } if (downR) { paddleYR = paddleYR + paddleS; } } //detects when a paddle has hit the wall void restrictPaddle() { if (paddleYL - paddleH/2 < 0) { paddleYL = paddleYL + paddleS; } if (paddleYL + paddleH/2 > height) { paddleYL = paddleYL - paddleS; } if (paddleYR - paddleH/2 < 0) { paddleYR = paddleYR + paddleS; } if (paddleYR + paddleH/2 > height) { paddleYR = paddleYR - paddleS; } } //This detects left and right collision; respectively void contactPaddle() { if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) { if (speedX < 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } } else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) { if (speedX > 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } } } //creates circle void drawCircle() { fill(0,0,255); ellipse(x, y, w, h); } //this allows the circle to bounce void moveCircle() { x = x + speedX*2; y = y + speedY*2; } //This detects if the ball has bounced or not void bounceOff() { if ( x > width - w/2) { setup(); speedX = -speedX*1; scoreL = scoreL + 1; time = time + 1 ; } else if ( x < 0 + w/2) { setup(); scoreR = scoreR + 1; time = time + 1 ; } if ( y > height - h/2) { speedY = -speedY; } else if ( y < 0 + h/2) { speedY = -speedY*1; } } //This provides the user to see the score void scores() { fill(colorL); text("Score: " + scoreL, 100, 50); fill(colorR); text("Score: " + scoreR, width-100, 50); } //This determines who wins void gameOver() { if(scoreL == winScore) { gameOverPage("Orange wins!", colorL); } if(scoreR == winScore) { gameOverPage("Dark-Green wins!", colorR); } } //This stops the ball from bouncing around, gets the user to know that the game is over void gameOverPage(String text, color c) { speedX = 0; speedY = 0; fill(255); text("Game over", width/2, height/3 - 40); text("Press enter to play again", width/2, height/3 + 40); fill(c); text(text, width/2, height/3); //this gets the user to click on the screen to play again if(keyPressed) { if (key==ENTER) { //this sets the score to zero scoreR = 0; scoreL = 0; //this gets the speed to a 1 speedX = 1; speedY = 1; winScore = winScore + 1 ; } } } //this gets the user to press a key and allow the paddle(s) to move void keyPressed() { if (key == 'w' || key == 'W') { upL = true; } if (key == 's' || key == 'S') { downL = true; } if (keyCode == UP) { upR = true; } if (keyCode == DOWN) { downR = true; } if (keyCode== ENTER) { speedX = 1 ; speedY = 1 ; } } //this creates a false key interaction void keyReleased() { if (key == 'w' || key == 'W') { upL = false; } if (key == 's' || key == 'S') { downL = false; } if (keyCode == UP) { upR = false; } if (keyCode == DOWN) { downR = false; } }line 34
.cd
what is that?
you need
.mp3
or
.wav
Neither work
Do you have a .mp3 or .wav file? Please assure me that you are not just changing the file's extension.
I honestly do not know, I downloaded the music file and it did not tell me whether it was mp3 or wav so I tried both...I do have a music folder in my program, though.
you don't know... whether it's mp3 or wav...
woe, that's basic PC knowledge
you got a music folder in your program? do you mean in the processing sketch folder?
you have to register on the site
this is a wave
https://www.freesound.org/people/Goup_1/sounds/195396/
Just realized that it was a wave and that I opened the file instead of saving it...well that sucks because I just exceeded the download max for the internet thus I cannot work with sound anymore....
Anywho, I'd like to add two balls to the game after three times of restarting the game and the other after six times. How would I go about doing it?
Here's a little program: http://www.drpetter.se/project_sfxr.html It's only 50 kb so you should be able to download it. It exports as wav. The produced samples should be great for a pong game.
If you're using Windows, do this: http://windows.microsoft.com/en-us/windows/show-hide-file-name-extensions#show-hide-file-name-extensions=windows-7 so it shows your file extensions. Then you can immediately see if you're dealing with a wave file.
I have the code and it allows me to run the program but no sound is heard...would you like to see my code?
And thank you so very much, colouredmirrorball, that was very helpful :)
when you can cut audio files (use audacity)
http://www.drpetter.se/files/sfxdemo.mp3
That link does not open on my browser, chrisir, do not know why
What is wrong with my code?
import ddf.minim.*; Minim minim; AudioSample play; AudioSample stop; PImage bg ; int num; int time = 1; int t ; int x, y, w, h, speedX, speedY; int paddleXL, paddleYL, paddleW, paddleH, paddleS; int paddleXR, paddleYR; boolean upL, downL; boolean upR, downR; //colorL and colorR are not needed, but it can be helpful color colorL = color(225, 115,0); color colorR = color(0, 255, 0); //scoreL is for left score and scoreR is for the right score int scoreL = 0; int scoreR = 0; //this sets the final winScore to a count of 10 int winScore = 1; //Sets the game and window size up, along with the background and other important things //happens only once; at the beginning of the game void setup() { //this sets the window size size(500, 500); minim = new Minim(this); frameRate (60); smooth(); // load BD.wav from the data folder minim.loadSample( "ball.wav", 512) ; // An AudioSample will spawn its own audio processing Thread, // and since audio processing works by generating one buffer // of samples at a time, we can specify how big we want that // buffer to be in the call to loadSample. // above, we requested a buffer size of 512 because // this will make the triggering of the samples sound more responsive. // on some systems, this might be too small and the audio // will sound corrupted, in that case, you can just increase // the buffer size. // // if a file doesn't exist, loadSample will return null // if ( kick == null ) println("Didn't get kick!"); // // // load SD.wav from the data folder // if ( snare == null ) println("Didn't get snare!"); //the x,y,w,h get the ball to begin at the center of the game x = width/2; y = height/2; w = 50; h = 50; // //this allows the ball to bounce // speedX = 4; // speedY = 4; //this creates a text textSize(30); textAlign(CENTER, CENTER); //this centers the height and width of the text rectMode(CENTER); //this allows both paddles to be centered //this sets up text for the beginning of the game //fill(120,6,152) ; text("Press the enter bar to start the game, press the 'p' key to pause the game. Good luck! " + "Go!", 10,350) ; //This sets the paddles to a certain place on the window, and makes them visible paddleXL = 17; paddleYL = height/2; paddleXR = width-17; paddleYR = height/2; paddleW = 30; paddleH = 100; paddleS = 5; bg = loadImage("pp.jpg") ; } //Draws the canvas for the game void draw() { background(bg); t++; if (t > height) { t = 0; } drawCircle(); moveCircle(); bounceOff(); drawPaddles(); movePaddle(); restrictPaddle(); contactPaddle(); scores(); gameOver(); if (keyPressed) { if (key=='p' || key=='P') { speedX = 0 ; speedY = 0 ; } if (key=='m' || key=='m') { play.trigger(); } else { if (key=='s' || key=='S') { stop.trigger(); } } } } //creates paddles void drawPaddles() { fill(colorL); rect(paddleXL, paddleYL, paddleW, paddleH); fill(colorR); rect(paddleXR, paddleYR, paddleW, paddleH); } //Makes both paddles move void movePaddle() { if (upL) { paddleYL = paddleYL - paddleS; } if (downL) { paddleYL = paddleYL + paddleS; } if (upR) { paddleYR = paddleYR - paddleS; } if (downR) { paddleYR = paddleYR + paddleS; } } //detects when a paddle has hit the wall void restrictPaddle() { if (paddleYL - paddleH/2 < 0) { paddleYL = paddleYL + paddleS; } if (paddleYL + paddleH/2 > height) { paddleYL = paddleYL - paddleS; } if (paddleYR - paddleH/2 < 0) { paddleYR = paddleYR + paddleS; } if (paddleYR + paddleH/2 > height) { paddleYR = paddleYR - paddleS; } } //This detects left and right collision; respectively void contactPaddle() { if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) { if (speedX < 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } } else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) { if (speedX > 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } } } //creates circle void drawCircle() { fill(0,0,255); ellipse(x, y, w, h); } //this allows the circle to bounce void moveCircle() { x = x + speedX*2; y = y + speedY*2; } //This detects if the ball has bounced or not void bounceOff() { if ( x > width - w/2) { setup(); speedX = -speedX*1; scoreL = scoreL + 1; time = time + 1 ; } else if ( x < 0 + w/2) { setup(); scoreR = scoreR + 1; time = time + 1 ; } if ( y > height - h/2) { speedY = -speedY; } else if ( y < 0 + h/2) { speedY = -speedY*1; } } //This provides the user to see the score void scores() { fill(colorL); text("Score: " + scoreL, 100, 50); fill(colorR); text("Score: " + scoreR, width-100, 50); } //This determines who wins void gameOver() { if(scoreL == winScore) { gameOverPage("Orange wins!", colorL); } if(scoreR == winScore) { gameOverPage("Dark-Green wins!", colorR); } } //This stops the ball from bouncing around, gets the user to know that the game is over void gameOverPage(String text, color c) { speedX = 0; speedY = 0; fill(255); text("Game over", width/2, height/3 - 40); text("Press enter to play again", width/2, height/3 + 40); fill(c); text(text, width/2, height/3); //this gets the user to click on the screen to play again if(keyPressed) { if (key==ENTER) { //this sets the score to zero scoreR = 0; scoreL = 0; //this gets the speed to a 1 speedX = 1; speedY = 1; winScore = winScore + 1 ; } } } //this gets the user to press a key and allow the paddle(s) to move void keyPressed() { if (key == 'w' || key == 'W') { upL = true; } if (key == 's' || key == 'S') { downL = true; } if (keyCode == UP) { upR = true; } if (keyCode == DOWN) { downR = true; } if (keyCode== ENTER) { speedX = 1 ; speedY = 1 ; } } //this creates a false key interaction void keyReleased() { if (key == 'w' || key == 'W') { upL = false; } if (key == 's' || key == 'S') { downL = false; } if (keyCode == UP) { upR = false; } if (keyCode == DOWN) { downR = false; } }you have to tell what you want
does it give an error message or does it do something else than you want it to do?
which lines are relevant?
the code is just too long to browse through it
you forgot to load play and stop files
This:
minim.loadSample( "ball.wav", 512) ;Sorry for the confusion, you need to do it like in the minim example:
(shouldn't ball.trigger() be somewhere in contactpaddle()?)
Sure, but that would mean you need to use classes and OOP. It's not that difficult but takes a bit of time getting used to. That, or you need to do a lot of duplicate code (which is bad).
I have a vague idea of how to do it....if classes mean array then that's what I mean. would duplicate code for two more really be that bad?
If by "bad" you mean it won't work then no... but it's going to be a lot less maintainable and much harder to work with. No, not just an array. You would have an array of Ball objects of the Ball class. It's one step further than arrays.
A class is a blueprint of an object. An object is an instance of a class. A class is the code which says what the object has, for example the x and y coordinates, in addition to some methods, eg. moving around. An object of the class is then the real application of the class: the x and y coordinates have values and you can call the methods of the class.
The thing is that you only need to program one class and you can effortlessly make as many objects out of it as you want. You can put all the objects in an array and because they're all the same thing (just with different values for their variables) they can all be treated the same. You can have another object with different x and y coordinates which are completely different and independent from the first object.
You were actually using classes and objects before. AudioSample is a class. In the Minim example, there are two objects of AudioSample, one named kick and one named snare. They're the same thing (an AudioSample), but in one a bass drum sample is loaded and in the other a snare drum. They also have a trigger() method which plays the loaded sample. The trigger() method is just coded once in the AudioSample class instead of once for every sample. That's the power of classes: it bundles up code and saves programmers doing things twice. We don't even know what's in the trigger() method, we just need to call it. That's another strong point of classes: they can easily be used elsewhere.
Mock up of the Ball class in your situation:
Did you mean like this? (It makes the window size really small)
import ddf.minim.*; Minim minim; AudioSample ball1; AudioSample wall; AudioSample GameOver1; PImage bg ; int num; int time = 1; int t ; class Ball //this is a class { int x, y, w, h, speedX, speedY; int paddleXL, paddleYL, paddleW, paddleH, paddleS; int paddleXR, paddleYR; boolean upL, downL; boolean upR, downR; //colorL and colorR are not needed, but it can be helpful color colorL = color(225, 115,0); color colorR = color(0, 255, 0); color c; //scoreL is for left score and scoreR is for the right score int scoreL = 0; int scoreR = 0; //this sets the final winScore to a count of 10 int winScore = 1; //Sets the game and window size up, along with the background and other important things //happens only once; at the beginning of the game void setup() { //this sets the window size size(500, 500); minim = new Minim(this); frameRate (60); smooth(); // load BD.wav from the data folder ball1 = minim.loadSample( "ball1.wav", 512) ; wall = minim.loadSample("wall.wav", 512) ; GameOver1 = minim.loadSample("GameOver1.wav", 512) ; // An AudioSample will spawn its own audio processing Thread, // and since audio processing works by generating one buffer // of samples at a time, we can specify how big we want that // buffer to be in the call to loadSample. // above, we requested a buffer size of 512 because // this will make the triggering of the samples sound more responsive. // on some systems, this might be too small and the audio // will sound corrupted, in that case, you can just increase // the buffer size. // // if a file doesn't exist, loadSample will return null // if ( kick == null ) println("Didn't get kick!"); // // // load SD.wav from the data folder // if ( snare == null ) println("Didn't get snare!"); //the x,y,w,h get the ball to begin at the center of the game x = width/2; y = height/2; w = 50; h = 50; // //this allows the ball to bounce // speedX = 4; // speedY = 4; //this creates a text textSize(30); textAlign(CENTER, CENTER); //this centers the height and width of the text rectMode(CENTER); //this allows both paddles to be centered //this sets up text for the beginning of the game //fill(120,6,152) ; text("Press the enter bar to start the game, press the 'p' key to pause the game. Good luck! " + "Go!", 10,350) ; //This sets the paddles to a certain place on the window, and makes them visible paddleXL = 17; paddleYL = height/2; paddleXR = width-17; paddleYR = height/2; paddleW = 30; paddleH = 100; paddleS = 5; bg = loadImage("pp.jpg") ; } //Draws the canvas for the game void draw() { background(bg); t++; if (t > height) { t = 0; } display(); move(); bounce(); drawPaddles(); movePaddle(); restrictPaddle(); contactPaddle(); scores(); gameOver(); if (keyPressed) { if (key=='p' || key=='P') { speedX = 0 ; speedY = 0 ; } } } //creates paddles void drawPaddles() { fill(colorL); rect(paddleXL, paddleYL, paddleW, paddleH); fill(colorR); rect(paddleXR, paddleYR, paddleW, paddleH); } //Makes both paddles move void movePaddle() { if (upL) { paddleYL = paddleYL - paddleS; } if (downL) { paddleYL = paddleYL + paddleS; } if (upR) { paddleYR = paddleYR - paddleS; } if (downR) { paddleYR = paddleYR + paddleS; } } //detects when a paddle has hit the wall void restrictPaddle() { if (paddleYL - paddleH/2 < 0) { paddleYL = paddleYL + paddleS; } if (paddleYL + paddleH/2 > height) { paddleYL = paddleYL - paddleS; } if (paddleYR - paddleH/2 < 0) { paddleYR = paddleYR + paddleS; } if (paddleYR + paddleH/2 > height) { paddleYR = paddleYR - paddleS; } } //This detects left and right collision; respectively void contactPaddle() { if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) { if (speedX < 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } ball1.trigger() ; } else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) { if (speedX > 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } ball1.trigger() ; } } Ball() { x = width/2; y = height/2; w = 50; h = 50; c = color(random(255),random(255),random(255)) ; } //creates (ball) class void display() { fill(0,0,255); ellipse(x, y, w, h); } //this allows the circle to bounce void move() { x = x + speedX*2; y = y + speedY*2; } //This detects if the ball has bounced or not void bounce() { if ( x > width - w/2) { setup(); speedX = -speedX*1; scoreL = scoreL + 1; time = time + 1 ; wall.trigger() ; } else if ( x < 0 + w/2) { setup(); scoreR = scoreR + 1; time = time + 1 ; wall.trigger() ; } if ( y > height - h/2) { speedY = -speedY; wall.trigger() ; } else if ( y < 0 + h/2) { speedY = -speedY*1; wall.trigger(); } } //This provides the user to see the score void scores() { fill(colorL); text("Score: " + scoreL, 100, 50); fill(colorR); text("Score: " + scoreR, width-100, 50); } //This determines who wins void gameOver() { if(scoreL == winScore) { gameOverPage("Orange wins!", colorL); GameOver1.trigger(); } if(scoreR == winScore) { gameOverPage("Dark-Green wins!", colorR); GameOver1.trigger(); } } //This stops the ball from bouncing around, gets the user to know that the game is over void gameOverPage(String text, color c) { speedX = 0; speedY = 0; fill(255); text("Game over", width/2, height/3 - 40); text("Press enter to play again", width/2, height/3 + 40); fill(c); text(text, width/2, height/3); //this gets the user to click on the screen to play again if(keyPressed) { if (key==ENTER) { //this sets the score to zero scoreR = 0; scoreL = 0; //this gets the speed to a 1 speedX = 1; speedY = 1; winScore = winScore + 1 ; } } } //this gets the user to press a key and allow the paddle(s) to move void keyPressed() { if (key == 'w' || key == 'W') { upL = true; } if (key == 's' || key == 'S') { downL = true; } if (keyCode == UP) { upR = true; } if (keyCode == DOWN) { downR = true; } if (keyCode== ENTER) { speedX = 1 ; speedY = 1 ; } } //this creates a false key interaction void keyReleased() { if (key == 'w' || key == 'W') { upL = false; } if (key == 's' || key == 'S') { downL = false; } if (keyCode == UP) { upR = false; } if (keyCode == DOWN) { downR = false; } } }No, a class exists outside of a Processing sketch (well, not literally of course, but you get the point). In fact, you can see a Processing sketch as a class itself, with setup() and draw() and mousePressed() methods and so on. You put these inside of the Ball class, effectively removing them from the main sketch. Undo your changes then create a new tab in the IDE (ctrl+shift+n) and paste my class stub there. Then remove all things belonging to the Ball from your main sketch. Then in the same spirit as the audio sample try something like this:
If you did everything right you should see no difference from your previous result.
for the "class stub" do you mean this code?
class Ball //By convention a class always starts with a capital letter { int x, y, w, h, speedX, speedY; //These are the basic properties of the ball color c; //Each instance (object) of this class can have //different values for these variables /* * This is the constructor of the Ball class * This is a special kind of method (notice it has no return type and the same name of the class: that's how you recognise a constructor) * When you create a new Ball object like this: * Ball ball = new Ball(); * this method is executed. * It's the perfect place to set the default values! */ Ball() { x = width/2; y = height/2; w = 50; h = 50; c = color(random(255), random(255), random(255)); } //Implementation of these methods is left as an exercice to the reader... //but it's about the same as the old methods drawCircle and moveCircle. //Those methods can then be deleted as they exist here. void display() { } void move() { } void bounce() { } }Yes but you should remove your class code from your original sketch.
Yep, I did that but the ball is not showing...
This is what I have in the first window:
import ddf.minim.*; Minim minim; AudioSample ball1; AudioSample wall; AudioSample GameOver1; PImage bg ; int num; int time = 1; int t ; int x, y, w, h, speedX, speedY; int paddleXL, paddleYL, paddleW, paddleH, paddleS; int paddleXR, paddleYR; boolean upL, downL; boolean upR, downR; //colorL and colorR are not needed, but it can be helpful color colorL = color(225, 115,0); color colorR = color(0, 255, 0); //scoreL is for left score and scoreR is for the right score int scoreL = 0; int scoreR = 0; //this sets the final winScore to a count of 10 int winScore = 1; //Sets the game and window size up, along with the background and other important things //happens only once; at the beginning of the game void setup() { //this sets the window size size(500, 500); minim = new Minim(this); frameRate (60); smooth(); // load BD.wav from the data folder ball1 = minim.loadSample( "ball1.wav", 512) ; wall = minim.loadSample("wall.wav", 512) ; GameOver1 = minim.loadSample("GameOver1.wav", 512) ; // An AudioSample will spawn its own audio processing Thread, // and since audio processing works by generating one buffer // of samples at a time, we can specify how big we want that // buffer to be in the call to loadSample. // above, we requested a buffer size of 512 because // this will make the triggering of the samples sound more responsive. // on some systems, this might be too small and the audio // will sound corrupted, in that case, you can just increase // the buffer size. // // if a file doesn't exist, loadSample will return null // if ( kick == null ) println("Didn't get kick!"); // // // load SD.wav from the data folder // if ( snare == null ) println("Didn't get snare!"); //the x,y,w,h get the ball to begin at the center of the game x = width/2; y = height/2; w = 50; h = 50; // //this allows the ball to bounce // speedX = 4; // speedY = 4; //this creates a text textSize(30); textAlign(CENTER, CENTER); //this centers the height and width of the text rectMode(CENTER); //this allows both paddles to be centered //this sets up text for the beginning of the game //fill(120,6,152) ; text("Press the enter bar to start the game, press the 'p' key to pause the game. Good luck! " + "Go!", 10,350) ; //This sets the paddles to a certain place on the window, and makes them visible paddleXL = 17; paddleYL = height/2; paddleXR = width-17; paddleYR = height/2; paddleW = 30; paddleH = 100; paddleS = 5; bg = loadImage("pp.jpg") ; } //Draws the canvas for the game void draw() { background(bg); t++; if (t > height) { t = 0; } drawPaddles(); movePaddle(); restrictPaddle(); contactPaddle(); scores(); gameOver(); if (keyPressed) { if (key=='p' || key=='P') { speedX = 0 ; speedY = 0 ; } } } //creates paddles void drawPaddles() { fill(colorL); rect(paddleXL, paddleYL, paddleW, paddleH); fill(colorR); rect(paddleXR, paddleYR, paddleW, paddleH); } //Makes both paddles move void movePaddle() { if (upL) { paddleYL = paddleYL - paddleS; } if (downL) { paddleYL = paddleYL + paddleS; } if (upR) { paddleYR = paddleYR - paddleS; } if (downR) { paddleYR = paddleYR + paddleS; } } //detects when a paddle has hit the wall void restrictPaddle() { if (paddleYL - paddleH/2 < 0) { paddleYL = paddleYL + paddleS; } if (paddleYL + paddleH/2 > height) { paddleYL = paddleYL - paddleS; } if (paddleYR - paddleH/2 < 0) { paddleYR = paddleYR + paddleS; } if (paddleYR + paddleH/2 > height) { paddleYR = paddleYR - paddleS; } } //This detects left and right collision; respectively void contactPaddle() { if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) { if (speedX < 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } ball1.trigger() ; } else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) { if (speedX > 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } ball1.trigger() ; } } //This provides the user to see the score void scores() { fill(colorL); text("Score: " + scoreL, 100, 50); fill(colorR); text("Score: " + scoreR, width-100, 50); } //This determines who wins void gameOver() { if(scoreL == winScore) { gameOverPage("Orange wins!", colorL); GameOver1.trigger(); } if(scoreR == winScore) { gameOverPage("Dark-Green wins!", colorR); GameOver1.trigger(); } } //This stops the ball from bouncing around, gets the user to know that the game is over void gameOverPage(String text, color c) { speedX = 0; speedY = 0; fill(255); text("Game over", width/2, height/3 - 40); text("Press enter to play again", width/2, height/3 + 40); fill(c); text(text, width/2, height/3); //this gets the user to click on the screen to play again if(keyPressed) { if (key==ENTER) { //this sets the score to zero scoreR = 0; scoreL = 0; //this gets the speed to a 1 speedX = 1; speedY = 1; winScore = winScore + 1 ; } } } //this gets the user to press a key and allow the paddle(s) to move void keyPressed() { if (key == 'w' || key == 'W') { upL = true; } if (key == 's' || key == 'S') { downL = true; } if (keyCode == UP) { upR = true; } if (keyCode == DOWN) { downR = true; } if (keyCode== ENTER) { speedX = 1 ; speedY = 1 ; } } //this creates a false key interaction void keyReleased() { if (key == 'w' || key == 'W') { upL = false; } if (key == 's' || key == 'S') { downL = false; } if (keyCode == UP) { upR = false; } if (keyCode == DOWN) { downR = false; } }This is in the second window:
Ball ball; class Ball //By convention a class always starts with a capital letter { int x, y, w, h, speedX, speedY; //These are the basic properties of the ball color c; //Each instance (object) of this class can have //different values for these variables /* * This is the constructor of the Ball class * This is a special kind of method (notice it has no return type and the same name of the class: that's how you recognise a constructor) * When you create a new Ball object like this: * Ball ball = new Ball(); * this method is executed. * It's the perfect place to set the default values! */ Ball() { x = width/2; y = height/2; w = 50; h = 50; c = color(random(255), random(255), random(255)); } //Implementation of these methods is left as an exercice to the reader... //but it's about the same as the old methods drawCircle and moveCircle. //Those methods can then be deleted as they exist here. void setup(){ ball = new Ball(); } void draw(){ ball.move(); ball.display(); ball.bounce(); } void display() { fill(0,0,255); ellipse(x, y, w, h); } void move() { x = x + speedX*2; y = y + speedY*2; } void bounce() { if ( x > width - w/2) { setup(); speedX = -speedX*1; scoreL = scoreL + 1; time = time + 1 ; wall.trigger() ; } else if ( x < 0 + w/2) { setup(); scoreR = scoreR + 1; time = time + 1 ; wall.trigger() ; } if ( y > height - h/2) { speedY = -speedY; wall.trigger() ; } else if ( y < 0 + h/2) { speedY = -speedY*1; wall.trigger(); } } }you have an object ball but you need to use it
so in setup
in draw()
you got both functions in the class now. deletet them there.
use the normal setup and draw()
The setup() and draw() shouldn't exist inside the ball class. Hmmm, how should I explain...
The Ball class is the code for the ball, but in itself it doesn't do anything. You need an object for that. This happens in three steps:
Declaring the object goes like this:
Ball ball;This tells Processing you want to have an object named "ball" of the class Ball.
Initializing then creates the actual object:
ball = new Ball();This line runs the constructor of the ball object. You always need to do both if you want to use a ball object. You can do it in one line:
Ball ball = new Ball();but that only works if you only use the ball locally (it won't do in this application so we need it split out, just like with the other variables).
So, in your code, declare the Ball like it's another variable like int or AudioSample or boolean, then initialize it inside setup(). You can then use ball.display(), ball.move() and ball.bounce() inside draw.
The lines 55-58 in your code are unnecessary since you shouldn't be using those variables anymore: they're now inside the Ball class. Also, the contactPaddle() method should also be inside the Ball class.
The next step is to add more Ball objects but let's get it working with one instance first.
I have this in the ball but the ball is still not showing:
Ball ball; int Ball; class Ball //By convention a class always starts with a capital letter { int x, y, w, h, speedX, speedY; //These are the basic properties of the ball color c; //Each instance (object) of this class can have //different values for these variables /* * This is the constructor of the Ball class * This is a special kind of method (notice it has no return type and the same name of the class: that's how you recognise a constructor) * When you create a new Ball object like this: * Ball ball = new Ball(); * this method is executed. * It's the perfect place to set the default values! */ Ball() { x = width/2; y = height/2; w = 50; h = 50; c = color(random(255), random(255), random(255)); } //Implementation of these methods is left as an exercice to the reader... //but it's about the same as the old methods drawCircle and moveCircle. //Those methods can then be deleted as they exist here. void setup(){ ball = new Ball(); } void draw(){ ball.move(); ball.display(); ball.bounce(); } void display() { fill(0,0,255); ellipse(x, y, w, h); } void move() { x = x + speedX*2; y = y + speedY*2; } void bounce() { if ( x > width - w/2) { setup(); speedX = -speedX*1; scoreL = scoreL + 1; time = time + 1 ; wall.trigger() ; } else if ( x < 0 + w/2) { setup(); scoreR = scoreR + 1; time = time + 1 ; wall.trigger() ; } if ( y > height - h/2) { speedY = -speedY; wall.trigger() ; } else { if ( y < 0 + h/2) { speedY = -speedY*1; wall.trigger(); } } } } void contactPaddle() { if (x - w/2 < paddleXL + paddleW/2 && y - h/2 < paddleYL + paddleH/2 && y + h/2 > paddleYL - paddleH/2 ) { if (speedX < 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } ball1.trigger() ; } else if (x + w/2 > paddleXR - paddleW/2 && y - h/2 < paddleYR + paddleH/2 && y + h/2 > paddleYR - paddleH/2 ) { if (speedX > 0) { speedX = -speedX*1; paddleH = paddleH - 2 ; paddleW = paddleW - 2 ; } ball1.trigger() ; } } //This determines who wins void gameOver() { if(scoreL == winScore) { gameOverPage("Orange wins!", colorL); GameOver1.trigger(); } if(scoreR == winScore) { gameOverPage("Dark-Green wins!", colorR); GameOver1.trigger(); } } //This stops the ball from bouncing around, gets the user to know that the game is over void gameOverPage(String text, color c) { speedX = 0; speedY = 0; fill(255); text("Game over", width/2, height/3 - 40); text("Press enter to play again", width/2, height/3 + 40); fill(c); text(text, width/2, height/3); //this gets the user to click on the screen to play again if(keyPressed) { if (key==ENTER) { //this sets the score to zero scoreR = 0; scoreL = 0; //this gets the speed to a 1 speedX = 1; speedY = 1; winScore = winScore + 1 ; } } }the error is outside the class in the main setup and draw
you're mixing the main setup and the setup in the class
Ok, I've got the ball displayed but the enter key is not working and I tried fixing it but it didn't work...