SoundCipher with Pong help.
in
Contributed Library Questions
•
2 months ago
I'm developing a small pong game, but I am completely new to SoundCipher, and would like some help writing code.
Here are my aims/criteria for this project.
1. The key ‘r’ should control the start and stop of the game
2. The paddles should follow the ball’s position (i.e. no longer controlled by the mouse or the arrow keys)
3. The left and right arrow keys should control the ball’s speed. The speed value cannot be greater than 4.
4. The screen should be divided into an 8 x 2 grid.
a. If the ball is in an odd numbered segment, a note of 8 times the segment value should be sent to the java sound synthesizer (i.e. the note should be played) (velocity and duration of your choice). The note can only hold values ranging between 0 and 127. The resulting note should also be stored in the array at the segment number’s position. b. Every five times the ball hits an odd numbered segment, the instrument value should change randomly. The instrument value cannot be greater than 127.
c. If the ball in an even numbered segment, a note message of 7 times the segment position should be sent to the java sound synthesizer (i.e. the note should be played) (velocity and duration of your choice). The note can only hold values ranging between 0 and 127. The resulting note should also be stored in the array at the segment number’s position.
d. If the ball is located in segment 6, it causes (an) action(s) of your choice (either graphical, sound or both) to happen. The action should be an algorithm, and more than the use of a single programming function.
5. If the user presses a key of your choice, the notes currently stored in the array should be added to the beginning of the score, with a velocity of 80 and duration of your choice. However, the durations of each note should be different. The notes should be stored one after the other (i.e. with a spacing of the duration of the note between each note).
7. The program should also contain the following additional functions: a. int ballSpeed(int currentSpeed) This function should be called to update the speed of the ball each time . It should pass in the current speed value. b. void gridDivision(int ellipseX, int ellipseY) This function should contain all the code to divide the screen into a grid-‐ like structure. It should also check if the ellipse has moved into a new segment. It should pass in the ellipse’s x and y position, and be called in draw()
My code so far.
Password is pong.
Here are my aims/criteria for this project.
1. The key ‘r’ should control the start and stop of the game
2. The paddles should follow the ball’s position (i.e. no longer controlled by the mouse or the arrow keys)
3. The left and right arrow keys should control the ball’s speed. The speed value cannot be greater than 4.
4. The screen should be divided into an 8 x 2 grid.
a. If the ball is in an odd numbered segment, a note of 8 times the segment value should be sent to the java sound synthesizer (i.e. the note should be played) (velocity and duration of your choice). The note can only hold values ranging between 0 and 127. The resulting note should also be stored in the array at the segment number’s position. b. Every five times the ball hits an odd numbered segment, the instrument value should change randomly. The instrument value cannot be greater than 127.
c. If the ball in an even numbered segment, a note message of 7 times the segment position should be sent to the java sound synthesizer (i.e. the note should be played) (velocity and duration of your choice). The note can only hold values ranging between 0 and 127. The resulting note should also be stored in the array at the segment number’s position.
d. If the ball is located in segment 6, it causes (an) action(s) of your choice (either graphical, sound or both) to happen. The action should be an algorithm, and more than the use of a single programming function.
5. If the user presses a key of your choice, the notes currently stored in the array should be added to the beginning of the score, with a velocity of 80 and duration of your choice. However, the durations of each note should be different. The notes should be stored one after the other (i.e. with a spacing of the duration of the note between each note).
7. The program should also contain the following additional functions: a. int ballSpeed(int currentSpeed) This function should be called to update the speed of the ball each time . It should pass in the current speed value. b. void gridDivision(int ellipseX, int ellipseY) This function should contain all the code to divide the screen into a grid-‐ like structure. It should also check if the ellipse has moved into a new segment. It should pass in the ellipse’s x and y position, and be called in draw()
My code so far.
Password is pong.
- import arb.soundcipher.*;
SoundCipher aSample = new SoundCipher(this);
//Globals for paddles
int paddle1x, paddle1y;
int paddle2x, paddle2y;
int sizeAcross = 10, sizeDown = 60;
//globals for ellipses
int ellipseX, ellipseY;
int speedX = 3, speedY = 2;
int ellipseSize = 10;
//globals for line spacings
int lineX, spacing = 10;
boolean startPong = false;
//globals for text input and saving
String userInput = "", saved = "" ;
void setup()
{
size(400, 400);
stroke(255);
paddle1x = 0;
paddle2x = width - sizeAcross;
ellipseX = width/2;
ellipseY = height/2;
lineX = width/2;
}
void draw()
{
//redrawing the background each time
background(0);
if (!startPong) //if the password hasn't matched yet
{
text("Welcome to pong! Please type the password and hit ENTER key", 0, 10);
text("Please enter your password here: " + userInput, 0, height/2);
}
else // if the password has matched
{
//draws the dashed line down the middle of the screen
for (int i = 0; i < height; i = i + spacing)
line(lineX, i, lineX, i + 5);
//draw the paddles
drawPaddles();
//draw circles
drawEllipses();
//displaying text
text("This is a very basic application of Pong",width/5, 10);
}
text("move cursor up and down to control left player",width/7, height - 30);
text("up arrow = up, down arrow = down for right player", width/7, height - 10);
}
void keyPressed()
{
//passing in the password each time a key is pressed
pass("pong");
}
void drawPaddles()
{
if (keyPressed)
{
if (key == CODED) //if the key is any one of the arrow keys
{
if (keyCode == DOWN) //if the down arrow has been pressed
paddle2y = paddle2y + 3;
else if (keyCode == UP) //if the up arrow has been pressed
paddle2y = paddle2y - 3;
if (paddle2y < 0) //ensuring the paddle doesn't go off the top of the screen
paddle2y = 0;
else if (paddle2y > height - sizeDown) //ensuring the paddle doesn't go off the bottom of the screen
paddle2y = height - sizeDown;
}
}
//keys are for other characters
paddle1y = mouseY;
if (paddle1y < 0) //ensure the paddle doesn't go off the top of the screen
paddle1y = 0;
else if (paddle1y > height - sizeDown) // ensure the paddle doesn't go off the bottom of the screen
paddle1y = height - sizeDown;
//draw the paddles
rect(paddle1x, paddle1y, sizeAcross, sizeDown);
rect(paddle2x, paddle2y, sizeAcross, sizeDown);
}
void drawEllipses()
{
ellipseX = ellipseX + speedX; //Move the ball along the x axis
ellipseY = ellipseY + speedY; //Move the ball along the y axis
if (ellipseX > width) //if the ellipse goes off the right of the screen. Note there is a nested if here.
{
speedX = -3; //set the speed to a negative number so it aims for the left hand paddle
ellipseX = width/2; //reset the ellipse to the centre of the screen
ellipseY = height/2;
}
if (ellipseX < 0) //if the ellipse goes off the left of the screen. Note there is a nested if here.
{
speedX = 3; //set the speed so it aims for the player on the right
ellipseX = width/2; //reset the ellipse to the center of the screen
ellipseY = height/2;
}
//if the ellipse goes off the bottom of the screen
if (ellipseY > height - ellipseSize/2)
speedY = speedY * - 1;
//if the ellipse goes off the top of teh screen
else if (ellipseY < ellipseSize/2)
speedY = speedY * - 1;
//draw ball
ellipse(ellipseX, ellipseY, ellipseSize, ellipseSize);
}
void pass(String pass)
{
if (key != CODED && key != ENTER && key != BACKSPACE)
userInput += key; //add user input to the key
else if (key == ENTER)
{
saved = userInput; //when Enter has been pressed, save user input
userInput = ""; //clear user input so we can type something else!
if (saved.equals(pass))
startPong = true; //if the password matches, start the game
}
}
1