# Homework 9: Objects in Processing

Due: Thursday Dec 13, 11:59PM.

To submit: Send an email to me at jal2016@email.vccs.edu with subject CSC 110 HW9 with your program(s) attached in a zip file.

The program below implements part of a very simple "Pong" style game. For this assignment, you should add at least two new features to it. Some ideas:

• Modify the computer opponent so that it is beatable (but still attempts to win).
• Modify the collision logic so that the ball bounces differently when it hits near the edge of the paddle.
• Modify the bouncing logic so that the ball goes off the sides of the screen when a player misses. Add a score display that counts how many times a player has missed.
• Add fancier graphics by modifying the draw methods.

If you would rather create your own game, you can do that instead of this assignment, but talk to me about what you want to do.

// Data type to represent bouncing ball.
class Ball {
// Position
float x, y;
// Velocity
float vx, vy;

// Ball diameter
float diam = 30;

// Constructor defines how object is initialized.
Ball(float x0, float y0, float vx0, float vy0) {
x = x0;
y = y0;
vx = vx0;
vy = vy0;
}

// Our program will call the update() method
// each frame to update object state.
// Method takes two paddle objects as input, which
// it will check for collisions.
// Update ball position.
x = x + vx;
y = y + vy;
// Bouncing means velocity is reversed.
// Check to see if it hits left or right side.
if (x + diam/2 > width)
vx = -abs(vx);
if (x - diam/2 < 0)
vx = abs(vx);

// Check to see if it hits top or bottom.
if (y + diam/2 > height)
vy = -abs(vy);
if (y - diam/2 < 0)
vy = abs(vy);

// Check to see if it hits either paddle.
// Note: "this" is a special keyword that
// represents the object whose method is
// being called.
if (pLeft.hits(this)) {
vx = abs(vx);
}
if (pRight.hits(this)) {
vx = -abs(vx);
}
}

// Our program will call the draw() method
// each frame to draw the ball.
void draw() {
// Draw ball.
ellipse(x, y, diam, diam);
}
}

// Data type to represent both human and computer
float x, y;

float dimX = 10;
float dimY = 50;

boolean left;

left = isLeft;
y = height/2;
if (left) {
x = 20;
}
else {
x = width - 20;
}
}

// Called to update human paddle.
void update(Ball b) {
y = mouseY;
}

// Called to update computer paddle.
void updateAI(Ball b) {
// Unbeatable computer opponent!
y = b.y - dimY/2;
}

void draw() {
float w;
if (left) {
w = -dimX;
}
else {
w = dimX;
}
rect(x, y, w, dimY);
}

// Determine if ball hits this paddle.
boolean hits(Ball b) {
boolean hitsY = b.y >= y && b.y <= y + dimY;
boolean hitsX;
if (left) {
hitsX  = (b.x - b.diam/2) <= x;
}
else {
hitsX = (b.x + b.diam/2) >= x;
}
return hitsX && hitsY;
}
}

Ball ball;

// This function is called once at program start.
void setup() {
size(300, 300);
smooth();
// Set drawing style for all future frames.
fill(0, 0, 255);
noStroke();

ball = new Ball(width/2, height/2,
random(5), random(5));

}

void draw() {
// Clear to a new white background.
background(255);