In: Computer Science
I need assistance on this problem in Pseudocode and in C++ 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) and implement (source code) 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
Sample run 2:
Enter the starting X position: 20
Enter the starting Y position: 45
Enter the starting X velocity: -3.7
Enter the starting Y velocity:
11.2 X:20 Y:45
X:16.3 Y:56.2
X:12.6 Y:67.4
X:8.9 Y:78.6
X:5.2 Y:89
.8 X:1.5 Y:101
X:-2.2 Y:112.2
This assignment is about Repetition Structures.
For Pseudocode, here are key words to use
: · DO … WHILE – A loop that will always run at least once ·
FOR … ENDFOR – A loop that runs until certain criteria is met ·
WHILE … ENDWHILE – A loop that runs only while certain criteria is met ·
FOREACH … ENDFOREACH – A loop that runs over elements in a data structure · BREAK - "break out" of the current loop (or other structure) you're in and start immediately after the loop · CONTINUE - skip over the current iteration of the loop and move on to the next one
Below is the C++ code I hope that i have provided sufficient comments for your better understanding Note that I have done proper indentation but this code is automatically left alligned on this interface
#include<bits/stdc++.h>
using namespace std;
#define WIDTH 100 //Let width of screen be 100
using namespace std;
int main()
{
double x_pos, y_pos;
double x_vel, y_vel;
cout<<"Using do while loop"<<endl;
//Take users input
cout<<"Enter the starting X position: ";
cin>>x_pos;
cout<<"Enter the starting Y position: ";
cin>>y_pos;
cout<<"Enter the starting X velocity: ";
cin>>x_vel;
cout<<"Enter the starting Y velocity: ";
cin>>y_vel;
//using do while loop
do
{
cout<<"X:"<<x_pos<<" Y:
"<<y_pos<<endl;
x_pos += x_vel;
y_pos += y_vel;
}while(x_pos>=0 && x_pos<=WIDTH); //Apply the
specified condition
cout<<"X:"<<x_pos<<" Y:
"<<y_pos<<endl;
cout<<endl;
cout<<"Using for loop"<<endl;
//Take users input
cout<<"Enter the starting X position: ";
cin>>x_pos;
cout<<"Enter the starting Y position: ";
cin>>y_pos;
cout<<"Enter the starting X velocity: ";
cin>>x_vel;
cout<<"Enter the starting Y velocity: ";
cin>>y_vel;
//using for loop
for( ;x_pos>=0&&x_pos<=WIDTH; ) //Apply the specified
condition
{
cout<<"X:"<<x_pos<<" Y:
"<<y_pos<<endl;
x_pos += x_vel;
y_pos += y_vel;
}
cout<<"X:"<<x_pos<<" Y:
"<<y_pos<<endl;
cout<<endl;
cout<<"Using while loop"<<endl;
//Take users input
cout<<"Enter the starting X position: ";
cin>>x_pos;
cout<<"Enter the starting Y position: ";
cin>>y_pos;
cout<<"Enter the starting X velocity: ";
cin>>x_vel;
cout<<"Enter the starting Y velocity: ";
cin>>y_vel;
//using while loop
while(x_pos>=0&&x_pos<=WIDTH) //Apply the specified
condition
{
cout<<"X:"<<x_pos<<" Y:
"<<y_pos<<endl;
x_pos += x_vel;
y_pos += y_vel;
}
cout<<"X:"<<x_pos<<" Y:
"<<y_pos<<endl;
cout<<endl;
}
Below is the screenshot of output
I have tried to explain it in very simple language and I hope that i have answered your question satisfactorily.Leave doubts in comment section if any.