In: Computer Science
Please do this in C#. 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
Dear Student ,
As per the requirement submitted above , kindly find the below solution.
Here a new Console Application in C# is created using Visual Studio 2017 with name "AnimationApplication".This application contains a class with name "Program.cs".Below are the details of this class.
Program.cs :
//namespace
using System;
//application namespace
namespace AnimationApplication
{
class Program //C# program
{
//entry point of the program , Main() method
static void Main(string[] args)
{
//asking user starting X position
Console.Write("Enter the starting X position:");
//reading X position
double xPosition = double.Parse(Console.ReadLine());
//asking user starting Y position
Console.Write("Enter the starting Y position:");
//reading Y position
double yPosition = double.Parse(Console.ReadLine());
//asking user starting X velocity
Console.Write("Enter the starting X velocity:");
//reading X velocity
double xVelocity = double.Parse(Console.ReadLine());
//asking user starting Y velocity
Console.Write("Enter the starting Y velocity:");
//reading Y velocity
double yVelocity = double.Parse(Console.ReadLine());
//using while loop finding X and Y positions
while(xPosition>0 && xPosition<100)
{
Console.WriteLine("X:"+xPosition+" Y:"+yPosition);
//to find new X position add X velocity
xPosition += xVelocity;
//to find new Y position add Y velocity
yPosition += yVelocity;
}
//used to print last line
Console.WriteLine("X:" + xPosition + " Y:" + yPosition);
//to hold the screen
Console.ReadKey();
}
}
}
======================================================
Output : Run application using F5 and will get the screen as shown below
Screen 1 :Program.cs
Screen 2:
NOTE : PLEASE FEEL FREE TO PROVIDE FEEDBACK ABOUT THE SOLUTION.