Pong Game help
in
Programming Questions
•
2 years ago
for a class assignment we have to make a pongish/handballish game so far i got two paddles, one blue and one red the ball starts off as green and turns the color of the paddle it hits it ounces off the paddle and hits the right wall and bounces back however if you miss the ball with the paddle it does not bounce of the left wall. i want to make a LOSE screen for when the ball goes off the left side and i also want to keep score of how many times each player hits the ball with the paddle i have tried doing both but neither attempts were successful i would GREATLY appreciate it if someone could help me. Thanks in Advance
Note: I have highlighted the part where i tried
//blue paddle is controlled by the 'a' and 'z' keys
//a=up and z=down
//red paddle is controlled by mouseY
ball B1;
int paddle_width = 15;
int paddle_height = 60;
int paddle_width_2 = 15;
int paddle_height_2 = 60;
int mousey=50;
void setup()
{
size(800,600);
B1= new ball(400,300,50);
B1.setColor(0.0,255.0,0.0);
B1.setSpeed(3.0,3.0);
frameRate(60);
}
void draw()
{
background(51);
B1.display();
B1.bounceOffWalls();
B1.move();
float paddle_y= constrain(mousey,0,height-paddle_height);
float paddle_y_2= constrain(mouseY,0,height-paddle_height_2);
fill(153);
stroke(0,0,255);
rect(15, paddle_y, paddle_width, paddle_height);
stroke(255,0,0);
rect(15, paddle_y_2, paddle_width_2, paddle_height_2);
if(B1.x-25<=15+paddle_width&&B1.y>=paddle_y&&B1.y<=paddle_y+paddle_height)
{
B1.dx=-B1.dx;
B1.setColor(0,0,255);
B1.dx=B1.dx+2;
}
if(B1.x-25<=15+paddle_width_2&&B1.y>=paddle_y_2&&B1.y<=paddle_y_2+paddle_height_2)
{
B1.dx=-B1.dx;
B1.setColor(255,0,0);
B1.dx=B1.dx+2;
}
if (keyPressed)
{
if(key=='A'||key=='a')
{
mousey=mousey-8;
}
else if((key=='Z'||key=='z'))
{
mousey=mousey+8;
}
}
//*************
if(B1.y+25<=0)
{
PFont font;
font = loadFont("AgencyFB-Reg-48.vlw");
textFont(font);
String s = "Lose";
text(s, 15, 20, 70, 70);
}
//****************
if(B1.x+25<=0)
{
B1.dx=0;
}
}
class ball {
float diameter;
float x,y;
float dx,dy;
float r,g,b;
ball(int x,int y, int d)
{
randomColor();
this.x=x;
this.y=y;
this.diameter=d;
dx=0;
dy=0;
}
void move()
{
x=x+dx;
y=y+dy;
}
void setSize(int d)
{
diameter=d;
}
void setSpeed(float dx,float dy) {
this.dx=dx;
this.dy=dy;
}
void randomColor()
{
r=random(255);
g=random(255);
b=random(255);
}
void setColor(float r,float g,float b)
{
this.r=r;
this.g=g;
this.b=b;
}
void display()
{
stroke(r,g,b);
fill(r,g,b);
if(x>0 && y>0)
ellipse(x,y,diameter,diameter);
}
void bounceOffWalls()
{
if(x+25>width)
{
dx=-dx;
}
if(y+25>height)
{
dy=-dy;
}
if(y-25<0)
{
dy=-dy;
}
}
}
1