I need help creating a Pong game that have
-
Points system
-
Victory and Loss condition
-
“Ball” with physics simulation
-
2 player option
-
1 player option with a CPU
-
Menu title, instructions, credits (with references)
-
Sprites and sounds
The thing is, I’ve found a program that have almost all those things besides the menu and i have another one with a menu, and as a beginner i have no idea at all how to combine those two and I’m doing this for a college work, so prob won’t never use this again, as we’re learning both in unity and processing how to program (don’t ask me why)
This is the PONG program
// //Variables for ball
float ball_x; //balls horizontal position
float ball_y; //balls vertical position
float ball_hori_speed = 2; //ball speed in x axis
float ball_size = 15; // ball size according to it's radius
float ball_vert_speed = 1; // ball speed in y axis
//Paddle Variables
int pad_width = 10; //paddle's Width
int pad_height = 30; //paddle's height
//score variable
int comp_score; //Computer's score
int hu_score; //Human's score
//General variables
color c = color(105,255,98); //Colour corresponding the colour selector
PFont font; //fonts variable
void setup(){
size(400,400); //Window size
noStroke(); //no outline for game ball
smooth(); //where this is declared shapes have smooth edges
ball_y = height/2; //ball's vertical position
ball_x = width/2; //ball's horizonal position
}
void telajogo(){
draw();
}
void draw(){
background(0);
rectMode(RADIUS);
ellipseMode(RADIUS);
//text
fill(c);
textSize(20);
text("Human",3,22);
text("Computer",300,22);
text(hu_score, 150, 22);
text(comp_score,250,22);
stroke(c);
line(0,27,400,27);
//center line
for(int i = 27; i < 400; i += 30){
line(200,i,200,i + 10);
}
ball_x += ball_hori_speed; //adds speed to ball hori position
ball_y += ball_vert_speed; //adds speed to ball vert pos
if(ball_x > width+ball_size) { //if hori pos is greater than screen width+ball size
ball_x = -width/2 - ball_size; //ball hori pos is minus half width minus ball size
ball_y = random(27, height);
}
// Constrain paddle to screen
float cpad_y = constrain(ball_y, 59 , height); //tracks ball y position
float pad_y = constrain(mouseY, 59 , height);
// Test to see if the ball is touching the paddle
float py = width-15 - pad_width - ball_size;
if(ball_x == py
&& ball_y > cpad_y - pad_height - ball_size
&& ball_y < cpad_y + pad_height + ball_size) {
ball_hori_speed *= -1;
if(mouseY != pmouseY) {
ball_vert_speed = (mouseY-pmouseY)/2.0;
if(ball_vert_speed > 5) { ball_vert_speed = 5; }
if(ball_vert_speed < -5) { ball_vert_speed = -5; }
}
}
float py1 = 15 + pad_width + ball_size;
if(ball_x == py1
&& ball_y > pad_y - pad_height - ball_size
&& ball_y < pad_y + pad_height + ball_size) {
ball_hori_speed *= -1;
if(mouseY != pmouseY) {
ball_vert_speed = (mouseY-pmouseY)/2.0;
if(ball_vert_speed > 5) { ball_vert_speed = 5; }
if(ball_vert_speed < -5) { ball_vert_speed = -5; }
}
}
//Scoring system and ball position reset
if(ball_x < -width/4){
comp_score = comp_score +1; ball_x = 200;
}
if(comp_score >= 5){
ball_vert_speed = 0; ball_hori_speed = 0;
text("Game Over\nComputer Wins!",210,175);
}
if((comp_score == 5) && (mousePressed)){
exit();
}
//Human score
if(ball_x > width){
hu_score = hu_score +1; ball_x = 200;
}
if(hu_score >= 15){
ball_vert_speed = 0; ball_hori_speed = 0;
text("Game Over\nyou win",60,175);
}
// If the ball is touching top or bottom edge, reverse direction
if(ball_y > height-ball_size) {
ball_vert_speed = ball_vert_speed * -1;
}
if(ball_y < ball_size+27) {
ball_vert_speed = ball_vert_speed * -1;
}
// Draw ball
fill(c);
noStroke();
ellipse(ball_x, ball_y, ball_size, ball_size);
// Draw the paddles
fill(c);
rect(width-15, cpad_y, pad_width, pad_height);
rect(15,pad_y,pad_width,pad_height);
}
And this is the MENU code, not finished, but i got the idea on how this works to create another button and how this generally works
float x = 100;
float y = 50;
float w = 150;
float h = 80;
void setup(){
size(500,500);
background(255);
stroke(0);
noFill();
}
void draw(){
background(255);
rect(x,y,w,h);
fill(255);
if(mousePressed){
if(mouseX>x && mouseX <x+w && mouseY>y && mouseY <y+h){
println("The mouse is pressed and over the button");
fill(0);
//do stuff
}
}
}
I just need to understand on how can I make this work, the sounds and sprites part are the most irrelevant as doing this is kinda easy as far as I know