Skip to content

Instantly share code, notes, and snippets.

@dc74089
Last active March 1, 2023 19:40
Show Gist options
  • Save dc74089/4094da7928839063ae06 to your computer and use it in GitHub Desktop.
Save dc74089/4094da7928839063ae06 to your computer and use it in GitHub Desktop.
Simple Pong Game in Processing
float x, y, speedX, speedY;
float diam = 10;
float rectSize = 200;
void setup() {
fullScreen();
fill(0, 255, 0);
reset();
}
void reset() {
x = width/2;
y = height/2;
speedX = random(3, 5);
speedY = random(3, 5);
}
void draw() {
background(0);
ellipse(x, y, diam, diam);
rect(0, 0, 20, height);
rect(width-30, mouseY-rectSize/2, 10, rectSize);
x += speedX;
y += speedY;
// if ball hits movable bar, invert X direction
if ( x > width-30 && x < width -20 && y > mouseY-rectSize/2 && y < mouseY+rectSize/2 ) {
speedX = speedX * -1;
}
// if ball hits wall, change direction of X
if (x < 25) {
speedX *= -1.1;
speedY *= 1.1;
x += speedX;
}
// if ball hits up or down, change direction of Y
if ( y > height || y < 0 ) {
speedY *= -1;
}
}
void mousePressed() {
reset();
}
@cifreCodex
Copy link

Thank you.
I am reading Learning Processing and have just started doing little programs.

@Rainster239
Copy link

//Pantalla
int screenX = 600;
int screenY = 450;

//Paddle 1(Derecha)
int pad1_sizeX = 10;
int pad1_sizeY = 50;
int pad1X = screenX-30;
int pad1Y = screenY/2-25;

//Paddle Ajustes Generales
color pad_color = #FFFFFF;
int pad_vel = 5;

//Paddle 2(Izquierda)
int pad2_sizeX = 10;
int pad2_sizeY = 50;
int pad2X = screenX-580;
int pad2Y = screenY/2-25;
color pad2_color = #FFFFFF;

//Pelota
float ball_posX = screenX/2;
float ball_posY = screenY/2;
float ball_velX = 3;
float ball_velY = 2;
int ball_size = 20;
color ball_color = #FFFFFF;

//Configuración de teclas
boolean w_pressed = false;
boolean s_pressed = false;
boolean space_pressed = false;
boolean up_pressed = false;
boolean down_pressed = false;
boolean lose = false;

color text_color = #FFFFFF;
PFont ds_digital;

//Configuración de pantalla
void setup(){
size(600,450);
loadFont("ds_digital.vlw" );
}

void draw(){
//Pulsación de teclas
if(w_pressed == true){
pad2Y -= pad_vel;
}
if(s_pressed == true){
pad2Y += pad_vel;
}
if(up_pressed == true){
pad1Y -= pad_vel;
}
if(down_pressed == true){
pad1Y += pad_vel;
}
if(pad1Y < 0){
pad1Y = 0;
}
if(pad1Y > screenY - pad2_sizeY){
pad1Y = screenY - pad2_sizeY;
}
if(pad2Y < 0){
pad2Y = 0;
}
if(pad2Y > screenY - pad2_sizeY){
pad2Y = screenY - pad2_sizeY;
}

//Saque de bola
if(key == ENTER ){
lose = false;
}

//Reset de la pelota
if(lose == false){
ball_posX = ball_posX+ball_velX;
ball_posY = ball_posY+ball_velY;
}

//Rebote Paddle Derecha
if ((ball_posX > pad1X - ball_size/2) && (ball_posX < pad1X + pad1_sizeX + ball_size/2) && (ball_posY > pad1Y - ball_size/2) && (ball_posY < pad1Y + pad1_sizeY + ball_size/2)){
if(( ball_velX <= 6)&&(ball_velX >= -6)){
ball_velX = -(ball_velX + ball_velX * 0.1);
}
else{
ball_velX = -ball_velX;
}
}

//Rebote Paddle Izquierda
if ((ball_posX < pad2X + pad2_sizeX + ball_size/2) && (ball_posX > pad2X - ball_size/2) && (ball_posY > pad2Y - ball_size/2) && (ball_posY < pad2Y + pad2_sizeY + ball_size/2)){
if(( ball_velX <= 6)&&(ball_velX >= -6)){
ball_velX = -(ball_velX + ball_velX * 0.1);
}
else{
ball_velX = -ball_velX;
}
}

//Rebote con suelo y techo
if((ball_posY > 440)||(ball_posY < 10)){
ball_velY = -ball_velY;
}

//Límites de marcaje de punto
if((ball_posX < 0)||(ball_posX > screenX-ball_size/2)){
ball_posX = screenX/2;
ball_posY = screenY/2;
ball_velX = 3;
ball_velY = 2;
lose = true;

}

//Visual
background(#000000);
fill(pad_color);
noStroke();
rect(pad1X,pad1Y,pad1_sizeX,pad1_sizeY);
fill(pad2_color);
rect(pad2X,pad2Y,pad2_sizeX,pad2_sizeY);
fill(ball_color);
ellipse(ball_posX,ball_posY,ball_size,ball_size);
//Este texto úselo para cuando sepa hacer un contador funcional si es necesario
//textSize(60);
//fill(text_color);
//text("0:0",255,100);
}

//Ajustes de teclas
void keyPressed(){
if(key == 'w'){
w_pressed = true;
}
if(key == 's'){
s_pressed = true;
}
if(keyCode == UP){
up_pressed = true;
}
if(keyCode == DOWN){
down_pressed = true;
}
}

//Ajustes de teclas
void keyReleased(){
if(key == 'w'){
w_pressed = false;
}
if(key == 's'){
s_pressed = false;
}
if(keyCode == UP){
up_pressed = false;
}
if(keyCode == DOWN){
down_pressed = false;
}
}

I have this, it's more complex and 2 player. It has some bugs and still need to do the scoreboard.
If want to try use:

  • 'w' and 's' for left.
  • 'up' and 'down' for right.
    The comments are in spanish.

@Rainster239
Copy link

Rainster239 commented Oct 14, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment