Pong game collision problems
in
Programming Questions
•
2 years ago
Hi, I am having some trouble with creating the collisions with a pong game.
I am trying to make a simple pong game as a first peice of programming.
I have made the play area, ball and collisions with the top and bottom of the screen so it rebounds.
I have also made the paddles and used a boolean array for the movement so they can both move at the same time with two simltanious key presses.
The thing I am now stuck on is getting the ball to bounce off the paddles.
I just cant get my head around what I need to write so a few pointers would be greatly apreciated.
Here is my code.
Thanks
I am trying to make a simple pong game as a first peice of programming.
I have made the play area, ball and collisions with the top and bottom of the screen so it rebounds.
I have also made the paddles and used a boolean array for the movement so they can both move at the same time with two simltanious key presses.
The thing I am now stuck on is getting the ball to bounce off the paddles.
I just cant get my head around what I need to write so a few pointers would be greatly apreciated.
Here is my code.
Thanks
- //sets up ball start and speed
float x = 250;
float y = 200;
float xspeed = 1;
float yspeed = 1;
int rightpaddle = 200;
int leftpaddle = 200;
boolean[] keys = new boolean[4]; //boolean array to allow multiple key presses
final int W = 0;
final int S = 1;
final int U = 2;
final int D = 3;
void setup() {
size(500,400);
smooth();
rectMode(CENTER);
for (int i = 0; i< 4; i++) {
keys[i] = false;
}
//sets up play area size, anti aliasing and second part of boolean array
}
void draw(){
background(155);
stroke(255);
line(250,0,250,400);
fill(155);
stroke(255);
ellipse(250,200,80,80);
move();
ball();
bounce();
paddle(15,leftpaddle,70); //player 1
paddle(485,rightpaddle,70); //player 2
if (keys[W] == true){
leftpaddle = max(35, leftpaddle - 4); // player 1 paddle up
}
if(keys[S] == true){
leftpaddle = min (365, leftpaddle + 4); // player 1 paddle down
}
if(keys[U] == true){
rightpaddle = max(35, rightpaddle - 4); //player 2 paddle up
}
if(keys[D] == true){
rightpaddle = min(365, rightpaddle +4); //player 2 paddle down
}
//sets up the background of play area classes, key presses for movment and constrains paddles to screen.
}
void keyPressed() {
if(key == 'w') {
keys[W] = true;
} else if(key == 's') {
keys[S] = true;
} else if(keyCode == UP) {
keys[U] = true;
} else if (keyCode == DOWN) {
keys[D] = true;
}
}//creates key presses, when key is pressed action happens
void keyReleased() {
if(key == 'w') {
keys[W] = false;
} else if(key == 's') {
keys[S] = false;
} else if(keyCode == UP) {
keys[U] = false;
} else if (keyCode == DOWN) {
keys[D] = false;
} // creates key releases, when key is released action stops
}
void move(){
x = x + xspeed;
y = y + yspeed;
//movement class
}
void ball(){
stroke(0);
fill(0);
ellipse(x,y,30,30);
//ball class
}
void bounce(){
if ((y>385) || (y < 15 )){
yspeed = yspeed *-1;
}
//sets up bounce off of edges and needs editing for paddles
}
void paddle(int x, int y, int Size) {
rectMode(CENTER);
stroke(0);
fill(0);
rect(x,y,Size/4,Size);
//Paddle Class
}
1