Collision and 2D array.
in
Programming Questions
•
1 month ago
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.
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);
{
//draw grid
gridDivision();
//draw the paddles
drawPaddles();
//draw circles
drawEllipses(); - gridcell1 = width/cols;
gridcell2 = height/rows;
xpos = ellipseX/gridcell1;
ypos = ellipseY/gridcell2;
println(xpos + ypos * cols);
}
}
void gridDivision()
{
//draw grid - 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 (ellipseX + ellipseSize/2 == paddle1x - sizeAcross/2
&& ellipseY + ellipseSize/2 <= paddle1y + sizeDown/2
&& ellipseY - ellipseSize/2 >= paddle1y - sizeDown/2)
{
speedX = 1;
}
//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 the screen
else if (ellipseY < ellipseSize/2)
speedY = speedY * - 1;
//draw ball
ellipse(ellipseX, ellipseY, ellipseSize, ellipseSize);
}
void keyPressed() {
final int k = keyCode;
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;
}
}
}
1