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):
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
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:
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?
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)
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?
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:
This is in the second window:
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:
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...