Question

In: Computer Science

Assignment6A: This used to be entertainment. If you haven’t played the classic game Pong, then you...

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

Solutions

Expert Solution

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();
        }
    }
}


Related Solutions

What would be the equilibrium of the classic game “Chicken” if it was played as a...
What would be the equilibrium of the classic game “Chicken” if it was played as a Stackleberg game in which one player gets to decide first?
Question: you will be making a game called “Snap, Crackle, Pop”, a game typically played with...
Question: you will be making a game called “Snap, Crackle, Pop”, a game typically played with a group of 3 persons. Each person chooses one number and it is assigned to either 'snap', 'crackle', or 'pop'. Then the number of rounds is decided (e.g. 12). The players play the game for the predetermined number of rounds (here 12). In each round: ● if the number of the round is a multiple of the snap number, the person who chose the...
I needed the code for pong game (using classes) in pygame.
I needed the code for pong game (using classes) in pygame.
Monshimout is a game from the Cheyenne people and played by women. It could be played...
Monshimout is a game from the Cheyenne people and played by women. It could be played by two or more players, and if played by more than two, then the players divided into two equal teams. Game equipment consisted of five plum stones and a basket made of woven grass or willow twigs. The basket measured 3-4 inches deep, 8 inches across at the top, and almost 1/2 inch thick. The plum stones were left plain on one side, but...
Consider the used car Lemons game played in class. Suppose there are two kinds of used cars, good and bad.
Consider the used car Lemons game played in class. Suppose there are two kinds of used cars, good and bad. The current owner (seller) values good cars at S10,000 and bad cars at S6,000. She knows if the car is good or bad. The dealer (buyer) values good cars at $12,000 and bad cars at $7,000. The buyer's payoff is his value of the car minus the price paid. The seller's payoff is the price she receives minus her value...
Develop a Java application to simulate a game played in an elementary classroom. In this game,...
Develop a Java application to simulate a game played in an elementary classroom. In this game, the teacher places herself in the center of a circle of students surrounding her. She then distributes an even number of pieces of candy to each student. Not all students will necessarily receive the same number of pieces; however, the number of pieces of candy for each student is even and positive. When the teacher blows a whistle, each student takes half of his...
Below is an example of a classic prisoners’ dilemma game. In Westeros (see Game of Thrones)...
Below is an example of a classic prisoners’ dilemma game. In Westeros (see Game of Thrones) the dead are coming with winter. Which outcome has the best overall consequences and the best chance of surviving for Cersei and Daenerys? Cersei fights (cooperates) Cersei runs (defects) Daenerys fights (cooperates) Each has 70% chance of surviving and 30% chance of being slaughtered Cersei has 10% chance of being slaughtered, 90% chance of escaping Daenerys is completely screwed and has 95% chance of...
Write a python program that simulates a simple dice gambling game. The game is played as...
Write a python program that simulates a simple dice gambling game. The game is played as follows: Roll a six sided die. If you roll a 1, 2 or a 3, the game is over. If you roll a 4, 5, or 6, you win that many dollars ($4, $5, or $6), and then roll again. With each additional roll, you have the chance to win more money, or you might roll a game-ending 1, 2, or 3, at which...
6. A and B are playing a short game of ping pong where A serves 3...
6. A and B are playing a short game of ping pong where A serves 3 times and B also serves 3 times. If after these six points one of them is ahead the game ends, otherwise they go into a second phase. Suppose that A wins 70% of the points when they serve and 40% of the points when B serves. Let’s look at the first phase. a) (3 pts) Find the probability that A or B wins 0,...
If you played this game 619 times how much would you expect to win or lose?
Suppose a basketball player has made 233 out of 303free throws. If the player makes the next 3 free throws, I will pay you $7. Otherwise you pay me $8Step 2 of 2 :  If you played this game 619 times how much would you expect to win or lose? Round your answer to two decimal places. Losses must be entered as negative.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT