my pong program isn't working and I'm not sure why. Can anyone help?
in
Programming Questions
•
10 months ago
This is my coding:
int intPaddleSideX = 15;
int intPaddleSideY = 115;
int intPaddleRightX = 10;
int intPaddleRightY = 350;
float fltPaddleLeftX = 690;
float fltPaddleLeftY = 350;
float fltEndPaddleX, fltEndPaddleY;
int Counter = 0;
int miss = 0;
int intp1score = 0;
int intp2score = 0;
float fltBallX, fltBallY, x, y;
float fltXSpeed = 8;
float fltYSpeed = 6;
float fltBallSize = 30;
float fltXDirec = -2;
float fltYDirec = 2;
float fltX = random(10,690);
int intX = width/2;
int intY = height/2;
PFont font;
int intPaddleSideY = 115;
int intPaddleRightX = 10;
int intPaddleRightY = 350;
float fltPaddleLeftX = 690;
float fltPaddleLeftY = 350;
float fltEndPaddleX, fltEndPaddleY;
int Counter = 0;
int miss = 0;
int intp1score = 0;
int intp2score = 0;
float fltBallX, fltBallY, x, y;
float fltXSpeed = 8;
float fltYSpeed = 6;
float fltBallSize = 30;
float fltXDirec = -2;
float fltYDirec = 2;
float fltX = random(10,690);
int intX = width/2;
int intY = height/2;
PFont font;
void setup() {
size(700,700);
fltBallX = width/2;
fltBallY = height/2;
font = loadFont ("Arial-BoldMT-48.vlw");
}
size(700,700);
fltBallX = width/2;
fltBallY = height/2;
font = loadFont ("Arial-BoldMT-48.vlw");
}
void draw() {
background(0);
rectMode(CENTER);
collision();
texts();
RightPaddle();
LeftPaddle();
objects();
fltBallX = fltBallX + (fltXSpeed*fltXDirec);
fltBallY = fltBallY + (fltYSpeed*fltYDirec);
background(0);
rectMode(CENTER);
collision();
texts();
RightPaddle();
LeftPaddle();
objects();
fltBallX = fltBallX + (fltXSpeed*fltXDirec);
fltBallY = fltBallY + (fltYSpeed*fltYDirec);
}
void texts() {
textFont(font, 48);
textAlign(CENTER);
text("pong", width/2, 200);
//player 1
textFont(font, 37);
textAlign(LEFT);
text("PLAYER 1: ", 10,300);
text(intp1score, 220,300);
if(fltBallY > 700){
intp1score = intp1score + 1;
}
//player 2
textFont(font, 37);
textAlign(LEFT);
text("PLAYER 2: ", 400, 300);
text(intp2score, 600, 300);
if(fltBallY < 0){
intp2score = intp2score + 1;
}
textFont(font, 48);
textAlign(CENTER);
text("pong", width/2, 200);
//player 1
textFont(font, 37);
textAlign(LEFT);
text("PLAYER 1: ", 10,300);
text(intp1score, 220,300);
if(fltBallY > 700){
intp1score = intp1score + 1;
}
//player 2
textFont(font, 37);
textAlign(LEFT);
text("PLAYER 2: ", 400, 300);
text(intp2score, 600, 300);
if(fltBallY < 0){
intp2score = intp2score + 1;
}
}
void objects(){
rect(intPaddleRightX,intPaddleRightY,intPaddleSideX,intPaddleSideY);
rect(fltPaddleLeftX,fltPaddleLeftY,intPaddleSideX,intPaddleSideY);
ellipse(fltBallX,fltBallY,fltBallSize,fltBallSize);
}
void objects(){
rect(intPaddleRightX,intPaddleRightY,intPaddleSideX,intPaddleSideY);
rect(fltPaddleLeftX,fltPaddleLeftY,intPaddleSideX,intPaddleSideY);
ellipse(fltBallX,fltBallY,fltBallSize,fltBallSize);
}
void RightPaddle(){
if(keyPressed){
if(key == 'w' || key == 'W'){
fltEndPaddleY = intPaddleRightY;
intPaddleRightY = intPaddleRightY - 2;
if(intPaddleRightY <= 30){
intPaddleRightY = intPaddleRightY + 2;
}
}
}
if(keyPressed){
if(key == 's' || key == 'S'){
fltEndPaddleY = intPaddleRightY;
intPaddleRightY = intPaddleRightY + 2;
if(intPaddleRightY >= height-50){
intPaddleRightY = height-52;
}
}
}
}
void LeftPaddle(){
if(keyPressed){
if(key == CODED){
if(keyCode == UP){
fltEndPaddleX = fltPaddleLeftY;
fltPaddleLeftY = fltPaddleLeftY - 2;
if(fltPaddleLeftY <= 30){
fltPaddleLeftY = fltPaddleLeftY + 2;
}
}
}
}
if(keyPressed){
if(key == CODED){
if(keyCode == DOWN){
fltEndPaddleX = fltPaddleLeftY;
fltPaddleLeftY = fltPaddleLeftY + 2;
if(fltPaddleLeftY >= height-50){
fltPaddleLeftY = height-52;
}
}
}
}
}
void collision(){
if(fltBallX >= intPaddleRightX && (fltBallX < intPaddleRightX + 70) && fltBallY > 660){
fltBallX = fltBallX * fltXDirec;
}
if(fltBallY < height - intPaddleRightY && fltBallX < intPaddleRightX + (intPaddleSideX/2) && fltBallX > intPaddleRightX - (intPaddleSideX/2)){
fltYDirec = fltYDirec * -1;
}
if(fltBallY < height - fltPaddleLeftY && fltBallY < fltPaddleLeftX + (intPaddleSideY/2) && fltBallY > fltPaddleLeftY - (intPaddleSideY/2)){
fltBallY = fltBallY * fltYDirec;
}
if (fltBallY > height-fltBallSize || fltBallY < fltBallSize) {
fltYDirec *= -1;
}
}
void mousePressed (){
fltBallX = width/2;
fltBallY = height/2;
fltBallSize = 30;
ellipse(fltBallX,fltBallY,fltBallSize,fltBallSize);
}
if(fltBallX >= intPaddleRightX && (fltBallX < intPaddleRightX + 70) && fltBallY > 660){
fltBallX = fltBallX * fltXDirec;
}
if(fltBallY < height - intPaddleRightY && fltBallX < intPaddleRightX + (intPaddleSideX/2) && fltBallX > intPaddleRightX - (intPaddleSideX/2)){
fltYDirec = fltYDirec * -1;
}
if(fltBallY < height - fltPaddleLeftY && fltBallY < fltPaddleLeftX + (intPaddleSideY/2) && fltBallY > fltPaddleLeftY - (intPaddleSideY/2)){
fltBallY = fltBallY * fltYDirec;
}
if (fltBallY > height-fltBallSize || fltBallY < fltBallSize) {
fltYDirec *= -1;
}
}
void mousePressed (){
fltBallX = width/2;
fltBallY = height/2;
fltBallSize = 30;
ellipse(fltBallX,fltBallY,fltBallSize,fltBallSize);
}
1