Creating a Pong using tutorials

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

Hi,

Welcome to the forum! :slight_smile:

Well let me first say that this forum is not really suited for this kind of post, don’t except people to give you the entire solution for your homework. Please read the guidelines : FAQ - Processing Foundation

Code is not just putting things together, otherwise you are not going to learn properly.
If you only copy without understanding the code it’s useless.

Also what is your major? What is this course for?
If you are learning Unity, the key concept here is to learn how to code. Then the framework doesn’t matter.

So the only advice I would give is to follow the Processing tutorials :

1 Like

Don’t get me wrong but i’m not trying to learn nothing new, I just need this project to be done. I’m not sure if this is the right place on forum to ask something like that and I’m not asking for a complete solution or someone doing everything for me, i want advices on things i should look for to make this work.
If I could I would even pay for someone to do this to me, it’s the end of the year and everything it matters at this point is grades.

Btw im doing game design

void setup(){
size(480,480);

}
void draw(){
if(mousePressed){
fill(0);
}else{
fill(255);
}
ellipse(mouseX,mouseY,80,120);//
}
Hi please could you explain what the fill else statement does to my ellipse.

@DijaAngel I answered in the other discussion.

@GalactusBR

Hello and welcome to the forum!

Great to have you here!

You can merge the two sketches when you use a “state” variable. This state tells you whether to show the menu or the game (or other screens, like a Splash Screen or a help screen. Even the Game with 2 players versus the Game against the computer can be seen as two different states.)

Warm regards,

Chrisir

Here is an Example for state, menu and game:


//

final int MENU=0;  // state and the values (constants) state can have; must be unique numbers
final int GAME=1;
int state = MENU; 

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);

  switch (state) {
  case MENU: 
    // step 1: draw the button rect with a fill color differently when mouse is upon it or not 
    fill(255);  // color when NOT over
    if (mouseOverButton1()) {
      fill(255, 255, 0);  // when over 
    }
    rect(x, y, w, h);  // rect 
    //------------------

    // step 2 : show text 
    fill(0);
    text ("CLICK", x+17, y+28);  // put something here like: 2 Players OR Player vs. AI
    break; 

  case GAME:
    fill(0);   // here goes your PONG Game !!!!!!!!!!!!!
    text ("IMPLEMENT Game here ", 
      117, y+128);
    break; 
    //
  }//switch 
  //
}//func

// ------------------------------------------------------------------------

void mousePressed() {
  // step 3: check mouse clicked // This is when mouse has been clicked 
  if (mousePressed) {
    if (mouseOverButton1()) {
      // println("The mouse is pressed and over the button");
      state = GAME;   
      fill(0);
      //do stuff
    }
  }
}

boolean mouseOverButton1() {  // This is for button number 1 
  return 
    mouseX>x && 
    mouseX <x+w &&
    mouseY>y && 
    mouseY <y+h;
}
//

Hello again,

What’s the whole point of studying then? I just want to say that this not the right forum to get things done when you don’t have time to do them.

But anyway thanks @Chrisir for answering this question! :wink:

1 Like