Can someone help me using java create the following list of functionalities prog
ID: 3842204 • Letter: C
Question
Can someone help me using java create the following list of functionalities programs listed below for the atari breakout game.
class Ball extends GameObject
{
PVector speed;
Ball(float inX, float inY, PVector inSpeed)
{
super(inX, inY, 25, 25);
type = OBJECT_TYPE.BALL;
speed = inSpeed;
}
void Update()
{
// Check for collision
if(x + w/2 > width || x - w/2 < 0)
{
speed.x *= -1;
}
if(y - h/2 < 0)
{
speed.y *= -1;
}
if(y + h/2 > height)
{
gameMan.BallKilled();
}
// Adjust x,y by speed
x += speed.x;
y += speed.y;
box.Update(x - w/2,y - h/2);
// Draw at x,y
fill(0, 0, 255);
ellipse(x,y,w,h);
}
void Delete()
{
super.Delete();
ballList.remove(this);
}
void OnCollision(GameObject obj)
{
if(obj.type == OBJECT_TYPE.PADDLE)
{
// Work here
Paddle p = (Paddle)obj;
// When ???????
//speed.x *= -1;
float paddleMidX = mouseX;
// float paddleMidX = p.x + p.w/2;
// speed?
if( x >= paddleMidX)
{
speed.x = abs(speed.x);
}
else
{
speed.x = -abs(speed.x);
}
speed.y = -abs(speed.y);
}
else if(obj.type == OBJECT_TYPE.BRICK)
{
// Work here
Brick b = (Brick)obj;
speed.y = abs(speed.y);
}
else
{
speed.y *= -1;
}
}
}
GameManager gameMan;
void setup()
{
size(800, 600);
gameMan = new GameManager();
}
void draw()
{
background(200);
gameMan.Update();
}
// Debug tool
void keyPressed()
{
if(key == 't')
{
if(brickList.size() > 0)
{
Brick b = brickList.get(0);
b.Delete();
}
}
}
class Brick extends GameObject
{
Brick(float inX, float inY, float inW, float inH)
{
super(inX, inY, inW, inH);
type = OBJECT_TYPE.BRICK;
}
void Update()
{
box.Update(x,y);
fill(255, 0, 0);
rect(x,y,w,h);
}
void Delete()
{
super.Delete();
brickList.remove(this);
}
void OnCollision(GameObject obj)
{
this.Delete();
}
}
void RunCollisions()
{
for(int i = 0; i < brickList.size(); i++)
{
Brick brick = brickList.get(i);
for(int j = 0; j < ballList.size(); j++)
{
Ball ball = ballList.get(j);
if(Collision(ball.box, brick.box))
{
ball.OnCollision(brick);
brick.OnCollision(ball);
break;
}
}
}
for(int i = 0; i < ballList.size(); i++)
{
Ball b = ballList.get(i);
if(Collision(b.box, player.box))
{
b.OnCollision(player);
player.OnCollision(b);
}
}
}
ArrayList<GameObject> gameObjectList;
ArrayList<Brick> brickList;
ArrayList<Ball> ballList;
Paddle player;
class GameManager
{
int lives;
int score;
GameManager()
{
lives = 3;
gameObjectList = new ArrayList<GameObject>();
ballList = new ArrayList<Ball>();
brickList = new ArrayList<Brick>();
ballList.add(new Ball(400, 300, new PVector(random(3, 4), random(3, 4))));
player = new Paddle(400, 500);
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 5; j++)
{
brickList.add(new Brick( i * 100 , j * 30, 100, 30));
}
}
}
void Update()
{
if(brickList.size() == 0)
{
println("Win");
}
RunCollisions();
for(int i = 0; i < gameObjectList.size(); i++)
{
GameObject obj = gameObjectList.get(i);
obj.Update();
}
}
void BallKilled()
{
lives--;
if(lives > 0)
{
Ball b = ballList.get(0);
b.x = 400;
b.y = 300;
println("Lives: " + lives);
}
else
{
Ball b = ballList.get(0);
b.Delete();
println("GAMEOVER");
}
}
}
enum OBJECT_TYPE
{
BALL,
BRICK,
PADDLE
}
abstract class GameObject
{
float x, y, w, h;
Hitbox box;
OBJECT_TYPE type;
GameObject(float inX, float inY, float inW, float inH)
{
x = inX;
y = inY;
w = inW;
h = inH;
box = new Hitbox(x,y,w,h);
gameObjectList.add(this);
}
abstract void Update();
abstract void OnCollision(GameObject obj);
void Delete()
{
gameObjectList.remove(this);
}
}
class Hitbox
{
boolean debugMode = true;
float x, y, w, h;
Hitbox(float inX, float inY, float inW, float inH) {
x = inX;
y = inY;
w = inW;
h = inH;
}
void Update(float inX, float inY) {
x = inX;
y = inY;
if(debugMode == true)
{
stroke(0,255, 0);
noFill();
rect(x, y, w, h);
}
}
}
boolean Collision(Hitbox a, Hitbox b)
{
float thisTop = a.y;
float thisBot = a.y + a.h;
float thisLeft = a.x;
float thisRight = a.x + a.w;
float otherTop = b.y;
float otherBot = b.y + b.h;
float otherLeft = b.x;
float otherRight = b.x + b.w;
boolean output = true;
if( thisBot < otherTop)
{
output = false;
}
else if( thisTop > otherBot)
{
output = false;
}
else if( thisRight < otherLeft)
{
output = false;
}
else if( thisLeft > otherRight)
{
output = false;
}
return output;
}
class Paddle extends GameObject
{
Paddle(float inX, float inY)
{
super(inX, inY, 200, 20);
type = OBJECT_TYPE.PADDLE;
}
void Update()
{
x = mouseX - w/2;
box.Update(x,y);
fill(0, 0, 255);
rect(x,y,w,h);
}
void OnCollision(GameObject obj)
{
}
}
Explanation / Answer
class Ball extends GameObject
{
PVector speed;
Ball(float inX, float inY, PVector inSpeed)
{
super(inX, inY, 25, 25);
type = OBJECT_TYPE.BALL;
speed = inSpeed;
}
void Update()
{
// Check for collision
if(x + w/2 > width || x - w/2 < 0)
{
speed.x *= -1;
}
if(y - h/2 < 0)
{
speed.y *= -1;
}
if(y + h/2 > height)
{
gameMan.BallKilled();
}
// Adjust x,y by speed
x += speed.x;
y += speed.y;
box.Update(x - w/2,y - h/2);
// Draw at x,y
fill(0, 0, 255);
ellipse(x,y,w,h);
}
void Delete()
{
super.Delete();
ballList.remove(this);
}
void OnCollision(GameObject obj)
{
if(obj.type == OBJECT_TYPE.PADDLE)
{
// Work here
Paddle p = (Paddle)obj;
// When ???????
//speed.x *= -1;
float paddleMidX = mouseX;
// float paddleMidX = p.x + p.w/2;
// speed?
if( x >= paddleMidX)
{
speed.x = abs(speed.x);
}
else
{
speed.x = -abs(speed.x);
}
speed.y = -abs(speed.y);
}
else if(obj.type == OBJECT_TYPE.BRICK)
{
// Work here
Brick b = (Brick)obj;
speed.y = abs(speed.y);
}
else
{
speed.y *= -1;
}
}
}
GameManager gameMan;
void setup()
{
size(800, 600);
gameMan = new GameManager();
}
void draw()
{
background(200);
gameMan.Update();
}
// Debug tool
void keyPressed()
{
if(key == 't')
{
if(brickList.size() > 0)
{
Brick b = brickList.get(0);
b.Delete();
}
}
}
class Brick extends GameObject
{
Brick(float inX, float inY, float inW, float inH)
{
super(inX, inY, inW, inH);
type = OBJECT_TYPE.BRICK;
}
void Update()
{
box.Update(x,y);
fill(255, 0, 0);
rect(x,y,w,h);
}
void Delete()
{
super.Delete();
brickList.remove(this);
}
void OnCollision(GameObject obj)
{
this.Delete();
}
}
void RunCollisions()
{
for(int i = 0; i < brickList.size(); i++)
{
Brick brick = brickList.get(i);
for(int j = 0; j < ballList.size(); j++)
{
Ball ball = ballList.get(j);
if(Collision(ball.box, brick.box))
{
ball.OnCollision(brick);
brick.OnCollision(ball);
break;
}
}
}
for(int i = 0; i < ballList.size(); i++)
{
Ball b = ballList.get(i);
if(Collision(b.box, player.box))
{
b.OnCollision(player);
player.OnCollision(b);
}
}
}
ArrayList<GameObject> gameObjectList;
ArrayList<Brick> brickList;
ArrayList<Ball> ballList;
Paddle player;
class GameManager
{
int lives;
int score;
GameManager()
{
lives = 3;
gameObjectList = new ArrayList<GameObject>();
ballList = new ArrayList<Ball>();
brickList = new ArrayList<Brick>();
ballList.add(new Ball(400, 300, new PVector(random(3, 4), random(3, 4))));
player = new Paddle(400, 500);
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 5; j++)
{
brickList.add(new Brick( i * 100 , j * 30, 100, 30));
}
}
}
void Update()
{
if(brickList.size() == 0)
{
println("Win");
}
RunCollisions();
for(int i = 0; i < gameObjectList.size(); i++)
{
GameObject obj = gameObjectList.get(i);
obj.Update();
}
}
void BallKilled()
{
lives--;
if(lives > 0)
{
Ball b = ballList.get(0);
b.x = 400;
b.y = 300;
println("Lives: " + lives);
}
else
{
Ball b = ballList.get(0);
b.Delete();
println("GAMEOVER");
}
}
}
enum OBJECT_TYPE
{
BALL,
BRICK,
PADDLE
}
abstract class GameObject
{
float x, y, w, h;
Hitbox box;
OBJECT_TYPE type;
GameObject(float inX, float inY, float inW, float inH)
{
x = inX;
y = inY;
w = inW;
h = inH;
box = new Hitbox(x,y,w,h);
gameObjectList.add(this);
}
abstract void Update();
abstract void OnCollision(GameObject obj);
void Delete()
{
gameObjectList.remove(this);
}
}
class Hitbox
{
boolean debugMode = true;
float x, y, w, h;
Hitbox(float inX, float inY, float inW, float inH) {
x = inX;
y = inY;
w = inW;
h = inH;
}
void Update(float inX, float inY) {
x = inX;
y = inY;
if(debugMode == true)
{
stroke(0,255, 0);
noFill();
rect(x, y, w, h);
}
}
}
boolean Collision(Hitbox a, Hitbox b)
{
float thisTop = a.y;
float thisBot = a.y + a.h;
float thisLeft = a.x;
float thisRight = a.x + a.w;
float otherTop = b.y;
float otherBot = b.y + b.h;
float otherLeft = b.x;
float otherRight = b.x + b.w;
boolean output = true;
if( thisBot < otherTop)
{
output = false;
}
else if( thisTop > otherBot)
{
output = false;
}
else if( thisRight < otherLeft)
{
output = false;
}
else if( thisLeft > otherRight)
{
output = false;
}
return output;
}
class Paddle extends GameObject
{
Paddle(float inX, float inY)
{
super(inX, inY, 200, 20);
type = OBJECT_TYPE.PADDLE;
}
void Update()
{
x = mouseX - w/2;
box.Update(x,y);
fill(0, 0, 255);
rect(x,y,w,h);
}
void OnCollision(GameObject obj)
{
double xDist, yDist;
for(int i = 0; i < balls.size(); i++){
Ball A = balls.get(i);
for(int j = i+1; j < balls.size(); j++){
Ball B = balls.get(j);
xDist = A.getCenterX() - B.getCenterX();
yDist = A.getCenterY() - B.getCenterY();
double distSquared = xDist*xDist + yDist*yDist;
//Check the squared distances instead of the the distances, same result, but avoids a square root.
if(distSquared <= (A.radius + B.radius)*(A.radius + B.radius)){
double xVelocity = B.xVel - A.xVel;
double yVelocity = B.yVel - A.yVel;
double dotProduct = xDist*xVelocity + yDist*yVelocity;
//Neat vector maths, used for checking if the objects moves towards one another.
if(dotProduct > 0){
double collisionScale = dotProduct / distSquared;
double xCollision = xDist * collisionScale;
double yCollision = yDist * collisionScale;
//The Collision vector is the speed difference projected on the Dist vector,
//thus it is the component of the speed difference needed for the collision.
double combinedMass = A.mass + B.mass;
double collisionWeightA = 2 * B.mass / combinedMass;
double collisionWeightB = 2 * A.mass / combinedMass;
A.xVel += collisionWeightA * xCollision;
A.yVel += collisionWeightA * yCollision;
B.xVel -= collisionWeightB * xCollision;
B.yVel -= collisionWeightB * yCollision;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.