In: Computer Science
Assignment6A: This used to be entertainment. If you haven’t played the classic game Pong, then you are now required to do so. Though it doesn’t capture the poor quality of the original, you can find an emulator at Pong-2.com. Play it (using the keyboard). Do you see how the ball bounces off of the walls and the paddles? You’re going to learn how to do this by creating class Ball.
A Ball has an X and Y position. Equally important, a ball has an
x velocity and a y velocity. Every time the ball moves (in one unit
of time), it changes its X and Y position by its x and y velocity.
However, before moving it, you need to check to see if it’s
touching a wall. If so, you need to reverse either its x or y
velocity depending on whether or not its touching a wall. What is
the ball’s location if its touching a wall? For simplicity, we’re
going to assume that the ball is on a 10x10 field and that the x
and y velocities can only be -1, 0, or +1.
Your task is to 1) write class Ball that has the variables/attributes above, 2) has a constructor that takes in the starting X and Y position as well as the starting X and Y velocity, 3) has a method called “move” that takes no parameters and updates the position of the ball and 4) has a print statement called “print” that takes no parameters and prints out the ball’s current position.
You must call the class “Ball” and put it in a file called Ball(.java, .cs, .cpp, .h). To test your ball, you should create a file called Assignment6A(.java, .cs, .cpp) that creates a ball based off of user input and calls the “move” and “print” methods of the ball the number of times the user wants. It should behave like the sample output below.
Sample Output #1: x: 7 y: 4 x velocity: 1 y velocity: 1 Number of moves: 20 X:7 Y:4 X:8 Y:5 X:9 Y:6 X:8 Y:7 X:7 Y:8 X:6 Y:9 X:5 Y:8 X:4 Y:7 X:3 Y:6 X:2 Y:5 X:1 Y:4 X:0 Y:3 X:1 Y:2 X:2 Y:1 X:3 Y:0 X:4 Y:1 X:5 Y:2 X:6 Y:3 X:7 Y:4 X:8 Y:5 X:9 Y:6 |
Sample Output #2: x: 5 y: 2 x velocity: 0 y velocity: -1 Number of moves: 20 X:5 Y:2 X:5 Y:1 X:5 Y:0 X:5 Y:1 X:5 Y:2 X:5 Y:3 X:5 Y:4 X:5 Y:5 X:5 Y:6 X:5 Y:7 X:5 Y:8 X:5 Y:9 X:5 Y:8 X:5 Y:7 X:5 Y:6 X:5 Y:5 X:5 Y:4 X:5 Y:3 X:5 Y:2 X:5 Y:1 X:5 Y:0 |
Program Screenshot for Indentation Reference:
C++:
Java:
Sample Output:
Program code to copy:
C++ -------------------------------------------------------------------
Ball.h
#ifndef BALL_H
#define BALL_H
// create the class
class Ball{
private:
int x_pos;
int y_pos;
int x_vel; // x
velocity
int y_vel; // y
velocity
//field size
const int field_width =
10;
const int field_height =
10;
public:
// constructor
Ball(int x_p, int y_p,
int x_v, int y_v);
void move();
void print();
};
#endif
Ball.cpp
#include <iostream>
#include "Ball.h"
using namespace std;
Ball::Ball(int x_p, int y_p, int x_v, int y_v) {
// if any memmber is invalid then set it to
0
if (x_p < 0 || x_p >= field_width) {
x_p = 0;
}
if (y_p < 0 || y_p >= field_height)
{
y_p = 0;
}
if (x_v < -1 || x_v > 1) {
x_v = 0;
}
if (y_v < -1 || y_v > 1) {
y_v = 0;
}
// set the variables
x_pos = x_p;
y_pos = y_p;
x_vel = x_v;
y_vel = y_v;
}
void Ball::move() {
// if x_pos is at border then change x_vel
if(x_pos == 0 || x_pos == field_width - 1)
{
x_vel *= -1; //
invert
}
// check y_pos
if(y_pos == 0 || y_pos == field_height - 1)
{
y_vel *= -1;
}
// mode positions
x_pos += x_vel;
y_pos += y_vel;
}
void Ball::print() {
// print
cout << "X:" << x_pos << " Y:"
<< y_pos << endl;
}
Assignment6A.cpp:
#include <iostream>
#include "Ball.h"
using namespace std;
int main() {
// get inputs
int x, y, x_v, y_v, moves;
cout << "x: ";
cin >> x;
cout << "y: ";
cin >> y;
cout << "x velocity: ";
cin >> x_v;
cout << "y velocity: ";
cin >> y_v;
cout << "Number of moves: ";
cin >> moves;
// create a ball object
Ball b(x, y, x_v, y_v);
// print initial
b.print();
// loop moves times
for (int i = 0; i < moves; i++) {
// move
b.move();
// print
b.print();
}
return 0;
}
Java -------------------------------------------------------------------
Ball.java:
public class Ball {
private int x_pos, y_pos, x_vel, y_vel;
private final int field_width = 10;
private final int field_height = 10;
// constructor
public Ball(int x_p, int y_p, int x_v, int y_v)
{
// if any memmber is
invalid then set it to 0
if (x_p < 0 || x_p
>= field_width) {
x_p = 0;
}
if (y_p < 0 || y_p
>= field_height) {
y_p = 0;
}
if (x_v < -1 || x_v
> 1) {
x_v = 0;
}
if (y_v < -1 || y_v
> 1) {
y_v = 0;
}
// set the
variables
x_pos = x_p;
y_pos = y_p;
x_vel = x_v;
y_vel = y_v;
}
public void move() {
// if x_pos is at border
then change x_vel
if(x_pos == 0 || x_pos
== field_width - 1) {
x_vel *= -1; // invert
}
// check y_pos
if(y_pos == 0 || y_pos
== field_height - 1) {
y_vel *= -1;
}
// mode positions
x_pos += x_vel;
y_pos += y_vel;
}
public void print() {
// print
System.out.println("X:"
+ x_pos + " Y:" + y_pos);
}
}
Assignment6A.java:
import java.util.Scanner;
public class Assignment6A {
// implement main method
public static void main(String[] args)
{
int x, y, x_v, y_v,
moves;
// get Scanner for
input
Scanner in = new
Scanner(System.in);
// prompt and read
inputs
System.out.print("x:
");
x = in.nextInt();
System.out.print("y:
");
y = in.nextInt();
System.out.print("x
velocity: ");
x_v = in.nextInt();
System.out.print("y
velocity: ");
y_v = in.nextInt();
System.out.print("Number of moves: ");
moves =
in.nextInt();
// create a
ball
Ball b = new Ball(x, y,
x_v, y_v);
// print initial
b.print();
// loop moves
times
for (int i = 0; i <
moves; i++) {
// move
b.move();
// print
b.print();
}
}
}