Suikiyip

Menu

Skip to content
  • Home
  • EXERCISE
    • EX.2 – Processing
    • EX.3 – Audrino Workshop
    • EX.4 – Serial Communication
  • LITTER HACK PROJECT
    • Background
    • Form Development
    • Making Process
    • Program
    • Scenario
    • Work Demo
    • Artifact
  • IMITATE PROJECT

EX.2 – Processing

MODIFY PONG GAME


 “The Pong Game” will be used to finish this exercise

1. Adding a second player on the left uses (UP/DOWN) as control & adding score

2. Improve PONG game by adding colour and image


 //  Adding a second player on the left uses (UP/DOWN) as control & adding score
Demo Video :
Programme :

boolean gameStart = false;

float x = 250;
float y = 150;
float speedX = random(3, 5);
float speedY = random(3, 5);
int leftColor = 128;
int rightColor = 128;
int diam;
int rectSize = 150;
float diamHit;

int player1score = 0;          // score
int player2score = 0;
void setup() {
size(500, 500);
noStroke();
smooth();
ellipseMode(CENTER);
}

void draw() {
background(255);

fill(128,128,128);
diam = 20;
ellipse(x, y, diam, diam);

fill(leftColor);
rect(30,mouseY – rectSize/2, 10, rectSize);
fill(rightColor);
rect(width-30, mouseY-rectSize/2, 10, rectSize);

//draw text here
text(“score:” + player1score + “:” + player2score, 30, 30);            // score

if (gameStart) {

x = x + speedX;
y = y + speedY;

// if ball hits movable bar, invert X direction and apply effects
if ( x > width – 30 && x < width – 20 && y > mouseY-rectSize/2 && y < mouseY+rectSize/2 )
{
speedX = speedX * -1;
x = x + speedX;
rightColor = 0;
fill(random(0,128),random(0,128),random(0,128));
diamHit = random(75,150);
ellipse(x,y,diamHit,diamHit);
rectSize = rectSize-10;
rectSize = constrain(rectSize, 10,150);
}
if ( x < 30 && x > 20 && y > mouseY-rectSize/2 && y < mouseY+rectSize/2 )
{
speedX = speedX * -1;
x = x + speedX;
leftColor = 0;
fill(random(0,128),random(0,128),random(0,128));
diamHit = random(75,150);
ellipse(x,y,diamHit,diamHit);
rectSize = rectSize-10;
rectSize = constrain(rectSize, 10,150);
}

// if ball hits wall, change direction of X
else if (x < 25) {
speedX = speedX * -1.1;
x = x + speedX;
leftColor = 0;
}

else {
leftColor = 128;
rightColor = 128;
}
// resets things if you lose
if (x > width) {
gameStart = false;
x = 150;
y = 150;
speedX = random(3, 5);
speedY = random(3, 5);
rectSize = 150;
player1score = player1score + 1;        // adding player1 score
}
if (x < 30) {
gameStart = false;
x = 150;
y = 150;
speedX = random(3, 5);
speedY = random(3, 5);
rectSize = 150;
player2score = player1score + 1;       // adding player2 score
}
// if ball hits up or down, change direction of Y
if ( y > height || y < 0 ) {
speedY = speedY * -1;
y = y + speedY;
}
}
}
void mousePressed() {
gameStart = !gameStart;
}


 // Improve PONG game by adding colour and image
Demo Video :
Programme :

boolean gameStart = false;

float x = 250;
float y = 150;
float speedX = random(3, 5);
float speedY = random(3, 5);
//int leftColor = 128;
//int rightColor = 128;
color leftColor = color(255, 255, 255);
color rightColor = color(255, 255, 255);
int diam;
int rectSize = 150;
float diamHit;

int player1score = 0;
int player2score = 0;
PImage img;

void setup() {
size(500, 500);
noStroke();
smooth();
ellipseMode(CENTER);
img = loadImage(“ball.png”);               // adding image for the ellipse
}

void draw() {
background(255);

fill(128,128,128);;
diam = 20;
//ellipse(x, y, diam, diam);
image(img, x – diam/2, y- diam/2, 30, 30);             //replace ellipse with an image

fill(leftColor);
rect(30,mouseY – rectSize/2, 10, rectSize);
fill(rightColor);
rect(width-30, mouseY-rectSize/2, 10, rectSize);

//draw text here
fill(0);
text(“score:” + player1score + “vs” + player2score, 30, 30);          // score

if (gameStart) {

x = x + speedX;
y = y + speedY;

// if ball hits movable bar, invert X direction and apply effects
if ( x > width – 30 && x < width – 20 && y > mouseY-rectSize/2 && y < mouseY+rectSize/2 )
{
speedX = speedX * -1;
x = x + speedX;
rightColor = 0;
fill(random(0,128),random(0,128),random(0,128));              // colour of  right movable bar 
diamHit = random(75,150);
ellipse(x,y,diamHit,diamHit);
rectSize = rectSize-10;
rectSize = constrain(rectSize, 10,150);

}
if ( x < 30 && x > 20 && y > mouseY-rectSize/2 && y < mouseY+rectSize/2 )
{
speedX = speedX * -1;
x = x + speedX;
leftColor = 0;
fill(random(0,128),random(0,128),random(0,128));            // colour of  left movable bar 
diamHit = random(75,150);
ellipse(x,y,diamHit,diamHit);
rectSize = rectSize-10;
rectSize = constrain(rectSize, 10,150);
}

// if ball hits wall, change direction of X
else if (x < 25) {
speedX = speedX * -1.1;
x = x + speedX;
leftColor = 0;
}

else {
leftColor = color(47,203,143);
rightColor = color(47,73,143);
}
// resets things if you lose
if (x > width) {
gameStart = false;
x = 150;
y = 150;
speedX = random(3, 5);
speedY = random(3, 5);
rectSize = 150;
player1score = player1score + 1;
}
if (x < 30) {
gameStart = false;
x = 150;
y = 150;
speedX = random(3, 5);
speedY = random(3, 5);
rectSize = 150;
player2score = player2score + 1;
}
// if ball hits up or down, change direction of Y
if ( y > height || y < 0 ) {
speedY = speedY * -1;
y = y + speedY;
}
}
}
void mousePressed() {
gameStart = !gameStart;
}

Create a free website or blog at WordPress.com.
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy
    • Suikiyip
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • Manage subscriptions
Design a site like this with WordPress.com
Get started