Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

image text in transcribed
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 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 Sor 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 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. ip 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 Questions 3 and 4. 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 I 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-s'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. 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 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 Sor 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 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. ip 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 Questions 3 and 4. 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 I 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-s'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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Information Modeling And Relational Databases

Authors: Terry Halpin, Tony Morgan

2nd Edition

0123735688, 978-0123735683

More Books

Students also viewed these Databases questions

Question

Bachelors degree in Information Systems or Statistics

Answered: 1 week ago