Question
Q2: FLY THE SPACESHIP [8 MARKS] Now add code that will allow you to fly the ship around the canvas, controlled by the keyboard. The
Q2: FLY THE SPACESHIP [8 MARKS] Now add code that will allow you to fly the ship around the canvas, controlled by the keyboard. The ship should fly at a constant speed. It can never speed up, slow down, or stop. Define a constant to control this speed. When the user presses the D or L key on the keyboard, the ship should turn slowly to the right. When the user presses the A or J key on the keyboard, the ship should turn slowly to the left. The spaceship will always fly in the direction that it is pointing. When the user presses the S or K key on the keyboard, the ship should stop turning, and fly in a straight line. The user does not have to hold the key down. So, for example, after pressing D or L, the spaceship should slowly fly in a perfect clockwise circle, until some other key is pressed. Define a state variable to control the rotation of the spaceship. Define a suitable constant to define how fast the spaceship turns. Experiment to find a suitable value. The spaceship should fly in circles almost as big as the canvas (you may want to have a very large canvas, such as 1000x1000). The spaceship should start in the centre of the canvas and should not be turning. Define a function void moveShip() which will control the position and angle of the spaceship. The spaceship should wrap around from any edge of the canvas to the opposite edge. For example, if it flies off the top of the canvas, it should reappear at the bottom. Do not use % to do this. Instead, use four separate IF statements to check for the four edges individually. This will be important in Q3 and Q4. Define the special void keyPressed() function which will be called automatically whenever the user presses a key on the keyboard. You do not call this function yourself! Control the rotation of the spaceship if a, s, d, j, k or l is pressed. Ignore all other keys. You do not want the user to have to hold down the shift key, so check for lowercase letters (a) and not uppercase letters (A). the built-in variable key will tell you which key was pressed, and you can check its value like this: key==a. These are char variables and constants which will be covered more fully in Week 6. Note: If your program does not respond to key presses, click once anywhere in the canvas. Only the active window will receive key presses, and sometimes, the canvas is not the active window until you click on it.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started