I have two problems in my code, if anybody can help out it would be great.
1. for some reason every time i try to make my ellipse in Pong collide with my paddles, it doesn't work, if anyone could help I'd be grateful.
2. I have to write a 2D array for this game, there is a 8 x 2 grid seen on the game screen, and every time the ball goes through a section of a grid, i want to add a sound effect (I'm using SoundCipher but i dont need help with that). Could someone help me set up this Array? I'm really stuck.
The code is a bit messy at the moment, i'm trying out a few things and forget to delete some code.
//Globals for paddles int paddle1x, paddle1y; int paddle2x, paddle2y; int sizeAcross = 10, sizeDown = 60;
//globals for ellipses int ellipseX, ellipseY; int speedX = 1, speedY = -1; int ellipseSize = 10;
//globals for line spacings int lineX, spacing = 10; boolean startPong = false;
//globals for grid PFont f;
int gridcell1;//width of a grid cell int gridcell2; int xpos; //x coordinate in the grid in cell number units, 0 to number of columns - 1. int ypos;
// Number of columns and rows in the grid int cols = 8; int rows = 2;
void setup() { size(400, 400); stroke(255); paddle1x = 0; paddle2x = width - sizeAcross; ellipseX = width/2; ellipseY = height/2; lineX = width/2; f = createFont("Arial", 48); textFont(f, 10); // No need to call it on each cell, if you don't change it textAlign(CENTER); cols = width/50; rows = height/200; }
void draw() { //redrawing the background each time background(0);
rectMode(CORNER); for (int i = 0; i < cols; i++) { for (int j = 0; j < rows; j++) { int x = i*50; int y = j*200; if (ellipseX >= x && ellipseX < x + 50 && ellipseY >= y && ellipseY < y + 200) { } else { fill(255);
}
stroke(0); rect(x, y, 50, 200);
fill(100, 150, 200);
text(str(j * cols + i + 1), x + 50 / 2, y + 200 / 2);
} }
for (float i = 0; i < height; i = i + spacing) line(lineX, i, lineX, i + 5); }
void drawPaddles() {
rectMode(CENTER);
//keys are for other characters paddle1y = ellipseY;
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;
paddle2y = ellipseY; 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; //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 (k == 'R')//press r to pause game. if (looping) noLoop(); else loop(); if (keyPressed) { if (key == CODED) { if (keyCode == RIGHT) //if the right arrow has been pressed ellipseX = ellipseX + 3; else if (keyCode == LEFT) //if the left arrow has been pressed ellipseX = ellipseX - 3; } } }
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.
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 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;
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 } }