In: Computer Science
Please do in Java!!
Stay on the Screen! Animation in video games is just like animation in movies – it’s drawn image by image (called “frames”). Before the game can draw a frame, it needs to update the position of the objects based on their velocities (among other things). To do that is relatively simple: add the velocity to the position of the object each frame. For this program, imagine we want to track an object and detect if it goes off the left or right side of the screen (that is, it’s X position is less than 0 and greater than the width of the screen, say, 100).
Write a program that asks the user for the starting X and Y position of the object as well as the starting X and Y velocity, then prints out its position each frame until the object moves off of the screen. Design (pseudocode) and implement (source code) for this program.
Sample run 1: Enter the starting X position: 50
Enter the starting X velocity: 4.7
Enter the starting Y velocity: 2
X:50 Y:50
X:54.7 Y:52
X :59.4 Y:54
X:64.1 Y:56
X:68.8 Y:58
X:73.5 Y:60
X:78.2 Y:62
X:82.9 Y:64
X:87.6 Y:66
X:92.3 Y:68
X:97 Y:70
X:101.7 Y:72
CursorPosition.java :
//import package
import java.util.*;
//java class
public class CursorPosition {
//main method
public static void main(String[] args) {
// creating object of Scanner
class
Scanner input = new
Scanner(System.in);
// asking user starting X
position
System.out.print("Enter the
starting X position: ");
double XX = input.nextDouble();//
reading X position
// asking user starting Y
position
System.out.print("Enter the
starting Y position: ");
double YY = input.nextDouble();//
reading y position
// asking user starting X
velocity
System.out.print("Enter the
starting X velocity: ");
double xVelocity =
input.nextDouble();// reading X velocity
// asking user starting Y
velocity
System.out.print("Enter the
starting Y velocity: ");
double yVelocity =
input.nextDouble();// reading Y velocity
//this loop will executes till
value of X becomes less then 0 or
//greater than 100 using while
loop
while (XX > 0 && XX
<= 100) {
System.out.printf("X:%.1f",XX);//printing X position
System.out.print(" ");// used for space
System.out.printf("Y:%.1f", YY);
System.out.println();// used for new line
//increment
position by adding velocity
XX = XX +
xVelocity;
// increment
position by adding velocity
YY = YY +
yVelocity;
}
System.out.printf("X:%.1f",
XX);//print position of X
System.out.print(" ");// used for
space
System.out.printf("Y:%.1f",
YY);//print position of Y
}
}
*************************************
Screen 1:CursorPosition.java