In: Computer Science
Program 1: 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) for this program
Sample run 1:
Enter the starting X position: 50
Enter the starting Y 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
Pseudocode for screen animation, to print the position of the object until it goes out of frame:
Declaration
number x_pos, y_pos, x_velocity, y_velocity;
number width;
Start
// Input the start position of x and y and starting x velocity and
y velocity
Input x_pos, y_pos, x_velocity. y_velocity;
width = 100; // set the width of the screen
// loop that continues till x_pos or y_pos are beyond the boundary
i.e until the object moves off of the screen
// i.e position of x < 0 or position of x > 100 or position
of y < 0 or position of y > 100
while(x_pos >=0 and x_pos <= width and y_pos >=0 and y_pos
<=100)
do
Print "X: ",x_pos,"Y: ",y_pos; // print the values of x_pos and
y_pos
x_pos = x_pos + x_velocity; // update x_pos after every frame
y_pos = y_pos + y_velocity; // update y_pos after every frame
end while
Print "X: ",x_pos,"Y: ",y_pos; // print the final values of
x_pos and y_pos
End
//end of pseudocode
C++ code:
// C++ program to track the position of an object each frame until the object moves off of the screen
#include <iostream>
using namespace std;
int main()
{
float x_pos, y_pos, x_velocity, y_velocity;
float width = 100;
// input of starting x position
cout<<"Enter the starting X position: ";
cin>>x_pos;
// input of starting y position
cout<<"Enter the starting Y position: ";
cin>>y_pos;
// input of starting x velocity
cout<<"Enter the starting X velocity: ";
cin>>x_velocity;
// input of starting y velocity
cout<<"Enter the starting Y velocity: ";
cin>>y_velocity;
// loop that prints out the object's position and
update the position
// each frame until the object moves off of the
screen
while(x_pos >=0 && x_pos <= width
&& y_pos >=0 && y_pos <=width)
{
cout<<"X :
"<<x_pos<<" Y : "<<y_pos<<endl; // display
the current position
// update the x position and y
position for the next frame
x_pos += x_velocity;
y_pos += y_velocity;
}
// display the final positions when the object is
off the screen
cout<<"X : "<<x_pos<<" Y :
"<<y_pos<<endl;
return 0;
}
//end of program
Output: